com.google.zxing.NotFoundException-在Android中扫描gallery中的QR图像时
尝试从gallery扫描QR图像时出现以下错误。而且文件大小只有4kb com.google.zxing.NotFoundException 下面是示例代码:com.google.zxing.NotFoundException-在Android中扫描gallery中的QR图像时,android,Android,尝试从gallery扫描QR图像时出现以下错误。而且文件大小只有4kb com.google.zxing.NotFoundException 下面是示例代码: @Override protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturn
@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;
}
}
@覆盖
ActivityResult上受保护的void(int-requestCode、int-resultCode、Intent-ImageReturnedContent){
super.onActivityResult(请求代码、结果代码、图像返回内容);
开关(请求代码){
//这种情况是因为您可能在这里处理多个请求代码
案例111:
Uri selectedImage=imageReturnedIntent.getData();
InputStream imageStream=null;
试一试{
//获取图像
imageStream=getContentResolver().openInputStream(SelecteImage);
}catch(filenotfounde异常){
Toast.makeText(getApplicationContext(),“找不到文件”,Toast.LENGTH_SHORT.show();
e、 printStackTrace();
}
//解码位图
位图bMap=BitmapFactory.decodeStream(imageStream);
int[]intArray=new int[bMap.getWidth()*bMap.getHeight()];
//将位图中的像素数据复制到“intArray”数组中
bMap.getPixels(intArray,0,bMap.getWidth(),0,0,bMap.getWidth(),bMap.getHeight());
亮度源=新的RGBLuminanceSource(bMap.getWidth(),bMap.getHeight(),intArray);
BinaryBitmap位图=新的BinaryBitmap(新的混合二进制程序(源));
Reader Reader=new multiformatrader();//否则请使用此选项
试一试{
Hashtable decodeHits=新的Hashtable();
DecodeHintType.TRY_,Boolean.TRUE);
decodeHints.put(DecodeHintType.PURE_条形码,布尔值.TRUE);
结果=reader.decode(位图、解码提示);
字符串条形码=result.getText().toString();
}捕获(未发现异常){
e、 printStackTrace();
}捕获(检查异常){
e、 printStackTrace();
}捕获(格式化异常){
e、 printStackTrace();
}捕获(NullPointerException e){
e、 printStackTrace();
}
打破
}
}
通过使用google play services vision API修复了该问题。下面的链接确实有助于解决此问题