5

我使用 Google play services 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)

问题是因为设备找不到库/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 服务会在所有设备上更新。

谁能帮我?我该如何解决?

4

2 回答 2

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

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

于 2015-12-26T14:59:35.990 回答
0

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

for (int i = 0; i < barcodes.size(); i++) {
    Barcode barcode = barcodes.get(barcodes.keyAt(i));
    String value = barcode.displayValue
}
于 2017-01-04T14:52:43.327 回答