使用ZXing在Java后台生成和读取二维码

关于ZXing
ZXing,一个支持在图像中解码和生成条形码(如二维码、PDF 417、EAN、UPC、Aztec、Data Matrix、Codabar)的库。ZXing(“zebra crossing”)是一个开源的、多格式的、用Java实现的一维/二维条码图像处理库,具有到其他语言的端口。
 
上手使用
pom文件引入相关依赖
 1 <!-- ZXing -->
 2 <dependency>
 3     <groupId>com.google.zxing</groupId>
 4     <artifactId>core</artifactId>
 5     <version>3.3.3</version>
 6 </dependency>
 7 <dependency>
 8     <groupId>com.google.zxing</groupId>
 9     <artifactId>javase</artifactId>
10     <version>3.3.3</version>
11 </dependency>

 

二维码生成

 1     /**
 2      * 二维码生成
 3      *
 4      * @throws WriterException
 5      * @throws IOException
 6      */
 7     public static void QREncode() throws WriterException, IOException {
 8         String content = "Hello ZXing!";
 9         int width = 200; // 宽度
10         int height = 200; // 高度
11         String format = "png"; // 图片类型
12         Map<EncodeHintType, Object> hints = new HashMap<>();
13         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置内容编码格式
14         hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 设置纠错等级
15         hints.put(EncodeHintType.MARGIN, 1); //设置二维码边的空度,非负数
16         BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
17         //MatrixToImageWriter.writeToPath(bitMatrix, format, new File("src/main/resources/static/image/qr/qr1.png").toPath());
18 
19         /*
20         存在问题:能够正常生成二维码,但是生成二维码logo会变成黑白色
21         原因:MatrixToImageConfig默认黑白,需要设置BLACK、WHITE
22         解决:https://ququjioulai.iteye.com/blog/2254382
23          */
24         MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
25         BufferedImage bufferedImage = LogoMatrix(MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig), new File("src/main/resources/static/image/qr/头像.jpeg"));
26         ImageIO.write(bufferedImage, "png", new File("src/main/resources/static/image/qr/qr2.png"));
27         System.out.println("输出成功");
28     }

 

二维码添加logo

 1 /**
 2 * 二维码添加logo
 3 *
 4 * @param martixImage 源二维码图片
 5 * @param logoFile    logo图片
 6 * @return 返回带有logo的二维码图片
 7 * @throws IOException
 8 */
 9 public static BufferedImage LogoMatrix(BufferedImage martixImage, File logoFile) throws IOException {
10     Graphics2D graphics2D = martixImage.createGraphics();
11     int martixWidth = martixImage.getWidth();
12     int martixHeight = martixImage.getHeight();
13  
14  
15     // 读取Logo图片
16     BufferedImage logo = ImageIO.read(logoFile);
17     // 开始绘制图片
18     graphics2D.drawImage(logo, martixWidth / 5 * 2, martixHeight / 5 * 2, martixWidth / 5, martixHeight / 5, null); // 绘制
19     BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
20     graphics2D.setStroke(stroke); // 设置笔画对象
21     // 指定弧度的圆角矩形
22     RoundRectangle2D.Float round = new RoundRectangle2D.Float(martixWidth / 5 * 2, martixHeight / 5 * 2, martixWidth / 5, martixHeight / 5, 20, 20);
23     graphics2D.setColor(Color.WHITE);
24     graphics2D.draw(round); // 绘制圆弧矩形
25  
26  
27     // 设置logo有一道灰色边框
28     BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
29     graphics2D.setStroke(stroke2); // 设置笔画对象
30     RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(martixWidth / 5 * 2 + 2, martixHeight / 5 * 2 + 2, martixWidth / 5 - 4, martixHeight / 5 - 4, 20, 20);
31     graphics2D.setColor(new Color(128, 128, 128));
32     graphics2D.draw(round2);
33  
34  
35     graphics2D.dispose();
36     martixImage.flush();
37     return martixImage;
38 }

 

二维码识别
 1 /**
 2 * 二维码识别
 3 * @param file 二维码图片
 4 * @throws IOException
 5 * @throws NotFoundException
 6 */
 7 public static void QRReader(File file) throws IOException, NotFoundException {
 8     MultiFormatReader formatReader = new MultiFormatReader();
 9     // 读取指定的二维码文件
10     BufferedImage bufferedImage = ImageIO.read(file);
11     BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
12     // 定义二维码参数
13     Map hints = new HashMap<EncodeHintType, Object>();
14     hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
15     Result result = formatReader.decode(binaryBitmap, hints);
16     System.out.println("解析结果:"+ result.toString());
17     System.out.println("二维码格式类型:"+ result.getBarcodeFormat());
18     System.out.println("二维码文本内容:"+ result.getText());
19     bufferedImage.flush();
20 }

 

posted @ 2020-09-26 11:46  JKLOPPdream  阅读(522)  评论(0编辑  收藏  举报