谷歌zxing 二維碼生成工具


一、加入maven依賴

<!-- 谷歌zxing 二維碼 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.3</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.3</version>
</dependency>

二、工具類代碼

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

效果圖:

 


免責聲明!

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



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