首页 归档 关于 文件 Github
×

zxing开发之二维码

2023-09-08 10:51:16
J-Tools
  • QR
本文总阅读量(次):
本文字数统计(字):1.3k
本文阅读时长(分):7

ZXing是Google开发的用于Java,Android的条形码扫描库。

依赖

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>

代码

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.yetech.hello.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* 参考 https://www.wetools.com/qrcode
*/
public class QrCodeUtil {

// 二维码内容
private String content;
// 二维码大小-宽
private int qrWidth;
// 二维码大小-高
private int qrHeight;
// 二维码边框-内边距
private int qrMargin = 0;
// 二维码前景色
private Color onColor = new Color(0, 0, 0);
// 二维码背景色
private Color offColor = new Color(255, 255, 255);
// Logo
private InputStream logoPath;
// Logo 边框宽度
private int logoBorder = 0;
// Logo 边框背景色
private Color logoBorderColor = Color.WHITE;
// Logo 圆角大小
private int logoRoundCorner = 0;

// 属性
private Map<EncodeHintType, Object> hints;

public static QrCodeUtil Builder() {
return new QrCodeUtil();
}

public QrCodeUtil setHints(Map<EncodeHintType, Object> hints) {
this.hints = hints;
return this;
}

public QrCodeUtil setContent(String content) {
this.content = content;
return this;
}

public QrCodeUtil setSize(int width, int height) {
this.qrWidth = width;
this.qrHeight = height;
return this;
}

public QrCodeUtil setColor(Color onColor, Color offColor) {
this.onColor = onColor;
this.offColor = offColor;
return this;
}

public QrCodeUtil setMargin(int margin) {
this.qrMargin = margin;
return this;
}

public QrCodeUtil setLogo(FileInputStream logoPath) {
this.logoPath = logoPath;
return this;
}

public QrCodeUtil setLogoBorder(int logoBorder, Color logoBorderColor) {
this.logoBorder = logoBorder;
this.logoBorderColor = logoBorderColor;
return this;
}

public QrCodeUtil setLogoRoundCorner(int logoRoundCorner) {
this.logoRoundCorner = logoRoundCorner;
return this;
}

// 创建二维码 ImageIO.write(bufferedImage, "png", outFile);
public BufferedImage create() throws WriterException {
try {
if (this.hints == null) {
this.hints = new LinkedHashMap<>(3);
//设置容错等级
this.hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//定义字符集,我们设置为utf-8
this.hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//设置边距,二维码边距空白宽度为0
this.hints.put(EncodeHintType.MARGIN, 0);
}
// 添加 hintType
BufferedImage bufferedImage = this.getBufferedImage(this.hints);
// 添加 logo
this.drawLogo(bufferedImage);
// 生成二维码图片
return bufferedImage;
} catch (WriterException e) {
System.out.println("生成二维码异常:" + e.getMessage());
throw new WriterException(e);
}
}

private void drawLogo(BufferedImage image) {
if (this.logoPath != null) {
// 读取二维码图片,并构建绘图对象
Graphics2D graphics2D = image.createGraphics();
int imageWidth = image.getWidth(), imageHeight = image.getHeight();
// 设置消除锯齿
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP));
try {
// 读取logo图片
BufferedImage logo = ImageIO.read(this.logoPath);
// 设置logo的大小,设置为二维码图片的20%,因为过大会盖掉二维码
int logoWidth = logo.getWidth(null) > imageWidth * 3 / 10 ? (imageWidth * 3 / 10) : logo.getWidth(null);
int logoHeight = logo.getHeight(null) > imageHeight * 3 / 10 ? (imageHeight * 3 / 10) : logo.getWidth(null);
// 定义logo位置x,y 放在中心,
int x = (imageWidth - logoWidth) / 2, y = (imageHeight - logoHeight) / 2;
// 设置logo 有一道边框 宽度(其实就是画一块贴面)
graphics2D.setStroke(new BasicStroke(this.logoBorder, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
// logo边框颜色
graphics2D.setColor(this.logoBorderColor);
// logo边框弧形
graphics2D.draw(new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, this.logoRoundCorner, this.logoRoundCorner));
// 绘制LOGO圆弧矩形
graphics2D.setClip(new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, this.logoRoundCorner, this.logoRoundCorner));
// 开始绘制LOGO到二维码上
graphics2D.drawImage(logo, x, y, logoWidth, logoHeight, null);
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
graphics2D.dispose();
}
}
}

// 绘制二维码图片流
private BufferedImage getBufferedImage(Map<EncodeHintType, Object> hints) throws WriterException {
// 生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(this.content, BarcodeFormat.QR_CODE, this.qrWidth, this.qrHeight, hints);
// 更新白边边框 让生成的二维码在图片属性上跟我们设置的图片大小size一致
BitMatrix bitMatrixUpdate = this.updateBitMatrix(bitMatrix, this.qrMargin);
// 是否设置二维码背景色/前景色
MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(this.onColor.getRGB(), this.offColor.getRGB());
// 生成二维码对象
int matrixUpdateWidth = bitMatrixUpdate.getWidth(), matrixUpdateHeight = bitMatrixUpdate.getHeight();
BufferedImage bufferedImage = new BufferedImage(matrixUpdateWidth, matrixUpdateHeight, BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < matrixUpdateWidth; x++) {
for (int y = 0; y < matrixUpdateHeight; y++) {
bufferedImage.setRGB(x, y, bitMatrixUpdate.get(x, y) ? matrixToImageConfig.getPixelOnColor() : matrixToImageConfig.getPixelOffColor());
}
}
// 根据size放大、缩小生成的二维码
BufferedImage newImage = new BufferedImage(this.qrWidth, this.qrHeight, bufferedImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(bufferedImage, 0, 0, this.qrWidth, this.qrHeight, null);
g.dispose();
return newImage;
}

// 因为二维码边框设置那里不起作用,不管设置多少,都会生成白边,所以需要自定义控制白边宽度,
// 该方法生成自定义白边框后的bitMatrix
private BitMatrix updateBitMatrix(BitMatrix matrix, int margin) {
int tempM = margin * 2;
int[] rec = matrix.getEnclosingRectangle(); // 获取二维码图案的属性
int resWidth = rec[2] + tempM;
int resHeight = rec[3] + tempM;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix
resMatrix.clear();
for (int i = margin; i < resWidth - margin; i++) { // 循环,将二维码图案绘制到新的bitMatrix中
for (int j = margin; j < resHeight - margin; j++) {
if (matrix.get(i - margin + rec[0], j - margin + rec[1])) {
resMatrix.set(i, j);
}
}
}
return resMatrix;
}
}

生成

普通二维码

1
2
3
4
5
6
7
8
Map<EncodeHintType, Object> hints = new LinkedHashMap<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//定义字符集,我们设置为utf-8
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
ImageIO.write(QrCodeUtil.Builder().setContent("普通二维码")
.setSize(300, 300)
.setHints(hints)
.create(), "png", new File("E:\\qrcode\\zxingNormal.png"));

彩色二维码

1
2
3
4
5
6
7
8
9
10
// 设置颜色1
ImageIO.write(QrCodeUtil.Builder().setContent("黑白二维码")
.setSize(300, 300)
.setColor(new Color(0, 0, 0), new Color(255, 255, 255))
.create(), "png", new File("E:\\qrcode\\zxingNorma2.png"));
// 设置颜色2
ImageIO.write(QrCodeUtil.Builder().setContent("彩色二维码")
.setSize(300, 300)
.setColor(new Color(255, 0, 0), new Color(0, 0, 255))
.create(), "png", new File("E:\\qrcode\\zxingNorma3.png"));

LOGO二维码

1
2
3
4
5
ImageIO.write(QrCodeUtil.Builder().setContent("LOGO二维码")
.setSize(300, 300).setMargin(0)
.setLogo(new FileInputStream(new File("E:\\qrcode\\logo.png")))
.setLogoBorder(10, Color.RED).setLogoRoundCorner(10)
.create(), "png", new File("E:\\qrcode\\zxingNorma4.png"));
完
OkHttp3 工具类
JetBrains的IDE中EvalReset的使用(重置试用期)

本文标题:zxing开发之二维码

文章作者:十二

发布时间:2023-09-08 10:51:16

最后更新:2023-09-11 11:20:46

原始链接:https://www.zhuqiaolun.com/2023/09/1694141476592/1694141476592/

许可协议:署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

头像

十二

我想起那天夕阳下的奔跑,那是我逝去的青春。

分类

  • Blog4
  • ElasticSearch13
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx3
  • Kafka4
  • Linux2
  • Logger5
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot14
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 六月 20251
  • 二月 20251
  • 十二月 20244
  • 八月 202416
  • 六月 20241
  • 九月 20231
  • 八月 20231
  • 七月 20232
  • 八月 20222
  • 三月 202214
  • 二月 20224
  • 十一月 20211
  • 七月 20215
  • 六月 20213
  • 五月 20213
  • 四月 20211
  • 三月 202116
  • 二月 20212
  • 一月 20211
  • 十一月 202014
  • 十月 20201
  • 九月 202014
  • 八月 20205
  • 七月 20204
  • 六月 20208
  • 五月 20208

作品

我的微信 我的文件

网站信息

本站运行时间统计: 载入中...
本站文章字数统计:97.8k
本站文章数量统计:133
© 2025 十二  |  鄂ICP备18019781号-1  |  鄂公网安备42118202000044号
驱动于 Hexo  | 主题 antiquity  |  不蒜子告之 阁下是第个访客
首页 归档 关于 文件 Github