Android zxing NotFoundException

隐身守侯 提交于 2020-01-11 10:32:09

问题


I'm using zxing to decode QRcode images, but it always returns a NotFoundException. The online decoder at http://zxing.org/w/decode.jspx scans these images perfectly fine, so it should be able to do so in my app. I'm using this code :

String path = Environment.getExternalStorageDirectory().getPath()+"/QRPictures/QRTest.bmp";
Bitmap bmp = BitmapFactory.decodeFile(path);
int[] pixels = new int[bmp.getWidth()*bmp.getHeight()];
bmp.getPixels(pixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
LuminanceSource source = new RGBLuminanceSource(bmp.getWidth(), bmp.getHeight(), pixels); 
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
try {
    Result result = reader.decode(bitmap);
    String contents = result.getText(); 
    Log.d(TAG, content);
} catch (NotFoundException e) {
    Log.d(TAG, "NFE");
} catch (ChecksumException e) {
    Log.d(TAG, "CSE");
} catch (FormatException e) {
    Log.d(TAG, "FE");
} 

Could you help with this please ?


回答1:


According to an answer to a related question, using the TRY_HARDER decode hint might help, since it "optmizes for accuracy, not speed":

Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>();
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = reader.decode(bitmap, decodeHints);

If the same image is being correctly interpreted in the online service but failing in your case, it's probable that they have TRY_HARDER on while you have it off.




回答2:


I had this problem too. And I solved it. Try add this hint to your code:

hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);



回答3:


I solved this by: hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);



来源:https://stackoverflow.com/questions/14994674/android-zxing-notfoundexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!