android - com.google.zxing.NotFoundException - 在 Android 中从图库中扫描 QR 图像时
问题描述
尝试从图库扫描 QR 图像时出现以下错误。而且文件大小只有4kb。
com.google.zxing.NotFoundException
下面是示例代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
//the case is because you might be handling multiple request codes here
case 111:
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
//getting the image
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), "File not found", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
//decoding bitmap
Bitmap bMap = BitmapFactory.decodeStream(imageStream);
int[] intArray = new int[bMap.getWidth() * bMap.getHeight()];
// copy pixel data from the Bitmap into the 'intArray' array
bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();// use this otherwise
try {
Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>();
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
decodeHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
Result result = reader.decode(bitmap, decodeHints);
String barcode = result.getText().toString();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
break;
}
}
解决方案
通过使用 google play services vision API 修复了该问题。以下是对解决问题非常有帮助的链接。
https://code.tutsplus.com/tutorials/reading-qr-codes-using-the-mobile-vision-api--cms-24680
推荐阅读
- ios - iPhone 6 / iPhone6 Plus上的Wordpress图像渲染问题
- javascript - 对齐带有图像的 HTML 中心
- c - 如何在生成随机表达式的 C 程序中检查除数是否为 0?
- c# - 我应该创建 DTO 以从 put 方法中的对象中删除 id 吗?
- sql - SQL 依赖关系查询 - SQL Server
- ios - 一个人的 Fastlane 会话在 CI 管道中工作,而另一个人却没有的常见原因是否有?
- php - 如何在 PHP 中使用 WCF 服务?
- javascript - 使用公共 ACM 在 AWS 上部署 Expressjs
- android - 使用 Mapbox SDK for Android 开始地理围栏的任何可能方式
- php - 在 Genesis Child Theme Wordpress 中创建全高移动菜单