QREncode.java
7.36 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.bs.common.utils;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import jakarta.servlet.ServletOutputStream;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.bs.common.constant.Constants;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
public class QREncode {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
/**
* 默认宽度
*/
private static final Integer WIDTH = 140;
/**
* 默认高度
*/
private static final Integer HEIGHT = 140;
/**
* LOGO 默认宽度
*/
private static final Integer LOGO_WIDTH = 22;
/**
* LOGO 默认高度
*/
private static final Integer LOGO_HEIGHT = 22;
/**
* 图片格式
*/
private static final String IMAGE_FORMAT = "png";
private QREncode() {
}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
/**
* 生成带头像的二维码
*
* @param content 内容
* @param logoUrl logo在线地址
* @return
*/
public static BufferedImage crateQRCode(String content, String logoUrl) {
if (StringUtils.isNotBlank(content)) {
ServletOutputStream stream = null;
HashMap<EncodeHintType, Comparable> hints = new HashMap<>(4);
// 指定字符编码为utf-8
hints.put(EncodeHintType.CHARACTER_SET, Constants.UTF8);
// 指定二维码的纠错等级为中级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
// 设置图片的边距
hints.put(EncodeHintType.MARGIN, 2);
try {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
if (StringUtils.isNotBlank(logoUrl)) {
insertLogo(bufferedImage, logoUrl);
}
return bufferedImage;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
}
/**
* 二维码插入logo
*
* @param source 二维码
* @param logoUrl logo 在线地址
* @throws Exception
*/
private static void insertLogo(BufferedImage source, String logoUrl) throws Exception {
// logo 源可为 File/InputStream/URL
Image src = ImageIO.read(new URL(logoUrl));
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (WIDTH - LOGO_WIDTH) / 2;
int y = (HEIGHT - LOGO_HEIGHT) / 2;
graph.drawImage(src, x, y, LOGO_WIDTH, LOGO_HEIGHT, null);
Shape shape = new RoundRectangle2D.Float(x, y, LOGO_WIDTH, LOGO_HEIGHT, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
/**
* 生成带二维码并生成流文件进行传输
*
* @param content 内容
* @param output 输出流
* @throws Exception
*/
public void getQRCode(String content, OutputStream output) throws Exception {
BufferedImage image = crateQRCode(content, null);
if (StringUtils.isNotNull(image)) {
ImageIO.write(image, IMAGE_FORMAT, output);
}
}
/**
* 生成带logo的二维码并生成流文件进行传输
*
* @param content 内容
* @param logoUrl logo资源
* @param output 输出流
* @throws Exception
*/
public void getQRCode(String content, String logoUrl, OutputStream output) throws Exception {
BufferedImage image = crateQRCode(content, logoUrl);
if (StringUtils.isNotNull(image)) {
ImageIO.write(image, IMAGE_FORMAT, output);
}
}
public static InputStream writeToInputStream(BufferedImage bufferedImage) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(bufferedImage, IMAGE_FORMAT, os);
return new ByteArrayInputStream(os.toByteArray());
} catch (Exception e) {
throw new IOException(e.getMessage(), e);
}
}
public static MultipartFile getQRCodeMultipartFile(String content, String logoUrl) throws IOException {
BufferedImage bufferedImage = crateQRCode(content, logoUrl);
//读取图片转换为 BufferedImage
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bufferedImage, "jpg", baos );
//转换为MultipartFile
return new MockMultipartFile("groupQrCode","groupQrCode.jpg","text/plain", baos.toByteArray());
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static void main(String[] args) throws Exception {
String text = "http://open.weixin.qq.com/qr/code/?username=LinkWeChat2021";
int width = 300;
int height = 300;
String format = "png";
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, Constants.UTF8);
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
File outputFile = new File("d:" + File.separator + "qr.png");
QREncode.writeToFile(bitMatrix, format, outputFile);
}
}