1,添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.github.liuyueyi.media</groupId>
<artifactId>qrcode-plugin</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
2,application.yml配置上传文件大小
spring:
servlet:
multipart:
max-file-size: 50MB
max-request-size: 100MB
3,创建QrCodeUtil类,生成base64编码,前面拼接”data:image/png;base64,”
package com.huan.util;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.WriterException;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
/**
* @author zyhstart
* @create 2021-10-04-14:57
*/
public class QrCodeUtil {
//普通二维码
public static String normal(String text) throws IOException, WriterException {
return QrCodeGenWrapper.of(text)
.asString();
}
//logo二维码
public static String logo(String text, InputStream logoFile) throws IOException, WriterException {
return QrCodeGenWrapper.of(text)
.setLogo(logoFile) // logo地址 支持网络图片,本地相对路劲图片,本地绝对路径图片
.setLogoRate(7)
.setLogoStyle(QrCodeOptions.LogoStyle.ROUND)
.asString();
}
//logo二维码
public static String logo2(String text, String logoPath) throws IOException, WriterException {
return QrCodeGenWrapper.of(text)
.setLogo(logoPath) //logo地址 支持网络图片,本地相对路劲图片,本地绝对路径图片
.setBgH(400).setBgW(400)
.setLogoStyle(QrCodeOptions.LogoStyle.ROUND) //圆角
.setLogoBgColor(0xfffefefe)
.setLogoBorderBgColor(0xffc7c7c7)
.setLogoBorder(true)
.asString();
}
//彩色二维码
public static String color(String text) throws IOException, WriterException {
return QrCodeGenWrapper.of(text)
.setDrawPreColor(Color.RED)
.asString();
}
//背景色二维码
public static String bg(String text,InputStream bgFile) throws IOException, WriterException {
return QrCodeGenWrapper.of(text)
.setBgImg(bgFile)
.setBgStyle(QrCodeOptions.BgImgStyle.PENETRATE)
.setBgH(500)
.setBgW(500)
.setW(500)
.setH(500)
.asString();
}
//特殊二维码
public static String style(String text) throws IOException, WriterException {
return QrCodeGenWrapper.of(text)
.setBgH(500)
.setBgW(500)
.setW(500)
.setH(500)
.setDrawEnableScale(true)
.setDrawStyle(QrCodeOptions.DrawStyle.CIRCLE)
.asString();
}
//图片填充二维码
public static String fill(String text,InputStream bgFile) throws IOException, WriterException {
return QrCodeGenWrapper.of(text)
.setW(500)
.setH(500)
.setDrawEnableScale(true)
.setErrorCorrection(ErrorCorrectionLevel.H)
.setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)
.addImg(1,1,bgFile)
.asString();
}
//gif二维码
public static String gif(String text,InputStream bgFile) throws IOException, WriterException {
return QrCodeGenWrapper.of(text)
.setW(500)
.setH(500)
.setBgImg(bgFile)
.setBgOpacity(0.5f)
.setPicType("gif")
.asString();
}
}