5

I use Google play services Visible API for barcode reading. I tried a code from official CodeLabs example which doesn't work on some (not at all) devices. Here is Logcat message:

I/Vision﹕ Supported ABIS: [armeabi-v7a, armeabi]
D/Vision﹕ Library not found: /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/armeabi-v7a/libbarhopper.so
I/Vision﹕ Requesting barcode detector download.
D/AndroidRuntime﹕ Shutting down VM
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: PID: 24921
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
        at android.util.SparseArray.valueAt(SparseArray.java:273)
        at MainActivity$1.onClick(MainActivity.java:50)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

The problem is because device can't find library /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/armeabi-v7a/libbarhopper.so, and after that I got exception because device doesn't detect barcode (list of barcodes is empty).

Here is code:

    BarcodeDetector detector = new BarcodeDetector.Builder(getApplicationContext()).build();
    Bitmap bitmap = ((BitmapDrawable) mBarcodeImageView.getDrawable()).getBitmap();
    Frame frame = new Frame.Builder().setBitmap(bitmap).build();
    SparseArray<Barcode> barcodes = detector.detect(frame);

    Barcode thisCode = barcodes.valueAt(0);
    TextView txtView = (TextView) findViewById(R.id.txtContent);
    txtView.setText(thisCode.rawValue);

The Google Play Service is updated on all devices.

Can anyone help me? How I can fix it?

stolle
  • 51
  • 1
  • 4
  • Looks like your app is crashing due to an `ArrayIndexOutOfBoundsException` on `MainActivity.java:50`, which isn't Google Play services related at all. What is that line? – ianhanniballake Aug 18 '15 at 17:19
  • @ianhanniballake, I just added google example code below. I mentioned that list of barcodes is empty and that is reason of exception. But list of barcodes is empty because `detector` can't recognize barcodes and that is real problem. It happens because device can't find library that I also mentioned above. – stolle Aug 19 '15 at 13:48
  • 1
    See my answer here: http://stackoverflow.com/questions/32099530/google-vision-barcode-library-not-found/32100941#32100941 – Jared Burrows Aug 19 '15 at 16:30
  • 1
    @JaredBurrows thx! I fixed the problem. Actually I need to clear cache, free up some space and reinstall app. – stolle Aug 21 '15 at 15:20

2 Answers2

0

I know it is late but someone may get the error and still find this info useful.

Your app is crashing due to ArrayIndexOutOfBoundsException. Here is why:

SparseArray<Barcode> barcodes = detector.detect(frame); stores all the detected data in barcodes array. When no data is found, it creates a blank array and you are trying to get the value at index 0 from a blank array.

You should check for the size of the array first before trying to retrieve the data. Change your code to following:

int totalCodes = barcodes.size();
if (totalCodes > 0) {
    Barcode thisCode = barcodes.valueAt(0);
    TextView txtView = (TextView) findViewById(R.id.txtContent);
    txtView.setText(thisCode.rawValue);
}

Or you should use a loop to get all the elements in barcodes array.

arvinchhi
  • 477
  • 3
  • 13
0

The detect method returns a SparseArray that contains just keys to the values, you should iterate through the results like this:

for (int i = 0; i < barcodes.size(); i++) {
    Barcode barcode = barcodes.get(barcodes.keyAt(i));
    String value = barcode.displayValue
}
Fernando Gallego
  • 4,064
  • 31
  • 50