未找到Android Google Play Service Vision条形码扫描程序库

sto*_*lle 5 java android google-play-services google-vision android-vision

我使用Google Play服务Visible API进行条形码阅读.我尝试了官方CodeLabs示例中的代码在某些(不是所有)设备上不起作用.这是Logcat消息:

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)
Run Code Online (Sandbox Code Playgroud)

问题是因为设备无法找到库 /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/armeabi-v7a/libbarhopper.so,之后我得到了异常,因为设备没有检测到条形码(条形码列表为空).

这是代码:

    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);
Run Code Online (Sandbox Code Playgroud)

所有设备都会更新Google Play服务.

谁能帮我?我怎么解决它?

小智 0

我知道已经晚了,但有人可能会收到错误,但仍然发现此信息有用。

您的应用程序因以下原因崩溃ArrayIndexOutOfBoundsException。原因如下:

SparseArray<Barcode> barcodes = detector.detect(frame);将所有检测到的内容存储databarcodes数组中。当没有找到数据时,它会创建一个空白数组,并且您尝试0从空白数组中获取索引处的值。

在尝试检索数据之前,您应该首先检查数组的大小。将您的代码更改为以下内容:

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

或者您应该使用循环来获取数组中的所有元素barcodes