Zxing选择本地图片扫码失败的bug**过程
0.0
当然,用的轮子的轮子:QrCodeDemo4
再一次 向 Ctrl C ,Ctrl V 致敬。
bug如下。
当然,并不是所有的二维码都是识别失败。观察了一下,像素较低的,如500以内像素,且二维码占图片比例较大的就会提示失败。
ok,问题了解,我就开始面向百度编程了。然而。
没有发现解决方案。GG 思密达。
这一瞬间,我对我的狗生,产生了怀疑,难道我就只能解决那些百度可以解决的问题么?
ε=(´ο`*)))唉
然后我在Zxing 官方网站试了下在线解码,可以解码。
好吧,不想讲故事了,debug,一路跳进去。
public static Bitmap decodeUri(Context context, Uri uri, int maxWidth, int maxHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //只读取图片尺寸
readBitmapScale(context, uri, options);
//计算实际缩放比例
int scale = 1;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if ((options.outWidth / scale > maxWidth &&
options.outWidth / scale > maxWidth * 1.4) ||
(options.outHeight / scale > maxHeight &&
options.outHeight / scale > maxHeight * 1.4)) {
scale++;
} else {
break;
}
}
options.inSampleSize = scale;
// options.inSampleSize = 2;
options.inJustDecodeBounds = false;//读取图片内容
options.inPreferredConfig = Bitmap.Config.RGB_565; //根据情况进行修改
Bitmap bitmap = null;
try {
bitmap = readBitmapData(context, uri, options);
} catch (Throwable e) {
e.printStackTrace();
}
return bitmap;
}
发现如果把options.inSampleSize这个值改为2的话,图片可以识别出来。
查了下这个值的含义
/**
* If set to a value > 1, requests the decoder to subsample the original
* image, returning a smaller image to save memory. The sample size is
* the number of pixels in either dimension that correspond to a single
* pixel in the decoded bitmap. For example, inSampleSize == 4 returns
* an image that is 1/4 the width/height of the original, and 1/16 the
* number of pixels. Any value <= 1 is treated the same as 1. Note: the
* decoder uses a final value based on powers of 2, any other value will
* be rounded down to the nearest power of 2.
*/
public int inSampleSize;
借助一下有道。
/ * *`
`*如果设置为值> 1,则请求解码器对原始代码进行子采样`
`*图像,返回较小的图像以节省内存。样本量为`
`*对应于单个维度的像素数`
`*解码位图中的像素。例如,inSampleSize == 4返回`
`*图像的宽度/高度是原始图像的1/4,而宽度/高度是原始图像的1/16`
`*像素数。任何<= 1的值都被视为1。注意:`
`*解码器使用基于2的幂的最终值,任何其他值都会`
`*四舍五入到最近的2次方。`
`\* /
好吧,虽然不知道这个参数究竟会对识别结果有多大影响,生成的位图究竟会是什么狗jb东西,怎么导致识别失败的。但我好像知道该改哪里了。
scanBitmap = BitmapUtil.decodeUri(this, uri, 200, 200);
// scanBitmap = BitmapUtil.decodeUri(this, uri, 500, 500);
原值是500,然后我改成200,ojbk,可以识别了,虽然不知道为什么,但bug解决了。
至于会不会产生其他bug ,who care。
好嗨哟,感觉人生已经到达了高潮。
我得给作者提个issue。