(Reproduced Please specify Source:http://blog.csdn.net/buptgshengod)
1. Introduction
Fast New Year, Bo Master of the application of the screen to take the words of reading glasses coding work is also in intensive progress. Here's a look at the core functional OCR in this application, which is the function of image recognition. Let's take a look at my implementation results. It's an English-language page that's been cut off on the internet, and it's my app's implementation.
2. Implement
(1) First to download my source code and language pack, the blog will give the address below. (The source is set to 10 points, is to let everyone cherish the fruits of other people's Labor)
(2) Import two folders and jar files from the Lib in the code.
(3) Two points to note please take a look at the comments of the code posted below
[Java]View Plaincopy
- Package com.example.tess;
- Import Java.io.File;
- Import Com.googlecode.tesseract.android.TessBaseAPI;
- Import Android.os.Bundle;
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.graphics.Bitmap;
- Import Android.graphics.BitmapFactory;
- Import Android.view.Menu;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Import Android.widget.TextView;
- Public class Mainactivity extends Activity {
- private TextView text;
- Tessbaseapi Baseapi;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Button bt=New button (Getbasecontext ());
- bt= (Button) Findviewbyid (R.id.button1);
- text=New TextView (Getbasecontext ());
- text= (TextView) Findviewbyid (R.ID.TEXTVIEW1);
- baseapi=new Tessbaseapi ();
- //(note) The previous address is the parent of the language pack. Eng says the parsing is in English
- Baseapi.init ("/mnt/sdcard/tesseract/", "Eng");
- Bt.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View sourse) {
- //Text.settext ("SB");
- //Set the image bitmap to be OCR, the image address to parse (note)
- Baseapi.setimage (Getdiskbitmap ("/mnt/sdcard/mypic.bmp"));
- //Based on the Init language, get the OCR string
- String text1= Baseapi.getutf8text ();
- Text.settext (Text1);
- //Release bitmap
- Baseapi.clear ();
- }
- }
- );
- }
- /*
- * Convert local image to bitmap
- */
- private Bitmap Getdiskbitmap (String pathstring)
- {
- Bitmap Bitmap = null;
- Try
- {
- File File = new file (pathstring);
- if (file.exists ())
- {
- Bitmap = Bitmapfactory.decodefile (pathstring);
- }
- } catch (Exception e)
- {
- //Todo:handle exception
- }
- return bitmap;
- }
- }
(4) The larger the picture takes, the longer it takes, this example takes almost half a minute
3. Source code and related documents
Well, a lot of people say that code can not download or say 10 points too expensive, here to provide free. Where the Tess folder is the Android program Tessdata is the language pack
4. Chinese recognition
You can download it at the following address, unzip it under/tesseract/tessdata, and change Eng to Chi_sim
Http://code.google.com/p/tesseract-ocr/downloads/detail?name=chi_sim.traineddata.gz&can=2&q=
Android TESSERACT-OCR Example tutorial (with Chinese recognition) (with source)