如何生成二維碼過程詳解


 

如何生成二維碼過程詳解

 

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 運行結果:

6 生成的二維碼如下圖所示:

 

  原創文章歡迎轉載,轉載時請注明出處。

  作者推薦文章:

    》Java自學之道

    》Eclipse中部署Hadoop2.3.0並直接在Eclipse中提交mapreduce任務

    》Java如何獲取系統信息(包括操作系統、jvm、cpu、內存、硬盤、網絡等)

    》Java如何生成二維碼過程詳解

    總結5種高效常用的排序算法




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM