The Chinese New Year is approaching, and the new application of the blogger-screen word-taking-the-old image coding work is also in full swing. Next we will share with you the core feature ocr in this application, that is, the image recognition function. Let's take a look at my implementation results. It is an English page that is randomly cut off on the Internet. It is the Implementation Effect of my application.
2. Implementation
(1) first download my source code and Language Pack. The address is provided below the blog. (The source code is set to 10 points to let everyone cherish the fruits of others' work)
(2) import the two folders and jar files in the lib in the code.
(3) Pay attention to the following two points. Please carefully read the comments of the code below.
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; p Ublic class MainActivity extends Activity {private TextView text; TessBaseAPI baseApi; @ Overrideprotected 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 TessBa The address before seAPI (); // (Note) is the parent level of the Language Pack. Eng indicates that the English baseApi is parsed. init (/mnt/sdcard/tesseract/, eng); bt. setOnClickListener (new OnClickListener () {@ Override public void onClick (View sourse) {// text. setText (sb); // sets the bitmap of the image to be ocr. The address of the image to be parsed (Note) is baseApi. setImage (getDiskBitmap (/mnt/sdcard/mypic.bmp); // obtain the String text1 = baseApi after ocr Based on the Init language. getUTF8Text (); text. setText (text1); // release bitmap baseApi. clear () ;}}) ;}/ ** converts a 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 image, the longer it takes. In this example, it takes about half a minute.
3. source code and Related Files
Instance source code
Language Pack eng