700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android zxing扫描本地二维码图片NotFoundException

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

时间:2021-02-16 11:58:52

相关推荐

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

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

下面是我的图片

使用手机摄像头扫描没问题,但是从相册中选一直NotFoundException

采坑1:

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

采坑 2:

即使 二维码占整个图片比例够大,甚至是整个二维码,扫描本地相册还会NotFoundException

解决方法:可能是你的图片太大导致扫描不出来,但是缩太小也会扫描不出来

所有在获得图片后适当的缩小图片试试:

以 io.github.monQrUtils.直接修改修改获得新的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 == nullif (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 dimensionsfinal BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(imgPath, options);// Calculate inSampleSizeoptions.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);// Decode bitmap with inSampleSize setoptions.inJustDecodeBounds = false;return BitmapFactory.decodeFile(imgPath, options);}//下面是修改的方法public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {// Raw height and width of imagefinal 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;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。