SpringBoot + zxing 生成二维码

SpringBoot + qrcode生成二维码

原视频地址: Java生成二维码教程 | 两小时学会Java生成二维码

源码地址: JasonsGong/two-dimensional-code: 使用java生成二维码 (github.com)

一.谷歌zxing开源库生成二维码

1.创建一个sprinBoot项目

image-20230828215513289

2.引入相关的依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

3.在templates目录下新建index.html文件

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

</body>
</html>

4.编写controller跳转到该页面,完成后重启项目,访问localhost:8080 测试

1
2
3
4
5
6
7
8
@Controller
public class CodeController {

@GetMapping("/")
public String index(){
return "index";
}
}

5.打开index.html,编写前端页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>二维码</title>
</head>
<body>
<h1>谷歌zxing开源库生成黑白二维码</h1>
<hr>
请输入文本内容:<input type="text" id="url"><button onclick="generateQRCode()">生成二维码</button><br>
<img id="qrCodeImg">
<script>
function generateQRCode(){
//获取url
let url = document.getElementById("url").value
//设置img标签的src属性
let qrCodeImg = document.getElementById("qrCodeImg")
qrCodeImg.src = "/generate?url="+ url
}
</script>
</body>
</html>

由于后端部分没有编写,这时我们访问项目的时候会返回404

image-20230828223201038

6.后端代码的编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.mine.code.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.aztec.encoder.AztecCode;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* @author Jason Gong
* @version 1.0
* @website https://qingling.icu
* @Date 2023/8/28
* @Description
*/
@Slf4j
@Controller
public class CodeController {

@GetMapping("/")
public String index() {
return "index";
}


@GetMapping("/generate")
public String generate(@RequestParam("url") String url, HttpServletResponse response) {
log.info("文本内容:{}", url);
try {
//创建一个map集合,存储二维码的相关属性
Map map = new HashMap<>();
//EncodeHintType 编码的提示类型
//设置二维码的误差校正级别 可选值有 L(7%) M(15%) Q(25%) H(30%)
//选择L级别的容错率,相当于允许二维码在整体的颜色区域中,最多有7%的坏像素点;择H级别的容错率,相当于允许二维码在整体的颜色区域中,最多有30%的坏像素点
map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//设置二维码的字符集
map.put(EncodeHintType.CHARACTER_SET, "utf-8");
//设置二维码四周的留白 1表示1像素
map.put(EncodeHintType.MARGIN, 1);
//创建zxing的核心对象MultiFormatWriter (多格式写入器)
//通过MultiFormatWriter对象来生成二维码
MultiFormatWriter writer = new MultiFormatWriter();
//参数一:内容
//参数二:二维码格式
//BarcodeFormat(码格式) QR_CODE :常见的二维码格式之一,广泛应用于商品包装、扫码支付
//AZTEC_CODE:高密度,可靠性很高 容错率更低 储存个人信息、证件信息、账户密码
//PDF417 可以存储大量的信息 数据密度高 应用于航空机票、配送标签、法律文件
//DATA_MATRIX: 小巧的二维码格式 编码格式类似于QR_CODE 但是优于QR_CODE 适合嵌入简单的产品标签 医疗图像 检测数据
//参数三四:二维码的宽度和高度
//参数五:二维码参数
//位矩阵对象 (位矩阵对象对象的内部实际上是一个二位数组,二维数组中每一个元素是boolean类型 true代表黑色 false代表白色)
BitMatrix bitMatrix = writer.encode(url, BarcodeFormat.QR_CODE, 300, 300, map);
//获取矩阵的宽度和高度
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
//生成二维码图片
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
//遍历位矩阵对象
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
//设置每一块的颜色值
//0xFF000000表示黑色 0xFFFFFFFF表示白色
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
//将图片响应到客户端
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image,"png",out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

7.测试生成二维码

image-20230828233052417

完整的项目结构

image-20230828233917645

8.生成一个带Logo的黑白二维码

1.编写qrcode.html页面和跳转到该页面的controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>生成带logo的黑白二维码</title>
</head>
<body>
<form action="/generateWithLogo" method="post" enctype="multipart/form-data">
请输入文本内容:<input type="text" name="url"><br>
请选择logo图片:<input type="file" name="logo"><br>
<input type="submit" value="生成二维码">
</form>
</body>
</html>
1
2
3
4
5
6
7
/**
* 跳转到生成带logo的黑白二维码
*/
@GetMapping("/logo")
public String toLogo() {
return "qrcode";
}

2.编写后端生成二维码逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 生成带logo的黑白二维码
*/
@PostMapping("/generateWithLogo")
public String generateWithLogo(@RequestParam("url") String url, HttpServletResponse response, HttpServletRequest request) {
try {
Map map = new HashMap<>();
map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
map.put(EncodeHintType.CHARACTER_SET, "utf-8");
map.put(EncodeHintType.MARGIN, 1);
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix bitMatrix = writer.encode(url, BarcodeFormat.QR_CODE, 300, 300, map);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
//给二维码添加logo
//1.获取logo
Part logoPart = request.getPart("logo");
InputStream inputStream = logoPart.getInputStream();
BufferedImage logoImage = ImageIO.read(inputStream);
//2.对获取的logo图片进行缩放
int logoWidth = logoImage.getWidth(null);
int logoHeight = logoImage.getHeight(null);
if (logoWidth > 60){
logoWidth = 60;
}
if (logoHeight > 60){
logoHeight = 60;
}
//使用平滑缩放算法对原始的logo图像进行缩放到一个全新的图像
Image scaledLogo = logoImage.getScaledInstance(logoWidth, logoHeight, Image.SCALE_SMOOTH);
//3.将缩放的图片画在黑白的二维码上
//获取一个画笔
Graphics2D graphics2D = image.createGraphics();
//计算从哪里开始画 300指的是二维码的宽度和高度
int x = (300 - logoWidth) /2;
int y = (300 - logoHeight) /2;
//画上去
graphics2D.drawImage(scaledLogo,x,y,null);
//实现logo的圆角效果
Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 10, 10);
//使用一个宽度为4像素的基本笔触
graphics2D.setStroke(new BasicStroke(4f));
//给logo画圆角矩形
graphics2D.draw(shape);
//释放画笔
graphics2D.dispose();
//将二维码响应到浏览器
ImageIO.write(image, "png", response.getOutputStream());
//关闭流
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

访问 http://localhost:8080/logo 测试

实现的效果

image-20230829160901904

image-20230829160938795

二.github开源项目qrcode生成二维码

1.引入依赖文件

这里就不在重新创建工程了,直接在上面工程的基础上操作

1
2
3
4
5
<dependency>
<groupId>com.github.liuyueyi.media</groupId>
<artifactId>qrcode-plugin</artifactId>
<version>2.5.2</version>
</dependency>

2.生成黑白二维码

1.创建github-qrcode.html文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>qrcode生成黑白二维码</title>
</head>
<body>
<h1>使用github上的开源项目qrcode生成二维码</h1>
<hr>
<form action="/generateWithQrCode" method="post" enctype="multipart/form-data">
请输入文本内容:<input type="text" name="url"><br>
请选择logo图片:<input type="file" name="logo"><br>
<input type="submit" value="生成二维码">
</form>
</body>
</html>

2.编写controller处理请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.mine.code.controller;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeDeWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.google.zxing.WriterException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
* @author Jason Gong
* @version 1.0
* @website https://qingling.icu
* @Date 2023/8/29
* @Description 使用qrcode生成二维码
*/
@Controller
public class GithubQrCodeController {
/**
* 编写请求跳转到使用qrcode生成二维码的页面
*/
@GetMapping("/qrcode")
public String toQrCode(){
return "github-qrcode";
}


/**
* 使用qrcode生成黑白二维码
*/
@PostMapping("/generateWithQrCode")
public String generateWithQrCode(@RequestParam("url") String url, HttpServletResponse response, HttpServletRequest request){
try {
//生成二维码
BufferedImage image = QrCodeGenWrapper.of(url).asBufferedImage();
//将生成的二维码响应到浏览器
ImageIO.write(image,"png",response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

3.访问测试

image-20230829223442421

3.生成带有logo的黑白二维码

前端页面延续使用上面的github-qrcode.html

1.后端代码的编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.mine.code.controller;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeDeWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.WriterException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

/**
* @author Jason Gong
* @version 1.0
* @website https://qingling.icu
* @Date 2023/8/29
* @Description 使用qrcode生成二维码
*/
@Controller
public class GithubQrCodeController {
/**
* 编写请求跳转到使用qrcode生成二维码的页面
*/
@GetMapping("/qrcode")
public String toQrCode(){
return "github-qrcode";
}


/**
* 使用qrcode生成黑白二维码
*/
@PostMapping("/generateWithQrCode")
public String generateWithQrCode(@RequestParam("url") String url, HttpServletResponse response, HttpServletRequest request){
try {
//生成二维码
//生成黑白的二维码
//BufferedImage image = QrCodeGenWrapper.of(url).asBufferedImage();
//生成带logo的黑白二维码
InputStream inputStream = request.getPart("logo").getInputStream();
BufferedImage image = QrCodeGenWrapper.of(url)
.setLogo(inputStream)//logo图片的输入流
.setLogoRate(7)//设置logo图片和二维码之间的比例,7表示logo的宽度等于二维码的1/7
.setLogoStyle(QrCodeOptions.LogoStyle.ROUND)//设置logo图片的样式,将logo的边框形状设置成圆形
.asBufferedImage();
//将生成的二维码响应到浏览器
ImageIO.write(image,"png",response.getOutputStream());
//关闭流
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

实现的效果

image-20230829224339809

4.生成彩色的二维码

前端页面延续使用上面的github-qrcode.html

1.后端代码的编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.mine.code.controller;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeDeWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.WriterException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

/**
* @author Jason Gong
* @version 1.0
* @website https://qingling.icu
* @Date 2023/8/29
* @Description 使用qrcode生成二维码
*/
@Controller
public class GithubQrCodeController {
/**
* 编写请求跳转到使用qrcode生成二维码的页面
*/
@GetMapping("/qrcode")
public String toQrCode(){
return "github-qrcode";
}


/**
* 使用qrcode生成黑白二维码
*/
@PostMapping("/generateWithQrCode")
public String generateWithQrCode(@RequestParam("url") String url, HttpServletResponse response, HttpServletRequest request){
try {
//生成彩色的二维码
BufferedImage image = QrCodeGenWrapper.of(url).setDrawPreColor(Color.GREEN).asBufferedImage();
//将生成的二维码响应到浏览器
ImageIO.write(image,"png",response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

实现的效果

image-20230829225017032

5.生成带有背景图的二维码

前端页面延续使用上面的github-qrcode.html

1.后端代码的编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.mine.code.controller;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeDeWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.WriterException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

/**
* @author Jason Gong
* @version 1.0
* @website https://qingling.icu
* @Date 2023/8/29
* @Description 使用qrcode生成二维码
*/
@Controller
public class GithubQrCodeController {
/**
* 编写请求跳转到使用qrcode生成二维码的页面
*/
@GetMapping("/qrcode")
public String toQrCode(){
return "github-qrcode";
}


/**
* 使用qrcode生成黑白二维码
*/
@PostMapping("/generateWithQrCode")
public String generateWithQrCode(@RequestParam("url") String url, HttpServletResponse response, HttpServletRequest request){
try {
//生成带有背景图的二维码
InputStream inputStream = request.getPart("logo").getInputStream();
BufferedImage image = QrCodeGenWrapper.of(url)
.setBgImg(inputStream)
.setBgOpacity(0.5F)//设置透明度
.asBufferedImage();
//将生成的二维码响应到浏览器
ImageIO.write(image,"png",response.getOutputStream());
//关闭流
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

实现的效果

image-20230829225740156

6.生成特殊形状的二维码

前端页面延续使用上面的github-qrcode.html

1.后端代码的编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.mine.code.controller;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeDeWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.WriterException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

/**
* @author Jason Gong
* @version 1.0
* @website https://qingling.icu
* @Date 2023/8/29
* @Description 使用qrcode生成二维码
*/
@Controller
public class GithubQrCodeController {
/**
* 编写请求跳转到使用qrcode生成二维码的页面
*/
@GetMapping("/qrcode")
public String toQrCode(){
return "github-qrcode";
}


/**
* 使用qrcode生成黑白二维码
*/
@PostMapping("/generateWithQrCode")
public String generateWithQrCode(@RequestParam("url") String url, HttpServletResponse response, HttpServletRequest request){
try {
//生成特殊形状的二维码
BufferedImage image = QrCodeGenWrapper.of(url)
.setDrawEnableScale(true)//启用二维码绘制时的缩放功能
.setDrawStyle(QrCodeOptions.DrawStyle.DIAMOND)//绘制钻石形状的二维码
.asBufferedImage();
//将生成的二维码响应到浏览器
ImageIO.write(image,"png",response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

实现的效果

image-20230829230236500

7.生成图片填充二维码

前端页面延续使用上面的github-qrcode.html

1.后端代码的编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.mine.code.controller;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeDeWrapper;
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 org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

/**
* @author Jason Gong
* @version 1.0
* @website https://qingling.icu
* @Date 2023/8/29
* @Description 使用qrcode生成二维码
*/
@Controller
public class GithubQrCodeController {
/**
* 编写请求跳转到使用qrcode生成二维码的页面
*/
@GetMapping("/qrcode")
public String toQrCode() {
return "github-qrcode";
}


/**
* 使用qrcode生成黑白二维码
*/
@PostMapping("/generateWithQrCode")
public String generateWithQrCode(@RequestParam("url") String url, HttpServletResponse response, HttpServletRequest request) {
try {
//生成图片填充二维码
InputStream inputStream = request.getPart("logo").getInputStream();
BufferedImage image = QrCodeGenWrapper.of(url)
.setErrorCorrection(ErrorCorrectionLevel.H)//设置二维码的纠正级别
.setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)//绘制二维码的时候采用图片填充
.addImg(1, 1, inputStream)//添加图片
.asBufferedImage();
//将生成的二维码响应到浏览器
ImageIO.write(image, "png", response.getOutputStream());
//关闭流
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

实现的效果

二维码中的每一个像素点都是上传的图片,生成的速度很慢

image-20230829230843477