Last Updated: October 14, 2021
·
86.5K
· itseranga

Tesseract with andoird and gradle

About tesseract

Tesseract is an Open Source OCR library

More info - https://code.google.com/p/tesseract-ocr/

Tesseract in android

If you are using tesseract library in android application, there is a previously built android application project exists(A fork of tesseract tools for android).

More info - https://github.com/rmtheis/tess-two

You have to build this tess-two project with android-ndk and then add the build project as library project to you android project. But this tess-two library not supports for build with gradle android projects. So we have to create gradle project with tess-two(configure tesst-two with gradle)

Configure tess-two with gradle

There are few steps you need to follow when integrating tesst-two with gradle android project.

  • Create a libraries folder underneath your project's main directory. For example, if your project is FirstProject, you would create a FirstProject/libraries folder

  • Now copy the entire tess-two directory into the libraries folder you just created.

  • Delete the libs folder in the tess-two directory. If you like, delete the project.properties, build.xml, .classpath, and .project. files as well. You don't need them.

  • Create a build.gradle file in the tess-two directory, (I have created a build.gradle file, you can just reuse it). Following is my build.gradle file

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}

apply plugin: 'android-library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
    }

    sourceSets.main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        res.srcDirs = ['res']
        jniLibs.srcDirs = ['libs']
    }
}

https://github.com/erangaeb/dev-notes/blob/master/tesseract/build.gradle

  • Edit your settings.gradle file in your application’s(FirstProject/settings.gradle) main directory and add this line
include ':libraries:tess-two'
  • Then sync the project in Android Studio and add the new tess-two library as module dependency to you main project(after sync tess-two library should appear as a module, you can add it to your project from project settings in android studio)

36 Responses
Add your response

How do we use it then, can you give an example?

over 1 year ago ·

Hi,

Following is a really good tutorial about tesseract android integration.
http://wolfpaulus.com/jounal/android-journal/android-and-ocr/
If you are using gradle to build your android app, you can use the build script that I have posted above(to build tesseract library as an android library project).

over 1 year ago ·

Thank you I found it already and I think it is really good.

over 1 year ago ·

What should there be if it says that a newer version of gradle should be used here com.android.tools.build:gradle:0.9.+ (at least 12.0) change it to 12.0+?

over 1 year ago ·

What is the gradle version you have installed?

over 1 year ago ·

The newest that comes with AS 0.8.

over 1 year ago ·

i did everything as you described, how can i import the library into an activity?

over 1 year ago ·

TessBaseAPI baseApi = new TessBaseAPI();
baseApi.setDebug(true);
baseApi.init(DATA_PATH, LANG);
baseApi.setImage(bitmap);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();

(It described in the tutorial that I have posted in previous comment)

over 1 year ago ·

Hi!

I did everything that you said, but unfortunately, my app keeps crashing and I have this error in logcat : Couldn't load lept from loader dalvik.system.PathClassLoader[...]: findLibrary returned null

Do you have any idea what can cause that?

over 1 year ago ·

Did you build the tesseract library with android-ndk ??
Seems like "liblept.so" (shared library) file can not found on specific library path

over 1 year ago ·

Hi eranga,

Nice guide as i am able to blend tesseract with my android studio project, just one issue when i try to make module it kept on saying "Error:(14, 40) error: package com.googlecode.tesseract.android does not exist" even though i've set it as a dependency and import it to my code

over 1 year ago ·

Thank you for posting this. Very useful!

over 1 year ago ·

Thank you. Follow all steps, it is correct.
If someone has project including tess-two in Eclipse, now convert to Android Studio, should remove directory tess-two from generating. In build.gradle, remove:
compile project(':tesstwo')

over 1 year ago ·

I followed the steps and got this error:
java.lang.UnsatisfiedLinkError: Couldn't load lept from loader dalvik.system.PathClassLoader[dexPath=/data/app/br.com.notasocial.cupom-38.apk,libraryPath=/data/app-lib/br.com.notasocial.cupom-38]: findLibrary returned null

thrown by "new TessBaseAPI();"
Folder structure:
http://www.italomaia.com/media/uploads/tree01.png
http://www.italomaia.com/media/uploads/tree02.png

over 1 year ago ·

I followed all the steps. I am still getting the following error on running my app.

Error:(15, 40) error: package com.googlecode.tesseract.android does not exist

Any ideas are greatly appreciated.

over 1 year ago ·

Hi.... Even i got the same error - java.lang.UnsatisfiedLinkError: Couldn't load lept from loader.
Is there any particular reason why we should delete the libs folder from the tess-two library?

over 1 year ago ·

Issue Resolved :) .libs folder shouldn't be removed

over 1 year ago ·

After spending a whole day on trying to compile, build and redoing it all many times, Finally a solution that works. Thanks a lot Mr. bandara

over 1 year ago ·

I dont' find "project settings" in android studio, pls help. I'am stuck at the last step. Did you compile tess-two with 32/64bits ndk? Thanks D:

over 1 year ago ·

https://code.google.com/p/android/issues/detail?id=74132 does anyone have a solution to this problem?

over 1 year ago ·

I solved my problem above, Ctrl+alt+shift+s ->Dependencies

over 1 year ago ·

Use ndk-build to build the tess-two library. If you have already built it before copying to libraries, then don't delete the libs folder. Rest everything worked like charm. Thanks a lot.

over 1 year ago ·

Hi, after adding it as a module dependency, it shows "Error: Configuration with name 'default' not found". May i know whats wrong? Everything was working fine before that. Thank you!

over 1 year ago ·

After adding library to settings.gradle file. I got following error:

Error:A problem occurred configuring project ':app'.

A problem occurred configuring project ':libraries:tess-two'.
java.io.FileNotFoundException: /home/usr/OCRApplication/libraries/tess-two/AndroidManifest.xml (No such file or directory)

To fix this issue, I modified the code as:

sourceSets.main {
    manifest.srcFile 'src/main/AndroidManifest.xml'
    java.srcDirs = ['src']
    resources.srcDirs = ['src']
    res.srcDirs = ['res']
    jniLibs.srcDirs = ['libs']
}

By this change, I got list of errors and build failed. Here are few errors:

Error:(16, 27) error: package org.piccolo2d.nodes does not exist
Error:(24, 19) error: package javax.swing does not exist
Error:(25, 19) error: package javax.swing does not exist
Error:(28, 3) error: cannot find symbol class JMenuItem
Error:(35, 43) error: cannot find symbol class JMenuItem
Error:(48, 19) error: cannot find symbol class JMenu

I spent a lot of time but no luck. What's wrong here.
Please reply ASAP. Thanks for prompt response.

over 1 year ago ·

thanks dude.. you made my day. But only correction to be made is we should not remove libs folder and target version in gradle file of our app and tess-two should be same.

over 1 year ago ·

The libs folder is REQUIRED for the app to work. If the libs folder is missing from the commit of tess-two obtained from github, Use ndk to build the tess-two project. But everything else works okay. Thanks.

over 1 year ago ·

hi, should i really add the whole tess-two directory to my project? after android-ndk the directory is about 400mb! thanks

over 1 year ago ·

Hi, am new to this and I would like to play with tess on android. So far I managed to build the tess two library with NDK10 but am stuck with android update project --path and ant release (android not a valid command). Ant has something to do with Apache so I guess I have to install ant, but what's update project command and what the path var means? Am already on a terminal inside tess two folder and after building with SDK I noticed the files changed close to an android build with a manifest and build xml file. Am using android studio so do I just have to copy tess two file to MyLib folder or do I have to go trough android update project --path and ant release first before I copy the folder to my MyLib folder?

Regards
a fellow senior programmer.

over 1 year ago ·

Use this if you are using latest Android Studio.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}

apply plugin: 'android-library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 21
}

sourceSets.main {
    manifest.srcFile 'AndroidManifest.xml'
    java.srcDirs = ['src']
    resources.srcDirs = ['src']
    res.srcDirs = ['res']
    jniLibs.srcDirs = ['libs']
}

}

It worked for me :)

over 1 year ago ·

I am following the steps mentioned above
In Configure tess-two with gradle tells to copy and paste the tess-two folder to my Project but when I paste, I get an error stating Cannot create class-file and none of my `.java' files get copied.
Where am I going wrong?

over 1 year ago ·

This article is outdated. tess-two can now be used directly with gradle: compile 'com.rmtheis:tess-two:5.4.1'. See this

over 1 year ago ·

Error:(4, 0) Plugin with id 'com.github.dcendents.android-maven' not found.

does anyone know how to fix this...

over 1 year ago ·

Hey,I have tried this and the project is workin...thanks for that but the output is bit weird.....
Like if the text contains 10 words only one word is correct rest all are just symbols or random letters..
Do we have to copy the eyes-two directory also or is it the whole tess-two master that we have to copy...

over 1 year ago ·

UFC 237: Namajunas vs Andrade</a> fight card at the Jeunesse Arena in Rio de Janeiro, Brazil. Sat, May 11 / 10:00 PM EDT. Checkout: https://twitter.com/fightingvideo

over 1 year ago ·

Great work did by you. Informative and problem-solving content that users can easily digest can work for all types of blogs. When a user finds something informative on a blog, he/she is more likely to come back from your side.
<a href="https://bolly4share.blogspot.com/2019/05/shazam-2019.html" rel="dofollow">Shazam Movie Download</a>
<a href="https://bolly4share.blogspot.com/2019/05/dumbo-2019.html" rel="dofollow">Dumbo Movie Donwload</a>
<a href="https://bolly4share.blogspot.com/2019/05/glass-2019.html" rel="dofollow">Glass Movie Download</a>
<a href="https://bolly4share.blogspot.com/2019/05/how-to-train-your-dragon-3-hidden-world.html" rel="dofollow">How to train your dragon3</a>
<a href="https://bolly4share.blogspot.com/2019/05/hellboy-2019.html" rel="dofollow">hellboy movie download</a>
<a href="https://bolly4share.blogspot.com/2019/05/the-curse-of-la-llorona-2019-hindi-720p.html" rel="dofollow">The curse of la movie download</a>
<a href="https://bolly4share.blogspot.com/2019/05/captain-marvel-2019-hindi-720p-480p.html" rel="dofollow">captain marvel movie download</a>
<a href="https://bolly4share.blogspot.com/2019/05/polaroid-2019-unrated-hd-720p-web-dl.html" rel="dofollow">polaroid movie download</a>
<a href="https://bolly4share.blogspot.com/2019/05/avengers-endgame-2019-hindi-clean-dual.html" rel="dofollow">Avengers endgame movie download</a>

over 1 year ago ·