使用zxing,io.github.xudaojie.qrcodelib掃描本地二維碼圖檔時總是NotFoundException
下面是我的圖檔
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIwczX0xiRGZkRGZ0Xy9GbvNGL2EzXlpXazxSPFdkWoJVbalmRywEMW1mY1RzRapnTtxkb5ckYplTeMZTTINGMShUYfRHelRHLwEzX39GZhh2css2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xyayFWbyVGdhd3LcV2Zh1Wa9M3clN2byBXLzN3btg3Pn5GcukjM5QzNxQTM3ATMwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
使用手機攝像頭掃描沒問題,但是從相冊中選一直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;
}