我是Android编程新手。我正在使用Zxing Library创建一个QR码并将其保存为png文件,以便我可以共享它,而其他用户可以使用它。 QR代码是正确生成的,但是当我尝试读取它时,它有时会正确读取它,但很多次都会产生NotFoundException。 我编写的用于从文件中读取QR代码的代码是标准的。不知道如何找出问题。
Uri selectedImage = intent.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
String picturePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
cursor.close();
Bitmap bMap = null;
try {
bMap = decodeUri(selectedImage);
int[] intbMapArray = new int[bMap.getWidth()*bMap.getHeight()];
bMap.getPixels(intbMapArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bMap.getHeight(), bMap.getWidth(), intbMapArray);
BinaryBitmap binBitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Result result = null;
result = reader.decode(binBitmap);
if (genQRCodeUpdateFields(intent,result.getText())){
ToQRCode.setImageBitmap(bMap);
}
} catch (FileNotFoundException | NotFoundException | ChecksumException | FormatException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Failed to read code from "+ picturePath,
Toast.LENGTH_SHORT).show();
Reset();
}