vijay
vijay

Reputation: 2034

zxing barcode scanner giving notfoundexception

Using below code, I get NotFoundException everytime. Please help.

 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(); 
 } 

}

I have added zxing-2.1.jar under libs/ folder. It works when I call IntentIntegrator. enter image description here

Above image is the barcode_dummy in drawable/ folder.

EDIT :

I did try with below image as well.

enter image description here

Thanks in advance.

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

Upvotes: 0

Views: 265

Answers (1)

Sean Owen
Sean Owen

Reputation: 66866

NotFoundException is normal. It means no barcode was found in the image. You have to handle this, yes. Your image is rotated 90 degrees and by default barcodes are looked for in normal orientation.

You should indeed use IntentIntegrator if you can. It works, and saves you from having to figure out again all of these things.

Upvotes: 1

Related Questions