OCR solution (tesseract) in Android)

Source: Internet
Author: User

There are roughly two OCR solutions for Android applications, and the most popular one is tesseract. Here I will write down my solutions for the last two days. If you have any defects, please click here:

There are two solutions. One is to use tesseract cloud-service, which sends the image information to the cloud and obtains the image analysis data. The other is not to connect to the Internet, localized Analysis of image information. Let me talk about the second one. I will give you a link at the end (the article is good ).

First, download tesseract native Android library. There are two links here. You can select any link:

A. SVN checkout http://tesseract-android-tools.googlecode.com/svn/trunk/ Tesseract-Android-tools. (If you can not checkout to, don't say nonsense to the official up and down: http://code.google.com/p/tesseract-android-tools)

B. Some people may encounter some problems after the above download. For example, the jgep library cannot be found and the compilation fails. So with this project: git clone git: // github.com/rmtheis/tess-two.git (this package contains too much content, but it also saves so many libraries)

Here we first use the first source download: After the download is successful, open the README file and make the following changes ):

git clone git://android.git.kernel.org/platform/external/jpeg.git libjpeg
To:
git clone https://android.googlesource.com/platform/external/jpeg libjpeg
Ndk-build // This compilation should be compiled in the JNI folder

  

For the second source download, because there is no readme file in it, the Operation Command is as follows:

cd <project-directory>/tess-two
export TESSERACT_PATH=${PWD}/external/tesseract-3.01
export LEPTONICA_PATH=${PWD}/external/leptonica-1.68
export LIBJPEG_PATH=${PWD}/external/libjpeg
ndk-build
android update project --path .
ant release

In the end, both get the so file in the libs you want and the so file encapsulation class in the SRC. This is what we use for development.

Then create a project with the following code:

public class MainActivity extends Activity {
private static final String TAG = "MainActivity ...";

private static final String TESSBASE_PATH = "/mnt/sdcard/tesseract/";
private static final String DEFAULT_LANGUAGE = "eng";
private static final String IMAGE_PATH = "/mnt/sdcard/test1.jpg";
private static final String EXPECTED_FILE = TESSBASE_PATH + "tessdata/" + DEFAULT_LANGUAGE
+ ".traineddata";

private TessBaseAPI service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testOcr();

}

public void testOcr(){
mHandler.post(new Runnable() {

@Override
public void run() {
Log.d(TAG, "begin>>>>>>>");
ocr();
//test();
}
});

}
public void test(){
// First, make sure the eng.traineddata file exists.
/*assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to "
+ EXPECTED_FILE, new File(EXPECTED_FILE).exists());*/
final TessBaseAPI baseApi = new TessBaseAPI();
baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE);
final Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.test);
//digits is a .jpg image I found in one of the issues here.
ImageView img = (ImageView) findViewById(R.id.image);
img.setImageBitmap(bmp);//I can see the ImageView. So we know that it should work if I sent it to the setImage()
baseApi.setImage(bmp);
Log.v("Kishore","Kishore:Working");//This statement is never reached. Futhermore, on putting some more Log.v commands in the setImage function, I found out that the native function nativeSetImagePix is never accessed. I have attached the Logcat output below to show that it is not accessed.

String outputText = baseApi.getUTF8Text();
Log.v("Kishore","Kishore:"+outputText);
baseApi.end();
bmp.recycle();
}

protected void ocr() {

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(IMAGE_PATH, options);

try {
ExifInterface exif = new ExifInterface(IMAGE_PATH);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

Log.v(TAG, "Orient: " + exifOrientation);

int rotate = 0;
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}

Log.v(TAG, "Rotation: " + rotate);

if (rotate != 0) {

// Getting width & height of the given image.
int w = bitmap.getWidth();
int h = bitmap.getHeight();

// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(rotate);

// Rotating Bitmap
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
// tesseract req. ARGB_8888
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
}

} catch (IOException e) {
Log.e(TAG, "Rotate or coversion failed: " + e.toString());
}

ImageView iv = (ImageView) findViewById(R.id.image);
iv.setImageBitmap(bitmap);
iv.setVisibility(View.VISIBLE);

Log.v(TAG, "Before baseApi");

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

Log.v(TAG, "OCR Result: " + recognizedText);

// clean up and show
if (DEFAULT_LANGUAGE.equalsIgnoreCase("eng")) {
recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " ");
}
if (recognizedText.length() != 0) {
((TextView) findViewById(R.id.field)).setText(recognizedText.trim());
}
}
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {

};
};
}

When you are very happy to run the program, you find that things are not as simple as you think. This file must use a language pack. Otherwise, how do you match? Think too:

adb shell mkdir /mnt/sdcard/tesseract
adb shell mkdir /mnt/sdcard/tesseract/tessdata
adb push eng.traineddata /mnt/sdcard/tesseract/tessdata/eng.traineddata
adb shell ls -l /mnt/sdcard/tesseract/tessdata
ls -l bin/tesseract-android-tools-test.apk
adb install -r -s bin/tesseract-android-tools-test.apk
adb shell am instrument -w -e class com.googlecode.tesseract.android.test.TessBaseAPITest com.googlecode.tesseract.android.test/android.test.InstrumentationTestRunner

The above amount of ENG. traineddata. You can search for networks. (Attachment, I still don't know how to upload attachments)

Final result (in fact, the resolution result is: 44 m> <9. But I don't know that character ):

References:

Http://wolfpaulus.com/journal/android-and-ocr

Http://labs.makemachine.net/2010/03/simple-android-photo-capture/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.