B4A Library OCR - Optical Character Recognition with Google Play Services (Android Vision)

This is a wrap for this Github project. It is a SUPER OCR (Optical Character Recognition) app. I have even managed to read motor car number plates with it! Posting the following:
1. B4A sample project (it will only work with B4A V6.00)
2. B4A library files
3. resource.zip - extract it and copy the resource folder to be on the same folder level as that of the /Files and /Objects folders of the B4A project
4. OCRres.zip - extract it and copy the OCRres folder to be on the same folder level as that of the /Files and /Objects folders of the B4A project

You need to have the Google Repository installed (via the SDK manager). I have Google Repository V32 installed.

I am using this on a Samsung S4 mini with KitKat.

This is the libraries that I have enabled in my B4A project:

libsenabled.png


You will need the following jars in your additional library folder:
android-support-design.jar
android-support-v4.jar
android-support-v7-appcompat.jar
android-support-annotations.jar

You will also need the following libraries by @corwin42 (download from the forum):
AppCompat V3.20 - https://www.b4x.com/android/forum/t...compatible-with-older-android-versions.48423/
DesignSupport V2.00 - https://www.b4x.com/android/forum/threads/designsupport-additional-material-design-components.58893/

startb4a.png


startocr.png


Click on Detect Text to start the OCR (you can set the flash mode as well as autofocus).


1.png


Touch any of the highlighted blocks of text - I have clicked on the big block:

2.png


Just another sample of the OCR capability:

3.png


Can do some mods to bring back info to the B4A project via an event from the library....

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

#AdditionalRes: ..\resource
#AdditionalRes: ..\OCRres


'#AdditionalRes: C:\Users\----------2\Documents\Basic 4 Android\JOHAN APPS\JHS LIBS\resource\b4a_appcompat, de.amberhome.objects.appcompat
'#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
'#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\google\google-play-services\libproject\google-play-services_lib\res, com.google.android.gms
'#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\design\res, android.support.design

'#ExcludeClasses: .games, .drive, .ads, .fitness, .wearable, .measurement, .cast, .auth, .nearby
'#ExcludeClasses: .tagmanager, .analytics, .wallet, .plus, .gcm, .maps, .panorama

'#Extends: android.support.v7.app.AppCompatActivity

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



End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Dim avocr As AndroidVisionOCR


End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

    Activity.LoadLayout("main")
    avocr.Initialize("")

    avocr.OCRtextColor = Colors.Yellow
    avocr.OCRtextSize = 30.0


End Sub

Sub Activity_Resume



End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click

avocr.StartCameraFilter

End Sub

Library as it is at present:
AndroidVisionOCR
Author:
Github: Benjamin Johns, Wrapped by: Johan Schoeman
Version: 1
  • AndroidVisionOCR
    Fields:
    • OCRtextColor As Int
    • OCRtextSize As Float
    Methods:
    • Initialize (paramString As String)
    • IsInitialized As Boolean
    • StartCameraFilter
    Permissions:
    • android.permission.CAMERA

Take note of the B4A manifest Files:
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="23"/>
<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" />

        <activity
            android:name="com.google.android.gms.samples.vision.ocrreader.MainActivity"
            android:label="@string/title_activity_main" >

        </activity>

        <activity android:name="com.google.android.gms.samples.vision.ocrreader.OcrCaptureActivity"
            android:label="Read Text" />)
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")
 

Attachments

  • b4aAndroidVisionOCR.zip
    8.7 KB · Views: 1,604
  • AndroidVisionOCRLibFiles.zip
    37.2 KB · Views: 1,524
  • resource.zip
    423 bytes · Views: 1,552
  • OCRres.zip
    30.7 KB · Views: 1,498
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Hi
I have installed all the jar's etc but get an error when I try to compile.

B4A version: 6.80
Parsing code. (0.00s)
Compiling code. (0.15s)
Compiling layouts code. (0.01s)
Organizing libraries. (0.00s)
Generating R file. Error
invalid resource directory name: C:\Program Files (x86)\Anywhere Software\Basic4android\Programs\b4aAndroidVisionOCR\Objects\bin\extra\res6\res/drawable-anydpi-v21

The path is correct and there is a folder drawable-anydpi-v21 but I still get the error.

Any idea what might be wrong.
I have just downloaded B4A V6.80 and it works without any problems.

I don't have such a folder structure in my B4A project....?
invalid resource directory name: C:\Program Files (x86)\Anywhere Software\Basic4android\Programs\b4aAndroidVisionOCR\Objects\bin\extra\res6\res/drawable-anydpi-v21
 

Attachments

  • 5.png
    5.png
    319.8 KB · Views: 381

billmoultrie

Member
Licensed User
Longtime User
Files1.png
Files2.png

I have just downloaded B4A V6.80 and it works without any problems.

I don't have such a folder structure in my B4A project....?
invalid resource directory name: C:\Program Files (x86)\Anywhere Software\Basic4android\Programs\b4aAndroidVisionOCR\Objects\bin\extra\res6\res/drawable-anydpi-v21

Thanks for your reply.
I created a new version of b4aAndroidVisionOCR and this is the file structure before compiling (Files1).
Then I compiled and got (Files2)
Files1.png


Have I put OCRres and resource in the wrong place.
As you can see drawable-anydpi-v21 is created.

I would be grateful if you could give me a clue as to what I am doing wrong.
 

Douglas Farias

Expert
Licensed User
Longtime User
@Johan Schoeman
Hi.
i m testing your lib, it works fine on my device, i can read big words etc..
but the auto focus dont is working, and my device have auto focus, i use auto focus in cam ex.

my device is a samsung galaxy j2

here on your lib really dont work the auto focus, can u fix this pls?
if it works i can send u a donation for u work, i will use your lib.

Obs: the focus seems to work, but only for long distances, I can not use it for closer distances

thx
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
@Johan Schoeman
Hi.
i m testing your lib, it works fine on my device, i can read big words etc..
but the auto focus dont is working, and my device have auto focus, i use auto focus in cam ex.

my device is a samsung galaxy j2

here on your lib really dont work the auto focus, can u fix this pls?
if it works i can send u a donation for u work, i will use your lib.

Obs: the focus seems to work, but only for long distances, I can not use it for closer distances

thx
Try with the attached library files (V1.03). Have added the following options as far as focus is concerned:
B4X:
'Use one of the following:
'   FOCUS_MODE_CONTINUOUS_PICTURE
'   FOCUS_MODE_CONTINUOUS_VIDEO
'   FOCUS_MODE_AUTO
'   FOCUS_MODE_EDOF
'   FOCUS_MODE_FIXED
'   FOCUS_MODE_INFINITY
'   FOCUS_MODE_MACRO
    avocr.FocusMode = avocr.FOCUS_MODE_MACRO

I guess you are looking for FOCUS_MODE_MACRO....

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

#AdditionalRes: ..\resource
#AdditionalRes: ..\OCRres


'#AdditionalRes: C:\Users\----------2\Documents\Basic 4 Android\JOHAN APPS\JHS LIBS\resource\b4a_appcompat, de.amberhome.objects.appcompat
'#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
'#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\google\google-play-services\libproject\google-play-services_lib\res, com.google.android.gms
'#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\design\res, android.support.design

'#ExcludeClasses: .games, .drive, .ads, .fitness, .wearable, .measurement, .cast, .auth, .nearby
'#ExcludeClasses: .tagmanager, .analytics, .wallet, .plus, .gcm, .maps, .panorama

'#Extends: android.support.v7.app.AppCompatActivity

#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.
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Dim avocr As AndroidVisionOCR
   
    Private CheckBox1 As CheckBox
    Private CheckBox2 As CheckBox
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
   
    Activity.LoadLayout("main")
    avocr.Initialize("avocr")
   
    avocr.OCRtextColor = Colors.Yellow                                    'the color of the text while OCR takes place
    avocr.OCRtextSize = 30.0                                              'the size of the text while OCR take place
    avocr.OCRrectangleColor = Colors.Green
    avocr.OCRrectangleWidth = 2.0
    avocr.OCRInfoMessage = "Toccare per acquisire. Pizzica/Stretch per lo zoom"         'used https://www.freetranslation.com/en/translate-english-italian for translation

    'Use one of the following:
'   FOCUS_MODE_CONTINUOUS_PICTURE
'   FOCUS_MODE_CONTINUOUS_VIDEO
'   FOCUS_MODE_AUTO
'   FOCUS_MODE_EDOF
'   FOCUS_MODE_FIXED
'   FOCUS_MODE_INFINITY
'   FOCUS_MODE_MACRO
    avocr.FocusMode = avocr.FOCUS_MODE_MACRO
   

End Sub

Sub Activity_Resume
   

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click

    If CheckBox1.Checked = True Then
      avocr.UseFlash = True
    Else
      avocr.UseFlash = False   
    End If   

    If CheckBox2.Checked = True Then
      avocr.UseAutoFocus = True
    Else
      avocr.UseAutoFocus = False       
    End If

    avocr.StartOCRReader

End Sub

Sub avocr_scanned_text(scannedText As String)
   
    Log("Scanned text = " & scannedText)
    Label1.Text = " " & scannedText
   
End Sub
 

Attachments

  • resource.zip
    423 bytes · Views: 594
  • b4aAndroidVisionOCR.zip
    9.5 KB · Views: 638
  • OCRres.zip
    30.7 KB · Views: 607
  • AndroidVisionOCR_libFilesV1.03.zip
    37.5 KB · Views: 639

Douglas Farias

Expert
Licensed User
Longtime User
Try with the attached library files (V1.03). Have added the following options as far as focus is concerned:
B4X:
'Use one of the following:
'   FOCUS_MODE_CONTINUOUS_PICTURE
'   FOCUS_MODE_CONTINUOUS_VIDEO
'   FOCUS_MODE_AUTO
'   FOCUS_MODE_EDOF
'   FOCUS_MODE_FIXED
'   FOCUS_MODE_INFINITY
'   FOCUS_MODE_MACRO
    avocr.FocusMode = avocr.FOCUS_MODE_MACRO

I guess you are looking for FOCUS_MODE_MACRO....

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

#AdditionalRes: ..\resource
#AdditionalRes: ..\OCRres


'#AdditionalRes: C:\Users\----------2\Documents\Basic 4 Android\JOHAN APPS\JHS LIBS\resource\b4a_appcompat, de.amberhome.objects.appcompat
'#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
'#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\google\google-play-services\libproject\google-play-services_lib\res, com.google.android.gms
'#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\design\res, android.support.design

'#ExcludeClasses: .games, .drive, .ads, .fitness, .wearable, .measurement, .cast, .auth, .nearby
'#ExcludeClasses: .tagmanager, .analytics, .wallet, .plus, .gcm, .maps, .panorama

'#Extends: android.support.v7.app.AppCompatActivity

#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.
 
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Dim avocr As AndroidVisionOCR
 
    Private CheckBox1 As CheckBox
    Private CheckBox2 As CheckBox
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
 
    Activity.LoadLayout("main")
    avocr.Initialize("avocr")
 
    avocr.OCRtextColor = Colors.Yellow                                    'the color of the text while OCR takes place
    avocr.OCRtextSize = 30.0                                              'the size of the text while OCR take place
    avocr.OCRrectangleColor = Colors.Green
    avocr.OCRrectangleWidth = 2.0
    avocr.OCRInfoMessage = "Toccare per acquisire. Pizzica/Stretch per lo zoom"         'used https://www.freetranslation.com/en/translate-english-italian for translation

    'Use one of the following:
'   FOCUS_MODE_CONTINUOUS_PICTURE
'   FOCUS_MODE_CONTINUOUS_VIDEO
'   FOCUS_MODE_AUTO
'   FOCUS_MODE_EDOF
'   FOCUS_MODE_FIXED
'   FOCUS_MODE_INFINITY
'   FOCUS_MODE_MACRO
    avocr.FocusMode = avocr.FOCUS_MODE_MACRO
 

End Sub

Sub Activity_Resume
 

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click

    If CheckBox1.Checked = True Then
      avocr.UseFlash = True
    Else
      avocr.UseFlash = False 
    End If 

    If CheckBox2.Checked = True Then
      avocr.UseAutoFocus = True
    Else
      avocr.UseAutoFocus = False     
    End If

    avocr.StartOCRReader

End Sub

Sub avocr_scanned_text(scannedText As String)
 
    Log("Scanned text = " & scannedText)
    Label1.Text = " " & scannedText
 
End Sub

hi Johan, many thx for your suport.

i tested all focus mode here.
' FOCUS_MODE_CONTINUOUS_PICTURE
' FOCUS_MODE_CONTINUOUS_VIDEO
' FOCUS_MODE_AUTO
' FOCUS_MODE_EDOF
' FOCUS_MODE_FIXED
' FOCUS_MODE_INFINITY
avocr.FocusMode = avocr.FOCUS_MODE_MACRO

the best for my device is the MODE_MACRO, all anothers dont FOCUS here.
but in mode macro have a problem.

He is focusing only if you approach the document very closely.

here is the result using FOCUS_MODE_MACRO (i need scan this document)
Screenshot_2017-03-11-12-20-16.png




and here is FOCUS_MODE_MACRO Next to the document (with works next, but i need take all document Like the picture above)
Screenshot_2017-03-11-12-22-07.png




Very close: has focus
Distant: no

its possible make a continuos focus for this lib?
on cam ex class in any distance the focus Self adjust

i found this subs on cam ex (maybe can help)
B4X:
Public Sub GetSupportedFocusModes As List
    r.target = parameters
    Return r.RunMethod("getSupportedFocusModes")
End Sub

Public Sub SetContinuousAutoFocus
    Dim modes As List = GetSupportedFocusModes
    If modes.IndexOf("continuous-picture") > -1 Then
        SetFocusMode("continuous-picture")
    Else If modes.IndexOf("continuous-video") > -1 Then
        SetFocusMode("continuous-video")
    End If
End Sub

Public Sub SetFocusMode(Mode As String)
    r.target = parameters
    r.RunMethod2("setFocusMode", Mode, "java.lang.String")
End Sub

Public Sub GetFocusDistances As Float()
    Dim F(3) As Float
    r.target = parameters
    r.RunMethod4("getFocusDistances", Array As Object(F), Array As String("[F"))
    Return F
End Sub

Public Sub GetSupportedPictureFormats As List
    r.target = parameters
    Return r.RunMethod("getSupportedPictureFormats")
End Sub

B4X:
        camEx.SetJpegQuality(100)
        camEx.SetContinuousAutoFocus
        camEx.CommitParameters
        camEx.StartPreview

the continuous focus is working very good on cam ex.
i think on your lib have only fixed modes

can u try add this pls?


another question.
its possible get all text of this document? And not only of a square touched
i need scan a document like the images, get all string possible and send via server.

thx again for u support
i really need this lib, if works ok, i send u 50$

i made a job offer about this, u lib is very next what i need
https://www.b4x.com/android/forum/threads/ocr-lib-to-b4a-real-time.77197/

thx
 

Johan Schoeman

Expert
Licensed User
Longtime User
hi Johan, many thx for your suport.

i tested all focus mode here.
' FOCUS_MODE_CONTINUOUS_PICTURE
' FOCUS_MODE_CONTINUOUS_VIDEO
' FOCUS_MODE_AUTO
' FOCUS_MODE_EDOF
' FOCUS_MODE_FIXED
' FOCUS_MODE_INFINITY
avocr.FocusMode = avocr.FOCUS_MODE_MACRO

the best for my device is the MODE_MACRO, all anothers dont FOCUS here.
but in mode macro have a problem.

He is focusing only if you approach the document very closely.

here is the result using FOCUS_MODE_MACRO (i need scan this document)
View attachment 53693



and here is FOCUS_MODE_MACRO Next to the document (with works next, but i need take all document Like the picture above)
View attachment 53694



Very close: has focus
Distant: no

its possible make a continuos focus for this lib?
on cam ex class in any distance the focus Self adjust

i found this subs on cam ex (maybe can help)
B4X:
Public Sub GetSupportedFocusModes As List
    r.target = parameters
    Return r.RunMethod("getSupportedFocusModes")
End Sub

Public Sub SetContinuousAutoFocus
    Dim modes As List = GetSupportedFocusModes
    If modes.IndexOf("continuous-picture") > -1 Then
        SetFocusMode("continuous-picture")
    Else If modes.IndexOf("continuous-video") > -1 Then
        SetFocusMode("continuous-video")
    End If
End Sub

Public Sub SetFocusMode(Mode As String)
    r.target = parameters
    r.RunMethod2("setFocusMode", Mode, "java.lang.String")
End Sub

Public Sub GetFocusDistances As Float()
    Dim F(3) As Float
    r.target = parameters
    r.RunMethod4("getFocusDistances", Array As Object(F), Array As String("[F"))
    Return F
End Sub

Public Sub GetSupportedPictureFormats As List
    r.target = parameters
    Return r.RunMethod("getSupportedPictureFormats")
End Sub

B4X:
        camEx.SetJpegQuality(100)
        camEx.SetContinuousAutoFocus
        camEx.CommitParameters
        camEx.StartPreview

the continuous focus is working very good on cam ex.
i think on your lib have only fixed modes

can u try add this pls?


another question.
its possible get all text of this document? And not only of a square touched
i need scan a document like the images, get all string possible and send via server.

thx again for u support
i really need this lib, if works ok, i send u 50$

i made a job offer about this, u lib is very next what i need
https://www.b4x.com/android/forum/threads/ocr-lib-to-b4a-real-time.77197/

thx

Enjoy...
 

Johan Schoeman

Expert
Licensed User
Longtime User
hi @Johan Schoeman ,

any update for this? is it possible for grab/get all the text that already marked?
Hi Leni

Have not figured it out - but have spent very little time on this as I am too occupied by my actual work and other special requests. When I have some time I will look into it.

JS
 

wirog80

Member
Licensed User
thanks @Johan Schoeman for great library.

i want to use this lib as LPR (license plate recognition).
i have try a sample source/APK and it work successfully to detect a plate number.

maybe this is my idea/request for added :)
before start scan, can you set the camera on the one area/region to focused recognition,
so not recognition other text.
like this picture..

WhatsApp Image 2017-07-29 at 8.13.35 PM.jpeg


thanks for all
 

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
To use this library do I need to create an account to get an API key?
Is this a google online service with OCR?


Sorry if this question is wrong
 

Johan Schoeman

Expert
Licensed User
Longtime User
To use this library do I need to create an account to get an API key?
Is this a google online service with OCR?


Sorry if this question is wrong
No need to create an account or to get an API key. It just requires an initial Internet connection as it will download some dependencies. Once downloaded it will function without being connected to the Internet.
 

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
No need to create an account or to get an API key. It just requires an initial Internet connection as it will download some dependencies. Once downloaded it will function without being connected to the Internet.


Thanks for the answer.

This is amazing. I really need an OCR solution.

Google Cloud vision is very good however it is online and paid.
Https://cloud.google.com/vision/?hl=en-us

Can you tell if the results are as good as the online vision?
 

Johan Schoeman

Expert
Licensed User
Longtime User

Fabri

Member
Licensed User
if I add
#AdditionalRes: ..\resource
#AdditionalRes: ..\OCRres
to a subroutine, it give me this error:
Attribute not supported: additionalres

Running B4A version 6
Any idea?
TIA
 
Last edited:

imbault

Well-Known Member
Licensed User
Longtime User
Hi @Johan Schoeman, super work
It is possible to add the recognition of Barecodes and QR codes in your library (I can pay for that)

Thanks
 

Johan Schoeman

Expert
Licensed User
Longtime User
Top