• 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
  • 相关阅读:
    Huffman树与编码
    Python引用复制,参数传递,弱引用与垃圾回收
    Git使用说明
    numpy使用指南
    Python Socket
    温故知新之 数据库的事务、隔离级别、锁
    Oracle数据库的语句级读一致性
    VirtualBox NAT方式与主机互相通信
    Linux的定时任务
    Redis学习
  • 原文地址:https://www.cnblogs.com/zhaoran8775/p/8471707.html
Copyright © 2020-2023  润新知