OCR Solutions in Android (Tesseract)

Source: Internet
Author: User

There are roughly two solutions for OCR in Android applications, and the most are tesseract. My little brother is here to write down the last two days of my thinking, such as defective, welcome to shoot Bricks:
There are two solutions, one is to use the tesseract Cloud-service, the clock is to send the picture information to the cloud, and then get the image analysis data, the second is to use the network, the localization analysis of the picture information. I'll just say the second kind, the first one I'll give you a link at the end (the article is very good).
Search first is to download tesseract native Android Library. There are two links that you can choose which link:
A.SVN Checkout Http://tesseract-android-tools.googlecode.com/svn/trunk/tesseract-android-tools. (If you can't checkout, don't say it to the official: http://code.google.com/p/tesseract-android-tools/)
B. Maybe one of the above download after compiling some people will encounter some problems, such as the JGEP library cannot be found, the compilation is unsuccessful. So with this project: Git cloneGit://github.com/rmtheis/tess-two.git(There is too much content in this package, but it saves so many libraries.)
Let's start with the first source download: After the successful download, open the Readme file, make the following changes (below):
git clone git://android.git.kernel.org/platform/external/jpeg.git libjpeg
Modified to:
git clone https://android.googlesource.com/platform/external/jpeg libjpeg
N
  
For the second source download, because there is no Readme file, 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

Finally two get the libs inside the so file you want and SRC inside the encapsulation class of the so file. This is what we use to develop the stuff.
Then create a new 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", "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 this 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 with the SetImage function, I found out that the native function Nativesetim Agepix is never accessed. I have attached the Logcat output below to show this 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 run the program with joy, you find that things are not as simple as you think. This file must be used in a language pack. How else would you match it? Consider also:
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





================================================================================

Https://github.com/rmtheis/tess-two




OCR Solutions in Android (Tesseract)

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.