eurvaq
Last Updated: December 08, 2016
·
50.29K
· itseranga
892414 10151468467596730 1847316210 o

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)

34 Responses
Add your response

15810
C2da435cced21aa3af99e1f60347d6f0

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

over 1 year ago ·
15811
892414 10151468467596730 1847316210 o

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 ·
15812
C2da435cced21aa3af99e1f60347d6f0

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

over 1 year ago ·
15813
C2da435cced21aa3af99e1f60347d6f0

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 ·
15819
892414 10151468467596730 1847316210 o

What is the gradle version you have installed?

over 1 year ago ·
15821
C2da435cced21aa3af99e1f60347d6f0

The newest that comes with AS 0.8.

over 1 year ago ·
16009
59207d42a17b0f5dcff16f42eba9788b

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

over 1 year ago ·
16010
892414 10151468467596730 1847316210 o

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 ·
17047
None

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 ·
17050
892414 10151468467596730 1847316210 o

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 ·
17749
None

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 ·
17878
None

Thank you for posting this. Very useful!

over 1 year ago ·
17949
None

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 ·
18131
None

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 ·
18345
None

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 ·
18670
None

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 ·
18672
None

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

over 1 year ago ·
18826
Profile

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 ·
18861
None

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 ·
18919
None
18932
None

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

over 1 year ago ·
19286
None

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

over 1 year ago ·
21114
0 y5gdzlsx5infnma yiyvz1dtbhkindo pfwnz zczlk1wwdt  slut2uwmrpzo0oxqxcvvol8g9e

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 ·
21131
None

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 ·
21856
W  bnkvq normal

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 ·
21945
0 hw3lf00jhhfjqyfccfr9fyf4gil9qsmcgizsfyjeja5fmombu7adwpwq25ahlg7r3eirhrimj mw

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 ·
22158
None

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 ·
23363
None

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 ·
24891
None

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 ·
26250
None

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 ·
26445
None

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 ·
27044

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 ·
28241

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

does anyone know how to fix this...

5 months ago ·
28367

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...

4 months ago ·