1. 程式人生 > >Java利用zxing創建和讀取二維碼

Java利用zxing創建和讀取二維碼

eve override AI 讀取 技術 gin margin version exc

一、maven依賴

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.zxing/core 
--> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.zxing/javase --> <
dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency
> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency> </dependencies>

二、用的的實體類

技術分享圖片
public class FiveElements {
    private String name;
    private String gender;
    private String idType;
    private String idno;
    private String mobile;
    
    public FiveElements() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getIdType() {
        return idType;
    }

    public void setIdType(String idType) {
        this.idType = idType;
    }

    public String getIdno() {
        return idno;
    }

    public void setIdno(String idno) {
        this.idno = idno;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Override
    public String toString() {
        return "FiveElements [name=" + name + ", gender=" + gender + ", idType=" + idType + ", idno=" + idno
                + ", mobile=" + mobile + "]";
    }
    
}
View Code

三、工具類

技術分享圖片
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * 
 * @ClassName:QrCodeUtils
 * @Description:創建和讀取二維碼
 * @author doubledumbao
 * @修改時間:2018年2月26日 上午9:47:00
 * @修改內容:創建
 */
public class  QrCodeUtils{

    public static final int WIDTH = 300;
    public static final int HEIGHT = 300;
    public static final String FORMAT = "png";
    public static final String CHARTSET = "utf-8";

    public static void main(String[] args) {
        String filePath = "/Users/doubledumbao/zhao.png";
        createQRcode(filePath);
        testReadQRcode(filePath);
    }
    private static void testReadQRcode(String filePath) {
        Result result = getQRresult(filePath);
        if (result != null) {
            System.out.println("二維碼內容:" + result.getText());
            if (result.getText() != null) {
                FiveElements fiveElements = JSONObject.parseObject(result.getText(), FiveElements.class);
                System.out.println(fiveElements);
            }
            System.out.println("二維碼格式:" + result.getBarcodeFormat());
        }
    }
    /**
     * 
     * @Title:createQRcode
     * @Description:創建二維碼
     * @param filePath
     * @author doubledumbao
     * @修改時間:2018年2月26日 上午9:44:45
     * @修改內容:創建
     */
    public static void createQRcode(String filePath) {
        /**
         * 如果用的jdk是1.9,需要配置下面這一行。
         */
        //System.setProperty("java.specification.version", "1.9");
        FiveElements fiveElements = new FiveElements();
        fiveElements.setName("zhao");
        fiveElements.setGender("M");
        fiveElements.setIdType("I");
        fiveElements.setIdno("370983");
        fiveElements.setMobile("1805310");
        String contents = JSON.toJSONString(fiveElements);
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, CHARTSET);
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
            Path file = new File(filePath).toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, file);
            System.out.println("創建二維碼完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 
     * @Title:getQRresult
     * @Description:讀取二維碼
     * @param filePath
     * @return
     * @author doubledumbao
     * @修改時間:2018年2月26日 上午9:45:19
     * @修改內容:創建
     */
    public static Result getQRresult(String filePath) {
        /**
         * 如果用的jdk是1.9,需要配置下面這一行。
         */
        //System.setProperty("java.specification.version", "1.9");
        Result result = null;
        try {
            File file = new File(filePath);

            BufferedImage bufferedImage = ImageIO.read(file);
            BinaryBitmap bitmap = new BinaryBitmap(
                    new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));

            HashMap hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, CHARTSET);
            result = new MultiFormatReader().decode(bitmap, hints);
        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

}
View Code

Java利用zxing創建和讀取二維碼