Tesseract for Android OCR

Source: Internet
Author: User
Tags image processing library

OCR belongs to the category of CV, that is, computer vision. Currently, apart from the leading boss of opencv, tesseract developed by HP, it is relatively easy to use, although it has been a long time, but now it is maintained by Google and hosted on Google Code.

 

Now the Android version is available
Address: http://code.google.com/p/tesseract-android-tools/

This version requires three git libraries, leptonica tesseract libjpeg. I have compiled it myself, but the native layer is always crash during testing.

 

So we found another branch of Tess Android, Tess-two.

Compile on Linux

 

1. Download and compile

1. First download Tess-two

git clone git://github.com/rmtheis/tess-two tess

2. Go to the Tess directory. There are three projects in it. We only need to go to Tess-two to compile it directly.

cd tess/tess-two
ndk-build

3. After compilation, You can import the two packages and libs under SRC to your project.

Here, I will release the compiled content. If it is used, no compilation is needed.

Download: tess-two.zip

 

Ii. Use

Tesseract uses leptonica's image processing library, which is powerful for image processing.

Official Android address: Tesseract-Android-Tools

However, it must have a matching library, that is, tessdata. We can copy it from the official website. In the previous git project, the Tesseract source code directory has the ready-made tessdata available for use. For Chinese, google Code is also available for download, of course, you can also train tessdata in different languages.

We do not need to use the class of the leptonica package. We only need to use the class of the Tess package.

Tessbaseapi

Create a tessbaseapi object

Tessbaseapi baseapi = new tessbaseapi ();

// Initialize Tess
// Under Android, tessdata must be placed in the SD card.
// If the tessdata directory is placed in the root directory of the SD card
// Then the path is directly passed into the SD card directory
// Eng is in English. For more information about the language, follow the ISO 639-3 Standard Code. For more information, see Wikipedia.
Baseapi. INIT ("parent directory of the tessdata folder", "eng ");

// Options is used to scale the image. If the image size is small, it can be scaled.
Bitmapfactory. Options = new bitmapfactory. Options ();
// Scale down to 1/2
Options. insamplesize = 2;

// Bitmap. Here we use a stream, as long as bitmap can be formed.
Bitmap bitmap = bitmapfactory. decodestream (instream, null, options );
Instream. Close ();


// If the image has an Alpha value, you 'd better set it.
/* Exifinterface EXIF = new exifinterface (filename)
Int exiforientation = EXIF. getattributeint (exifinterface. tag_orientation, exifinterface. orientation_normal );


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;
}


If (rotate! = 0 ){

// Getting width & height of the given image.
Int W = bitmap. getwidth ();
Int H = bitmap. getheight ();

// Setting pre rotate
Matrix CTX = new matrix ();
CTX. prerotate (rotate );

// Rotating bitmap
Bitmap = bitmap. createbitmap (bitmap, 0, 0, W, H, CTX, false );
// Tesseract Req. argb_8888
Bitmap = bitmap. Copy (bitmap. config. argb_8888, true );
}*/

// Sets the bitmap of the image to be OCR.
Baseapi. setimage (Bitmap );
// Obtain the OCR String Based on the init Language
String text = baseapi. getutf8text ();
// Release bitmap
Baseapi. Clear ();

// If multiple consecutive OCR images exist, this end can not be called, but after each OCR, clear must be called to release bitmap.
// Release native memory
Baseapi. End ();

/// // Other methods /////////// ///////////////////////


// Obtain the character border

Pixa = baseapi. getcharacters ();

// Same as above. This is the border of the entire text.
Baseapi. getregions ();

// Same as above, but this is a line
Baseapi. gettextlines ();

// Test the rest by yourself.

// Convert to the rect array. After that, you can easily box the box on the image.
// How to frame it with you
Arraylist <rect> rects = pixa. getboxrects ();

End.

 

OCR in Android is so simple that you can use a third-party library.

PS: it takes about 50 ms to OCR the verification code of 12306 in this database.

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.