【发布时间】:2014-04-24 09:54:09
【问题描述】:
使用下面的代码,我每次都会得到 NotFoundException。请帮忙。
Bitmap bMap= BitmapFactory.decodeResource(MyActivity.this.getResources(),
R.drawable.barcode_dummy);
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 ChecksumException
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);
if(result!=null){
Utility.ShowToastShort(MyActivity.this,result.getText());
}
else{
Utility.ShowToastShort(MyActivity.this, "Bar code couldn't be scanned");
}
} catch (NotFoundException e) {
e.printStackTrace();
}
catch (ChecksumException e) {
e.printStackTrace();
}
catch (FormatException e) {
e.printStackTrace();
}
}
我在 libs/ 文件夹下添加了 zxing-2.1.jar。它在我调用 IntentIntegrator 时起作用。
上图是drawable/文件夹中的barcode_dummy。
编辑:
我也试过下图。
提前致谢。
LOGCAT:
FATAL EXCEPTION:
main
E/AndroidRuntime( 9580): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webintesive.expertly/com.webintesive.expertly.MyActivity}: java.lang.NullPointerException
E/AndroidRuntime( 9580): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2313)
E/AndroidRuntime( 9580): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
E/AndroidRuntime( 9580): at android.app.ActivityThread.access$600(ActivityThread.java:156)
E/AndroidRuntime( 9580): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
E/AndroidRuntime( 9580): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 9580): at android.os.Looper.loop(Looper.java:153)
E/AndroidRuntime( 9580): at android.app.ActivityThread.main(ActivityThread.java:5336)
E/AndroidRuntime( 9580): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 9580): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 9580): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
E/AndroidRuntime( 9580): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
E/AndroidRuntime( 9580): at dalvik.system.NativeStart.main(Native Method)
`E/AndroidRuntime( 9580): Caused by: java.lang.NullPointerException`
`E/AndroidRuntime( 9580): at com.webintesive.expertly.MyActivity.showPhotoFromImageUri(MyActivity.java:167)`
`E/AndroidRuntime( 9580): at com.webintesive.expertly.MyActivity.init(MyActivity.java:120)`
`E/AndroidRuntime( 9580): at com.webintesive.expertly.MyActivity.onCreate(MyActivity.java:84)`
E/AndroidRuntime( 9580): at android.app.Activity.performCreate(Activity.java:5122)
E/AndroidRuntime( 9580): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
E/AndroidRuntime( 9580): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
E/AndroidRuntime( 9580): ... 11 more
E/AppErrorDialog( 503): Failed to get ILowStorageHandle instance
【问题讨论】:
-
你能添加你的logcat吗?
-
E/AndroidRuntime(9580):由:java.lang.NullPointerException E/AndroidRuntime(9580):在 com.webintesive.expertly.MyActivity.showPhotoFromImageUri(MyActivity.java:167) E/AndroidRuntime (9580): 在 com.webintesive.expertly.MyActivity.init(MyActivity.java:120) E/AndroidRuntime(9580): 在 com.webintesive.expertly.MyActivity.onCreate(MyActivity.java:84)
-
请您编辑您的问题,插入完整的 logcat 并在您的代码中突出显示该行
com.webintesive.expertly.MyActivity.showPhotoFromImageUri(MyActivity.java:167) -
那是
NullPointerException,而不是NotFoundException。澄清你的问题是什么。 -
当我调试代码时,控制去 catch (NotFoundException e) { e.printStackTrace(); } 然后应用程序被停止。
标签: android zxing barcode-scanner