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

com.google.zxing.NotFoundException

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

com.google.zxing.NotFoundException

使用zxing识别图片二维码com.google.zxing.NotFoundException的问题
最开始只使用了扫描单个二维码的方法,一直com.google.zxing.NotFoundException
后面增加了扫描多个二维码方法成功了。

public class QrBarToolUtils {

    //一张图有多个二维码
    public static Result[] decodeMultiFromPhoto(Bitmap photo) {
        HashMap hints = new HashMap();
        hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); // 指定编码方式,防止中文乱码
        Result[] rawResults = null;
        int width = photo.getWidth();
        int height = photo.getHeight();
        int[] pixels = new int[width * height];
        photo.getPixels(pixels, 0, width, 0, 0, width, height);
        // 新建一个RGBLuminanceSource对象
        RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
        // 将图片转换成二进制图片
        QRCodeMultiReader reader = new QRCodeMultiReader();// 初始化解析对象
        try {
            rawResults = reader.decodeMultiple(new BinaryBitmap(new HybridBinarizer(source)), hints);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                rawResults = reader.decodeMultiple(new BinaryBitmap(new GlobalHistogramBinarizer(source)), hints);
            } catch (Exception secondE) {
                secondE.printStackTrace();
            }
        }
        return rawResults;
    }

    
    public static Result decodeFromPhoto(Bitmap smallBitmap ) {
        Result rawResult = null;
        if (smallBitmap != null) {
        //注释掉 回收bitmap 容易出问题。
            //Bitmap smallBitmap = ImageToolUtils.zoomBitmap(photo, photo.getWidth() / 2, photo.getHeight() / 2);// 为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存
            //photo.recycle(); // 释放原始图片占用的内存,防止out of memory异常发生

            MultiFormatReader multiFormatReader = new MultiFormatReader();

            // 解码的参数
            Hashtable hints = new Hashtable<>(2);
            // 可以解析的编码类型
            Vector decodeFormats = new Vector<>();
            decodeFormats = new Vector<>();

            Vector PRODUCT_FORMATS = new Vector<>(5);
            PRODUCT_FORMATS.add(BarcodeFormat.UPC_A);
            PRODUCT_FORMATS.add(BarcodeFormat.UPC_E);
            PRODUCT_FORMATS.add(BarcodeFormat.EAN_13);
            PRODUCT_FORMATS.add(BarcodeFormat.EAN_8);
            // PRODUCT_FORMATS.add(BarcodeFormat.RSS14);
            Vector ONE_D_FORMATS = new Vector<>(PRODUCT_FORMATS.size() + 4);
            ONE_D_FORMATS.addAll(PRODUCT_FORMATS);
            ONE_D_FORMATS.add(BarcodeFormat.CODE_39);
            ONE_D_FORMATS.add(BarcodeFormat.CODE_93);
            ONE_D_FORMATS.add(BarcodeFormat.CODE_128);
            ONE_D_FORMATS.add(BarcodeFormat.ITF);
            Vector QR_CODE_FORMATS = new Vector<>(1);
            QR_CODE_FORMATS.add(BarcodeFormat.QR_CODE);
            Vector DATA_MATRIX_FORMATS = new Vector<>(1);
            DATA_MATRIX_FORMATS.add(BarcodeFormat.DATA_MATRIX);

            // 这里设置可扫描的类型,我这里选择了都支持
            decodeFormats.addAll(ONE_D_FORMATS);
            decodeFormats.addAll(QR_CODE_FORMATS);
            decodeFormats.addAll(DATA_MATRIX_FORMATS);
            hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); // 指定编码方式,防止中文乱码
            // 设置继续的字符编码格式为UTF8
            // hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
            // 设置解析配置参数
            multiFormatReader.setHints(hints);
            // 开始对图像资源解码
            try {
                rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(smallBitmap))));
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new GlobalHistogramBinarizer(new BitmapLuminanceSource(smallBitmap))));
                } catch (Exception secondE) {
                    secondE.printStackTrace();
                }
            }
            if (rawResult == null) {
                Result[] rawResults = decodeMultiFromPhoto(smallBitmap);
                if (rawResults != null && rawResults.length > 0) {
                    rawResult = rawResults[0];
                }
            }
        }
        return rawResult;
    }
}

代码调用
QrBarToolUtils.decodeFromPhoto(bitmap)
代码是demo可以优化,自行优化吧。

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

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

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