Android Tutorial [Example] Add OCR features to your Android application

Status
Not open for further replies.
This example demonstrates how you can use an OCR web service to recognize text in camera images.

The program allows you to take a picture and then send it to OCRWebService.

OCRWebService returns the recognized text in various formats. This is not a free service, however they offer a free trial and their prices are not high.

Camera picture:

SS-2013-03-07_16.27.37.png


Recognized html:

SS-2013-03-07_16.26.38.png


While the response is not perfect, the text can still be extracted.

The web service is a SOAP web service. The request is created by loading a template file and changing its fields. The response is then parsed and the data file is extracted.

In order to run this example you need to create an account and enter your user name and license key.

Updated example written by DonManfred: https://www.b4x.com/android/forum/t...-android-application.27080/page-2#post-656725
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Tested it, got the same error message.
If i uncommented the Log(...) part, the eroor message is another:

B4X:
B4A Version: 8.80
Parse den Code.    (0.04s)
Building folders structure.    (0.13s)
Kompiliere den Code.    (0.09s)
Kompiliere Layoutcode.    (0.01s)
Organisiere Libraries.    (0.00s)
Generiere R Datei.    (0.21s)
Kompiliere generierten Java Code.    Error
B4A line: 16
hc.Initialize(\
javac 1.8.0_66
src\b4a\example\httputils2service.java:208: error: cannot access ClientProtocolException
_hc.Initialize("hc");
              ^
  class file for org.apache.http.client.ClientProtocolException not found
 

GMan

Well-Known Member
Licensed User
Longtime User
I removed the HttpUtils2Service.bas and used the HttpUtils2 lib.

NOW the code compiles, but the App could not be installed on the device ;-)
 

GMan

Well-Known Member
Licensed User
Longtime User
Thougth it could have the reason in the "age" of the file (its from 2013 and many things have changed since them)
Added RuntimePermissions - no success
Removed the depreceated HttpUtil2 lib and chooses OKHttpUtils2 and removed the httpJob.bas - no success

Now i won't waste my time anymore with this - would be a nice solution to build some apps for business people, who often have these ugly bills to "store" FOR TAX etc.
Sun is shining here in DE, going for a short shopping trip to the netherlands now - just some minutes away
 

DonManfred

Expert
Licensed User
Longtime User
Thougth it could have the reason in the "age" of the file (its from 2013 and many things have changed since them)
Added RuntimePermissions - no success
Removed the depreceated HttpUtil2 lib and chooses OKHttpUtils2 and removed the httpJob.bas - no success

Now i won't waste my time anymore with this - would be a nice solution to build some apps for business people, who often have these ugly bills to "store" FOR TAX etc.

this example is based on Camera2 Example and some parts from the old example.

Basically it is this Code

B4X:
        Dim userbase64 As String = "USERNAME:code"
        Dim job As HttpJob
        job.Initialize("ocr", Me)
        job.PostBytes("https://www.ocrwebservice.com/restservices/processDocument?gettext=true&language=english,german", ReadFile(VideoFileDir, "1.jpg"))
        job.GetRequest.SetHeader("Authorization", "Basic "&su.EncodeBase64(userbase64.GetBytes("UTF8")))
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            'File.WriteString(File.DirRootExternal, "JobResult.txt", j.GetString)
            Log(j.GetString)
        Else
            Log(j.ErrorMessage)
        End If
        j.Release

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
FULL
** Activity (main) Resume **
Start success: true
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Start success: true
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
{"ErrorMessage":"","OutputInformation":null,"AvailablePages":23,"ProcessedPages":1,"OCRText":[[". • fitbit charge: ADVANCED FITNESS TRACKER "]],"OutputFileUrl":"","OutputFileUrl2":"","OutputFileUrl3":"","Reserved":[],"OCRWords":[],"TaskDescription":null}
Picture taken: (Bitmap): 1920 x 1080, scale = 1,00

This is the Picture i´ve taken
WhatsApp Image 2019-04-13 at 13.57.53.jpeg

Have fun :)
 

Attachments

  • OCREx.zip
    16 KB · Views: 776

GMan

Well-Known Member
Licensed User
Longtime User
Works fine so far but:
{"ErrorMessage":"","OutputInformation":null,"AvailablePages":24,"ProcessedPages":1,"OCRText":[["Einlieteruogsboleg / Ouittung Bit Bel gut aofbewahren eg Deutsche Post AG 47918 Tönisvorst 82026149 7301 08.04.2019 15:34 •., ...... ..... Sendungs n Mtn : &op fangol and E Int ..... , „ , Gesu turosatz (Brutto) Zah lb e tr ag: ervicentamer International 228 4333112 800 - 18:00 Uhr RC326592082 DE FR tornot • 0444. deut6chopcoi t rietstatus ?len Dank fir Ihren Besuch. .e Deutsche Post AG *0,00 EUR *0,00 EUR "]],"OutputFileUrl":"","OutputFileUrl2":"","OutputFileUrl3":"","Reserved":[],"OCRWords":[],"TaskDescription":null}

The numbers and the text is recognized (as it looks), but has another issue

And an additional positive message:
Picture taken: (Bitmap): 1088 x 1920, scale = 1,00
 
Last edited:

Tadeu Botelho

Member
Licensed User
Longtime User
Thank you all for the help!
I came to the conclusion not to use this example because there are many bugs to be fixed.
Does anyone know of any professional API service available for B4A?
I would like to use a trust API for a very serious project in healthcare. I need an API that is not free. Because the free ones I've analyzed do not work correctly.
But I'm having trouble locating a service from some OCR company.
Hug to everyone.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I came to the conclusion not to use this example because there are many bugs to be fixed.
The issue you experienced is not a bug. This example was written when the old HttpUtils2 was the recommended way to make http requests. All that you need to do is to remove it and switch to OkHttpUtils2.

It is also possible that the 3rd party web service has changed since this example was written (in 2013). Start a new thread for any question you have.
 
Status
Not open for further replies.
Top