Android zxing扫描本地二维码图片NotFoundException

使用zxing,io.github.xudaojie.qrcodelib扫描本地二维码图片时总是NotFoundException

下面是我的图片
在这里插入图片描述
使用手机摄像头扫描没问题,但是从相册中选一直NotFoundException

采坑1:

二维码尽量居中,而且相对于整个扫描的图片占比要大(如果图片就是整个二维码不考虑这个)

采坑 2:

即使 二维码占整个图片比例够大,甚至是整个二维码,扫描本地相册还会NotFoundException
解决方法:可能是你的图片太大导致扫描不出来,但是缩太小也会扫描不出来
所有在获得图片后适当的缩小图片试试:
以 io.github.xudaojie.qrcodelib.commonQrUtils.直接修改修改获得新的Options 的增长系数由2改为1.75
不使用io.github.xudaojie.qrcodelib也可以参考下面代码或者思路

//原方法 无修改
 public static Result decodeImage(final String path) {
	 //**********注意 256 这是一个合适的大小,我们要把图片限制在这个左右 易于识别 这个参数并不是为了限定图片大小是256**********
        Bitmap bitmap = QrUtils.decodeSampledBitmapFromFile(path, 256, 256);
        // Google Photo 相册中选取云照片是会出现 Bitmap == null
        if (bitmap == null) return null;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
        PlanarYUVLuminanceSource source1 = new PlanarYUVLuminanceSource(getYUV420sp(width, height, bitmap), width, height, 0, 0, width, height, false);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source1));
        HashMap<DecodeHintType, Object> hints = new HashMap<>();

        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");

        try {
            return new MultiFormatReader().decode(binaryBitmap, hints);
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    //原方法 无修改
   public static Bitmap decodeSampledBitmapFromFile(String imgPath, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imgPath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(imgPath, options);
    }
    
    //下面是修改的方法
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        float inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            //inSampleSize=Math.max(height/reqHeight,width/reqWidth);
            float halfHeight = (height / 1.75f) //**********注意 这里是修改后的增长因子 原来是2 缩小这个数据有利于选中更合适的大小**********
            float halfWidth = (width / 1.75f); //**********注意 这里是修改后的增长因子 原来是2 缩小这个数据有利于选中更合适的大小**********
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize = (inSampleSize * 1.75f); //**********注意 这里是修改后的增长因子 原来是2 缩小这个数据有利于选中更合适的大小**********
            }
        }
        return (int) inSampleSize;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值