Java利用zxing创建和读取二维码

Posted doubledumbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java利用zxing创建和读取二维码相关的知识,希望对你有一定的参考价值。

一、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创建和读取二维码的主要内容,如果未能解决你的问题,请参考以下文章

Java生成读取条形码和二维码图片

qrcode render 二维码扫描读取

利用Zxing扫描和产生二维码

javagoogle的zxing架包生成二维码和读取二维码可带文字和logo

使用 zxing 读取图像中的二维码

[Python]在Windows系统中使用ZXing模块实现二维码条形码读码