Spanish Barcode con Google Play Vision

benji

Active Member
Licensed User
Longtime User
tengo un problema con esta libreria, trabaje con el demo y funciona muy bien, por lo que la incorpore en una de mis apps, que usaba una libreria mas antigua.
el tema es que no cuando la activo para scanear el codigo, la aplicacion se cae en esta linea...
builder.InitializeNewInstance("com/google/android/gms/vision/barcode/BarcodeDetector.Builder".Replace("/", "."), Array(ctxt))

que me puede estar faltando? tengo las dependencias en el manifiesto.
si ocupo la app demo en el mismo teléfono, esta funciona...
 

josejad

Expert
Licensed User
Longtime User
¿Puedes poner todo el código y sobre todo, poner qué error te muestra el log?
Así das pocas pistas.

saludos,
 

benji

Active Member
Licensed User
Longtime User
error mio, me estaba faltando el Jar adicional....
claro que ahora me salta otro error...

Error occurred on line: 49 (Main)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at anywheresoftware.b4a.samples.camera.main._createdetector(main.java:462)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.samples.camera.main.afterFirstLayout(main.java:104)
at anywheresoftware.b4a.samples.camera.main.access$000(main.java:17)
at anywheresoftware.b4a.samples.camera.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6934)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/collection/ArrayMap;
at com.google.android.gms.internal.vision.zzaq.<clinit>(com.google.android.gms:play-services-vision-common@@19.0.0:73)
at com.google.android.gms.internal.vision.zzbe.init(com.google.android.gms:play-services-vision-common@@19.0.0:6)
at com.google.android.gms.internal.vision.zzbe.maybeInit(com.google.android.gms:play-services-vision-common@@19.0.0:16)
at com.google.android.gms.internal.vision.zzn.<init>(com.google.android.gms:play-services-vision-common@@19.0.0:12)
at com.google.android.gms.internal.vision.zzh.<init>(com.google.android.gms:play-services-vision@@19.0.0:1)
at com.google.android.gms.vision.barcode.BarcodeDetector$Builder.build(com.google.android.gms:play-services-vision@@19.0.0:7)
... 23 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.collection.ArrayMap" on path: DexPathList[[zip file "/data/app/anywheresoftware.b4a.samples.camera-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 29 more
Suppressed: java.lang.ClassNotFoundException: androidx.collection.ArrayMap
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 30 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
** Activity (main) Resume **
 

josejad

Expert
Licensed User
Longtime User
De nuevo, sería bueno ver qué hay en la línea 49 de Main.
También prueba a buscar en el foro:
NoClassDefFoundError
ClassNotFoundException
Didn't find class "androidx.collection.ArrayMap"
DexPathList...

o que subas tu proyecto al foro
 

benji

Active Member
Licensed User
Longtime User
estoy probando esta libreria, con el demo que ahi esta...
https://www.b4x.com/android/forum/threads/barcode-reader-based-on-google-play-services-vision.89705/

he estado buscando los errores pero no encuentro nada muy claro, sigo en eso, dentro del tiempo que me queda jajaja

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    If FirstTime Then
        CreateDetector (Array("CODE_128", "CODE_93", "EAN_13"))
    End If
    cvs.Initialize(pnlDrawing)
End Sub

Private Sub CreateDetector (Codes As List)
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim builder As JavaObject
    builder.InitializeNewInstance("com/google/android/gms/vision/barcode/BarcodeDetector.Builder".Replace("/", "."), Array(ctxt))
    Dim barcodeClass As String = "com/google/android/gms/vision/barcode/Barcode".Replace("/", ".")
    Dim barcodeStatic As JavaObject
    barcodeStatic.InitializeStatic(barcodeClass)
    Dim format As Int
    For Each formatName As String In Codes
        format = Bit.Or(format, barcodeStatic.GetField(formatName))
    Next
    builder.RunMethod("setBarcodeFormats", Array(format))
    detector = builder.RunMethod("build", Null)
    Dim operational As Boolean = detector.RunMethod("isOperational", Null)
    Log("Is detector operational: " & operational)
    SearchForBarcodes = operational
    
End Sub

Sub Camera1_Preview (data() As Byte)
    If SearchForBarcodes Then
        If DateTime.Now > LastPreview + IntervalBetweenPreviewsMs Then
            'Dim n As Long = DateTime.Now
            cvs.ClearRect(cvs.TargetRect)
            Dim frameBuilder As JavaObject
            Dim bb As JavaObject
            bb = bb.InitializeStatic("java.nio.ByteBuffer").RunMethod("wrap", Array(data))
            frameBuilder.InitializeNewInstance("com/google/android/gms/vision/Frame.Builder".Replace("/", "."), Null)
            Dim cs As CameraSize = camEx.GetPreviewSize
            frameBuilder.RunMethod("setImageData", Array(bb, cs.Width, cs.Height,  842094169))
            Dim frame As JavaObject = frameBuilder.RunMethod("build", Null)
            Dim SparseArray As JavaObject = detector.RunMethod("detect", Array(frame))
            LastPreview = DateTime.Now
            For i = 0 To SparseArray.RunMethod("size", Null) - 1
                Dim barcode As JavaObject = SparseArray.RunMethod("valueAt", Array(i))
                Dim raw As String = barcode.GetField("rawValue")
                Log(raw)
                ToastMessageShow("Found: " & raw, True)
                
                Dim points() As Object = barcode.GetField("cornerPoints")
                Dim tl As JavaObject = points(0)
'                Dim tr As JavaObject = points(1)
                Dim br As JavaObject = points(2)
'                Dim bl As JavaObject = points(3)
                Dim r As B4XRect
                r.Initialize(tl.GetField("x"), tl.GetField("y"), br.GetField("x"), br.GetField("y"))
                cvs.DrawRect(r, Colors.Red, False, 5dip)
                cvs.Invalidate
            Next
            
            'Log(DateTime.Now - n)
        End If
    End If
End Sub

Sub Activity_Resume
    InitializeCamera
End Sub

Private Sub InitializeCamera
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        camEx.Initialize(Panel1, frontCamera, Me, "Camera1")
        frontCamera = camEx.Front
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If camEx.IsInitialized Then
        camEx.Release
    End If
End Sub


la linea que esta dando el error es...
detector = builder.RunMethod("build", Null)
 
Top