Google 愿景 API:处理本机崩溃

Google Vision API: Handle native crash

有人知道如何处理来自本机方法调用的异常吗?

我是 运行 Barcode Reader example from Google Vision API,它在阅读一些 2d - pdf417 代码时效果很好,但在某些情况下,它会因尝试使用 NewStringUTF 之类的本机异常而崩溃这个:

    art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0x90
    art/runtime/java_vm_ext.cc:410]     in call to NewStringUTF
    art/runtime/java_vm_ext.cc:410]     from com.google.android.gms.vision.barcode.internal.NativeBarcode[] com.google.android.gms.vision.barcode.internal.NativeBarcodeDetector.recognizeNative(int, int, byte[], com.google.android.gms.vision.barcode.internal.NativeOptions)
    art/runtime/java_vm_ext.cc:410] "Thread-4533" prio=5 tid=19 Runnable
    art/runtime/java_vm_ext.cc:410]   | group="main" sCount=0 dsCount=0 obj=0x12c8e0a0 self=0x7f849f4200
    art/runtime/java_vm_ext.cc:410]   | sysTid=9051 nice=0 cgrp=default sched=0/0 handle=0x7f7b7bf440
    art/runtime/java_vm_ext.cc:410]   | state=R schedstat=( 1663092757 5060164 94 ) utm=165 stm=1 core=2 HZ=100
    art/runtime/java_vm_ext.cc:410]   | stack=0x7f7b6bd000-0x7f7b6bf000 stackSize=1037KB
    art/runtime/java_vm_ext.cc:410]   | held mutexes= "mutator lock"(shared held)
    art/runtime/java_vm_ext.cc:410]   native: #00 pc 00000000004897a8  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+200)
    art/runtime/java_vm_ext.cc:410]   native: #01 pc 0000000000458644  /system/lib64/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEP12BacktraceMap+224)
    art/runtime/java_vm_ext.cc:410]   native: #02 pc 000000000030c9e4  /system/lib64/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+1004)
    art/runtime/java_vm_ext.cc:410]   native: #03 pc 000000000030d29c  /system/lib64/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+116)
    art/runtime/java_vm_ext.cc:410]   native: #04 pc 0000000000141f9c  /system/lib64/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+144)
    art/runtime/java_vm_ext.cc:410]   native: #05 pc 000000000014a1d0  /system/lib64/libart.so (_ZN3art11ScopedCheck5CheckERNS_18ScopedObjectAccessEbPKcPNS_12JniValueTypeE.constprop.116+11084)
    art/runtime/java_vm_ext.cc:410]   native: #06 pc 0000000000153418  /system/lib64/libart.so (_ZN3art8CheckJNI12NewStringUTFEP7_JNIEnvPKc+468)
    art/runtime/java_vm_ext.cc:410]   native: #07 pc 0000000000005774  /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/arm64-v8a/libbarhopper.so (_ZN9barhopper9JniObject14SetStringFieldEPKcRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE+168)
    art/runtime/java_vm_ext.cc:410]   native: #08 pc 00000000000036f8  /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/arm64-v8a/libbarhopper.so (???)
    art/runtime/java_vm_ext.cc:410]   native: #09 pc 0000000000004c8c  /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/arm64-v8a/libbarhopper.so (Java_com_google_android_gms_vision_barcode_internal_NativeBarcodeDetector_recognizeNative+84)

我想处理崩溃以捕获它并向用户提供一些反馈。

不幸的是,没有办法处理来自非您自己的本机代码的异常。

我对 google-vision 也有类似的问题。我向 google vision github https://github.com/googlesamples/android-vision

报告了这个问题

遗憾的是,这个库有一些错误。就我而言,这是条形码文本解析器中的错误。 如果您也报告您的问题,那就太好了。

有趣的是,有一种方法可以从您自己的本机代码中捕获异常:Catching exceptions thrown from native code running on Android

Google 的视觉库存在问题,导致它在扫描某些二维码时崩溃。 (参见 https://github.com/googlesamples/android-vision/issues/136)。如果你只需要扫描普通的条码,你可以这样做来避免视觉库检测二维码:

    val barcodeFormatsToScan = Barcode.CODE_128 or Barcode.CODE_39 or Barcode.CODE_93 or Barcode.CODABAR or Barcode.DATA_MATRIX or Barcode.EAN_13 or
            Barcode.EAN_8 or Barcode.ITF or Barcode.UPC_A or Barcode.UPC_E or Barcode.PDF417 or Barcode.AZTEC
    val barcodeDetector = BarcodeDetector.Builder(context).setBarcodeFormats(barcodeFormatsToScan).build()
    val barcodeFactory = BarcodeTrackerFactory(fragment_barcode_scanner_graphic_overlay as GraphicOverlay<BarcodeGraphic>, true, callback)
    barcodeDetector.setProcessor(MultiProcessor.Builder<Barcode>(barcodeFactory).build())