最近我在使用zxing解码位图时遇到了困难。我在网上搜索解决方案,我已经尝试了其中的一些。这是我的尝试:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.qrcode);
String result = DecodeUtils.decodeWithZxing(bitmap);
R.drawable.qrcode
是.jpg
文件。
, BarCodeUtil.java
为
public static String decodeWithZxing(Bitmap bitmap) {
MultiFormatReader multiFormatReader = new MultiFormatReader();
Map<DecodeHintType, Object> hints = new Hashtable<>();
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
multiFormatReader.setHints(hints);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
Result rawResult = null;
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
if (source != null) {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = multiFormatReader.decodeWithState(binaryBitmap);
} catch (ReaderException re) {
re.printStackTrace();
} finally {
multiFormatReader.reset();
}
}
return rawResult != null ? rawResult.getText() : null;
}
但是当我运行上面的代码时,我得到了一个异常:
com.google.zxing.NotFoundException
所以我搜索异常,有人认为位图大小导致这个异常。然后我调整位图的大小:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.zhifubao,options);
String result = DecodeUtils.decodeWithZxing(bitmap)
但它仍然不适合我。
有一个很好的解决方案解码位图与qrcode?
我知道这可能已经很晚了,但是,这只是意味着在图像中没有发现条形码。看到
NotFoundException
当图像中没有找到条形码时抛出。
如果其他人最终遇到同样的问题,即无法解码条形码(如QRcode或upc),则可能是使用了错误的位图颜色空间。当来自相机的数据实际上是YUV时,您可能正在尝试使用RGB。
代替rgblumancesource,你应该使用PlanarYUVLuminanceSource,就像这里的例子
您的开发工具是eclipse还是android studio ?
如果是android studio:
在你的project> app> build中。add:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}