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: 1,046
  • resource.zip
    423 bytes · Views: 1,018
  • AndroidTextRecognition.jar
    6 KB · Views: 964
  • AndroidTextRecognition.xml
    9.2 KB · Views: 977
Last edited:

peacemaker

Expert
Licensed User
Longtime User
B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.vision.CameraSource com.google.android.gms.vision.CameraSource.start(android.view.SurfaceHolder)' on a null object reference
at
B4X:
atr1.startScanning
on Android 9 tablet in B4A 10.0.

upd.: the log has "Detector dependencies not loaded yet", but no any warning from B4A SDK Manager, all updated.
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.vision.CameraSource com.google.android.gms.vision.CameraSource.start(android.view.SurfaceHolder)' on a null object reference
at
B4X:
atr1.startScanning
on Android 9 tablet in B4A 10.0.

upd.: the log has "Detector dependencies not loaded yet", but no any warning from B4A SDK Manager, all updated.
The dependencies should download automatically when you start the project - it sometimes takes a while...Need an initial internet connection for the dependencies to download.

See the B4A manifest
B4X:
AddApplicationText(<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.gms.vision.DEPENDENCIES"
            android:value="ocr" />)

From the wrapper:
B4X:
        if (!textRecognizer.isOperational()) {
            BA.Log("Detector dependencies not loaded yet");
        } else {
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Ha !
Mention in the first post that the Google Play Services must be active on a device :)

So, only English is recognized OK.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Ha !
Mention in the first post that the Google Play Services must be active on a device :)

So, only English is recognized.
Latin based languages (French, German, English, etc.)
 

amidgeha

Active Member
Licensed User
Longtime User
Got
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......"

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


View attachment 98243
Got this message:
d:\android\android-sdk\tools\..\extras\b4a_remote\androidx\core\core\1.1.0\unpacked-core-1.1.0\res\values\values.xml:163: error: Attribute "fontVariationSettings" has already been defined
 

peacemaker

Expert
Licensed User
Longtime User
Attribute "fontVariationSettings" has already been defined
 

amidgeha

Active Member
Licensed User
Longtime User
I created a new project using sdk 28 and B4A v10.0 and still showing the same error
 

DonManfred

Expert
Licensed User
Longtime User
Got this message:
d:\android\android-sdk\tools\..\extras\b4a_remote\androidx\core\core\1.1.0\unpacked-core-1.1.0\res\values\values.xml:163: error: Attribute "fontVariationSettings" has already been defined
Please post the FULL Error. What you posted is just a part of the error; missing the 2nd part describing where fontVariationSettings is defined already.
 

Johan Schoeman

Expert
Licensed User
Longtime User
That would be good wish or better provide a method for the preview to start capturing
Version 1.01 attached

Have added a button that can be used to "Grab" the text while the scanner is active or just touch the preview while the scanner is active.
Have added Torch functionality
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



2.png


Sample code:

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
 
'    Dim t As Timer

End Sub

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

Sub Activity_Create(FirstTime As Boolean)
 
    Activity.LoadLayout("Layout")
'    t.Initialize("t", 2000)             'initialize the timer
 
End Sub

Sub Activity_Resume
 

 
End Sub

Sub Activity_Pause (UserClosed As Boolean)

'    t.Enabled = False  'stop the timer

End Sub

Sub Button1_Click
 
    Dim Result As Boolean = True                           'check for camera permission                                                              
    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                                 'if permission granted then start the scan
'        t.Enabled = True                                   'enable the timer only once the scanner has been started
    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

Sub t_tick
    'timer is fired every 2 seconds
 
    Dim blk As List = atr1.TextBlocks             'get the blocks every 2 seconds - sufficient time to process inbetween every timer tick
'    Dim lns As List = atr1.TextLines              'you can use this too
'    Dim wrds As List = atr1.TextWords             'you can use this too
 
    'in this case only using blocks and looking for "3 0282 0780" in any of the blocks
    If blk.Size > 0 Then
        For i = 0 To blk.Size - 1
            Log("block(" & i & ") = " & blk.Get(i))
            If blk.get(i) = "3 0282 0780" Then               'check if the block contains "3 0282 0780"
                Log("GOTCHA")
'                t.Enabled = False                            'if it does contain "3 0282 0780" -> stop the timer
                atr1.stopScanning                            'stop the scanner
            End If
         
        Next
    End If
End Sub



Sub Button2_Click
 
    atr1.setFlashOn
 
End Sub

Sub Button3_Click
 
    atr1.setFlashOff
 
End Sub

Sub Button4_Click
     
        Dim blk As List
        blk.Initialize
        blk = atr1.TextBlocks              'for blocks
     
        Dim lns As List
        lns.Initialize
        lns = atr1.TextLines               'for Lines
     
        Dim wrd As List
        wrd.Initialize
        wrd = atr1.TextWords               'for Words
     
        If blk.Size > 0 Then
            For i = 0 To blk.Size - 1
                Log("block(" & i & ") = " & blk.Get(i))
                If blk.get(i) = "3 0282 0780" Then               'check if the block contains "3 0282 0780"
                    Log("GOTCHA")
                    atr1.stopScanning                            'stop the scanner
                End If
             
            Next
        End If  
 
End Sub

Take note of the B4A Manifest when starting a new project:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@style/MyAppTheme")
AddApplicationText(<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.gms.vision.DEPENDENCIES"
            android:value="ocr" />)
     
AddPermission(android.permission.CAMERA) ' Required to be able to access the camera device.    
AddPermission(android.permission.GET_TASKS)
AddPermission("android.hardware.camera")
AddPermission("android.permission.FLASHLIGHT")




CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.

Designer code:
B4X:
'All variants script
AutoScaleAll

atr1.Left = 5%x
atr1.Top = 2%y
atr1.width = 90%x
atr1.Height = atr1.Width

Button1.Left = 35%x
Button1.Top = atr1.Bottom + 2%y
Button1.Width = 30%x
Button1.Height = 15%y

Button2.Left = atr1.Left
Button2.Top = Button1.Bottom + 2%y
Button2.Width = Button1.Width
Button2.Height = Button1.height

Button3.Left = atr1.Right - Button2.width
Button3.Top = Button1.Bottom + 2%y
Button3.Width = Button1.Width
Button3.Height = Button1.height

Button4.Left = 50%x - 0.5 * Button1.Width
Button4.Top = Button2.Bottom + 2%y
Button4.Width = Button2.Width
Button4.Height = Button2.Height

Check the B4A log for the OCR scan result...

Enjoy! ??
 

Attachments

  • B4A_lib_files.zip
    23.2 KB · Views: 760
  • resource.zip
    423 bytes · Views: 699
  • b4aAndroidTextRecognition.zip
    11 KB · Views: 752
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User

Attachments

  • JavaCode.zip
    15.5 KB · Views: 517

SMOOTSARA

Active Member
Licensed User
Longtime User
Hi Johan Schoeman ?
Thank you for your functional library?



I'm looking at a photo and get text but When I want to go to another activity
I quit the program by mistake

B4X:
** Activity (favorite) Resume **
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.camerasource.CameraSource.stop()' on a null object reference
    at textrecognitionwrapper.textrecognitionWrapper$1.surfaceDestroyed(textrecognitionWrapper.java:236)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:588)
    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:247)
    at android.view.View.dispatchWindowVisibilityChanged(View.java:10514)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1551)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1261)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6345)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
    at android.view.Choreographer.doCallbacks(Choreographer.java:686)
    at android.view.Choreographer.doFrame(Choreographer.java:621)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6138)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
 

trepdas

Active Member
Licensed User
Longtime User
Thank you so much for this. it works like a charm. the only thing I need now is to add the foreign language support.

I found and tried this
with no success...


I have found this tesseract, which suppose to deal with the foreign languages :



how can I implemented this inside?
is there any other way to make it support all foreign language letters?
 

ninja3

New Member
hello Johan. There is some way to do the recognition of some jpg,bmp,png file. Without taking a picture ??? Already amazed at how the library works. a genius
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
hello Johan. There is some way to do the recognition of some jpg,bmp,png file. Without taking a picture ??? Already amazed at how the library works. a genius
 
Top