QRCodeUtils.java
7.45 KB
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
package cn.com.glsx.busassistant.util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.swetake.util.Qrcode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
/**
* @author hedong
* @version V1.0
* @Title: QRCodeUtils
* @Description:
* @Company: Didihu.com.cn
* @Date 2021/9/16 13:53
*/
public class QRCodeUtils {
private static final int WIDTH = 300; // 二维码宽度
private static final int HEIGHT = 300; // 二维码高度
private static final String FORMAT = "png"; // 二维码图片格式
private static final String CHARSET = "UTF-8"; // 二维码编码格式
private static final int LOGO_WIDTH = 100; // logo宽度
private static final int LOGO_HEIGHT = 100; // logo高度
/**
* 生成二维码图片,并在中间加入logo
*
* @param content 二维码内容
* @param inputStream logo文件流
* @param outputStream 二维码图片输出文件流
*/
public static void createQRCodeWithLogo(String content, InputStream inputStream, ByteArrayOutputStream outputStream) {
try {
// 1. 生成二维码图片
QRCodeWriter writer = new QRCodeWriter();
HashMap<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage qrImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
qrImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
// 2. 读取logo文件
BufferedImage logoImage = ImageIO.read(inputStream);
// 3. 在二维码中间加入logo
Graphics2D g2d = qrImage.createGraphics();
int x = (WIDTH - LOGO_WIDTH) / 2;
int y = (HEIGHT - LOGO_HEIGHT) / 2;
g2d.drawImage(logoImage, x, y, LOGO_WIDTH, LOGO_HEIGHT, null);
// 画圆角矩形
RoundRectangle2D round = new RoundRectangle2D.Float(x, y, LOGO_WIDTH, LOGO_HEIGHT, 6, 6);
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(3));
g2d.draw(round);
g2d.dispose();
// 4. 输出二维码图片
ImageIO.write(qrImage, FORMAT, outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public QRCodeUtils() {
}
public static void encoderQRCode(String content, String imgPath) throws Exception {
encoderQRCode(content, (String)imgPath, "jpg", 7);
}
public static void encoderQRCode(String content, OutputStream output) throws Exception {
encoderQRCode(content, (OutputStream)output, "jpg", 7);
}
public static void encoderQRCode(String content, String imgPath, String imgType) throws Exception {
encoderQRCode(content, (String)imgPath, imgType, 7);
}
public static void encoderQRCode(String content, OutputStream output, String imgType) throws Exception {
encoderQRCode(content, (OutputStream)output, imgType, 7);
}
public static void encoderQRCode(String content, String imgPath, String imgType, int version) throws Exception {
BufferedImage bufImg = qRCodeCommon(content, imgType, version);
File imgFile = new File(imgPath);
ImageIO.write(bufImg, imgType, imgFile);
}
public static void encoderQRCode(String content, String imgPath, int width, int heigth) throws Exception {
encoderQRCode(content, (String)imgPath, "jpg", 7, width, heigth);
}
public static void encoderQRCode(String content, String imgPath, int version, int width, int heigth) throws Exception {
encoderQRCode(content, imgPath, "jpg", version, width, heigth);
}
public static void encoderQRCode(String content, OutputStream output, int width, int heigth) throws Exception {
encoderQRCode(content, (OutputStream)output, "jpg", 7, width, heigth);
}
public static void encoderQRCode(String content, OutputStream output, String imgType, int version) throws Exception {
BufferedImage bufImg = qRCodeCommon(content, imgType, version);
ImageIO.write(bufImg, imgType, output);
}
public static void encoderQRCode(String content, String imgPath, String imgType, int version, int width, int heigth) throws Exception {
BufferedImage bufImg = qRCodeCommon(content, imgType, version, width, heigth);
File imgFile = new File(imgPath);
ImageIO.write(bufImg, imgType, imgFile);
}
public static void encoderQRCode(String content, OutputStream output, String imgType, int version, int width, int heigth) throws Exception {
BufferedImage bufImg = qRCodeCommon(content, imgType, version, width, heigth);
ImageIO.write(bufImg, imgType, output);
}
private static BufferedImage qRCodeCommon(String content, String imgType, int version) throws Exception {
int width = 67 + 12 * (version - 1);
int height = 67 + 12 * (version - 1);
return qRCodeCommon(content, imgType, version, width, height);
}
private static BufferedImage qRCodeCommon(String content, String imgType, int version, int width, int height) throws Exception {
BufferedImage bufImg = null;
try {
Qrcode qrcodeHandler = new Qrcode();
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(version);
byte[] contentBytes = content.getBytes("utf-8");
bufImg = new BufferedImage(width, height, 12);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, width, height);
gs.setColor(Color.BLACK);
int pixoff = 0;
if (contentBytes.length > 0 && contentBytes.length < 256) {
boolean[][] s = qrcodeHandler.calQrcode(contentBytes);
for(int i = 0; i < s.length; ++i) {
for(int j = 0; j < s.length; ++j) {
if (s[j][i]) {
gs.fillRect(j * 5 + pixoff, i * 5 + pixoff, 5, 5);
}
}
}
//去掉边框
// Rectangle2D mfRect = new Float();
// mfRect.setRect(0.0D, 0.0D, (double)width, (double)height);
// gs.setStroke(new BasicStroke(2.0F));
// gs.draw(mfRect);
gs.dispose();
bufImg.flush();
return bufImg;
} else {
throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 256].");
}
} catch (Exception var13) {
var13.printStackTrace();
throw new Exception("QRCode generate exception:" + var13.getMessage(), var13);
}
}
}