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



This code, crash


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

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.


@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
@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?

 
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
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 . 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.


.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…