1、支持qrcode、zxing 二维码生成、解析;
2、qrcode 方式生成二维码支持添加图片,如下:
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
|
package common;
import java.awt.color;
import java.awt.graphics2d;
import java.awt.image;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import java.util.hashmap;
import java.util.map;
import javax.imageio.imageio;
import jp.sourceforge.qrcode.qrcodedecoder;
import jp.sourceforge.qrcode.exception.decodingfailedexception;
import com.google.zxing.barcodeformat;
import com.google.zxing.binarizer;
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.writerexception;
import com.google.zxing.common.bitmatrix;
import com.google.zxing.common.hybridbinarizer;
import com.swetake.util.qrcode;
/**
* 二维码生成工具类
* @author cloud
* @data 2016-12-15
* qrcode
*/
public class qrcodeutil {
//二维码颜色
private static final int black = 0xff000000 ;
//二维码颜色
private static final int white = 0xffffffff ;
/**
* <span style="font-size:18px;font-weight:blod;">zxing 方式生成二维码</span>
* @param text <a href="javascript:void();" rel="external nofollow" >二维码内容</a>
* @param width 二维码宽
* @param height 二维码高
* @param outputpath 二维码生成保存路径
* @param imagetype 二维码生成格式
*/
public static void zxingcodecreate(string text, int width, int height, string outputpath, string imagetype){
map<encodehinttype, string> his = new hashmap<encodehinttype, string>();
//设置编码字符集
his.put(encodehinttype.character_set, "utf-8" );
try {
//1、生成二维码
bitmatrix encode = new multiformatwriter().encode(text, barcodeformat.qr_code, width, height, his);
//2、获取二维码宽高
int codewidth = encode.getwidth();
int codeheight = encode.getheight();
//3、将二维码放入缓冲流
bufferedimage image = new bufferedimage(codewidth, codeheight, bufferedimage.type_int_rgb);
for ( int i = 0 ; i < codewidth; i++) {
for ( int j = 0 ; j < codeheight; j++) {
//4、循环将二维码内容定入图片
image.setrgb(i, j, encode.get(i, j) ? black : white);
}
}
file outputimage = new file(outputpath);
//如果图片不存在创建图片
if (!outputimage.exists())
outputimage.createnewfile();
//5、将二维码写入图片
imageio.write(image, imagetype, outputimage);
} catch (writerexception e) {
e.printstacktrace();
system.out.println( "二维码生成失败" );
} catch (ioexception e) {
e.printstacktrace();
system.out.println( "生成二维码图片失败" );
}
}
/**
* <span style="font-size:18px;font-weight:blod;">二维码解析</span>
* @param analyzepath 二维码路径
* @return
* @throws ioexception
*/
@suppresswarnings ({ "rawtypes" , "unchecked" })
public static object zxingcodeanalyze(string analyzepath) throws exception{
multiformatreader formatreader = new multiformatreader();
object result = null ;
try {
file file = new file(analyzepath);
if (!file.exists())
{
return "二维码不存在" ;
}
bufferedimage image = imageio.read(file);
luminancesource source = new luminancesourceutil(image);
binarizer binarizer = new hybridbinarizer(source);
binarybitmap binarybitmap = new binarybitmap(binarizer);
map hints = new hashmap();
hints.put(encodehinttype.character_set, "utf-8" );
result = formatreader.decode(binarybitmap, hints);
} catch (notfoundexception e) {
e.printstacktrace();
}
return result;
}
/**
* <span style="font-size:18px;font-weight:blod;">qrcode 方式生成二维码</span>
* @param content 二维码内容
* @param imgpath 二维码生成路径
* @param version 二维码版本
* @param isflag 是否生成logo图片 为null不生成
*/
public static void qrcodecreate(string content, string imgpath, int version, string logopath){
try {
qrcode qrcodehandler = new qrcode();
//设置二维码排错率,可选l(7%) m(15%) q(25%) h(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcodehandler.setqrcodeerrorcorrect( 'm' );
//n代表数字,a代表字符a-z,b代表其他字符
qrcodehandler.setqrcodeencodemode( 'b' );
//版本1为21*21矩阵,版本每增1,二维码的两个边长都增4;所以版本7为45*45的矩阵;最高版本为是40,是177*177的矩阵
qrcodehandler.setqrcodeversion(version);
//根据版本计算尺寸
int imgsize = 67 + 12 * (version - 1 ) ;
byte [] contentbytes = content.getbytes( "gb2312" );
bufferedimage bufimg = new bufferedimage(imgsize , imgsize ,bufferedimage.type_int_rgb);
graphics2d gs = bufimg.creategraphics();
gs.setbackground(color.white);
gs.clearrect( 0 , 0 , imgsize , imgsize);
// 设定图像颜色 > black
gs.setcolor(color.black);
// 设置偏移量 不设置可能导致解析出错
int pixoff = 2 ;
// 输出内容 > 二维码
if (contentbytes.length > 0 && contentbytes.length < 130 ) {
boolean [][] codeout = qrcodehandler.calqrcode(contentbytes);
for ( int i = 0 ; i < codeout.length; i++) {
for ( int j = 0 ; j < codeout.length; j++) {
if (codeout[j][i]) {
gs.fillrect(j * 3 + pixoff, i * 3 + pixoff, 3 , 3 );
}
}
}
} else {
system.err.println( "qrcode content bytes length = " + contentbytes.length + " not in [ 0,130 ]. " );
}
/* 判断是否需要添加logo图片 */
if(logopath != null){
file icon = new file(logopath);
if(icon.exists()){
int width_4 = imgsize / 4;
int width_8 = width_4 / 2;
int height_4 = imgsize / 4;
int height_8 = height_4 / 2;
image img = imageio.read(icon);
gs.drawimage(img, width_4 + width_8, height_4 + height_8,width_4,height_4, null);
gs.dispose();
bufimg.flush();
}else{
system.out.println("error: login图片不存在!");
}
}
gs.dispose();
bufimg.flush();
//创建二维码文件
file imgfile = new file(imgpath);
if(!imgfile.exists())
imgfile.createnewfile();
//根据生成图片获取图片
string imgtype = imgpath.substring(imgpath.lastindexof(".") + 1, imgpath.length());
// 生成二维码qrcode图片
imageio.write(bufimg, imgtype, imgfile);
} catch (exception e) {
e.printstacktrace();
}
}
/**
* <span style="font-size:18px;font-weight:blod;">qrcode二维码解析</span>
* @param codepath 二维码路径
* @return 解析结果
*/
public static string qrcodeanalyze(string codepath) {
file imagefile = new file(codepath);
bufferedimage bufimg = null;
string decodeddata = null;
try {
if(!imagefile.exists())
return "二维码不存在";
bufimg = imageio.read(imagefile);
qrcodedecoder decoder = new qrcodedecoder();
decodeddata = new string(decoder.decode(new imageutil(bufimg)), "gb2312");
} catch (ioexception e) {
system.out.println("error: " + e.getmessage());
e.printstacktrace();
} catch (decodingfailedexception dfe) {
system.out.println("error: " + dfe.getmessage());
dfe.printstacktrace();
}
return decodeddata;
}
}
3、最后贴测试代码:
package test;
import java.awt.image.bufferedimage;
import java.io.inputstream;
import java.net.url;
import javax.imageio.imageio;
import common.imageutil;
import common.qrcodeutil;
import jp.sourceforge.qrcode.qrcodedecoder;
/**
* 二维码生成测试类
* @author cloud
* @data 2016-11-21
* qrcodetest
*/
public class qrcodetest {
public static void main(string[] args) throws exception {
/**
* qrcode 二维码生成测试
* qrcodeutil.qrcodecreate("http://blog.csdn.net/u014266877", "e://qrcode.jpg", 15, "e://icon.png");
*/
/**
* qrcode 二维码解析测试
* string qrcodeanalyze = qrcodeutil.qrcodeanalyze("e://qrcode.jpg");
*/
/**
* zxingcode 二维码生成测试
* qrcodeutil.zxingcodecreate("http://blog.csdn.net/u014266877", 300, 300, "e://zxingcode.jpg", "jpg");
*/
/**
* zxingcode 二维码解析
* string zxinganalyze = qrcodeutil.zxingcodeanalyze("e://zxingcode.jpg").tostring();
*/
system.out.println( "success" );
}
}
|
原文链接:https://blog.csdn.net/u014266877/article/details/53665729