Xml代码
1
2
3
4
5
6
7
8
9
10
|
<
dependency
>
<
groupId
>com.google.zxing</
groupId
>
<
artifactId
>core</
artifactId
>
<
version
>1.7</
version
>
</
dependency
>
<
dependency
>
<
groupId
>com.google.zxing</
groupId
>
<
artifactId
>javase</
artifactId
>
<
version
>1.7</
version
>
</
dependency
>
|
java
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
|
package
util.qrcode;
import
java.awt.image.BufferedImage;
import
java.io.Closeable;
import
java.io.File;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.net.URL;
import
java.util.Hashtable;
import
java.util.List;
import
javax.imageio.ImageIO;
import
com.google.zxing.BarcodeFormat;
import
com.google.zxing.BinaryBitmap;
import
com.google.zxing.EncodeHintType;
import
com.google.zxing.LuminanceSource;
import
com.google.zxing.MultiFormatReader;
import
com.google.zxing.MultiFormatWriter;
import
com.google.zxing.NotFoundException;
import
com.google.zxing.Result;
import
com.google.zxing.WriterException;
import
com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import
com.google.zxing.client.j2se.MatrixToImageWriter;
import
com.google.zxing.common.BitMatrix;
import
com.google.zxing.common.HybridBinarizer;
import
com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public
class
QRCodeUtils {
/** 私有构造方法 */
private
QRCodeUtils() {
super
(); }
private
static
final
Hashtable<EncodeHintType, Object> ZXING_HINTS;
static
{
ZXING_HINTS =
new
Hashtable<EncodeHintType, Object>();
ZXING_HINTS.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
ZXING_HINTS.put(EncodeHintType.CHARACTER_SET,
"UTF-8"
);
}
public
void
encode(String contents, File file,String filePostfix, BarcodeFormat format,
int
width,
int
height, Map<EncodeHintType, ?> hints) {
Hashtable hintsw =
new
Hashtable();
// 指定纠错等级
hintsw.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
hintsw.put(EncodeHintType.CHARACTER_SET,
"UTF-8"
);
//要使用utf8 才能编码,并解码中文!
try
{
BitMatrix bitMatrix =
new
MultiFormatWriter().encode(contents,
BarcodeFormat.QR_CODE, width, height, ZXING_HINTS);
MatrixToImageWriter.writeToFile(bitMatrix,
"png"
, file);
}
catch
(Exception e) {
e.printStackTrace();
}
}
/**
* 将二维码写入数据流
*
* @param out 数据流
* @param lines 数据
* @param width 二维码宽度
* @param height 二维码高度
*/
public
static
void
writeToOutputStream(OutputStream out, List<String> lines,
int
width,
int
height) {
StringBuilder sb =
new
StringBuilder();
for
(
int
i =
0
; i < lines.size(); i ++) {
sb.append(lines.get(i));
if
(i != lines.size() -
1
) {
sb.append(
"\n"
);
}
}
writeToOutputStream(out, sb.toString(), width, height);
}
/**
* 将二维码写入数据流
*
* @param out 数据流
* @param contents 数据
* @param width 二维码宽度
* @param height 二维码高度
*/
public
static
void
writeToOutputStream(OutputStream out, String contents,
int
width,
int
height) {
try
{
BitMatrix bitMatrix =
new
MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, ZXING_HINTS);
MatrixToImageWriter.writeToStream(bitMatrix,
"png"
, out);
}
catch
(WriterException e) {
throw
new
RuntimeException(e.getMessage(), e);
}
catch
(IOException e) {
throw
new
RuntimeException(e.getMessage(), e);
}
}
/**
* 将二维码写入文件
*
* @param file 文件
* @param lines 数据
* @param width 二维码宽度
* @param height 二维码高度
*/
public
static
void
writeToFile(File file, List<String> lines,
int
width,
int
height) {
StringBuilder sb =
new
StringBuilder();
for
(
int
i =
0
; i < lines.size(); i ++) {
sb.append(lines.get(i));
if
(i != lines.size() -
1
) {
sb.append(
"\n"
);
}
}
writeToFile(file, sb.toString(), width, height);
}
/**
* 将二维码写入文件
*
* @param file 文件
* @param contents 数据
* @param width 二维码宽度
* @param height 二维码高度
*/
public
static
void
writeToFile(File file, String contents,
int
width,
int
height) {
try
{
BitMatrix bitMatrix =
new
MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, ZXING_HINTS);
MatrixToImageWriter.writeToFile(bitMatrix,
"png"
, file);
}
catch
(WriterException e) {
throw
new
RuntimeException(e.getMessage(), e);
}
catch
(IOException e) {
throw
new
RuntimeException(e.getMessage(), e);
}
}
/**
* 解析二维码
*
* @param image 图片
* @return 信息
*/
public
static
String decode(BufferedImage image) {
Result result =
null
;
LuminanceSource source =
new
BufferedImageLuminanceSource(image);
BinaryBitmap bitmap =
new
BinaryBitmap(
new
HybridBinarizer(source));
try
{
result =
new
MultiFormatReader().decode(bitmap, ZXING_HINTS);
}
catch
(NotFoundException e) {
throw
new
RuntimeException(e.getMessage(), e);
}
return
result.getText();
}
/**
* 解析二维码
*
* @param file 图片
* @return 信息
*/
public
static
String decode(File file) {
BufferedImage image =
null
;
try
{
image = ImageIO.read(file);
}
catch
(IOException e) {
throw
new
RuntimeException(e.getMessage(), e);
}
return
decode(image);
}
/**
* 解析二维码
*
* @param input 图片
* @return 信息
*/
public
static
String decode(InputStream input) {
BufferedImage image =
null
;
try
{
image = ImageIO.read(input);
}
catch
(IOException e) {
throw
new
RuntimeException(e.getMessage(), e);
}
return
decode(image);
}
/**
* 解析二维码
*
* @param url 图片
* @return 信息
*/
public
static
String decode(URL url) {
BufferedImage image =
null
;
try
{
image = ImageIO.read(url);
}
catch
(IOException e) {
throw
new
RuntimeException(e.getMessage(), e);
}
return
decode(image);
}
/**
* 关闭
*
* @param closeable 关闭对象
*/
public
static
void
closeQuietly(Closeable closeable) {
if
(closeable ==
null
)
return
;
try
{ closeable.close(); }
catch
(Exception e) {}
}
本文转自yunlielai51CTO博客,原文链接:
http://blog.51cto.com/4925054/1354838
,如需转载请自行联系原作者
|