Android Vision API barcode image return

android api debugging camera vision

569 просмотра

1 ответ

15 Репутация автора

I've just started getting into slightly more complex things in android development. I'm trying to set up a page in my application to have a button that opens a "simple camera", with a checkbox, then returns the thumbnail of the image and uses that to process the barcode. My application seems to crash after clicking the check box on the page, and I have been following the google codelabs tutorial on getting this API to work. Perhaps it is due to the fact that the image is a thumbnail? If you guys could help out that would be awesome.

public class AddItemActivity extends AppCompatActivity {

    static final int REQUEST_IMAGE_CAPTURE = 1;
    public TextView txtView;
    BarcodeDetector detector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setTitle("Add Item");
        setContentView(R.layout.activity_add_item);
    }

    public void ItemInfo(View view){
        Intent intent = new Intent(this, ItemInfoActivity.class);
        AddItemActivity.this.startActivity(intent);
    }

    public void QRLookup(View view){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            txtView = (TextView) this.findViewById(R.id.ResultQR);
            detector =
                    new BarcodeDetector.Builder(getApplicationContext())
                            .setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE)
                            .build();
            if(!detector.isOperational()){
                txtView.setText("Could not set up the detector!");
                return;}
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            Frame frame = new Frame.Builder().setBitmap(imageBitmap).build();
            SparseArray<Barcode> barcodes = detector.detect(frame);
            Barcode thisCode = barcodes.valueAt(0);
            txtView.setText(thisCode.rawValue);
        }
    }
}

That's my activity. And here's my xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#383838"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="40dp"
    android:paddingRight="40dp"
    android:paddingTop="20dp"
    android:paddingBottom="100dp"
    tools:context=".AddItemActivity">
//Buttons
    <Button
        android:layout_marginTop="30dp"
        android:layout_width="125dp"
        android:layout_gravity="center"
        android:layout_height="30dp"
        android:text="Add Item"
        android:background="#31be31"
        android:onClick="ItemInfo" />
    <Button
        android:layout_marginTop="30dp"
        android:layout_width="125dp"
        android:layout_gravity="center"
        android:layout_height="60dp"
        android:background="#31be31"
        android:text="QR LOOKUP"
        android:onClick="QRLookup"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ResultQR" />

</LinearLayout>

And finally my crash log, or my android monitor as it's called in Android Studio.

    07-18 09:33:12.890 16396-16396/com.arcx.cranium I/art: Late-enabling -Xcheck:jni
07-18 09:33:12.939 16396-16396/com.arcx.cranium W/System: ClassLoader referenced unknown path: /data/app/com.arcx.cranium-2/lib/arm64
07-18 09:33:22.187 16396-16396/com.arcx.cranium W/System: ClassLoader referenced unknown path: /data/app/com.arcx.cranium-2/lib/arm64
07-18 09:33:22.389 16396-16396/com.arcx.cranium W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-18 09:33:22.589 16396-16856/com.arcx.cranium D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-18 09:33:22.684 16396-16856/com.arcx.cranium I/Adreno: QUALCOMM build                   : fcf4852, I1af360237c
                                                          Build Date                       : 09/23/15
                                                          OpenGL ES Shader Compiler Version: XE031.05.13.02
                                                          Local Branch                     : mybranch14465806
                                                          Remote Branch                    : quic/LA.BF64.1.2.9_v2
                                                          Remote Branch                    : NONE
                                                          Reconstruct Branch               : NOTHING
07-18 09:33:22.738 16396-16856/com.arcx.cranium I/OpenGLRenderer: Initialized EGL, version 1.4
07-18 09:34:45.808 16396-16396/com.arcx.cranium D/ChimeraCfgMgr: Reading stored module config
07-18 09:34:45.884 16396-16396/com.arcx.cranium W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/00000004/n/arm64-v8a
07-18 09:34:45.898 16396-16396/com.arcx.cranium D/ChimeraFileApk: Primary ABI of requesting process is arm64-v8a
07-18 09:34:45.902 16396-16396/com.arcx.cranium D/ChimeraFileApk: Classloading successful. Optimized code found.
07-18 09:34:45.963 16396-16396/com.arcx.cranium D/AndroidRuntime: Shutting down VM
07-18 09:34:45.963 16396-16396/com.arcx.cranium E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.arcx.cranium, PID: 16396
                                                                  java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.arcx.cranium/com.arcx.cranium.AddItemActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                      at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
                                                                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                                      at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                   Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                      at android.util.SparseArray.valueAt(SparseArray.java:291)
                                                                      at com.arcx.cranium.AddItemActivity.onActivityResult(AddItemActivity.java:55)
                                                                      at android.app.Activity.dispatchActivityResult(Activity.java:6428)
                                                                      at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
                                                                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                                      at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-18 09:34:46.967 16396-17395/com.arcx.cranium I/Vision: Connection error: null
07-18 09:34:47.969 16396-17395/com.arcx.cranium I/Vision: Connection error: null
07-18 09:34:50.965 16396-16396/com.arcx.cranium I/Process: Sending signal. PID: 16396 SIG: 9
07-18 09:34:51.116 17414-17414/com.arcx.cranium W/System: ClassLoader referenced unknown path: /data/app/com.arcx.cranium-2/lib/arm64
07-18 09:34:51.343 17414-17414/com.arcx.cranium W/System: ClassLoader referenced unknown path: /data/app/com.arcx.cranium-2/lib/arm64
07-18 09:34:51.554 17414-17414/com.arcx.cranium W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-18 09:34:51.914 17414-17444/com.arcx.cranium D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-18 09:34:51.985 17414-17444/com.arcx.cranium I/Adreno: QUALCOMM build                   : fcf4852, I1af360237c
                                                          Build Date                       : 09/23/15
                                                          OpenGL ES Shader Compiler Version: XE031.05.13.02
                                                          Local Branch                     : mybranch14465806
                                                          Remote Branch                    : quic/LA.BF64.1.2.9_v2
                                                          Remote Branch                    : NONE
                                                          Reconstruct Branch               : NOTHING
07-18 09:34:51.995 17414-17444/com.arcx.cranium I/OpenGLRenderer: Initialized EGL, version 1.4

Thank you guys a lot, I'm not exactly sure how to interpret some of the crash logs and i'm not 100% sure as to how the barcode api works. I'm trying to make this application in terms of the simplest way possible and then move up from there. :)

Автор: Arin Yaldizciyan Источник Размещён: 18.07.2016 01:44

Ответы (1)


1 плюс

517 Репутация автора

As your java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 exception says, you are trying to access the first item of an empty array.

In here:

    SparseArray<Barcode> barcodes = detector.detect(frame);
    Barcode thisCode = barcodes.valueAt(0);

After you fetch barcodes, make sure your SparseArray is not empty before accessing it's elements.

    SparseArray<Barcode> barcodes = detector.detect(frame);
    if (barcodes.size() > 0) {
            Barcode thisCode = barcodes.valueAt(0);
            txtView.setText(thisCode.rawValue);
    }
Автор: Lera Размещён: 03.03.2017 12:10
Вопросы из категории :
32x32