Java解析二维码

最近用到了解析二维码,生成二维码,这里将解析二维码的简单介绍分享在这里啦?。
用到的jar
com.google.zxing.core-3.3.3,com.google.zxing.javase-3.3.3
版本(3.2.1)也可以,这两个在测试都时候都用到了……主要是走弯路踩到了他们?

生成简单二维码:(想要复杂二维码的猿可以自行丰富哈)

	int width=300;
    int height=300;
    String format="png";
    String contents="http://www.taobao.com/help/getip.php";//获取IP(淘宝)获取地址成功
    String filepath="D:/QRCodeTest/picture003.png";//生成二维码路径
    HashMap map=new HashMap();
    map.put(EncodeHintType.CHARACTER_SET, "utf-8");
    map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
    map.put(EncodeHintType.MARGIN, 0);
    try {
        BitMatrix bm = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height);
        Path file=new File(filepath).toPath();
        MatrixToImageWriter.writeToPath(bm, format, file);
    } catch (WriterException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

三种代码情况:
第一种:黑白色二维码 最普通

		BufferedImage image=ImageIO.read(new File(filepath));
        BinaryBitmap bb=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        HashMap map =new HashMap();
        map.put(DecodeHintType.CHARACTER_SET, "utf-8");
        Result result = new MultiFormatReader().decode(bb,map);
        System.out.println("解析结果:"+result.toString());
        System.out.println("二维码格式类型:"+result.getBarcodeFormat());
        System.out.println("二维码文本内容:"+result.getText());

第二种:黑白色有 logo的二维码

		BufferedImage image=ImageIO.read(new File(filepath));
        BinaryBitmap bb=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        HashMap map =new HashMap();
        map.put(DecodeHintType.CHARACTER_SET, "utf-8");
        map.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        map.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        Result result = new MultiFormatReader().decode(bb,map);
        System.out.println("解析结果:"+result.toString());
        System.out.println("二维码格式类型:"+result.getBarcodeFormat());
        System.out.println("二维码文本内容:"+result.getText());

代码差异:比第一种多了两行代码
map.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
map.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

第三种:彩色有 logo的二维码,(可能还有不同程度的透明图层)

(先将彩色转为黑白色,提高解析成功率)

public static void changeImge(String filepath) {
    try {
        File f =new File(filepath);
        Image image = ImageIO.read(f);
        int srcH = image.getHeight(null);
        int srcW = image.getWidth(null);
        BufferedImage bufferedImage = new BufferedImage(srcW, srcH,BufferedImage.TYPE_3BYTE_BGR);
        bufferedImage.getGraphics().drawImage(image, 0,0, srcW, srcH, null);
        bufferedImage=new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),null).filter (bufferedImage,null);
        FileOutputStream fos = new FileOutputStream(f);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
        encoder.encode(bufferedImage);
        fos.close();
        // System.out.println("转换成功...");
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalStateException("图片转换出错!", e);
    }
}

解析二维码

		BufferedImage image=ImageIO.read(new File(filepath));
		LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bb=new BinaryBitmap(new HybridBinarizer(source));
        HashMap map =new HashMap();
        map.put(DecodeHintType.CHARACTER_SET, "utf-8");
        Result result = new MultiFormatReader().decode(bb,map);
        System.out.println("解析结果:"+result.toString());
        System.out.println("二维码格式类型:"+result.getBarcodeFormat());
        System.out.println("二维码文本内容:"+result.getText());

代码差异:解读二维码及基本信息
(自认为是二维码从图片信息到字符信息的初步转化,描述得有点模糊,希望有路过的大侠有知道的在评论区科普一下)
第一种和第二种:
BinaryBitmap bb=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
第三种:
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bb=new BinaryBitmap(new HybridBinarizer(source));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值