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

來源:互聯網
上載者:User

標籤:png   client   epo   http   null   character   spec   element   lap   

一、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建立和讀取二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.