Springboot整合Zxing生成二维码
本篇是利用SpringBoot后端整合Zxing生成二维码。
原文参考链接:https://www.jianshu.com/p/a046ff84226e
1.在pom文件中引入项目所需依赖
<!--谷歌二维码-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
</dependencies>
2.编写QRcodeUtil工具类
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* 二维码生成解析工具类
* @author 程就人生
* @date 2019年7月27日
* @Description
*
*/
public class QRCodeUtil {
//编码格式,采用utf-8
private static final String UNICODE = "utf-8";
//图片格式
private static final String FORMAT = "JPG";
//二维码宽度,单位:像素pixels
private static final int QRCODE_WIDTH = 300;
//二维码高度,单位:像素pixels
private static final int QRCODE_HEIGHT = 300;
//LOGO宽度,单位:像素pixels
private static final int LOGO_WIDTH = 100;
//LOGO高度,单位:像素pixels
private static final int LOGO_HEIGHT = 100;
/**
* 生成二维码图片
* @param content 二维码内容
* @param logoPath 图片地址
* @param needCompress 是否压缩
* @return
* @throws Exception
*/
private static BufferedImage createImage(String content, String logoPath, boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, UNICODE)<