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,405
  • AndroidVisionOCRLibFiles.zip
    37.2 KB · Views: 1,338
  • resource.zip
    423 bytes · Views: 1,364
  • OCRres.zip
    30.7 KB · Views: 1,340
Last edited:

roberto64

Active Member
Licensed User
Longtime User
no I am using LibraryCompiler 1:03 and -b4aignore "org.com.android.com.OCRReader.com.hoho" and fair?
and the error is
B4X:
C:\Android\TheJavaCode\src\com\google\android\gms\samples\vision\ocrreader\OcrCaptureActivity.java:49: error: package com.google.android.gms.vision.text does not exist
import com.google.android.gms.vision.text.TextBlock;
                                         ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: C:\Android\TheJavaCode\src\com\google\android\gms\samples\vision\ocrreader\OcrCaptureActivity.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error


Error.
regards
 

Johan Schoeman

Expert
Licensed User
Longtime User
no I am using LibraryCompiler 1:03 and -b4aignore "org.com.android.com.OCRReader.com.hoho" and fair?
and the error is
B4X:
C:\Android\TheJavaCode\src\com\google\android\gms\samples\vision\ocrreader\OcrCaptureActivity.java:49: error: package com.google.android.gms.vision.text does not exist
import com.google.android.gms.vision.text.TextBlock;
                                         ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: C:\Android\TheJavaCode\src\com\google\android\gms\samples\vision\ocrreader\OcrCaptureActivity.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error


Error.
regards
if you want to compile the java code then -b4aignore should only have com as its entry - provided that you have browsed to the correct folder to try and compile from and that you have created a libs folder on the same folder level as the src folder that contains the JAR's from the link in post #41 except for:

OCRReaderComplete.jar
OCRReaderComplete.xml

The above jar and xml will be created and added to you additional library folder when you compile the java code into a library.

But if you only want to run the last B4A project that I have posted then you need all the JAR's and XML's that you can download from the link in post #41 to be in your additional library folder.
 

roberto64

Active Member
Licensed User
Longtime User
thanks for your time, and now everything is ok, I can modify the library? I am creating a program for license plate recognition.
regards
 

Johan Schoeman

Expert
Licensed User
Longtime User
thanks for your time, and now everything is ok, I can modify the library? I am creating a program for license plate recognition.
regards
See post #17 - it should do it.
 

roberto64

Active Member
Licensed User
Longtime User
Hello Johan, and you can have the lib matrix. AndroidVisionOCR ?, I do not know java very well and I would like to understand the operation.
Greetings
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hello Johan, and you can have the lib matrix. AndroidVisionOCR ?, I do not know java very well and I would like to understand the operation.
Greetings
Not sure I understand what you mean with lib matrix Roberto. Please explain
 

roberto64

Active Member
Licensed User
Longtime User
hi Johan, you can make a native library in B4A using #If JAVA "Java code" #End If entering classes of OCRReaderComplete library.
 

roberto64

Active Member
Licensed User
Longtime User
Hi Johan, I would ask a cortesiaper the TheJavaCode source code Library Updated Version 1.1 AndroidVisionOCR, I do not know a lot about java.
regards
 

fabio55

Member
Licensed User
Longtime User
HI! I am trying the example but I got the following compiler error:

Generating R file. Error
D:\B4A\b4aAndroidVisionOCR\Objects\bin\extra\res6\res\values-v23\values-v23.xml:4: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'.
D:\B4A\b4aAndroidVisionOCR\Objects\bin\extra\res6\res\values-v23\values-v23.xml:34: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'.
D:\B4A\b4aAndroidVisionOCR\Objects\bin\extra\res6\res\values-v24\values-v24.xml:3: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Borderless.Colored'.
D:\B4A\b4aAndroidVisionOCR\Objects\bin\extra\res6\res\values-v24\values-v24.xml:4: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Colored'.

Can someone help? What I am missing?
PS
Libraries are there
Resource and OCRres
In the project folder
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan, I would ask a cortesiaper the TheJavaCode source code Library Updated Version 1.1 AndroidVisionOCR, I do not know a lot about java.
regards

Roberto, do you want a copy of the Java code? Is that what you are asking?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan, I would ask a cortesiaper the TheJavaCode source code Library Updated Version 1.1 AndroidVisionOCR, I do not know a lot about java.
regards
Roberto, download the Java code and the JARS that you need from this link (folders src and libs):
https://www.dropbox.com/s/4sg0ifcz5mxgofb/JavaCode_and_libs.zip?dl=0

If you want to compile the code then you need to create a folder called libs on the same folder level as the src folder and copy the JARS into the libs folder. You will also need to copy the JARS to your B4A additional library folder to run the B4A project.

Thus, folder structure to compile should be:
your_folder_name
src
androidvisionocrwrapper
androidvisionocrWrapper.java​
com
all the subfolders and java files......​
libs
all the jar files downloaded from the link above​
 

roberto64

Active Member
Licensed User
Longtime User
thanks for your time, I would like to figure out how to build a library B4A, I'm trying but in vain to figure out how to write it in eclipse that I have never used, or there is another editor to scruvere code, I do not understand when you import anywheresoftware .. . if there is a manual to understand its functions, I thank you anyway for your time that you gave me.
regards
 
Last edited:

roberto64

Active Member
Licensed User
Longtime User
hi johan, I compiled with LibraryCompiler your AndroidVisionOCR package ristituisce me this error
regards
 

Attachments

  • Immagine4.png
    Immagine4.png
    94.7 KB · Views: 259

Johan Schoeman

Expert
Licensed User
Longtime User
thanks for your time, I would like to figure out how to build a library B4A, I'm trying but in vain to figure out how to write it in eclipse that I have never used, or there is another editor to scruvere code, I do not understand when you import anywheresoftware .. . if there is a manual to understand its functions, I thank you anyway for your time that you gave me.
regards
Roberto, I cannot help you with Eclipse. I do all my editing with Notepad ++ and then compile it with @Erel's Simple Library Compiler. Much easier than trying to learn what is happening in Eclipse.
 

Johan Schoeman

Expert
Licensed User
Longtime User
thanks for your time, I would like to figure out how to build a library B4A, I'm trying but in vain to figure out how to write it in eclipse that I have never used, or there is another editor to scruvere code, I do not understand when you import anywheresoftware .. . if there is a manual to understand its functions, I thank you anyway for your time that you gave me.
regards
Roberto, I think it will be easier to start with something simpler. There is unfortunately no manual available for this. You have to search the forum and post questions in the Libraries Developer Forum. There are also a number of good examples by @DonManfred that should assist.

But to get back to your original question- you can scan license plates with the library. I have tested it and it works very well.
 

Johan Schoeman

Expert
Licensed User
Longtime User
hi johan, I compiled with LibraryCompiler your AndroidVisionOCR package ristituisce me this error
regards
Do you perhaps have my library and your newly complied library both enabled at the same time in your B4A project?
 

billmoultrie

Member
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.
 
Top