B4A Library TextRecognitionAndroid - OCR

A wrap for this Github project.

1. Jar and XML attached - copy them to your additional libs folder
2. B4A sample project attached
3. resource.zip - extract the folder and copy the folder and its contents to be on the same folder level as that of the /Files folder of the B4A project

Click on the active preview to bring back the Blocks, Lines, and Words (events will be raised is the B4A project)
Watch the B4A Log when you click the active preview with text inside the active preview.

Sure you will figure it out.

Note that it does not show the graphic overlay!
Take note of the B4A manifest file when starting a new project.

".....Google Play Services must be active on a device......"

Edit: Update in post #14 of this thread

B4X:
#Region  Project Attributes
    #ApplicationLabel: AndroidTextRecognition
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#AdditionalRes: ..\resource

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Dim rp As RuntimePermissions

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private atr1 As AnroidTextRecognition
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("Layout")

End Sub

Sub Activity_Resume



End Sub

Sub Activity_Pause (UserClosed As Boolean)



End Sub

Sub Button1_Click

    Dim Result As Boolean = True                                                                 
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then                                                 
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)                                                 
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)             
    End If                                                                                       
    If Result Then                                                                               
        atr1.startScanning
    End If
End Sub

Sub atr1_text_scanned_blocks(Blocks As Object)

    Dim scannedBlocks As List = Blocks
    For i = 0 To scannedBlocks.Size - 1
        Log("scannedBlocks(" & i & ") = " & scannedBlocks.Get(i))
    Next
    Log(" ")

End Sub

Sub atr1_text_scanned_lines(Lines As Object)
    Dim scannedLines As List = Lines
    For i = 0 To scannedLines.Size - 1
        Log("scannedLines(" & i & ") = " & scannedLines.Get(i))
    Next
    Log(" ")

End Sub

Sub atr1_text_scanned_words(Words As Object)
    Dim scannedWords As List = Words
    For i = 0 To scannedWords.Size - 1
        Log("scannedWords(" & i & ") = " & scannedWords.Get(i))
    Next
    Log(" ")

End Sub


1.png
 

Attachments

  • b4aAndroidTextRecognition.zip
    10.3 KB · Views: 896
  • resource.zip
    423 bytes · Views: 879
  • AndroidTextRecognition.jar
    6 KB · Views: 822
  • AndroidTextRecognition.xml
    9.2 KB · Views: 828
Last edited:

simone77gl

Member
Licensed User
problem compilation:

Generazione file R. Error
c:\android\tools\..\extras\b4a_remote\androidx\savedstate\savedstate\1.1.0\unpacked-savedstate-1.1.0\res\values\values.xml:3: error: Found tag id where item is expected
c:\android\tools\..\extras\b4a_remote\androidx\lifecycle\lifecycle-viewmodel\2.3.1\unpacked-lifecycle-viewmodel-2.3.1\res\values\values.xml:3: error: Found tag id where item is expected
c:\android\tools\..\extras\b4a_remote\androidx\lifecycle\lifecycle-runtime\2.3.1\unpacked-lifecycle-runtime-2.3.1\res\values\values.xml:3: error: Found tag id where item is expected
 

ilan

Expert
Licensed User
Longtime User
Thank you very much @Johan Schoeman.
this is the first OCR lib that really worked for me (after so many tries)

i have tried the example from post#14

One question please. can we scan text from bitmap instead of the live camera?

thank you :)
 

Johan Schoeman

Expert
Licensed User
Longtime User
Thank you very much @Johan Schoeman.
this is the first OCR lib that really worked for me (after so many tries)

i have tried the example from post#14

One question please. can we scan text from bitmap instead of the live camera?

thank you :)
 

ilan

Expert
Licensed User
Longtime User

adriano.freitas

Active Member
Hi!

Very good job! Congratulations!

I just have one question: I would like to use it as a function for a specific part of my application. A panel that is displayed or hidden as needed. For the sake of use of device resources, I think that the ideal would be to turn on the camera when displaying the panel to do the OCR and after reading the text, turn off the camera when hiding the panel, preventing the system from having resources used unnecessarily.

How can I do this?
 

adriano.freitas

Active Member
Another thing I noticed when inserting the library into my application is that it takes two "startscans" for the preview to be functional. Why?

Thanks
 

Johan Schoeman

Expert
Licensed User
Longtime User
Another thing I noticed when inserting the library into my application is that it takes two "startscans" for the preview to be functional. Why?

Thanks
Does the original sample code behave this way or just when you use it in your project?
 

adriano.freitas

Active Member
Hi!

In this case, it happened in my application, because in it, unlike the example, I shouldn't leave the camera on all the time. Thus, when reading a text, I need the camera to be turned off (I use stopscan, followed by visible false) and the user can turn it on again on a button (I use start scan and visible=true). So, after disabling the camera once, I need to double-tap the enable button so that it actually becomes active again.

I must emphasize that I capture the text via a button as well and not by tapping the image (according to an update posted at the request of someone else).
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi!

In this case, it happened in my application, because in it, unlike the example, I shouldn't leave the camera on all the time. Thus, when reading a text, I need the camera to be turned off (I use stopscan, followed by visible false) and the user can turn it on again on a button (I use start scan and visible=true). So, after disabling the camera once, I need to double-tap the enable button so that it actually becomes active again.

I must emphasize that I capture the text via a button as well and not by tapping the image (according to an update posted at the request of someone else).
Probably best that you upload a sample project that illustrates what it is that you are seeing.
 

adriano.freitas

Active Member
Probably best that you upload a sample project that illustrates what it is that you are seeing.

Hi!

The whole project I think will be more confusing because it's a big application, with a lot of code and it wouldn't be relevant, so I'm going to copy here the parts of the code that refer to OCR, ok? My natural language is Portuguese, so some variable names and the like may be in this language.

Basically it's the code below. Just what I talked about before, it already happens:


B4X:
Private Sub OCRCamera_Click
	'
	'  Sub to start OCR by activating the camera
	'
       OCRTextoEscaneado.Text=""	
	Try
		OCRPreview.startScanning		
	Catch
		Log(LastException)
	End Try
        '
	OCRPreview.Visible=True
End Sub


Private Sub OCRCameraDesligar_Click
	'
	' Finish OCR. Turn off camera.
	'
	Try
	        OCRPreview.stopScanning		
	Catch
		Log(LastException)
	End Try
	OCRPreview.Visible=False
	OCRPainel.Visible=False
	PainelTela.Visible=False
End Sub


Private Sub OCRLer_Click
	'
	' To take a text from camera
	'
	Private Blocos   As List
	Private Texto    As String
	Private Altura   As Int
	Private Contador As Int
	'
	' Start Scan
	'
	Try
		OCRPreview.startScanning
	Catch
		Log(LastException)
	End Try
	Sleep(0)
	'
	' Blocos
	'
	Blocos.Initialize
	Blocos = OCRPreview.TextBlocks
	'
	'  Turn scan off
	'
	Try
		OCRPreview.stopScanning
	Catch
		Log("Nada")
	End Try
	'
	OCRPreview.Visible=False
	Sleep(0)
	'
	If (Blocos.IsInitialized=False) Then
		Texto=""
	Else
		'
		For Contador = 0 To Blocos.Size - 1
			Texto = Texto & Blocos.Get(Contador) & CRLF
		Next
	End If
	'
	Texto=Texto.Trim
	'
	If (Texto="") Then
		Texto=MsgOCRNaoFoiPossivelLer & CRLF
	End If
	'
	Texto=AFFuncoes.Left(Texto,Texto.Length-1)
	OCRTextoEscaneado.Text=Texto
	'
	Altura=StringUtilities.MeasureMultilineTextHeight(QRCodeTextoEscaneado,Texto)
	OCRTextoEscaneado.Height=Altura
	OCRScroll.Panel.Height=Altura
End Sub
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi!

The whole project I think will be more confusing because it's a big application, with a lot of code and it wouldn't be relevant, so I'm going to copy here the parts of the code that refer to OCR, ok? My natural language is Portuguese, so some variable names and the like may be in this language.

Basically it's the code below. Just what I talked about before, it already happens:


B4X:
Private Sub OCRCamera_Click
    '
    '  Sub to start OCR by activating the camera
    '
       OCRTextoEscaneado.Text=""   
    Try
        OCRPreview.startScanning       
    Catch
        Log(LastException)
    End Try
        '
    OCRPreview.Visible=True
End Sub


Private Sub OCRCameraDesligar_Click
    '
    ' Finish OCR. Turn off camera.
    '
    Try
            OCRPreview.stopScanning       
    Catch
        Log(LastException)
    End Try
    OCRPreview.Visible=False
    OCRPainel.Visible=False
    PainelTela.Visible=False
End Sub


Private Sub OCRLer_Click
    '
    ' To take a text from camera
    '
    Private Blocos   As List
    Private Texto    As String
    Private Altura   As Int
    Private Contador As Int
    '
    ' Start Scan
    '
    Try
        OCRPreview.startScanning
    Catch
        Log(LastException)
    End Try
    Sleep(0)
    '
    ' Blocos
    '
    Blocos.Initialize
    Blocos = OCRPreview.TextBlocks
    '
    '  Turn scan off
    '
    Try
        OCRPreview.stopScanning
    Catch
        Log("Nada")
    End Try
    '
    OCRPreview.Visible=False
    Sleep(0)
    '
    If (Blocos.IsInitialized=False) Then
        Texto=""
    Else
        '
        For Contador = 0 To Blocos.Size - 1
            Texto = Texto & Blocos.Get(Contador) & CRLF
        Next
    End If
    '
    Texto=Texto.Trim
    '
    If (Texto="") Then
        Texto=MsgOCRNaoFoiPossivelLer & CRLF
    End If
    '
    Texto=AFFuncoes.Left(Texto,Texto.Length-1)
    OCRTextoEscaneado.Text=Texto
    '
    Altura=StringUtilities.MeasureMultilineTextHeight(QRCodeTextoEscaneado,Texto)
    OCRTextoEscaneado.Height=Altura
    OCRScroll.Panel.Height=Altura
End Sub
Hard to tell without a sample project. Dont know from your code what panel is a parent or a child.
 

adriano.freitas

Active Member
I understand, but in fact the project itself is big and would generate more confusion. What I could say is that with regard to OCR there is nothing more. And as a parent I use a simple panel, the size of the entire screen... but anyway, I'll see if I can create a small project that replicates the problem and send it.

Thank you for your attention!
 

Ebic

Member
Licensed User
Longtime User
Hi Joh, congratulations for this beautiful library.
I wanted to make a request: is it possible to save the image that comes
scanned for a different use?
Thank you.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Joh, congratulations for this beautiful library.
I wanted to make a request: is it possible to save the image that comes
scanned for a different use?
Thank you.
Not sure this lib is still relevant. ML Kit should be the new to go if I am not mistaken.
 
Top