java 多个二维码读取报错

依然范特西╮ 提交于 2020-01-19 00:45:54

 多个二维码的图片读取返回数组,在异常里处理

 public static String decode(File file) throws Exception {
        BufferedImage image;
        image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result=null;
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        //优化精度
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        //复杂模式,开启PURE_BARCODE模式
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        try{
            result = new MultiFormatReader().decode(bitmap, hints);
        }catch(Exception e){
            e.printStackTrace();
            QRCodeMultiReader qc = new QRCodeMultiReader();//一张图片有多张二维码取最后一个
            Result[] r = qc.decodeMultiple(bitmap, hints);
            if(r!=null && r.length>0){
                String resultStrTemp = r[r.length-1].getText();
                return resultStrTemp;
            }
        }
        String resultStr = result.getText();
        return resultStr;
    }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!