Java二维码生成与解码工具Zxing使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java二维码生成与解码工具Zxing使用相关的知识,希望对你有一定的参考价值。
1 package com.csii.zxing.test; 2 3 import java.awt.image.BufferedImage; 4 import java.io.File; 5 import java.io.IOException; 6 import java.util.HashMap; 7 import java.util.Hashtable; 8 import java.util.Map; 9 10 import javax.imageio.ImageIO; 11 12 import org.junit.Test; 13 import com.google.zxing.BarcodeFormat; 14 import com.google.zxing.Binarizer; 15 import com.google.zxing.BinaryBitmap; 16 import com.google.zxing.DecodeHintType; 17 import com.google.zxing.EncodeHintType; 18 import com.google.zxing.LuminanceSource; 19 import com.google.zxing.MultiFormatReader; 20 import com.google.zxing.MultiFormatWriter; 21 import com.google.zxing.NotFoundException; 22 import com.google.zxing.Result; 23 import com.google.zxing.WriterException; 24 import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 25 import com.google.zxing.client.j2se.MatrixToImageWriter; 26 import com.google.zxing.common.BitMatrix; 27 import com.google.zxing.common.HybridBinarizer; 28 29 public class ZxingTest { 30 @SuppressWarnings("deprecation") 31 @Test 32 public void testEncoder() 33 { 34 //二维码内容 35 String text = "http://blog.csdn.net/rongbo_j"; 36 int width = 200; //二维码图片宽度 37 int height = 200; //高度 38 String format = "gif"; //图片格式 39 40 Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); 41 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 42 BitMatrix bitMatrix = null; 43 try { 44 //编码 45 bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); 46 } catch (WriterException e1) { 47 e1.printStackTrace(); 48 } 49 File outputFile = new File("src/1.gif"); 50 try { 51 //输出二维码图片 52 MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 57 } 58 @Test 59 public void testDecoder() throws NotFoundException 60 { 61 //二维码图片路径 62 String path = this.getClass().getClassLoader().getResource("1.gif").getPath(); 63 File imageFile = new File(path); 64 BufferedImage image = null; 65 try { 66 image = ImageIO.read(imageFile); 67 LuminanceSource source = new BufferedImageLuminanceSource(image); 68 Binarizer binarizer = new HybridBinarizer(source); 69 BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); 70 Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>(); 71 hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); 72 //解码获取二维码中信息 73 Result result = new MultiFormatReader().decode(binaryBitmap, hints); 74 System.out.println(result.getText()); 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } 78 } 79 }
package com.csii.zxing.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import javax.imageio.ImageIO; import org.junit.Test; import com.google.zxing.BarcodeFormat; import com.google.zxing.Binarizer; import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; import com.google.zxing.EncodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; public class ZxingTest { @SuppressWarnings("deprecation") @Test public void testEncoder() { //二维码内容 String text = "http://blog.csdn.net/rongbo_j"; int width = 200; //二维码图片宽度 int height = 200; //高度 String format = "gif"; //图片格式 Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = null; try { //编码 bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e1) { e1.printStackTrace(); } File outputFile = new File("src/1.gif"); try { //输出二维码图片 MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); } catch (IOException e) { e.printStackTrace(); } } @Test public void testDecoder() throws NotFoundException { //二维码图片路径 String path = this.getClass().getClassLoader().getResource("1.gif").getPath(); File imageFile = new File(path); BufferedImage image = null; try { image = ImageIO.read(imageFile); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); //解码获取二维码中信息 Result result = new MultiFormatReader().decode(binaryBitmap, hints); System.out.println(result.getText()); } catch (IOException e) { e.printStackTrace(); } } }
以上是关于Java二维码生成与解码工具Zxing使用的主要内容,如果未能解决你的问题,请参考以下文章