Recently I was playing with OCR library by google called as “Tesseract” (cool name for a library!).

It was a fun experience. This post shows how you can make a simple OCR app in Android using Tesseract.
We will be using Tess-Two a fork of Tesseract with some additional tools like Liptonica which is an image processing library.
If you want an even easier way to get started with OCR on Android you can try this library built by me. Easy OCR Library. Usage instructions are in the ReadMe.md file there.
Anyways, moving forward I am using Android Studio on Ubuntu 64 bit machine here.
Step 1 :
Clone the library Tess-Two.
git clone git://github.com/rmtheis/tess-two tess
Step 2 :
Now we need to build the library.
For building we will need Android NDK.
cd tess
cd tess-two
ndk-build
android update project --path .
ant release
Building may take some time so be patient. Don’t press ctrl+c too soon 😛 .
Step 3 :
Yay! Time to use the library in Android Project.
Copy the tess/tess-two folder into the root folder of your application project.
Step 4 :
In the tess-two folder you just pasted. Add build.gradle file as Android Studio uses gradle build system.
Add following gradle script in the file.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 22
}
sourceSets.main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
jniLibs.srcDirs = ['libs']
}
}
Step 5 :
Add the following line in project.settings file.
include ':tess-two'
Step 6 :
Now we have successfully included the Tess-Two library in our project and we are ready to use it.
First we need to capture the picture itself. You can use something like this code sample taken from Easy OCR Library.
public void takePicture(){
Intent e = new Intent("android.media.action.IMAGE_CAPTURE");
this.filePathOriginal = FileUtils.getDirectory(this.directoryPathOriginal) + File.separator + Calendar.getInstance().getTimeInMillis() + ".jpg";
e.putExtra("output", Uri.fromFile(new File(this.filePathOriginal)));
startActivity(e);
}
Or you can find the code here.
We will also downscale the image a little so that the recognition is fast.
You can use following code sample from again Easy OCR Library
private Bitmap getBitmapFromPath() {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(this.filePath, bmOptions);
return bitmap;
}
Step 7 :
Final step. Recognize the text using the library API.
private String scanImage(){
TessBaseAPI baseApi = new TessBaseAPI();
Log.d(Config.TAG, "Data path : " + FileUtils.getDirectory(this.directoryPath));
baseApi.init(FileUtils.getDirectory(this.directoryPath) + "/", this.trainedDataCode);
baseApi.setImage(this.mBitmap);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();
return recognizedText;
}
Again I would recommend using the Easy OCR Library if you are having facing any problem.
That library has many features :
- Very easy setup.
- Handles all the image processing part in a background thread.
- Provides very interface with relative callbacks for the functions of the library.
Leave a Reply