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: 955
  • AndroidVisionOCRLibFiles.zip
    37.2 KB · Views: 910
  • resource.zip
    423 bytes · Views: 912
  • OCRres.zip
    30.7 KB · Views: 894
Last edited:

MarcoRome

Expert
Licensed User
Hi Johan this is really great library ( 100+ )
I would give you some tips.
1. The theme is color orange is if i wanted to change this theme ?
2. In the first mask I find the "Start Scan" button and if you wanted to change the color and text in the button?
2016-08-28 07.06.15.png


3. The second form I find "Click Detect Text to Detect Text" and "Detect Text" button and if you wanted to change this ?

2016-08-28 07.06.18.png


The library is really great, but it might be possible to customize the same ( example in another language and with another color as you barcode where is possibile change all )
Great library...really great.
Thank you
Marco
 

Johan Schoeman

Expert
Licensed User
Hi Johan this is really great library ( 100+ )
I would give you some tips.
1. The theme is color orange is if i wanted to change this theme ?
2. In the first mask I find the "Start Scan" button and if you wanted to change the color and text in the button?

3. The second form I find "Click Detect Text to Detect Text" and "Detect Text" button and if you wanted to change this ?

1. The theme is color orange is if i wanted to change this theme ? - see themes.xml in the \resource\values folder of the B4A project.
2. In the first mask I find the "Start Scan" button and if you wanted to change the color and text in the button? - In the B4A designer
3. The second form I find "Click Detect Text to Detect Text" and "Detect Text" button and if you wanted to change this ? - I need to amend the library and will do so
 

MarcoRome

Expert
Licensed User
1. The theme is color orange is if i wanted to change this theme ? - see themes.xml in the \resource\values folder of the B4A project.
2. In the first mask I find the "Start Scan" button and if you wanted to change the color and text in the button? - In the B4A designer
3. The second form I find "Click Detect Text to Detect Text" and "Detect Text" button and if you wanted to change this ? - I need to amend the library and will do so
Thank you very much
 

MarcoRome

Expert
Licensed User
Another thing Johan ...when i have result will be possible save the same ?

For now i have this result. I can see but isnt possible save the result.

2016-08-28 08.12.36.png


Thank you
Marco
 

Johan Schoeman

Expert
Licensed User
Another thing Johan ...when i have result will be possible save the same ?

For now i have this result. I can see but isnt possible save the result.

View attachment 47325

Thank you
Marco
Yes, I will do some mods to bring the scanned info back to B4A via an event.
 

MarcoRome

Expert
Licensed User
This work only about latin characters language right ?
Is it possible add also chinese char ?
 

Johan Schoeman

Expert
Licensed User
Another thing Johan ...when i have result will be possible save the same ?

For now i have this result. I can see but isnt possible save the result.

View attachment 47325

Thank you
Marco
This update allows you to set the text to different languages. It will also bring back the text in the block that you have touched to B4A (use the back button to get back to the B4A project and check the B4A log for the returned text) via an event:
B4X:
Sub avocr_scanned_text(scannedText As String)
  
    Log("Scanned text = " & scannedText)
  
End Sub

Replace folder \OCRres in the B4A project with the attached folder. See the change that I have done to \OCRres\values\strings.xml to get some Italian text displayed.

The \resource folder stays as is as what I have posted in post #1. Make sure you add it to this project!

B4X:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="ok">OK</string>
    <string name="permission_camera_rationale">Access to the camera is needed for detection</string>
    <string name="no_camera_permission">This application cannot run because it does not have the camera permission.  The application will now exit.</string>
    <string name="low_storage_error">Ocr dependencies cannot be downloaded due to low device storage</string>
    <string name="title_activity_main">Ocr Detector Sample</string>
    <string name="ocr_header">Click &quot;Detect Text&quot; to detect text</string>
    <string name="read_text">Rilevare il testo</string>
    <string name="auto_focus">Auto Focus</string>
    <string name="use_flash">Use Flash</string>
    <string name="ocr_success">Text read successfully</string>
    <string name="ocr_failure">No text captured</string>
    <string name="ocr_error">"Error reading text: %1$s"</string>
</resources>

The other "different language text" can be set from within the B4A project.

1.png



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")
  
    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
  
    avocr.StatusMessage = "Fare clic su Rileva il testo per rilevare il testo"          'used https://www.freetranslation.com/en/translate-english-italian for translation
    avocr.StatusMessageTextColor = Colors.Magenta
    avocr.StatusMessageTextSize = 15
  
    avocr.TextValue = ""                                                                'used https://www.freetranslation.com/en/translate-english-italian for translation
    avocr.TextValueTextColor = Colors.Yellow
    avocr.TextValueTextSize = 12
  
    avocr.UseFlashText = "Uso di Flash"                                                 'used https://www.freetranslation.com/en/translate-english-italian for translation
    avocr.UseFlashTextColor = Colors.Cyan
  
    avocr.AutoFocusText = "La funzione di messa a fuoco automatica"                     'used https://www.freetranslation.com/en/translate-english-italian for translation
    avocr.AutoFocusTextColor = Colors.Red

End Sub

Sub Activity_Resume
  

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
  
avocr.StartCameraFilter

End Sub

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

Updated Library:
AndroidVisionOCR
Author:
Github: Benjamin Johns, Wrapped by: Johan Schoeman
Version: 1.01
  • AndroidVisionOCR
    Fields:
    • AutoFocusText As String
    • AutoFocusTextColor As Int
    • OCRInfoMessage As String
    • OCRrectangleColor As Int
    • OCRrectangleWidth As Float
    • OCRtextColor As Int
    • OCRtextSize As Float
    • StatusMessage As String
    • StatusMessageTextColor As Int
    • StatusMessageTextSize As Int
    • TextValue As String
    • TextValueTextColor As Int
    • TextValueTextSize As Int
    • UseFlashText As String
    • UseFlashTextColor As Int
    Methods:
    • Initialize (paramString As String)
    • IsInitialized As Boolean
    • StartCameraFilter
    Permissions:
    • android.permission.CAMERA

I need to do some more work on this to eliminate the initial activity so that the scan activity can be invoked directly from B4A - that will be the next exercise.

All translations from English to Italian was done using (not sure how accurate it is) -

https://www.freetranslation.com/en/translate-english-italian
 

Attachments

  • b4aAndroidVisionOCR.zip
    9.1 KB · Views: 377
  • AndroidVisionOCRLibFilesV1_01.zip
    38.2 KB · Views: 377
  • OCRres.zip
    30.7 KB · Views: 375
Last edited:

MarcoRome

Expert
Licensed User
Hi Johan i have this result:

B4X:
Sub avocr_scanned_text(scannedText As String)
   
    Log("Scanned text = " & scannedText)
   
End Sub

Result in Log windows:

sending message to waiting queue (avocr_scanned_text)
 

Johan Schoeman

Expert
Licensed User
Hi Johan i have this result:

B4X:
Sub avocr_scanned_text(scannedText As String)
  
    Log("Scanned text = " & scannedText)
  
End Sub

Result in Log windows:
Is this the result that you get when you click your device's back button (after a scan) and when you are back in B4A?
 

Johan Schoeman

Expert
Licensed User
An update that does not show the additional startup activity. You can now set the flash and the autofocus from B4A and immediately start the OCR scanner from B4A. Posting:

1. New B4A library files - AndroidVisionOCRLibFiles_V1_02.zip (extract the jar and xml and copy them to your additional library folder.
2. OCRres.zip - extract it and copy the folder to the B4A project so that it is on the same folder level as the /Files and /Objects folders of the B4A project
3. resource.zip - extract it and copy the folder to the B4A project so that it is on the same folder level as the /Files and /Objects folders of the B4A project
4. B4A sample project - b4aAndroidVisionOCR.zip

See post #1 for anything else that might be of importance. Also take note of the B4A manifest file. Keep in mind that you need B4A V6.00 for this project to work

Remember to touch a box when the scanner is active to return the text of that box to B4A

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
  

End Sub

Sub Activity_Resume
  

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click

    If CheckBox1.Checked = True Then
      avocr.UseFalse = True
    Else
      avocr.UseFalse = 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

Updated library (V1.02):

AndroidVisionOCR
Author:
Github: Benjamin Johns, Wrapped by: Johan Schoeman
Version: 1.02
  • AndroidVisionOCR
    Fields:
    • OCRInfoMessage As String
    • OCRrectangleColor As Int
    • OCRrectangleWidth As Float
    • OCRtextColor As Int
    • OCRtextSize As Float
    Methods:
    • Initialize (paramString As String)
    • IsInitialized As Boolean
    • StartOCRReader
    Permissions:
    • android.permission.CAMERA
    Properties:
    • UseAutoFocus As Boolean [write only]
    • UseFalse As Boolean [write only]

Use the check boxes to enable the Flash and / or Auto Focus and then click on Start Scan:
1.png


Click on one of the green boxes with yellow text to return that text to your B4A project
2.png



The text of the box that was touched returned to B4A and displayed in the label below the Start Scan button.
3.png
 

Attachments

  • b4aAndroidVisionOCR.zip
    9.4 KB · Views: 431
  • AndroidVisionOCRLibFiles_V1_02.zip
    37.2 KB · Views: 422
  • OCRres.zip
    30.7 KB · Views: 426
  • resource.zip
    423 bytes · Views: 408
Top