今天忙了一天的二维码,用了QRcode和ZXing两个开源包。结果发现

ZXing比QRcode更好用一些,它直接可以定义二维码生成图案的大小,而QRcode生成的二维码是根据二维码包含的内容多少来定的。所以生成的二维码图片大小不好确定。

下面主要说一下ZXing生成二维码的方法:

  ZXing是google的开源资源,但是由于中国的某些原因使得访问谷歌的资源比较难,所以在网上找到了一个可以下载的连接:http://www.cr173.com/soft/65529.html

  下载好需要的核心jar包后我们还需要下载一个可以将资源输出到文件或流的工具类MatrixToImageWriter,以下是百度到的源码:

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

接下来需要写一个测试类了,不过我已经测试过了,并进行了简单包装:

 

 1 package zxingTest;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.io.OutputStream;
 6 import java.util.Hashtable;
 7 
 8 import com.google.zxing.BarcodeFormat;
 9 import com.google.zxing.EncodeHintType;
10 import com.google.zxing.MultiFormatWriter;
11 import com.google.zxing.WriterException;
12 import com.google.zxing.common.BitMatrix;
13 
14 public class TwoDimCodeUtil {
15 
16     /**
17      * @Title: writeToFile 将二维码输出到文件
18      * @Description: 将二维码输出到文件
19      * @param fileName 文件名称
20      * @param contents  二维码包含的内容
21      * @param encodeType  字符编码
22      * @param imgSize  二维码生成图片大小,单位像素
23      * @param imgType 图片格式,jpg,png等
24      * @throws
25      */
26     public static void writeToFile(String fileName,String contents,String encodeType,int imgSize,String imgType){
27         Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
28         hints.put(EncodeHintType.CHARACTER_SET,encodeType);  
29         BitMatrix matrix = null;  
30         try {  
31             matrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, imgSize, imgSize, hints);  
32             File file = new File(fileName);  
33             MatrixToImageWriter.writeToFile(matrix, imgType, file);  
34         } catch (WriterException e) {  
35             e.printStackTrace();  
36         } catch (IOException e) {  
37                 e.printStackTrace();  
38             }  
39     }
40     
41     /**
42      * @Title: writeToFile 将二维码输出到流
43      * @Description: 将二维码输出到流
44      * @param outputStream 输出流
45      * @param contents  二维码包含的内容
46      * @param encodeType  字符编码
47      * @param imgSize  二维码生成图片大小
48      * @param imgType 图片格式,jpg,png等
49      * @throws
50      */
51     public static void writeToStream(OutputStream outputStream,String contents,String encodeType,int imgSize,String imgType){
52         Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
53         hints.put(EncodeHintType.CHARACTER_SET,encodeType);  
54         BitMatrix matrix = null;  
55         try {  
56             matrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, imgSize, imgSize, hints);  
57             MatrixToImageWriter.writeToStream(matrix, imgType, outputStream);
58         } catch (WriterException e) {  
59             e.printStackTrace();  
60         } catch (IOException e) {  
61                 e.printStackTrace();  
62         }  
63     }
64     
65     /**
66      * @Title: writeToFile 将二维码输出到流,默认编码utf-8,文件类型jpg
67      * @Description: 将二维码输出到流
68      * @param outputStream 输出流
69      * @param contents  二维码包含的内容
70      * @param imgSize  二维码生成图片大小
71      * @throws
72      */
73     public static void writeToStream(OutputStream outputStream,String contents,int imgSize){
74         TwoDimCodeUtil.writeToStream(outputStream, contents, "UTF-8", imgSize, "jpg");
75     }
76 }

下面是调用的一个例子:

 1 public void showQRCode(){
 2         HttpServletRequest request=this.getRequest();
 3         HttpServletResponse response=this.getResponse();
 4         response.setHeader("Pragma","No-cache");
 5         response.setHeader("Cache-Control","no-cache");
 6         response.setDateHeader("Expires", 0);
 7         response.setContentType("image/jpeg"); 
 8         
 9         String content=request.getParameter("content");
10         String widthstr= request.getParameter("width");
11         int width=(widthstr==null||widthstr.equals(""))?140:Integer.valueOf(widthstr);
12         System.out.println(content);
13         try {
14             TwoDimCodeUtil.writeToStream(response.getOutputStream(), content, width);
15         } catch (IOException e) {
16             e.printStackTrace();
17         }
18     }

页面调用:

<img src="/showQRCode.do?content=helloworld&width=140" />