Java利用Zxing生成二维码

本文详细介绍了如何使用ZXing工具生成与解析二维码,包括生成步骤和解析方法。

Zxing是Google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法。

1、二维码生成

try {
                 
                 String content = "http://www.oschina.net";
                 String path = "D:/testImage/qrcode";
                 
                 String filename = "qrcode.jpg";
                 MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                 
                 Map hints = new HashMap();
                 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                 BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints);
                 File file1 = new File(path);
                 if(!file1.exists()){
                     file1.mkdirs();
                 }
                 file1 = new File(path,filename);
                 MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
                 
             } catch (Exception e) {
                 e.printStackTrace();
             }

2、二维码解析

try {
            MultiFormatReader formatReader = new MultiFormatReader();
            String filePath = "D:/testImage/qrcode/qrcode169.jpg";
            File file = new File(filePath);
            BufferedImage image = ImageIO.read(file);;
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer  binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            Result result = formatReader.decode(binaryBitmap,hints);
            
            System.out.println("result = "+ result.toString());
            System.out.println("resultFormat = "+ result.getBarcodeFormat());
            System.out.println("resultText = "+ result.getText());
                        
        } catch (Exception e) {
            e.printStackTrace();
        }


转载于:https://my.oschina.net/u/992626/blog/224979

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值