-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQrCodeUtil.java
More file actions
288 lines (262 loc) · 13.2 KB
/
QrCodeUtil.java
File metadata and controls
288 lines (262 loc) · 13.2 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package cn.laoshini.dk.qrcode;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import cn.laoshini.dk.exception.BusinessException;
/**
* @author fagarine
*/
public class QrCodeUtil {
static final String DEFAULT_CHARSET = "UTF-8";
static final String DEFAULT_FORMAT = "JPG";
/**
* 二维码尺寸缺省值
*/
static final int DEFAULT_QR_CODE_LEN = 300;
/**
* LOGO图片的最大尺寸,仅在压缩时有效
*/
static final int LOGO_MAX_LEN = 100;
/**
* LOGO图片占二维码的最大百分比(边长占比),仅在压缩时有效
*/
static final int LOGO_MAX_PERCENT = 30;
private QrCodeUtil() {
}
/**
* 根据传入数据创建二维码,并将数据以字节数组的形式返回
*
* @param content 二维码数据内容
* @param logoPath 如果要显示LOGO,传入LOGO图片路径
* @param len 二维码边长,支持返回[100, 1024],其他值会被转为默认值{@link #DEFAULT_QR_CODE_LEN}
* @param rgb 二维码颜色,传入非正整数,则会使用默认颜色:黑色
* @param compress 如果使用LOGO图片,LOGO图片是否需要压缩,当LOGO图片尺寸较大时,应选择压缩以避免被LOGO覆盖太大面积
* @param charset 数据内容使用的编码集,默认为UTF-8
* @return 返回二维码数据,字节数组形式,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static byte[] buildToBytes(String content, String logoPath, int len, int rgb, boolean compress,
String charset) {
return new QrCodeBuilder(content, logoPath, len, rgb, compress, charset).toBytes();
}
/**
* 根据传入数据创建二维码,并将数据以字节数组的形式返回
*
* @param content 二维码数据内容
* @param len 二维码边长,支持返回[100, 1024],其他值会被转为默认值{@link #DEFAULT_QR_CODE_LEN}
* @param rgb 二维码颜色,传入非正整数,则会使用默认颜色:黑色
* @return 返回二维码数据,字节数组形式,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static byte[] buildToBytes(String content, int len, int rgb) {
return new QrCodeBuilder(content, len, rgb).toBytes();
}
/**
* 根据传入数据创建二维码,并将数据以字节数组的形式返回
*
* @param content 二维码数据内容
* @param len 二维码边长,支持返回[100, 1024],其他值会被转为默认值{@link #DEFAULT_QR_CODE_LEN}
* @return 返回二维码数据,字节数组形式,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static byte[] buildToBytes(String content, int len) {
return new QrCodeBuilder(content, len).toBytes();
}
/**
* 根据传入数据创建二维码,并将数据以字节数组的形式返回
*
* @param content 二维码数据内容
* @param logoPath 如果要显示LOGO,传入LOGO图片路径
* @return 返回二维码数据,字节数组形式,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static byte[] buildToBytes(String content, String logoPath) {
return new QrCodeBuilder(content, logoPath).toBytes();
}
/**
* 根据传入数据创建二维码,并将数据以字节数组的形式返回
*
* @param content 二维码数据内容
* @return 返回二维码数据,字节数组形式,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static byte[] buildToBytes(String content) {
return new QrCodeBuilder(content).toBytes();
}
/**
* 根据传入数据创建二维码,并将数据以BufferedImage的形式返回
*
* @param content 二维码数据内容
* @param logoPath 如果要显示LOGO,传入LOGO图片路径
* @param len 二维码边长,支持返回[100, 1024],其他值会被转为默认值{@link #DEFAULT_QR_CODE_LEN}
* @param rgb 二维码颜色,传入非正整数,则会使用默认颜色:黑色
* @param compress 如果使用LOGO图片,LOGO图片是否需要压缩,当LOGO图片尺寸较大时,应选择压缩以避免被LOGO覆盖太大面积
* @param charset 数据内容使用的编码集,默认为UTF-8
* @return 返回二维码数据,BufferedImage形式,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static BufferedImage buildToImage(String content, String logoPath, int len, int rgb, boolean compress,
String charset) {
return new QrCodeBuilder(content, logoPath, len, rgb, compress, charset).toImage();
}
/**
* 根据传入数据创建二维码,并将数据以BufferedImage的形式返回
*
* @param content 二维码数据内容
* @param len 二维码边长,支持返回[100, 1024],其他值会被转为默认值{@link #DEFAULT_QR_CODE_LEN}
* @param rgb 二维码颜色,传入非正整数,则会使用默认颜色:黑色
* @return 返回二维码数据,BufferedImage形式,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static BufferedImage buildToImage(String content, int len, int rgb) {
return new QrCodeBuilder(content, len, rgb).toImage();
}
/**
* 根据传入数据创建二维码,并将数据以BufferedImage的形式返回
*
* @param content 二维码数据内容
* @param len 二维码边长,支持返回[100, 1024],其他值会被转为默认值{@link #DEFAULT_QR_CODE_LEN}
* @return 返回二维码数据,BufferedImage形式,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static BufferedImage buildToImage(String content, int len) {
return new QrCodeBuilder(content, len).toImage();
}
/**
* 根据传入数据创建二维码,并将数据以BufferedImage的形式返回
*
* @param content 二维码数据内容
* @param logoPath 如果要显示LOGO,传入LOGO图片路径
* @return 返回二维码数据,BufferedImage形式,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static BufferedImage buildToImage(String content, String logoPath) {
return new QrCodeBuilder(content, logoPath).toImage();
}
/**
* 根据传入数据创建二维码,并将数据以BufferedImage的形式返回
*
* @param content 二维码数据内容
* @return 返回二维码数据,BufferedImage形式,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static BufferedImage buildToImage(String content) {
return new QrCodeBuilder(content).toImage();
}
/**
* 根据传入数据创建二维码,并将数据以传入的文件名保存
*
* @param content 二维码数据内容
* @param logoPath 如果要显示LOGO,传入LOGO图片路径
* @param len 二维码边长,支持返回[100, 1024],其他值会被转为默认值{@link #DEFAULT_QR_CODE_LEN}
* @param rgb 二维码颜色,传入非正整数,则会使用默认颜色:黑色
* @param compress 如果使用LOGO图片,LOGO图片是否需要压缩,当LOGO图片尺寸较大时,应选择压缩以避免被LOGO覆盖太大面积
* @param charset 数据内容使用的编码集,默认为UTF-8
* @param destFilePath 文件保存路径
*/
public static void buildToFile(String content, String logoPath, int len, int rgb, boolean compress, String charset,
String destFilePath) {
BufferedImage image = buildToImage(content, logoPath, len, rgb, compress, charset);
try {
ImageIO.write(image, DEFAULT_FORMAT, new FileOutputStream(destFilePath));
} catch (IOException e) {
throw new BusinessException("qr.code.write.error", "二维码数据写入文件出错:" + destFilePath, e);
}
}
public static void buildToFile(String content, String destFilePath) {
BufferedImage image = buildToImage(content);
try {
ImageIO.write(image, DEFAULT_FORMAT, new FileOutputStream(destFilePath));
} catch (IOException e) {
throw new BusinessException("qr.code.write.error", "二维码数据写入文件出错:" + destFilePath, e);
}
}
/**
* 根据传入数据创建二维码,并将数据写入指定目录下,随机一个文件名保存
*
* @param content 二维码数据内容
* @param logoPath 如果要显示LOGO,传入LOGO图片路径
* @param len 二维码边长,支持返回[100, 1024],其他值会被转为默认值{@link #DEFAULT_QR_CODE_LEN}
* @param rgb 二维码颜色,传入非正整数,则会使用默认颜色:黑色
* @param compress 如果使用LOGO图片,LOGO图片是否需要压缩,当LOGO图片尺寸较大时,应选择压缩以避免被LOGO覆盖太大面积
* @param charset 数据内容使用的编码集,默认为UTF-8
* @param destDir 文件保存目录
*/
public static void buildToRandomFile(String content, String logoPath, int len, int rgb, boolean compress,
String charset, String destDir) {
BufferedImage image = buildToImage(content, logoPath, len, rgb, compress, charset);
String filepath = destDir + File.separator + new Random().nextInt(999999) + "." + DEFAULT_FORMAT;
try {
ImageIO.write(image, DEFAULT_FORMAT, new FileOutputStream(filepath));
} catch (IOException e) {
throw new BusinessException("qr.code.write.error", "二维码数据写入随机文件出错:" + filepath, e);
}
}
public static void buildToRandomFile(String content, String destDir) {
BufferedImage image = buildToImage(content);
String filepath = destDir + File.separator + new Random().nextInt(999999) + "." + DEFAULT_FORMAT;
try {
ImageIO.write(image, DEFAULT_FORMAT, new FileOutputStream(filepath));
} catch (IOException e) {
throw new BusinessException("qr.code.write.error", "二维码数据写入随机文件出错:" + filepath, e);
}
}
/**
* 根据传入数据创建二维码,并将数据写入输出流中
*
* @param content 二维码数据内容
* @param logoPath 如果要显示LOGO,传入LOGO图片路径
* @param len 二维码边长,支持返回[100, 1024],其他值会被转为默认值{@link #DEFAULT_QR_CODE_LEN}
* @param rgb 二维码颜色,传入非正整数,则会使用默认颜色:黑色
* @param compress 如果使用LOGO图片,LOGO图片是否需要压缩,当LOGO图片尺寸较大时,应选择压缩以避免被LOGO覆盖太大面积
* @param charset 数据内容使用的编码集,默认为UTF-8
*/
public static void buildAndWrite(String content, String logoPath, int len, int rgb, boolean compress,
String charset, OutputStream outputStream) {
BufferedImage image = buildToImage(content, logoPath, len, rgb, compress, charset);
try {
ImageIO.write(image, DEFAULT_FORMAT, outputStream);
} catch (IOException e) {
throw new BusinessException("qr.code.write.error", "二维码数据写入文件出错", e);
}
}
public static void buildAndWrite(String content, OutputStream outputStream) {
BufferedImage image = buildToImage(content);
try {
ImageIO.write(image, DEFAULT_FORMAT, outputStream);
} catch (IOException e) {
throw new BusinessException("qr.code.write.error", "二维码数据写入文件出错", e);
}
}
/**
* 读取二维码数据,并以字节数组的形式返回
*
* @param filepath 二维码图片文件路径
* @return 返回读取到的数据,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static byte[] readWithBytes(String filepath) {
return new QrCodeReader(new File(filepath)).readWithBytes();
}
/**
* 读取二维码数据,并以字符串的形式返回
*
* @param filepath 二维码图片文件路径
* @return 返回读取到的数据,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static String readWithText(String filepath) {
return new QrCodeReader(new File(filepath)).readWithText();
}
/**
* 读取二维码数据,并以字节数组的形式返回
*
* @param in 二维码图片数据流
* @return 返回读取到的数据,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static byte[] readWithBytes(InputStream in) {
return new QrCodeReader(in).readWithBytes();
}
/**
* 读取二维码数据,并以字符串的形式返回
*
* @param in 二维码图片数据流
* @return 返回读取到的数据,该方法不会返回null,但可能抛出{@link BusinessException}
*/
public static String readWithText(InputStream in) {
return new QrCodeReader(in).readWithText();
}
}