zxing条形码扫描器给出了notfoundexception

时间:2014-04-24 09:54:10

标签: android zxing barcode-scanner

使用下面的代码,我每次都会得到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时有效。 enter image description here

上图是drawable /文件夹中的barcode_dummy。

编辑:

我也试过了下面的图像。

enter image description here

提前致谢。

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

1 个答案:

答案 0 :(得分:1)

NotFoundException是正常的。这意味着图像中未找到条形码。你必须处理这个,是的。您的图像旋转90度,默认情况下会以正常方向查找条形码。

如果可以的话,你确实应该使用IntentIntegrator。它可以工作,让您不必再次弄清楚所有这些事情。