zxing实现二维码生成和解析

转自:http://kesun-shy.iteye.com/blog/2154169

 

二维码的生成与解析。有多种途径。我选择用大品牌,google老大的zxing。

gitHub链接是(我用的3.0.0,已经是nio了)

https://github.com/zxing/zxing/tree/zxing-3.0.0

 参考文档:http://zxing.github.io/zxing/

 

Java代码  收藏代码
  1. // 其中输出图像和读取图像的类在core包  
  2.   
  3. MultiFormatReader  
  4.   
  5. MultiFormatWriter  
  6.   
  7. // 生成矩阵的类在javase的包里  
  8.   
  9. MatrixToImageWriter  

 

pom.xml中的配置为

Xml代码  收藏代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>com.shihy</groupId>  
  6.     <artifactId>qrcode</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>qrcode</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <dependencies>  
  18.         <dependency>  
  19.             <groupId>com.google.zxing</groupId>  
  20.             <artifactId>core</artifactId>  
  21.             <version>3.0.0</version>  
  22.         </dependency>  
  23.         <dependency>  
  24.             <groupId>com.google.zxing</groupId>  
  25.             <artifactId>javase</artifactId>  
  26.             <version>3.0.0</version>  
  27.         </dependency>  
  28.         <dependency>  
  29.             <groupId>junit</groupId>  
  30.             <artifactId>junit</artifactId>  
  31.             <version>4.10</version>  
  32.         </dependency>  
  33.         <dependency>  
  34.             <groupId>com.alibaba</groupId>  
  35.             <artifactId>fastjson</artifactId>  
  36.             <version>1.1.29</version>  
  37.         </dependency>  
  38.     </dependencies>  
  39. </project>  

 

生成图像与解析图像的测试工具类:

Java代码  收藏代码
  1. package com.polysaas.edu.qrcode;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import java.nio.file.FileSystems;  
  7. import java.nio.file.Path;  
  8. import java.util.HashMap;  
  9. import java.util.Map;  
  10.   
  11. import javax.imageio.ImageIO;  
  12.   
  13. import org.junit.Test;  
  14.   
  15. import com.alibaba.fastjson.JSONObject;  
  16. import com.google.zxing.BarcodeFormat;  
  17. import com.google.zxing.Binarizer;  
  18. import com.google.zxing.BinaryBitmap;  
  19. import com.google.zxing.DecodeHintType;  
  20. import com.google.zxing.EncodeHintType;  
  21. import com.google.zxing.LuminanceSource;  
  22. import com.google.zxing.MultiFormatReader;  
  23. import com.google.zxing.MultiFormatWriter;  
  24. import com.google.zxing.NotFoundException;  
  25. import com.google.zxing.Result;  
  26. import com.google.zxing.WriterException;  
  27. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;  
  28. import com.google.zxing.client.j2se.MatrixToImageWriter;  
  29. import com.google.zxing.common.BitMatrix;  
  30. import com.google.zxing.common.HybridBinarizer;  
  31.   
  32. public class QRCodeTest {  
  33.   
  34.     /** 
  35.      * 生成图像 
  36.      *  
  37.      * @throws WriterException 
  38.      * @throws IOException 
  39.      */  
  40.     @Test  
  41.     public void testEncode() throws WriterException, IOException {  
  42.         String filePath = "D://";  
  43.         String fileName = "zxing.png";  
  44.         JSONObject json = new JSONObject();  
  45.         json.put(  
  46.                 "zxing",  
  47.                 "https://github.com/zxing/zxing/tree/zxing-3.0.0/javase/src/main/java/com/google/zxing");  
  48.         json.put("author", "shihy");  
  49.         String content = json.toJSONString();// 内容  
  50.         int width = 200; // 图像宽度  
  51.         int height = 200; // 图像高度  
  52.         String format = "png";// 图像类型  
  53.         Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();  
  54.         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
  55.         BitMatrix bitMatrix = new MultiFormatWriter().encode(content,  
  56.                 BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵  
  57.         Path path = FileSystems.getDefault().getPath(filePath, fileName);  
  58.         MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像  
  59.         System.out.println("输出成功.");  
  60.     }  
  61.   
  62.     /** 
  63.      * 解析图像 
  64.      */  
  65.     @Test  
  66.     public void testDecode() {  
  67.         String filePath = "D://zxing.png";  
  68.         BufferedImage image;  
  69.         try {  
  70.             image = ImageIO.read(new File(filePath));  
  71.             LuminanceSource source = new BufferedImageLuminanceSource(image);  
  72.             Binarizer binarizer = new HybridBinarizer(source);  
  73.             BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);  
  74.             Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();  
  75.             hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");  
  76.             Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码  
  77.             JSONObject content = JSONObject.parseObject(result.getText());  
  78.             System.out.println("图片中内容:  ");  
  79.             System.out.println("author: " + content.getString("author"));  
  80.             System.out.println("zxing:  " + content.getString("zxing"));  
  81.             System.out.println("图片中格式:  ");  
  82.             System.out.println("encode: " + result.getBarcodeFormat());  
  83.         } catch (IOException e) {  
  84.             e.printStackTrace();  
  85.         } catch (NotFoundException e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.     }  
  89. }  

 

测试生成图像为:



 

 

 

解析到的内容为:

Java代码  收藏代码
  1. 图片中内容:    
  2. author: shihy  
  3. zxing:  https://github.com/zxing/zxing/tree/zxing-3.0.0/javase/src/main/java/com/google/zxing  
  4. 图片中格式:    
  5. encode: QR_CODE  

 

posted @ 2015-11-03 16:56  明明是悟空  阅读(711)  评论(0编辑  收藏  举报