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/developer.git libjpeg
To:
Git clone https://android.googlesource.com/platform/external/jpeg libjpeg
N
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 lib1__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"
+ 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 shocould 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 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 );
}
} 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.w.signorecase ("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
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 ):
From slider