B4A Library OCR - Extracting text from a bitmap using the Play Services Vision API

This request comes from here:
https://www.b4x.com/android/forum/threads/ocr-offline-on-screen.84867/#post-538774

The attached project extracts text from a bitmap that you can pass to the library making use of the Android Vision API.

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


#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.
 
    Dim mvbm As MobileVisionBitmap

    Private Button1 As Button
    Private iv1 As ImageView
    Private bm As Bitmap
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")
    bm.Initialize(File.DirAssets,"saying1.png")   'saying.png and saying1.png are in the /Files folder of the B4A project
    iv1.Bitmap = bm
 
    mvbm.Initialize("mvbm")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button1_Click
 
    mvbm.decodeBitmap(bm) 
    bm = Null
    bm.Initialize(File.DirAssets,"saying1.png")   'saying.png and saying1.png are in the /Files folder of the B4A project
    iv1.Bitmap = bm
 
End Sub

Sub mvbm_blocks_result(blocks As String)
 
    Log("B4A Blocks = " & blocks)
 
End Sub

Sub mvbm_lines_result(lines As String)
 
    Log("B4A Lines = " & lines)
 
End Sub

Sub mvbm_words_result(words As String)
 
    Log("B4A Words = " & words)
 
End Sub

Sub mvbm_error_result(error As String)
 
    Log("B4A ERROR = " & error)
 
End Sub

Take note of the B4A 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="5" android:targetSdkVersion="22"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

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" />)

If you don't have the Android Vision dependencies installed then you will need an initial internet connection for the dependencies to be installed. After that an internet connection will not be required.

This is version 1.00. It will expire on 31 October 2017.


Bitmap loaded into the imageview.
1.png



B4A Log when clicking on button Read Text
2.png
 

Attachments

  • MobileVisionBitmap.xml
    1.2 KB · Views: 1,856
  • MobileVisionBitmap.jar
    10 KB · Views: 932
  • b4aMobileVisionBitmap.zip
    130 KB · Views: 990
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
@Erel Please remove this library. the expiration date is already gone. Today i wasted my time downloading these files, setting up and running. Finally i recieved the expiration message. There is no use of this post since the library is already expired that has hardcoded date
At that time there was method in the madness. A challenge for those that have not used SLC to compile a Java project to a B4A library. The Java code is in post #12 without any date restrictions. Give it a go....always good to learn new things (me thinks). See post #21. The lib files without any restrictions as far as what I can recall.
 
Last edited:
Top