安卓Google Play服务视觉条码扫描库未找到。

5

我使用Google Play服务的可见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)

问题是设备找不到库/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);

所有设备上的Google Play服务已更新。

有人能帮我吗?我该如何解决?


你的应用程序好像因 MainActivity.java:50 上的 ArrayIndexOutOfBoundsException 而崩溃了,这与 Google Play 服务无关。那是什么代码? - ianhanniballake
@ianhanniballake,我刚刚在下面添加了Google示例代码。我提到条形码列表为空,这就是异常的原因。但是条形码列表为空是因为“detector”无法识别条形码,这才是真正的问题。这是因为设备找不到我上面提到的库。 - stolle
1
请查看我的答案:https://dev59.com/sFwY5IYBdhLWcg3w4LI3#32100941 - Jared Burrows
1
@JaredBurrows 谢谢!我解决了问题。实际上,我需要清除缓存,释放一些空间并重新安装应用程序。 - stolle
2个回答

0

我知道现在已经很晚了,但是有人可能会遇到这个错误,仍然觉得这些信息很有用。

你的应用程序由于ArrayIndexOutOfBoundsException而崩溃。原因如下:

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

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

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

或者你可以使用循环来获取 barcodes 数组中的所有元素。


0

detect 方法返回一个只包含键的 SparseArray,你应该像这样遍历结果:

for (int i = 0; i < barcodes.size(); i++) {
    Barcode barcode = barcodes.get(barcodes.keyAt(i));
    String value = barcode.displayValue
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接