您的位置:首页 > 其它

如何生成二维码过程详解

2014-11-04 10:42 435 查看
1 下载zxing2.1

2 本代码配置环境:eclipse、java1.6、windows8、zxing2.1

3 解压后将文件夹里面core/src下面的com文件夹导入到eclipse工程(工程可以自己建,如QrCode)中,图示如下:





 

  注意:在源码中需要修改其编码配置为UTF-8,否则后面解码后面的文件中中文会乱码,修改图示如下:



4 TestEnDeCode.java源代码

1 package test;
2     import java.awt.image.BufferedImage;
3     import java.io.File;
4     import java.io.IOException;
5     import java.util.Hashtable;
6     import java.util.Scanner;
7     import javax.imageio.ImageIO;
8     import com.google.zxing.BarcodeFormat;
9     import com.google.zxing.BinaryBitmap;
10     import com.google.zxing.DecodeHintType;
11     import com.google.zxing.LuminanceSource;
12     import com.google.zxing.MultiFormatReader;
13     import com.google.zxing.MultiFormatWriter;
14     import com.google.zxing.Reader;
15     import com.google.zxing.ReaderException;
16     import com.google.zxing.Result;
17     import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
18     import com.google.zxing.client.j2se.MatrixToImageWriter;
19     import com.google.zxing.common.BitMatrix;
20     import com.google.zxing.common.HybridBinarizer;
21
22     public class TestEnDeCode {
23
24         /**
25          *
26          */
27         public TestEnDeCode() {
28             // TODO Auto-generated constructor stub
29         }
30
31         /**
32          * @param args
33          */
34         public static void main(String[] args) {
35             TestEnDeCode t=new TestEnDeCode();
36             Scanner in = new Scanner(System.in);
37             System.out.println("编码内容:");
38             String str = in.next();
39 //            String str = "http://www.baidu.com";
40             String path = "D:/Qr_pics/test7.png";
41             t.encode(str, path);
42             t.decode(path);
43         }
44
45         /*
46          * 编码:
47          * 1 将内容转换成二维矩阵
48          * 2 将该二维矩阵转换成图片
49          * */
50         public void encode(String str, String path) {
51             try {
52 //                String str = "http://www.baidu.com百度看看";// 二维码内容
53 //                String path = "D:/Qr_pics/test7.png"; //二维码图片生成 路径及名称
54                 BitMatrix byteMatrix;
55                 byteMatrix = new MultiFormatWriter().encode(new String(str.getBytes("UTF-8"),"UTF-8"), BarcodeFormat.QR_CODE, 800, 800); //将文字转换成二维矩阵,并设置矩阵大小,这里的矩阵大小就是后面生成的图片像素大小
56                 File file = new File(path);//新建矩阵文件
57                 MatrixToImageWriter.writeToFile(byteMatrix, "gif", file);//将矩阵文件转换成图片文件
58             } catch (Exception e) {
59                 e.printStackTrace();
60             }
61         }
62
63         /*
64          * 解码:
65          * 1 将图片反解码为二维矩阵
66          * 2 将该二维矩阵解码为内容
67          * */
68         public void decode(String imgPath) {
69             try {
70                 Reader reader = new MultiFormatReader();
71 //                String imgPath = "D:/Qr_pics/test7.png";//获取即将被解码图片的路径
72                 File file = new File(imgPath);//获取该图片文件
73                 BufferedImage image;
74                 try {
75                     image = ImageIO.read(file);
76                     if (image == null) {
77                         System.out.println("Could not decode image");
78                     }
79                     LuminanceSource source = new BufferedImageLuminanceSource(image);
80                     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
81                     Result result;
82                     Hashtable hints = new Hashtable();//将图片反解码为二维矩阵
83                     hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
84                     result = new MultiFormatReader().decode(bitmap, hints);//将该二维矩阵解码成内容
85                     String resultStr = result.getText();
86                     System.out.println("\n解码结果:\n"+resultStr);
87
88                 } catch (IOException ioe) {
89                     System.out.println(ioe.toString());
90                 } catch (ReaderException re) {
91                     System.out.println(re.toString());
92                 }
93
94             } catch (Exception ex) {
95                 System.out.println(ex.toString());
96             }
97         }
98
99     }


 

5 运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: