B4A Library OCR - OCR Reader Complete

A wrap for this Github project. Touch a green box with yellow text when the scanner is active and it will speak out the text in the box (using the Android Text-To-Speech Engine).

You need B4A V6.00 to run this project.

Change the Java code to your liking. Read the postings in this thread - it will give the a very good idea of how to set up this project.

1.png


Posting the following:
1. B4A library files - copy then to your additional library folder
2. B4A sample project
3. The Java code
4. OCRCompleteRes.zip - extract it and copy the folder to be on the same folder level as that of the /Files and /Objects folders of the B4A project
5. resource.zip - extract it and copy the folder to be on the same folder level as that of the /Files and /Objects folders of the B4A project
6. The .apk file generated by the B4A project is here should you want to test it - https://www.dropbox.com/s/jio7fc29ezclqxq/b4aOCRComplete.apk?dl=0
7. This is the link to Other library files (jars) that you either need in your additional library folder or in the libs folder of the library folder structure to compile the Java code - click on the red text to download.


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/

Take note of the b4A project manifest file:
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.OcrCaptureActivity"
            android:label="Wrapped by Johan Schoeman">
        </activity>) 
     
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")

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

#AdditionalRes: ..\resource
#AdditionalRes: ..\OCRCompleteRes


'#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 OCRReaderComplete
 
    Private CheckBox1 As CheckBox
    Private CheckBox2 As CheckBox
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
 

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

Library as it is at present:
OCRReaderComplete
Author:
Github: Benjamin Johns, Wrapped by: Johan Schoeman
Version: 1
  • OCRReaderComplete
    Fields:
    • 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]
    • UseFlash As Boolean [write only]
 

Attachments

  • b4aOCRComplete.zip
    9 KB · Views: 993
  • OCRCompleteRes.zip
    30.9 KB · Views: 938
  • resource.zip
    423 bytes · Views: 925
  • OCRReaderCompleteLibFiles.zip
    36.6 KB · Views: 953
  • TheJavaCode.zip
    28.5 KB · Views: 836
Last edited:

susu

Well-Known Member
Licensed User
Longtime User
I tried your apk file. It worked great however there's no recognized text in textbox?
 

Johan Schoeman

Expert
Licensed User
Longtime User
I tried your apk file. It worked great however there's no recognized text in textbox?
Do you see no text at all? If so, then the OCR dependencies have probably not downloaded as yet or there could be a storage related issue on your device. The dependencies should automatically download when you start the app. It takes a while for it to happen. Device must be connected to the Internet. Read the below (taken from the Java code):


if (!textRecognizer.isOperational()) {
// Note: The first time that an app using a Vision API is installed on a
// device, GMS will download a native libraries to the device in order to do detection.
// Usually this completes before the app is run for the first time. But if that
// download has not yet completed, then the above call will not detect any text,
// barcodes, or faces.
//
// isOperational() can be used to check if the required native libraries are currently
// available. The detectors will automatically become operational once the library
// downloads complete on device.
 
Last edited:

susu

Well-Known Member
Licensed User
Longtime User
I see recognized text in green box, I can hear it too. But when I back to main activity, there's nothing in TextBox below Start Scan button.
 

Johan Schoeman

Expert
Licensed User
Longtime User
I see recognized text in green box, I can hear it too. But when I back to main activity, there's nothing in TextBox below Start Scan button.
OK, now I understand. I have not added code to the wrapper to return the text in this project. I can do so. Will do it sometime and then post an updated library. The text box is there from the other OCR project that I have posted as that project uses the text box to display the text that was touched. I just copied the B4A project from the other OCR project, renamed it, and did some mods for this project.
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Hello Johan ;
Thank you for sharing;
I wan t to try it. So App cant work OCR
I cant see like your yellow word or green squares. it has not any actions.

app screenshots:
View attachment 49998 View attachment 49999

Library folder:
View attachment 50000

Project folders/files

View attachment 50001
Did you manage to get this working? Are you running my project in post #1 of this thread or your own project? In case you run your own project then you need to take note of the B4A manifest file of the project in post #1
 

roberto64

Active Member
Licensed User
Longtime User
hi johan, compiler I ristituisce this error as in the image.
regards
 

Attachments

  • Compileimg.png
    Compileimg.png
    20.7 KB · Views: 422

roberto64

Active Member
Licensed User
Longtime User
Hi Tigrot, I solved the problem by putting in the right libs libraries, and in "-b4aignore: com" anyway thanks for your time
regards
 

Xenno

Member
Licensed User
Great work, Johan!

I'm monitoring this thread. I am expecting you or anyone else find a way how to just grab all visible text without the need to tap.

Thanks in advance.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Based on the request here:
https://www.b4x.com/android/forum/threads/ocr-with-another-font-style.83852/

Added the ability to change the font style (in this case digital-7.ttf). Copy the attached lib files to your additional library folder. Copy digital-7.ttf (once unzipped) to your B4A project's /Files folder and then add it to the Files Manager tab of the B4A project's IDE.

Then change your B4A code to this:

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

#AdditionalRes: ..\resource
#AdditionalRes: ..\OCRCompleteRes


'#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 OCRReaderComplete
  
    Private CheckBox1 As CheckBox
    Private CheckBox2 As CheckBox
'    Dim bm As Bitmap
  
    Dim myfont As Typeface
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")
    myfont = Typeface.LoadFromAssets("digital-7.ttf")
  
    avocr.OCRtypeFace = myfont
  
    avocr.OCRtextColor = Colors.Red                                    'the color of the text while OCR takes place
    avocr.OCRtextSize = 35.0                                              'the size of the text while OCR take place
    avocr.OCRrectangleColor = Colors.Transparent
    avocr.OCRrectangleWidth = 2.0
'    bm.Initialize(File.DirAssets,"takepicturebutton.png")
'    avocr.TakePictureImage = bm
  

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

I get this:

1.png
 

Attachments

  • OCRReaderComplete.xml
    2.8 KB · Views: 425
  • OCRReaderComplete.jar
    43.4 KB · Views: 427
  • digital-7.zip
    10.5 KB · Views: 429

microbox

Active Member
Licensed User
Longtime User
Hi Johan, I really appreciated the time for the doing my request(even I know you're busy)..but my apology if I was not clear regarding my issue. I'm using your library to read on gasoline station lcd display like the one below
LCDFont2.png

When I tried scanning it, it was not successful. I was hoping there is a way for a solution for this type of font?

Best regards,
joe
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan, I really appreciated the time for the doing my request(even I know you're busy)..but my apology if I was not clear regarding my issue. I'm using your library to read on gasoline station lcd display like the one below
View attachment 59731
When I tried scanning it, it was not successful. I was hoping there is a way for a solution for this type of font?

Best regards,
joe
You can investigate how to do it and then let me know. I have spent in excess of two hours on this and not prepared to look into this any further. If it does not get recognised then it is in the vision library - not in the wrapper

You should make sure that you are VERY clear on what it is that you want to accomplish when posting a request. Please don't send us down the wrong path - there are hours of work going into this for some of us that does this part time.
 
Last edited:

microbox

Active Member
Licensed User
Longtime User
You can investigate how to do it and then let me know. I have spent in excess of two hours on this and not prepared to look into this any further. If it does not get recognised then it is in the vision library - not in the wrapper

You should make sure that you are VERY clear on what it is that you want to accomplish when posting a request. Don't send us down the wrong path.
Yes..I will next time. Again please accept my sincere apology. And again thank you for taking the time.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Last edited:
Top