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/

  1. // 其中输出图像和读取图像的类在core包
  2. MultiFormatReader
  3. MultiFormatWriter
  4. // 生成矩阵的类在javase的包里
  5. MatrixToImageWriter

pom.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. <groupId>com.shihy</groupId>
  5. <artifactId>qrcode</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>qrcode</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>com.google.zxing</groupId>
  16. <artifactId>core</artifactId>
  17. <version>3.0.0</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>com.google.zxing</groupId>
  21. <artifactId>javase</artifactId>
  22. <version>3.0.0</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>junit</groupId>
  26. <artifactId>junit</artifactId>
  27. <version>4.10</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>com.alibaba</groupId>
  31. <artifactId>fastjson</artifactId>
  32. <version>1.1.29</version>
  33. </dependency>
  34. </dependencies>
  35. </project>

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

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

测试生成图像为:

zxing实现二维码生成和解析

解析到的内容为:

  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
上一篇:Expression Blend实例中文教程(3) - 布局控件快速入门Grid


下一篇:再起航,我的学习笔记之JavaScript设计模式21(命令模式)