java二維碼生成-谷歌(Google.zxing)開源二維碼生成學習及實例

java二維碼生成-谷歌(Google.zxing)開源二維碼生成的實例及介紹java

 這裏咱們使用比特矩陣(位矩陣)的QR碼編碼在緩衝圖片上畫出二維碼工具

  實例有如下一個傳入參數測試

      OutputStream outputStream, 要存儲的文件google

      String content, 攜帶信息的內容編碼

      int qrCodeSize, 圖片大小spa

      String imageFormat 編碼code

  步驟:orm

  1.設置二維碼的糾錯級別參數blog

  //設置二維碼糾錯級別MAP
       Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>(); 
       hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矯錯級別

   2.建立比特矩陣圖片

1 QRCodeWriter qrCodeWriter = new QRCodeWriter(); 
2 //建立比特矩陣(位矩陣)的QR碼編碼的字符串 
3 BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap); 
4 // 使BufferedImage勾畫QRCode (matrixWidth 是行二維碼像素點)
5 int matrixWidth = byteMatrix.getWidth();

  3.開始在緩衝圖片中畫二維碼

 1  BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);  
 2             image.createGraphics();  
 3             Graphics2D graphics = (Graphics2D) image.getGraphics();  
 4             graphics.setColor(Color.WHITE);  
 5             graphics.fillRect(0, 0, matrixWidth, matrixWidth);  
 6             // 使用比特矩陣畫並保存圖像
 7             graphics.setColor(Color.BLACK);  
 8             for (int i = 0; i < matrixWidth; i++){
 9                 for (int j = 0; j < matrixWidth; j++){
10                     if (byteMatrix.get(i, j)){
11                         graphics.fillRect(i, j, 1, 1);  
12                     }
13                 }
14             }
15             ImageIO.write(image, imageFormat, outputStream);  

 二維碼生成的工具類代碼;

 1 package 。。。;
 2 
 3 import java.awt.Color;
 4 import java.awt.Graphics2D;
 5 import java.awt.image.BufferedImage;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.FileOutputStream;
 9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12 import java.util.Hashtable;
13 
14 import javax.imageio.ImageIO;
15 
16 import com.google.zxing.BarcodeFormat;
17 import com.google.zxing.BinaryBitmap;
18 import com.google.zxing.EncodeHintType;
19 import com.google.zxing.LuminanceSource;
20 import com.google.zxing.ReaderException;
21 import com.google.zxing.Result;
22 import com.google.zxing.WriterException;
23 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
24 import com.google.zxing.common.BitMatrix;
25 import com.google.zxing.common.HybridBinarizer;
26 import com.google.zxing.qrcode.QRCodeReader;
27 import com.google.zxing.qrcode.QRCodeWriter;
28 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
29 
30 /**
31  * 二維碼生成和讀的工具類
32  *
33  */
34 public class QrCodeCreateUtil {
35     
36     /**
37      * 生成包含字符串信息的二維碼圖片
38      * @param outputStream 文件輸出流路徑
39      * @param content 二維碼攜帶信息
40      * @param qrCodeSize 二維碼圖片大小
41      * @param imageFormat 二維碼的格式
42      * @throws WriterException 
43      * @throws IOException 
44      */
45     public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{  
46             //設置二維碼糾錯級別MAP
47             Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();  
48             hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  // 矯錯級別  
49             QRCodeWriter qrCodeWriter = new QRCodeWriter();  
50             //建立比特矩陣(位矩陣)的QR碼編碼的字符串  
51             BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);  
52             // 使BufferedImage勾畫QRCode  (matrixWidth 是行二維碼像素點)
53             int matrixWidth = byteMatrix.getWidth();  
54             BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);  
55             image.createGraphics();  
56             Graphics2D graphics = (Graphics2D) image.getGraphics();  
57             graphics.setColor(Color.WHITE);  
58             graphics.fillRect(0, 0, matrixWidth, matrixWidth);  
59             // 使用比特矩陣畫並保存圖像
60             graphics.setColor(Color.BLACK);  
61             for (int i = 0; i < matrixWidth; i++){
62                 for (int j = 0; j < matrixWidth; j++){
63                     if (byteMatrix.get(i, j)){
64                         graphics.fillRect(i-100, j-100, 1, 1);  
65                     }
66                 }
67             }
68             return ImageIO.write(image, imageFormat, outputStream);  
69     }  
70       
71     /**
72      * 讀二維碼並輸出攜帶的信息
73      */
74     public static void readQrCode(InputStream inputStream) throws IOException{  
75         //從輸入流中獲取字符串信息
76         BufferedImage image = ImageIO.read(inputStream);  
77         //將圖像轉換爲二進制位圖源
78         LuminanceSource source = new BufferedImageLuminanceSource(image);  
79         BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
80         QRCodeReader reader = new QRCodeReader();  
81         Result result = null ;  
82         try {
83          result = reader.decode(bitmap);  
84         } catch (ReaderException e) {
85             e.printStackTrace();  
86         }
87         System.out.println(result.getText());  
88     }
89     /**
90      * 測試代碼
91      * @throws WriterException 
92      */
93     public static void main(String[] args) throws IOException, WriterException {  
94         
95         createQrCode(new FileOutputStream(new File("d:\\qrcode.jpg")),"WE1231238239128sASDASDSADSDWEWWREWRERWSDFDFSDSDF123123123123213123",900,"JPEG");
96         readQrCode(new FileInputStream(new File("d:\\qrcode.jpg")));  
97     }  
98   
99 }  

 

 要是文章對你有用,隨手點個贊哦

相關文章
相關標籤/搜索