栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java-二维码的生成与解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java-二维码的生成与解析

1.部署依赖

        
            com.google.zxing
            javase
            3.3.0
        

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();
        }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842854.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号