java zxing生成二維碼、二維碼識別

配置

maven倉庫:

<dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.0.0</version>
</dependency>
  1. 生成二維碼

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * 生成二維碼
 * @author crazy
 *
 */
public class CreateQRCode {

    public static void main(String[] args) {
        int width = 300;//圖片的寬度
        int height = 300;//圖片的高度
        String format = "png";//圖片的格式
        String contents = "www.baidu.com";//圖片的內容,即網址鏈接

        //定義二維碼的參數
        HashMap map = new HashMap();
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");//字符集
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//糾錯等級LMQH,一般用M
        map.put(EncodeHintType.MARGIN, 2);//邊距

        //生成二維碼
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, map);

            Path file = new File("D:/img.png").toPath();

            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



2.識別二維碼


import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

/**
 * 讀取二維碼的內容
 * @author crazy
 *
 */
public class ReadQRCode {

    public static void main(String[] args) {
        try {
            MultiFormatReader formatReader = new MultiFormatReader();

            //讀取文件識別成一個圖片
            File file = new File("D:/img.png");
            BufferedImage image = ImageIO.read(file);

            /*
            BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
            HybridBinarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            */
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));


            //定義二維碼的參數
            HashMap map = new HashMap();
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");//字符集


            Result result = formatReader.decode(binaryBitmap, map);

            System.out.println("解析結果:"+result.toString());
            System.out.println("二維碼格式:"+result.getBarcodeFormat());
            System.out.println("二維碼文本內容:"+result.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章