Java实现二维码生成 Google-Zxing

Java实现二维码生成 Google-Zxing本篇文章将介绍 java 中如何生成二维码 二维码的展示主要包括两各方面 1 直接生成图片 直接生成图片不需要 web 程序 maven 工程即可 2 将二维码转为字节数组 然后在 web 页面显示 web 项目的目录结构以前面的一篇文章中的目录结构为基础 传送门 生成二维码的功能主要是依赖 Google 的 Zxing 包 1 添加 Zxing 的依赖 maven 工程为例 lt depend

本篇文章将介绍java中如何生成二维码,二维码的展示主要包括两各方面:1.直接生成图片(直接生成图片不需要web程序,maven工程即可) 2.将二维码转为字节数组,然后在web页面显示。web项目的目录结构以前面的一篇文章中的目录结构为基础(传送门)。生成二维码的功能主要是依赖Google的Zxing包。

1.添加Zxing的依赖(maven工程为例)

 
   
   
     com.google.zxing 
    
   
     core 
    
   
     3.3.0 
    
   
   
   
     com.google.zxing 
    
   
     javase 
    
   
     3.3.0 
    
  

2.保存信息为二维码图片

名为generateQRCodeImage方法,将字符串封装成二维码、设置二维码的宽度和高度、声明二维码保存的路径与图片名称。

package org.thinkingingis.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { private static final String QR_CODE_IMAGE_PATH = "/Users/gisboy/Desktop/MyQRCode.png"; private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); Path path = FileSystems.getDefault().getPath(filePath); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); } public static void main(String[] args) { try { generateQRCodeImage("This is my first QR Code", 350, 350, QR_CODE_IMAGE_PATH); } catch (WriterException e) { System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage()); } catch (IOException e) { System.out.println("Could not generate QR Code, IOException :: " + e.getMessage()); } } } 

上面的代码将会生成一个内容为“This is my first QR Code”二维码,并保存在桌面,如下图:

Java实现二维码生成 Google-Zxing

上面的方法是将二维码保存为图片,如果你不想将二维码保存为图片,也可以将其保存为字节数组,可以用zxing 库提供的MatrixToImageWriter.writeToStream()方法:

 public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream); byte[] pngData = pngOutputStream.toByteArray(); return pngData; }

这个方法可以将字节数组在web页面展示为图片形式。结合Spring Boot + Thymeleaf搭建的web工程,如果想在页面显示该二维码信息的话,需要在html页面和controller中完成如下代码:

qrcode.html

 
   
    
   

QrCodeImage

qrcode

DefaultController.java

 @GetMapping("qrcode") public String qrcode() { return "/qrcode"; } @GetMapping(value="/qrimage") public ResponseEntity 
  
    getQRImage() { //二维码内的信息 String info = "This is my first QR Code"; byte[] qrcode = null; try { qrcode = QRCodeGenerator.getQRCodeImage(info, 360, 360); } catch (WriterException e) { System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.out.println("Could not generate QR Code, IOException :: " + e.getMessage()); } // Set headers final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); return new ResponseEntity 
   
     (qrcode, headers, HttpStatus.CREATED); } 
    
  

请注意qrcode.html中标签中 src属性的值。

启动spring boot程序,访问该页面,运行结果如下图:

Java实现二维码生成 Google-Zxing

源码地址:https://github.com/ThinkingInGIS/spring-boot-security

Java实现二维码生成 Google-Zxing

如果觉得本文对你有帮助,是可以赞赏作者的哦

Java实现二维码生成 Google-Zxing

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216276.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午12:11
下一篇 2026年3月18日 下午12:12


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号