JAVA生成解析二维码

  1 package com.mohe.twocode;
  2  
  3  
  4 import java.awt.Color;
  5 import java.awt.Graphics2D;
  6 import java.awt.image.BufferedImage;
  7 import java.io.File;
  8 import java.io.IOException;
  9 import java.io.InputStream;
 10 import java.io.OutputStream;
 11  
 12  
 13 import javax.imageio.ImageIO;
 14  
 15  
 16 import jp.sourceforge.qrcode.QRCodeDecoder;
 17 import jp.sourceforge.qrcode.data.QRCodeImage;
 18 import jp.sourceforge.qrcode.exception.DecodingFailedException;
 19  
 20  
 21 import com.swetake.util.Qrcode;
 22  
 23  
 24 public class TwoDimensionCode implements QRCodeImage {
 25  
 26  
 27 private BufferedImage bufImg = null;
 28  
 29  
 30 public TwoDimensionCode() {
 31  
 32  
 33 }
 34  
 35  
 36 public TwoDimensionCode(BufferedImage bufImg) {
 37 this.bufImg = bufImg;
 38 }
 39  
 40  
 41 @Override
 42 public int getHeight() {
 43 return bufImg.getHeight();
 44 }
 45  
 46  
 47 @Override
 48 public int getPixel(int x, int y) {
 49 return bufImg.getRGB(x, y);
 50 }
 51  
 52  
 53 @Override
 54 public int getWidth() {
 55 return bufImg.getWidth();
 56 }
 57  
 58  
 59 /**
 60 * 生成二维码(QRCode)图片
 61 * 
 62 * @param content
 63 *            存储内容
 64 * @param imgPath
 65 *            图片路径
 66 */
 67 public void encoderQRCode(String content, String imgPath) {
 68 this.encoderQRCode(content, imgPath, "png", 7);
 69 }
 70  
 71  
 72 /**
 73 * 生成二维码(QRCode)图片
 74 * 
 75 * @param content
 76 *            存储内容
 77 * @param output
 78 *            输出流
 79 */
 80 public void encoderQRCode(String content, OutputStream output) {
 81 this.encoderQRCode(content, output, "png", 7);
 82 }
 83  
 84  
 85 /**
 86 * 生成二维码(QRCode)图片
 87 * 
 88 * @param content
 89 *            存储内容
 90 * @param imgPath
 91 *            图片路径
 92 * @param imgType
 93 *            图片类型
 94 */
 95 public void encoderQRCode(String content, String imgPath, String imgType) {
 96 this.encoderQRCode(content, imgPath, imgType, 7);
 97 }
 98  
 99  
100 /**
101 * 生成二维码(QRCode)图片
102 * 
103 * @param content
104 *            存储内容
105 * @param output
106 *            输出流
107 * @param imgType
108 *            图片类型
109 */
110 public void encoderQRCode(String content, OutputStream output, String imgType) {
111 this.encoderQRCode(content, output, imgType, 7);
112 }
113  
114  
115 /**
116 * 生成二维码(QRCode)图片
117 * 
118 * @param content
119 *            存储内容
120 * @param imgPath
121 *            图片路径
122 * @param imgType
123 *            图片类型
124 * @param size
125 *            二维码尺寸
126 */
127 public void encoderQRCode(String content, String imgPath, String imgType, int size) {
128 try {
129 BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
130  
131  
132 File imgFile = new File(imgPath);
133  
134  
135 if (!imgFile.exists()) {
136 imgFile.mkdirs();
137 }
138  
139  
140 // 生成二维码QRCode图片
141 ImageIO.write(bufImg, imgType, imgFile);
142  
143  
144 } catch (Exception e) {
145 e.printStackTrace();
146 }
147 }
148  
149  
150 /**
151 * 生成二维码(QRCode)图片
152 * 
153 * @param content
154 *            存储内容
155 * @param output
156 *            输出流
157 * @param imgType
158 *            图片类型
159 * @param size
160 *            二维码尺寸
161 */
162 public void encoderQRCode(String content, OutputStream output, String imgType, int size) {
163 try {
164 BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
165 // 生成二维码QRCode图片
166 ImageIO.write(bufImg, imgType, output);
167 } catch (Exception e) {
168 e.printStackTrace();
169 }
170 }
171  
172  
173 /**
174 * 生成二维码(QRCode)图片的公共方法
175 * 
176 * @param content
177 *            存储内容
178 * @param imgType
179 *            图片类型
180 * @param size
181 *            二维码尺寸
182 * @return
183 */
184 private BufferedImage qRCodeCommon(String content, String imgType, int size) {
185 BufferedImage bufImg = null;
186 try {
187 Qrcode qrcodeHandler = new Qrcode();
188 // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
189 qrcodeHandler.setQrcodeErrorCorrect('M');
190 qrcodeHandler.setQrcodeEncodeMode('B');
191 // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
192 qrcodeHandler.setQrcodeVersion(size);
193 // 获得内容的字节数组,设置编码格式
194 byte[] contentBytes = content.getBytes("utf-8");
195 // 图片尺寸
196 int imgSize = 67 + 12 * (size - 1);
197 bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
198 Graphics2D gs = bufImg.createGraphics();
199 // 设置背景颜色
200 gs.setBackground(Color.WHITE);
201 gs.clearRect(0, 0, imgSize, imgSize);
202  
203  
204 // 设定图像颜色> BLACK
205 gs.setColor(Color.BLACK);
206 // 设置偏移量,不设置可能导致解析出错
207 int pixoff = 2;
208 // 输出内容> 二维码
209 if (contentBytes.length > 0 && contentBytes.length < 800) {
210 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
211 for (int i = 0; i < codeOut.length; i++) {
212 for (int j = 0; j < codeOut.length; j++) {
213 if (codeOut[j][i]) {
214 gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
215 }
216 }
217 }
218 } else {
219 throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");
220 }
221 gs.dispose();
222 bufImg.flush();
223 } catch (Exception e) {
224 e.printStackTrace();
225 }
226 return bufImg;
227 }
228  
229  
230 /**
231 * 解析二维码(QRCode)
232 * 
233 * @param imgPath
234 *            图片路径
235 * @return
236 */
237 public String decoderQRCode(String imgPath) {
238 // QRCode 二维码图片的文件
239 File imageFile = new File(imgPath);
240 BufferedImage bufImg = null;
241 String content = null;
242 try {
243 bufImg = ImageIO.read(imageFile);
244 QRCodeDecoder decoder = new QRCodeDecoder();
245 content = new String(decoder.decode(new TwoDimensionCode(bufImg)), "utf-8");
246 } catch (IOException e) {
247 System.out.println("Error: " + e.getMessage());
248 e.printStackTrace();
249 } catch (DecodingFailedException dfe) {
250 System.out.println("Error: " + dfe.getMessage());
251 dfe.printStackTrace();
252 }
253 return content;
254 }
255  
256  
257 /**
258 * 解析二维码(QRCode)
259 * 
260 * @param input
261 *            输入流
262 * @return
263 */
264 public String decoderQRCode(InputStream input) {
265 BufferedImage bufImg = null;
266 String content = null;
267 try {
268 bufImg = ImageIO.read(input);
269 QRCodeDecoder decoder = new QRCodeDecoder();
270 content = new String(decoder.decode(new TwoDimensionCode(bufImg)), "utf-8");
271 } catch (IOException e) {
272 System.out.println("Error: " + e.getMessage());
273 e.printStackTrace();
274 } catch (DecodingFailedException dfe) {
275 System.out.println("Error: " + dfe.getMessage());
276 dfe.printStackTrace();
277 }
278 return content;
279 }
280  
281  
282 /**
283 * 生成二维码
284 * 
285 * @param filePath
286 * @return
287 */
288 public static void encoder(String filePath, String deskCode) {
289  
290  
291 String imgPath = filePath + deskCode + ".png";
292 TwoDimensionCode handler = new TwoDimensionCode();
293 handler.encoderQRCode(deskCode, imgPath, "png");
294 }
295  
296  
297 /**
298 * 解析二维码
299 * 
300 * @param filePath
301 * @return
302 */
303 public static String decoder(String filePath, int deskType, String deskCode) {
304  
305  
306 String imgPath = filePath + deskCode + ".png";
307 TwoDimensionCode handler = new TwoDimensionCode();
308 return handler.decoderQRCode(imgPath);
309 }
310  
311  
312 /**
313 * 生成中间带LOG二维码(QRCode)图片
314 * 
315 * @param content
316 *            二维码图片的内容
317 * @param imgPath
318 *            生成二维码图片完整的路径
319 * @param ccbpath
320 *            二维码图片中间的logo路径
321 */
322 public static int createQRCode(String content, String imgPath, String ccbPath) {
323  
324  
325 try {
326 Qrcode qrcodeHandler = new Qrcode();
327 qrcodeHandler.setQrcodeErrorCorrect('M');
328 qrcodeHandler.setQrcodeEncodeMode('B');
329 qrcodeHandler.setQrcodeVersion(7);
330  
331  
332 // System.out.println(content);
333 byte[] contentBytes = content.getBytes("utf-8");
334 BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
335 Graphics2D gs = bufImg.createGraphics();
336  
337  
338 gs.setBackground(Color.WHITE);
339 gs.clearRect(0, 0, 140, 140);
340  
341  
342 // 设定图像颜色 > BLACK
343 // gs.setColor(new Color(255, 200, 0));
344 gs.setColor(Color.BLACK);
345  
346  
347 // 设置偏移量 不设置可能导致解析出错
348 int pixoff = 2;
349  
350  
351 // 输出内容 > 二维码
352 if (contentBytes.length > 0 && contentBytes.length < 120) {
353 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
354 for (int i = 0; i < codeOut.length; i++) {
355 for (int j = 0; j < codeOut.length; j++) {
356 if (codeOut[j][i]) {
357 gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
358 }
359 }
360 }
361 } else {
362 System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. ");
363 return -1;
364 }
365 // Image img = ImageIO.read(new File(ccbPath));// 实例化一个Image对象。
366 // gs.drawImage(img, 47, 54, null);
367 // gs.setColor(Color.BLUE);
368 // gs.setFont(new Font("微软雅黑", Font.BOLD, 18));
369 // gs.drawString("交享越", 50, 70);
370 // gs.dispose();
371 bufImg.flush();
372  
373  
374 // 生成二维码QRCode图片
375 File imgFile = new File(imgPath);
376 ImageIO.write(bufImg, "png", imgFile);
377  
378  
379 } catch (Exception e) {
380 e.printStackTrace();
381 return -100;
382 }
383  
384  
385 return 0;
386 }
387  
388  
389 public static void main(String[] args) {
390 // String imgPath = "F:/TDDOWNLOAD/Michael_QRCode.png";
391 // String encoderContent = "123131";
392 // TwoDimensionCode handler = new TwoDimensionCode();
393 // handler.encoderQRCode(encoderContent, imgPath, "png");
394 // // try {
395 // // OutputStream output = new FileOutputStream(imgPath);
396 // // handler.encoderQRCode(content, output);
397 // // } catch (Exception e) {
398 // // e.printStackTrace();
399 // // }
400 // System.out.println("========encoder success");
401 //
402 // String decoderContent = handler.decoderQRCode(imgPath);
403 // System.out.println("解析结果如下:");
404 // System.out.println(decoderContent);
405 // System.out.println("========decoder success!!!");
406  
407  
408 // TwoDimensionCode
409 // .createQRCode(
410 // "http://jxy.misuland.com//jxy_manager//downLoadAction!downLoad.action?path=jxy_android.apk",
411 // "F://jxy_android.png",
412 // "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\image\\white.jpg");
413 TwoDimensionCode.createQRCode("http://localhost:8080//jxy_manager//downLoadAction!downLoad.action?path=jxy_android.apk", "F://jxy_android.png", "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\image\\white.jpg");
414 TwoDimensionCode.createQRCode("http://localhost:8080//jxy_manager//downLoadAction!downLoad.action?path=jxy_ios.apk", "F://jxy_ios.png", "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\image\\white.jpg");
415 }
416 }
417  
418   
419  
420   
421  
422 所需jar包可以在这个地址下载http://download.csdn.net/detail/daixinmei/5974269

 

posted @ 2016-05-30 16:37  魔流剑  阅读(1236)  评论(0编辑  收藏  举报