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:

VictorTandil

Member
Licensed User
Longtime User
Hello Johan. Thanks for this wonderful library. It works perfect, but when you add it to my application, all the menus of the Activities have disappeared. How can I make them appear again?

From already thank you very much.-
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hello Johan. Thanks for this wonderful library. It works perfect, but when you add it to my application, all the menus of the Activities have disappeared. How can I make them appear again?

From already thank you very much.-
Can you upload a screenshot to show me what it is that you mean?
 

VictorTandil

Member
Licensed User
Longtime User
When I enabled this line in the "Manifest":

B4X:
SetApplicationAttribute(android:theme, "@style/MyAppTheme")

The three points corresponding to the Menu option disappear together with the title bar (see image at 9:00 p.m.)
On the contrary, if I disable it, the options bar appears again, but when I try to use the scanner, it throws an error saying:

"java.lang.RuntimeException: Unable to start activity ComponentInfo{**.*******.cprmoviles/com.google.android.gms.samples.vision.ocrreader.OcrCaptureActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity."

Screenshot_2018-03-11-21-00-15.png
Screenshot_2018-03-11-21-09-07.png
 
Last edited:

ssonap

Member
Licensed User
Sorry for my bad english.
I am beginner in b4a and I try to use "b4aAndroidVisionOCR".
And despite all the explanation of the forum I can not use it.
When I run the program, I have this error message.
I do not understand what blocks.

"Génération du fichier R. Error
C:\Users\CHTING\Desktop\Projets android\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'.
C:\Users\CHTING\Desktop\Projets android\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'."


Help me please

a big thank-you
 

Johan Schoeman

Expert
Licensed User
Longtime User
Sorry for my bad english.
I am beginner in b4a and I try to use "b4aAndroidVisionOCR".
And despite all the explanation of the forum I can not use it.
When I run the program, I have this error message.
I do not understand what blocks.

"Génération du fichier R. Error
C:\Users\CHTING\Desktop\Projets android\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'.
C:\Users\CHTING\Desktop\Projets android\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'."


Help me please

a big thank-you
See Erel's answer here

https://www.b4x.com/android/forum/t...vices-android-vision.70417/page-3#post-484498
 

scsjc

Well-Known Member
Licensed User
Longtime User
Hello, really good library
is possible get all data from a screen... (not only the select box)

thanks
 

Urishev

Member
Licensed User
Longtime User
Hello, if you explain how to get all the data from the screen, (not only the select box)
I'd be happy.
Thanks
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hello, if you explain how to get all the data from the screen, (not only the select box)
I'd be happy.
Thanks
I think you should maybe see if someone will be prepared to do a fully wrapped B4A library for this. I have done a "shortcut" wrapper only.
 

SMOOTSARA

Active Member
Licensed User
Longtime User
Try with the attached library files (V1.03). Have added the following options as far as focus is concerned:




hi everybody ,
i tried to use this library but i get this error , i use b4a 9.30 , and i use all the library that you said , but still when i click on the detect tex i get this error.

thanks in advance





B4X:
Logger connected to:  HUAWEI ALE-L21
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/design/widget/Snackbar;
    at com.google.android.gms.samples.vision.ocrreader.OcrCaptureActivity.onCreate(OcrCaptureActivity.java:109)
    at android.app.Activity.performCreate(Activity.java:6367)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)
    at android.app.ActivityThread.access$900(ActivityThread.java:165)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:5621)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.Snackbar" on path: DexPathList[[zip file "/data/app/JHS.AndroidVisionOCR-1/base.apk"],nativeLibraryDirectories=[/data/app/JHS.AndroidVisionOCR-1/lib/arm64, /vendor/lib64, /system/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    ... 13 more
    Suppressed: java.lang.ClassNotFoundException: android.support.design.widget.Snackbar
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 14 more
    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
 

David Hawkins

Active Member
Licensed User
Longtime User
Hi I hope somebody can help me. I have followed Johan's instructions to the letter to get the OCR app working, when I compile the app it compiles without any error but it does not appear to copy the file to my device Sony Experia ZX and therefore the rapid debugger fails to connect to process, I have tried restarting the ADB server to no avail.

All other apps I have created work perfectly just not this one.

I am using AndroidVisionOCR version 1.02 and B4A version 9.50 libraries.

I am baffled???

Does anybody have any ideas.

OK it appears that you cannot have a debug key when using this library. I added a proper key and hey presto all is well in the world.
 
Last edited:
Top