码迷,mamicode.com
首页 > 其他好文 > 详细

Google Zxing - 生成二维码

时间:2015-08-17 15:29:08      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

Google Zxing

最近在某论坛上看到Zxing 这个插件 , 决定用用 !首先它是用于生成二维码的 , 类似于登录时的验证码 , 他也是从后台生成一张图片 , 只不过二维码图案涉及一定算法 , 并且有一定的规律 ,这些都是前辈们定好了 ,并且已经把这里面复杂的代码封装了 , 接下来的工作就简单了!

首先在网上搜索Zxing ,或者去 maven repository 下载zxing jar!

1、手动添加如下代码(核心实现)

技术分享
 1 package com.wisesoft.ztree.util;
 2 import com.google.zxing.common.BitMatrix;
 3 import javax.imageio.ImageIO;
 4 import java.io.File;
 5 import java.io.OutputStream;
 6 import java.io.IOException;
 7 import java.awt.image.BufferedImage;
 8 public final class MatrixToImageWriter {
 9     private static final int BLACK = 0xFF000000;
10     private static final int WHITE = 0xFFFFFFFF;
11     private MatrixToImageWriter() {
12     }
13     public static BufferedImage toBufferedImage(BitMatrix matrix) {
14         int width = matrix.getWidth();
15         int height = matrix.getHeight();
16         BufferedImage image = new BufferedImage(width, height,
17                 BufferedImage.TYPE_INT_RGB);
18         for (int x = 0; x < width; x++) {
19             for (int y = 0; y < height; y++) {
20                 image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
21             }
22         }
23         return image;
24     }
25     public static void writeToFile(BitMatrix matrix, String format, File file)
26             throws IOException {
27         BufferedImage image = toBufferedImage(matrix);
28         if (!ImageIO.write(image, format, file)) {
29             throw new IOException("Could not write an image of format "
30                     + format + " to " + file);
31         }
32     }
33     public static void writeToStream(BitMatrix matrix, String format,
34             OutputStream stream) throws IOException {
35         BufferedImage image = toBufferedImage(matrix);
36         if (!ImageIO.write(image, format, stream)) {
37             throw new IOException("Could not write an image of format "
38                     + format);
39         }
40     }
41 }
42 
43                     
MatrixToImageWriter

“matrix.get(x, y) ? BLACK : WHITE”  MatrixToImageWriter 类是绘制图形的核心类 , 通过循环每一个像素点 , 并且询问BitMatrix.get(x,y)返回时true或者false 决定绘制黑白条纹

2、二维码规格定义部分

技术分享
 1 package com.wisesoft.ztree.controller;
 2 import java.io.IOException;
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import com.google.zxing.BarcodeFormat;
 8 import com.google.zxing.MultiFormatWriter;
 9 import com.google.zxing.WriterException;
10 import com.google.zxing.common.BitMatrix;
11 import com.wisesoft.ztree.util.MatrixToImageWriter;
12 
13 @Controller
14 @RequestMapping("/qr")
15 public class QRController {
16     @RequestMapping("put")
17     public void qr(HttpServletRequest req,HttpServletResponse resp) throws WriterException, IOException{
18         String contents = "http://sqdd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk"; //二维码链接地址
19         int width = 300;    //生成二维码宽度
20         int height = 300;    //高度
21         String format = "png"; //二维码文件格式 
22         BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height); //xzing插件核心,将参数传入
23         //MatrixToImageWriter.writeToFile(bitMatrix,format, new File("D:\\qq_download.png")); //将生成的二维码写入文件中
24         MatrixToImageWriter.writeToStream(bitMatrix,format, resp.getOutputStream()); //将生成的二维码放入流中
25         //MatrixToImageWriter.toBufferedImage(matrix); 
26     }
27 }
QRController

writeToStream 的三个参数 , 第一个是BitMatrix对象 ,第二个 是format为图片格式 ,第三个是一个输出流对象

如果format格式不对:

MatrixToImageWriter.writeToStream(bitMatrix,"aaaaa", resp.getOutputStream());

异常:

"Could not write an image of format aaaaa"

 

Google Zxing - 生成二维码

标签:

原文地址:http://www.cnblogs.com/iNewbie/p/4736654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!