Android Question Barcode reader

Sergio Haurat

Active Member
Licensed User
Longtime User
I have tried to use this library that works on B4X by @Erel.


With the example included in the original post, without touching anything in the code, just assigning the code that I want to read.

PDF 417:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    toast.Initialize(Root)
    StopCamera
    B4XPages.SetTitle(Me, "Barcode Example")
    #if B4A
    CreateDetector (Array("PDF417"))

    #Else if B4i
    scanner.Initialize2("scanner", pnlPreview, Array(scanner.TYPE_93, scanner.TYPE_128, scanner.TYPE_QR))
    Wait For Scanner_Ready (Success As Boolean)
    If Success = False Then
        btnStartStop.Enabled = False
        toast.Show("Failed to initialize the scanner.")
    End If
    #end if
End Sub

This PDF417 code, the application can read it without problems

1691333274350.png


This code, crash ☠️
1691333555276.png


With the original example, just switching to the code format I want to read, this is the error that stops execution and crashes. I tried it on the Motorola and on a Samsung S23 and the problem is the same
Logger connected to: motorola motorola edge 30 pro
--------- beginning of main
** Activity (main) Pause event (activity is not paused). **
** Activity (main) Resume **
findCamera
null
2
facing: 0, 0
findCamera
0
2
facing: 0, 0
Preview Orientation: 90
b4xmainpage_camera1_preview (java line: 110)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4a.example.b4xmainpage._camera1_preview(b4xmainpage.java:110)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1114)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:1069)
at b4a.example.cameraexclass._camera_preview(cameraexclass.java:146)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.CameraW$2$1$1.onPreviewFrame(CameraW.java:147)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1193)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loopOnce(Looper.java:238)
at android.os.Looper.loop(Looper.java:357)
at android.app.ActivityThread.main(ActivityThread.java:8118)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:957)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.google.android.gms.vision.barcode.BarcodeDetector.detect(com.google.android.gms:play-services-vision@@20.1.3:27)
... 20 more
Copying updated assets files (5)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
findCamera
null
2
facing: 0, 0
findCamera
0
2
facing: 0, 0
Preview Orientation: 90
Error occurred on line: 140 (B4XMainPage)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4a.example.b4xmainpage._camera1_preview(b4xmainpage.java:398)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1114)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:1069)
at b4a.example.cameraexclass._camera_preview(cameraexclass.java:354)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.CameraW$2$1$1.onPreviewFrame(CameraW.java:147)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1193)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loopOnce(Looper.java:238)
at android.os.Looper.loop(Looper.java:357)
at android.app.ActivityThread.main(ActivityThread.java:8118)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:957)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.google.android.gms.vision.barcode.BarcodeDetector.detect(com.google.android.gms:play-services-vision@@20.1.3:27)
... 28 more

In another post, @TILogistic and @aeric have suggested me to use ZXing, I did it and it works perfect, but it is not compatible with B4I. I have contacted @Johan Schoeman, he confirms that it is not compatible.
1691334437498.png


@Erel, can you please see why the app crashes when trying to read the second example of the PDF 417 code?
 
Solution
You can use any scanner library you like in B4A and use iBarcode in B4i.

There is no cross platform scanner library.

Erel

B4X founder
Staff member
Licensed User
Longtime User
In another post, @TILogistic and @aeric have suggested me to use ZXing, I did it and it works perfect, but it is not compatible with B4I
There is no cross platform barcode library so it doesn't matter which implementation you use on B4A. In both cases you will need to use some #Ifs to get it working on both platforms.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The error itself happens inside Google SDK. Maybe the barcode is invalid. Anyway you can catch the error:
B4X:
Private Sub Camera1_Preview (data() As Byte)
    If DateTime.Now > LastPreview + IntervalBetweenPreviewsMs Then
        'Dim n As Long = DateTime.Now
        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)
        Try
            Dim SparseArray As JavaObject = detector.RunMethod("detect", Array(frame))
            LastPreview = DateTime.Now
            Dim Matches As Int = SparseArray.RunMethod("size", Null)
            If Matches > 0 Then
                Dim barcode As JavaObject = SparseArray.RunMethod("valueAt", Array(0))
                Dim raw As String = barcode.GetField("rawValue")
                FoundBarcode(raw)
            End If
        Catch
            Log(LastException)
        End Try
    End If
End Sub
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
The error itself happens inside Google SDK. Maybe the barcode is invalid. Anyway you can catch the error:
B4X:
Private Sub Camera1_Preview (data() As Byte)
    If DateTime.Now > LastPreview + IntervalBetweenPreviewsMs Then
        'Dim n As Long = DateTime.Now
        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)
        Try
            Dim SparseArray As JavaObject = detector.RunMethod("detect", Array(frame))
            LastPreview = DateTime.Now
            Dim Matches As Int = SparseArray.RunMethod("size", Null)
            If Matches > 0 Then
                Dim barcode As JavaObject = SparseArray.RunMethod("valueAt", Array(0))
                Dim raw As String = barcode.GetField("rawValue")
                FoundBarcode(raw)
            End If
        Catch
            Log(LastException)
        End Try
    End If
End Sub
@Erel, the code is valid. I have tried it with ZXing in the application with the library and even from the site https://zxing.org/w/decode.jspx.
I want to use the scanning system to validate that the user who wants to modify some data is really that user.

It's not clear to me if you want me to test these changes and share the error in the log, or if you're telling me... if there are problems, indicate that the system can't read the code. If it's the latter, then I'll have to think about changing the method because I don't know how many of these cases there might be.

I'm reading documentation and I read this. Could this be the problem?

1691389342658.png
 
Last edited:
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
There is no cross platform barcode library so it doesn't matter which implementation you use on B4A. In both cases you will need to use some #Ifs to get it working on both platforms.
Yes, I know that in some segments I must use #if's.

When I tried to use ZXing in B4I, it asks me for a parameter called ba, so I contacted Johan Schoeman and he told me that his library was not compatible with iPhone.

Do you know what value I should pass in the ba parameter to test it?

B4X:
 #If B4A
    Dim Result As Boolean = True
    If Not(Main.rp.Check(Main.rp.PERMISSION_CAMERA)) Then
        Main.rp.CheckAndRequest(Main.rp.PERMISSION_CAMERA)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    End If
    If Result Then
        zx.BeginScan("scanCode")
    End If
#Else If B4I
    zx.BeginScan(ba, "scanCode")
#End If
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Yes, I know that in some segments I must use #if's.

When I tried to use ZXing in B4I, it asks me for a parameter called ba, so I contacted Johan Schoeman and he told me that his library was not compatible with iPhone.

Do you know what value I should pass in the ba parameter to test it?

B4X:
 #If B4A
    Dim Result As Boolean = True
    If Not(Main.rp.Check(Main.rp.PERMISSION_CAMERA)) Then
        Main.rp.CheckAndRequest(Main.rp.PERMISSION_CAMERA)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    End If
    If Result Then
        zx.BeginScan("scanCode")
    End If
#Else If B4I
    zx.BeginScan(ba, "scanCode")
#End If
The library will not work on B4I regardless of "ba". The library will only work on Android devices. You will need an iOS scanner library to work in B4I.
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
The library will not work on B4I regardless of "ba". The library will only work on Android devices. You will need an iOS scanner library to work in B4I.

Hi @Johan Schoeman, of course we have already discussed this topic. That's why I decided to try again with the B4X library developed by @Erel, so far this is Erel's answer

"The bug itself occurs within the Google SDK."

I replaced the code with Try in the example and even though the app doesn't crash, it doesn't scan either :rolleyes:. I have tried with 2 identity documents from Argentina. My fear is that of these troubled readings are a significant number. It would not make sense for the users of the application who most often receive the message "The system cannot read this code, you must contact us by WhatsApp", for example.

At this stage of the application, I need to scan the code as a security method. A handball player from a team who wants to update his data. Well, use your identity document to enable the edition.

Lastly, I tried to somehow understand the error and saw that on the Google site they indicate that the "Camera" class is deprecated. If I can't find a library that can be used on both operating systems, I have to drop this safety method and figure out how to replace it.

1691428664700.png

.
 
Upvote 0
Top