java-二维码的生成与解析

1.部署依赖

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>

2.二维码的生成

        String url="www.baidu.com";//二维码的内容
        int widthHeight=500;
        //二维码生成的路劲和名字
        String filePathName="C:\\users\\asus\\desktop\\"+System.currentTimeMillis()+".jpg";

        Path path= FileSystems.getDefault().getPath(filePathName);
        QRCodeWriter writer=new QRCodeWriter();
        BitMatrix matrix;
        try {
            matrix = writer.encode(url, BarcodeFormat.QR_CODE,widthHeight,widthHeight);
            try {
                //已生成二维码
                MatrixToImageWriter.writeToPath(matrix,"JPG",path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (WriterException e) {
            e.printStackTrace();
        }
        System.out.println("完成");

3.二维码的解析

        //要解析的二维码路径
        String filename="C:\\users\\asus\\desktop\\1651062084114.jpg";
        try {
            BufferedImage image= ImageIO.read(new File(filename));
            LuminanceSource source=new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap=new BinaryBitmap(new HybridBinarizer(source));
            Map map=new HashMap();
            map.put(DecodeHintType.CHARACTER_SET,"utf-8");
            try {
                Result result= new MultiFormatReader().decode(bitmap,map);
                System.out.println("解析结果:"+result.toString());
                System.out.println("二维码格式"+result.getBarcodeFormat());
                System.out.println("文本内容"+result.getText());
                System.out.println(result.getNumBits());
            } catch (NotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值