Android Question [B4X] [B4XPages] Barcode Reader issue

Sergio Haurat

Active Member
Licensed User
Longtime User
Original post: [B4X] [B4XPages] Barcode Reader

I'm using the example, I just changed the code type to PDF417. This information is printed on identity documents.

The only change in the entire code:
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 example reads it and works
1690762112464.png


This not

1690762159297.png



Logger connected to: motorola motorola edge 30 pro
--------- beginning of main
*** 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: 141 (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
 
Last edited:
Solution
friend and neighbor

A colleague from Argentina told me about the PDF417 DNI code.
I found this to inform me.


I have tried with Zxing and it reads PDF417 codes in images well.

example:
Picture 1:

00407821943@HAURAT@SERGIO RAUL@M@23644566@B@07/12/1973@06/11/2015@207

Image 2 (which does not read with play-services-vision)

00699849124@HAURAT@AGUSTÍN@M@49120247@A@09/10/2008@27/01/2023@206
I finally...

TILogistic

Expert
Licensed User
Longtime User
no the image is not bad see convert image to barcode.

1690770960714.png

It is the conversion of the quality of the framer that captures the image.

here.
B4X:
frameBuilder.RunMethod("setImageData", Array(bb, cs.Width, cs.Height, 842094169))
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
no the image is not bad see convert image to barcode.

View attachment 144253
It is the conversion of the quality of the framer that captures the image.

here.
B4X:
frameBuilder.RunMethod("setImageData", Array(bb, cs.Width, cs.Height, 842094169))
And what would be the solution for the conversion of the quality of the frame that captures the image? I have exactly the same line. I have added a Try and the message is as follows

(NullPointerException) java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference

Code:
Private Sub Camera1_Preview (data() As Byte)
    If DateTime.Now > LastPreview + IntervalBetweenPreviewsMs Then
        'Dim n As Long = DateTime.Now
        Try
            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
            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
Maybe good if you upload to a system to process but not for mobile camera as barcode scanner to focus.
Hi @aeric, thanks for replying. The scan must be from the camera.
The application will validate that the number registered in the database matches the document of the person trying to modify their own data.

The first column that says "Document" in Spanish, scanning my document many times with different positions did not generate problems (23644566). A final column in B4XTable will allow, by clicking on a button, the modification of that record. To validate it, I am going to request that it be scanned so that not anyone modifies the data.

1690772048437.png
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
null object reference usually means you are blindly
assuming an assignment of some sort succeeded
or you are using an uninitialized object (essentially
the same thing as a failed assignment).

for example, in the camer1_preview sub,
the javaobject frame is returned by the framebuilder()
method. if that method fails, frame is null (or
unintialized). when frame is blindly passed as a
parameter in the detect() method, it could be a
null object.

the same is true for the javaobject sparsearray.
if the detect() method fails, sparsearray is null.
when sparsearray is used to run the size() method,
it throws a null object exception.

i'm not saying any of these javaobjects are null in this
particular case, but use of assigned javaobjects without
testing whether they are initialized or not can result in
a null object exception. just add some tests before
assuming a javaobject is valide.

if your null object is not in the camera1_preview sub,
it is somewhere else.
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
null object reference usually means you are blindly
assuming an assignment of some sort succeeded
or you are using an uninitialized object (essentially
the same thing as a failed assignment).

for example, in the camer1_preview sub,
the javaobject frame is returned by the framebuilder()
method. if that method fails, frame is null (or
unintialized). when frame is blindly passed as a
parameter in the detect() method, it could be a
null object.

the same is true for the javaobject sparsearray.
if the detect() method fails, sparsearray is null.
when sparsearray is used to run the size() method,
it throws a null object exception.

i'm not saying any of these javaobjects are null in this
particular case, but use of assigned javaobjects without
testing whether they are initialized or not can result in
a null object exception. just add some tests before
assuming a javaobject is valide.

if your null object is not in the camera1_preview sub,
it is somewhere else.

@drgottjr, thanks for taking the time to reply to this post.

Everything you just explained is so far beyond my knowledge of Java.

The only thing I see with a Try preventing the app from stopping is this line repetitively in the log.

(NullPointerException) java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
I think the printing quality for second image is bad.
I have scanned the original document and changed cell phone (Motorola 30pro to Samsung S23), the error is the same. I think the problem is not the focus, they are two different devices, with different camera hardware
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I have scanned the original document and changed cell phone (Motorola 30pro to Samsung S23), the error is the same. I think the problem is not the focus, they are two different devices, with different camera hardware
My point is not the scanning devices. From my observation, I think the print out is the issue which cause the detection difficult. Can you hava a different printer that can print better quality? Not sure what kind printer you are using or you are printing from a low resolution image. Or maybe the printing media or paper surface.
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
My point is not the scanning devices. From my observation, I think the print out is the issue which cause the detection difficult. Can you hava a different printer that can print better quality? Not sure what kind printer you are using or you are printing from a low resolution image. Or maybe the printing media or paper surface.
It is the clipping of a photograph from a card, the national identity document of Argentina. @TILogistic says in #5, the problem doesn't seem to be image quality.

This document is from my son, at this time 1.42AM GMT-3, he is sleeping, but I have performed the scanning operation with different light and rotation. He always gives the same error.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Maybe just me. The code such as QRCode, Aztech or PDF format are constructed from small squares or rectangular which the first image quite good while the second image look blurry and has "teeth" at the sides. However I have seems some examples by Erel in this forum which uses circles/dots for QRCode. From my understanding there should have some kind of requirement specifications or limitation for the code to be successfully detected
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
Maybe just me. The code such as QRCode, Aztech or PDF format are constructed from small squares or rectangular which the first image quite good while the second image look blurry and has "teeth" at the sides. However I have seems some examples by Erel in this forum which uses circles/dots for QRCode. From my understanding there should have some kind of requirement specifications or limitation for the code to be successfully detected
Unfortunately in Argentina it was decided to use PDF in the identity documents of the population. As I explained above, my intention is to create an authentication methodology so that the user of the application can only modify his record in the database. The image is not the best and is a crop of the entire document. I insist that if you send it to scan by passing the image link, it says that the reading is correct.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User

The replies said use bigger image or maybe switch to Zxing
@aeric, it's not the South African driver's license. It is the identification of individuals from the Argentine Republic. On the other hand, the other photograph reads it without problems. It is very strange, what happens. I want to thank you infinitely for your time in trying to find the problem.

I'm going to look for the Zxing option
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
friend and neighbor

A colleague from Argentina told me about the PDF417 DNI code.
I found this to inform me.


I have tried with Zxing and it reads PDF417 codes in images well.

example:
Picture 1:

00407821943@HAURAT@SERGIO RAUL@M@23644566@B@07/12/1973@06/11/2015@207

Image 2 (which does not read with play-services-vision)

00699849124@HAURAT@AGUSTÍN@M@49120247@A@09/10/2008@27/01/2023@206
 
Upvote 0
Top