How to build Tesseract OCR library for Android Studio?

Star On GitHub

If you ever tried to create an OCR app for Android you must have stumbled upon the OCR library by Google Tesseract. And then the problems began.

To use the library in your project you first need to build it. But building the library to be compatible with gradle, which is the new build system for Android projects is little not so easily stated anywhere in the library manual.

When I tried to build the library, it took me freaking 9 hours to figure all the how tos?

So, here I am helping you to save your precious hours. (Don’t waste these watching late night infomercials for god’s sake! ).

Here we go.

Step 1 : 

The first step. Download the NDK. That is used to build the library.

Download it from here. It is around 300+ MB so keep your net plan nourished.

Step 2 : 

Better way to go is to use  a fork of Tesseract, Tess-Two. Tess-Two can be found on GitHub.

Execute following commands to build the library Tess-Two using NDK.


git clone git://github.com/rmtheis/tess-two tess
cd tess
cd tess-two
ndk-build
android update project --path .
ant release

We can live without building eyes-two for this time.

Building will take few minutes. After you have successfully built tess-two, give yourself a little treat. You are almost done.

Step 3 :

Now you are ready to use the library in your Android project.

Copy the tess-two folder (tess/tess-two), in the main folder of the application project.

Suppose name of your application is “MyApp” copy the folder at “MyApp/”.

Step 4 :

Now its time to play the trick. The library was build using ANT. But Android projects use Gradle these days. Interesting…

We need to add a “build.gradle” file at location “tess-two/”.

The build file can be found here.

Then include following line in project.settings file.

include ':tess-two'

Step 5 : 

Build the project and you are just one step away from being done.

Step 6 : 

The most important step. After completing steps 1-5, throw your hands up in the air! 😀 😀 You are now done!

Ready to harness the power of OCR.

 


https://buttons.github.io/buttons.js

20 responses to “How to build Tesseract OCR library for Android Studio?”

  1. Hi I’v got some errors following the tutorial , can u help me plyz ?

    Like

    1. Sure. Email your issues to me.

      Like

      1. I am having an issue will building the library…. I am getting errors at “ant release” in compile and build is getting failed

        Like

  2. Wow, this one the latest tutorial that I found .

    May I know, which OS, NDK that you use ..

    I had stuck one week on the ndk-build ..

    make.exe *** Waiting for unfinished job.

    I am very newbie .. But this project is very important .. 😥 .

    Thank you in advance ..

    Like

  3. I am getting this error. Please help me

    @Below Line
    baseApi.init(“/mnt/sdcard/tesseract/tessdata/eng.trainedata”, “eng”);

    Error : Caused by: java.lang.IllegalArgumentException: Data path does not exist!

    Like

  4. i got this error: Error: The project either has no target set or the target is invalid.
    when i’m trying to do: android update project –path .

    Like

    1. I think you should add –target option .
      You can get your all targetId with command : android list targets
      Then android update project –path . –target 1 for example
      They are API versions

      Like

  5. I am not able to copy the files from tess-two into android studio. It isays : Can’t copy class files and gives an error. Please help !

    Like

  6. salam. i need a bit help actually, i got stuck on ur step 2 tutorial, i do really apreciate if u want to help me this tutorial just so important for my final project, i dont even know how to execute following commands to build the library Tess-Two using NDK, please help me get out of step 2, ur help means a lot. if u want to help me please contact this email : aghnia35@gmail.com . thankyou. salam.

    Like

  7. My friend;
    I have tried to follow all steps you have; however, I could not performed two steps below. I have searched my computer for “android.exe and ant.exe”; however, I have no luck. I have tried google for android.exe, but nothing there. I also update sdk platform tools and download apache ant, but nothing seem to work.

    Thanks

    android update project –path .
    ant release

    Like

  8. I have downloaded the android NDK to my UBuntu 14.04 LTS. What am I supposed to do after downloading NDK ?

    Like

  9. So the app doesn’t need internet connection to use? Can’t I make an online app using tesseract? I’m really confuse here, please help 😦

    Like

    1. To doesn’t need internet as everything is happening on device.
      You can make online app but then you’ll have to send image to your server which analyses the image.

      Like

  10. Hello priyank! I have followed all 6 steps successfully. I run project also. But I am not getting how to use ocr library. Please help. It’s running activity in my project. Nothing in ocr is happening. Guide please.

    Like

  11. Hello priyank! I have followed all 6 steps successfully. I ran project also. But I am not getting how to use ocr library. Please help. It’s running activity in my project. Nothing in ocr is happening. Guide please.

    Like

  12. Hello Priyank. I’m not able to execute the ant release command. I’ve set my path variables. It gives an error package android.support.annotation does not exist. What do I do?

    Like

  13. OMG , Amazing !! Everything Informative This Is really Ah Very good post. Taking the time and actual effort to produce a superb article… Thanks For Sharing

    Like

  14. Hmm it looks like your website ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I too am an aspiring blog blogger but I’m still new to everything. Do you have any tips for novice blog writers? I’d genuinely appreciate it.|

    Like

  15. Pretty! This has been an extremely wonderful article. Thank you for providing these details.|

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.