How to Read Or Parse QR Code in Java

阳光穿透心脏的1/2处 2024-02-18 21:53 104阅读 0赞

摘要:有个群友在群里问,二维码生成和解析用java怎么做,正好有空,我也写了下,记录一下,有需要的同学可以参考下,这里主要是使用了Google的一个开源包zxing。

一:导入zxing的依赖,如下:

  1. <dependency>
  2. <groupId>com.google.zxing</groupId>
  3. <artifactId>core</artifactId>
  4. <version>3.2.1</version>
  5. </dependency>

二:ZxingUtils工具类,其中包括生成二维码,解析二维码方法:

  1. package com.micai.springboot.util;
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.EnumMap;
  8. import java.util.Map;
  9. import javax.imageio.ImageIO;
  10. import com.google.zxing.*;
  11. import com.google.zxing.common.BitMatrix;
  12. import com.google.zxing.common.HybridBinarizer;
  13. import com.google.zxing.qrcode.QRCodeReader;
  14. import com.google.zxing.qrcode.QRCodeWriter;
  15. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  16. /**
  17. * @Auther: zhaoxinguo
  18. * @Date: 2018/7/24 15:00
  19. * @Description: 二维码工具类
  20. */
  21. public class ZxingUtils {
  22. /**
  23. * 生成二维码
  24. * @param fromUrl
  25. * @param toUrl
  26. */
  27. public static void createQrCode(String fromUrl, String toUrl) {
  28. String myCodeText = fromUrl;
  29. String filePath = toUrl;
  30. int size = 250;
  31. String fileType = "png";
  32. File myFile = new File(filePath);
  33. try {
  34. Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
  35. hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
  36. // Now with zxing version 3.2.1 you could change border size (white border size to just 1)
  37. hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
  38. hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
  39. QRCodeWriter qrCodeWriter = new QRCodeWriter();
  40. BitMatrix byteMatrix = qrCodeWriter.encode(myCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
  41. int CrunchifyWidth = byteMatrix.getWidth();
  42. BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
  43. image.createGraphics();
  44. Graphics2D graphics = (Graphics2D) image.getGraphics();
  45. graphics.setColor(Color.WHITE);
  46. graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
  47. graphics.setColor(Color.BLACK);
  48. for (int i = 0; i < CrunchifyWidth; i++) {
  49. for (int j = 0; j < CrunchifyWidth; j++) {
  50. if (byteMatrix.get(i, j)) {
  51. graphics.fillRect(i, j, 1, 1);
  52. }
  53. }
  54. }
  55. ImageIO.write(image, fileType, myFile);
  56. } catch (WriterException e) {
  57. e.printStackTrace();
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. }
  61. System.out.println("\n\nYou have successfully created QR Code.");
  62. }
  63. /**
  64. * 解析二维码
  65. * @param filePath
  66. * @return
  67. */
  68. public static String readQRCode(String filePath) {
  69. File file = new File(filePath);
  70. BufferedImage image = null;
  71. BinaryBitmap bitmap = null;
  72. Result result = null;
  73. try {
  74. image = ImageIO.read(file);
  75. int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
  76. RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(), image.getHeight(), pixels);
  77. bitmap = new BinaryBitmap(new HybridBinarizer(source));
  78. } catch (IOException e) {
  79. // TODO Auto-generated catch block
  80. e.printStackTrace();
  81. }
  82. if (bitmap == null)
  83. return null;
  84. QRCodeReader reader = new QRCodeReader();
  85. try {
  86. result = reader.decode(bitmap);
  87. return result.getText();
  88. } catch (NotFoundException e) {
  89. // TODO Auto-generated catch block
  90. e.printStackTrace();
  91. } catch (ChecksumException e) {
  92. // TODO Auto-generated catch block
  93. e.printStackTrace();
  94. } catch (FormatException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. }
  98. return null;
  99. }
  100. public static void main(String [] args) {
  101. String myCodeText = "https://blog.csdn.net/sxdtzhaoxinguo/";
  102. String filePath = "D://CrunchifyQR.png";
  103. // createQrCode(myCodeText, filePath);
  104. String readQRCode = readQRCode(filePath);
  105. System.out.println(readQRCode);
  106. }
  107. }

三:运行结果:

1.生成二维码:

70

2.解析二维码:

70 1

这样就实现了java生成二维码和解析二维码,这里只是简单的实现,zxing里面包含了好多api,有兴趣的同学可以自己研究,这里放个zxing的api地址:https://zxing.github.io/zxing/apidocs/index.html

发表评论

表情:
评论列表 (有 0 条评论,104人围观)

还没有评论,来说两句吧...

相关阅读