[英]Android Google Play Service Vision Barcode scanner Library not found
I use Google play services Visible API for barcode reading. 我使用Google Play服务Visible API进行条形码阅读。 I tried a code from official CodeLabs example which doesn't work on some (not at all) devices.
我尝试了官方CodeLabs示例中的代码,该代码在某些(不是所有)设备上不起作用。 Here is Logcat message:
这是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)
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). 问题是因为设备无法找到库
/data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/armeabi-v7a/libbarhopper.so
,之后我得到了例外,因为设备没有检测到条形码(条形码列表为空)。
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. 所有设备都会更新Google Play服务。
Can anyone help me? 谁能帮我? How I can fix it?
我该怎么办呢?
I know it is late but someone may get the error and still find this info useful. 我知道它已经晚了但有人可能会收到错误,但仍然觉得这个信息很有用。
Your app is crashing due to ArrayIndexOutOfBoundsException
. 由于
ArrayIndexOutOfBoundsException
您的应用程序崩溃了。 Here is why: 原因如下:
SparseArray<Barcode> barcodes = detector.detect(frame);
stores all the detected data
in barcodes
array. 将所有检测到的
data
在barcodes
数组中。 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. 如果未找到任何数据,则会创建一个空白数组,并且您尝试从空白数组中获取索引
0
处的值。
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. 或者您应该使用循环来获取
barcodes
数组中的所有元素。
The detect
method returns a SparseArray
that contains just keys to the values, you should iterate through the results like this: detect
方法返回一个SparseArray
, SparseArray
包含值的键,你应该遍历结果,如下所示:
for (int i = 0; i < barcodes.size(); i++) {
Barcode barcode = barcodes.get(barcodes.keyAt(i));
String value = barcode.displayValue
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.