![](/img/trans.png)
[英]How to set zoom android camera vision.barcode-scanner API
[英]Android Google Play Service Vision Barcode scanner Library not found
我使用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)
问题是因为设备无法找到库/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
数组中的所有元素。
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.