You are on page 1of 11

28/1/2018 Tesseract with andoird and gradle (Example)

Coderwall Tips Sign In or Up

Last Updated: January 24, 2018 · itseranga

Tesseract with andoird and gradle


GRADLE ANDROID TESSERACT

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

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 1/11
28/1/2018 Tesseract with andoird and gradle (Example)

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

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 2/11
28/1/2018 Tesseract with andoird and gradle (Example)

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)

Written by eranga bandara

 Recommend

 Say Thanks

 Update Notifications Off

 Respond

34 Responses

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

over 1 year ago · 

itseranga
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 · 

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

over 1 year ago · 

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 3/11
28/1/2018 Tesseract with andoird and gradle (Example)

harbachek
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 · 

itseranga
What is the gradle version you have installed?

over 1 year ago · 

harbachek
The newest that comes with AS 0.8.

over 1 year ago · 

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

over 1 year ago · 

itseranga
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 · 

tonymalghem
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 · 

itseranga
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 · 

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 4/11
28/1/2018 Tesseract with andoird and gradle (Example)

blizzshot
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 · 

sagarwal10
Thank you for posting this. Very useful!

over 1 year ago · 

bravevu
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 · 

italomaia
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 · 

shashankdass
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 · 

matts
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 · 

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 5/11
28/1/2018 Tesseract with andoird and gradle (Example)

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

over 1 year ago · 

rijulg
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 · 

p-rothen
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 · 

xxkfjfredxx
Hi this example is best
http://www.codeproject.com/Tips/840623/Android-Character-Recognition

over 1 year ago · 

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

over 1 year ago · 

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

over 1 year ago · 

AnCh
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 · 

jyjy123
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 · 

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 6/11
28/1/2018 Tesseract with andoird and gradle (Example)

wasif_514
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 · 

sandeepgurram
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 · 

ekilla
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 · 

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 7/11
28/1/2018 Tesseract with andoird and gradle (Example)

meta0102
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 · 

Tazonis
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 · 

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 8/11
28/1/2018 Tesseract with andoird and gradle (Example)

yvtheja
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 · 

AnithaGK
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 · 

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 9/11
28/1/2018 Tesseract with andoird and gradle (Example)

shivanraptor
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 · 

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

does anyone know how to fix this...

over 1 year ago · 

rakeshvasal
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 · 

 Filed Under

Android Development Tips

 Awesome Job See All Jobs 

Fullstack Javascript Engineer


Contentful GmbH · Berlin · Full Time

Post a job for only $299

@coderwall   Contact Privacy Terms Copyright 2018

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 10/11
28/1/2018 Tesseract with andoird and gradle (Example)

https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle 11/11

You might also like