我使用 Google Play 服务 Visible API 读取条码。我尝试了来自 official CodeLabs example 的代码这在某些(根本不是)设备上不起作用。这是 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 服务已在所有设备上更新。
谁能帮帮我?我该如何解决?
最佳答案
我知道已经晚了,但有人可能会收到错误消息,但仍然觉得此信息有用。
您的应用因 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
中的所有元素数组。
关于java - 未找到 Android Google Play 服务视觉条码扫描器库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32078629/