I've trying to read the MRZ data at the bottom of the passport using the Text Recognition example https://www.b4x.com/android/forum/threads/b4x-textrecognition-based-on-mlkit.161210/ and suggestions from ChatGPT but the text recognition example doesn't appear to read the MRZ text on a passport well enough. I've adapted the code as follows but does not identify the text I need to read. Any suggestions or other ideas I should try to recognize this data?
MRZ Code:
Public Sub Recognize (bmp As B4XBitmap) As ResumableSub
Dim res As TextRecognizerResult
res.Initialize
#if B4I
recognizer.RunMethod("processImage:completion:", Array(ImageToMLImage(bmp), Me.as(NativeObject).RunMethod("createBlock", Null)))
Wait For Process_Result(Success As Boolean, MLKText As Object)
res.Success = Success
If Success Then
#if B4I
res.Text = MLKText.As(NativeObject).GetField("text").AsString
#else if B4A
#end if
End If
#else if B4A
Dim task As JavaObject = recognizer.RunMethod("process", Array(ImageToMLImage(bmp)))
Do While task.RunMethod("isComplete", Null).As(Boolean) = False
Sleep(50)
Loop
If task.RunMethod("isSuccessful", Null) Then
res.Success = True
Dim Text As JavaObject = task.RunMethod("getResult", Null)
res.Text = Text.RunMethod("getText", Null)
Else
Log(task.RunMethod("getException", Null))
End If
#End If
mlKit_TextRecognized(True,res.text)
Return res
End Sub
Private Sub ImageToMLImage(bmp As B4XBitmap) As Object
#if B4I
Dim image As NativeObject
image = image.Initialize("MLKVisionImage").RunMethod("alloc", Null).RunMethod("initWithImage:", Array(bmp))
#else if B4A
Dim image As JavaObject
image = image.InitializeStatic("com.google.mlkit.vision.common.InputImage").RunMethod("fromBitmap", Array(bmp, 0))
#end if
Return image
End Sub
Sub mlKit_TextRecognized (Success As Boolean, Texts As String)
If Success And Texts.Length > 0 Then
Dim fullText As String = Texts '.Get(0)
Dim mrzData As String = ExtractMRZ(fullText)
lblOutput.Text = "MRZ Data Found:" & CRLF & mrzData
'Log("MRZ Data Found:" & CRLF & mrzData)
Else
lblOutput.Text = "MRZ not detected, please try again."
Log("MRZ not detected, please try again.")
End If
End Sub
' Function to extract MRZ data (Assumes ICAO 9303 Format)
Sub ExtractMRZ(inputText As String) As String
Dim mrzPattern As String = "[A-Z0-9<]{44}" ' Regex for MRZ lines
Dim Matcher As Matcher = Regex.Matcher(mrzPattern, inputText)
Dim mrzLines As List
mrzLines.Initialize
Do While Matcher.Find
mrzLines.Add(Matcher.Match)
Loop
If mrzLines.Size >= 2 Then
Return mrzLines.Get(0) & CRLF & mrzLines.Get(1) ' Return the two MRZ lines
Else
Return "No valid MRZ detected."
End If
End Sub