大马资讯论坛 - 马来西亚中文资讯平台

 找回密码
 注册
搜索
打印 上一主题 下一主题

[教学] Android OCR tess-two librarry一看就上手Image to Text

[复制链接]
跳转到指定楼层
1#
发表于 2015-6-6 14:39:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
自作Android OCR的步骤如下Simple Android OCR App Tutorial

1. 先现在 tess-two download zip file
https://github.com/rmtheis/tess-two

2. unzip之后,倒入进你的eclipse
File -> Import -> Existing Projects into workspace -> tess-two directory

3. Add Android NDK library in tess-two project
Note: if no,  system error liblept.so shared library
java.lang.UnsatisfiedLinkError: Couldn't load lept from loader dalvik.system.PathClassLoader

a. Download NDK from https://developer.android.com/tools/sdk/ndk/index.html
b. 之后Extract android-ndk-r10e-windows-x86_64.exe
c. Go to Windows -> Preference -> Android -> NDK -> point to your android-ndk-r10e folder

a. Right click tess-two project -> Properties -> Android -> Tick "Is Library"
b. Go to Builders -> Click New -> Select Program -> Click OK
c. Enter Name "tess ndk", Location - Click Browser File System and choose android-ndk-r10e\ndk-build.cmd, Working Directory - Click Browse Workplace and select tess-two ${workspace_loc:/tess-two}  

Refer to https://droidcomp.wordpress.com/ ... ibrary-for-android/

4.  Download take photo app http://labs.makemachine.net/2010/03/simple-android-photo-capture/
Unzip it, File -> Import -> Existing Projects into workspace -> PhotoCaptureExample

a. Right click tess-two project -> Properties -> Android -> Library -> Add tess-two project
b. AndroidManifest.xml add
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
c. PhotoCaptureExample.java -> Replace the code below
  1. Log.i( "MakeMachine", "onPhotoTaken" );
  2.            
  3.             _taken = true;
  4.            
  5.             BitmapFactory.Options options = new BitmapFactory.Options();
  6.         options.inSampleSize = 4;
  7.            
  8.             Bitmap bitmap = BitmapFactory.decodeFile( _path, options );
  9.            
  10.             _image.setImageBitmap(bitmap);
  11.            
  12.             //_field.setVisibility( View.GONE );
  13.            
  14.             // _path = path to the image to be OCRed
  15.             ExifInterface exif = null;
  16.                 try {
  17.                         exif = new ExifInterface(_path);
  18.                 } catch (IOException e) {
  19.                         // TODO Auto-generated catch block
  20.                         e.printStackTrace();
  21.                 }
  22.             int exifOrientation = exif.getAttributeInt(
  23.                     ExifInterface.TAG_ORIENTATION,
  24.                     ExifInterface.ORIENTATION_NORMAL);

  25.             int rotate = 0;

  26.             switch (exifOrientation) {
  27.             case ExifInterface.ORIENTATION_ROTATE_90:
  28.                 rotate = 90;
  29.                 break;
  30.             case ExifInterface.ORIENTATION_ROTATE_180:
  31.                 rotate = 180;
  32.                 break;
  33.             case ExifInterface.ORIENTATION_ROTATE_270:
  34.                 rotate = 270;
  35.                 break;
  36.             }

  37.             if (rotate != 0) {
  38.                 int w = bitmap.getWidth();
  39.                 int h = bitmap.getHeight();

  40.                 // Setting pre rotate
  41.                 Matrix mtx = new Matrix();
  42.                 mtx.preRotate(rotate);

  43.                 // Rotating Bitmap & convert to ARGB_8888, required by tess
  44.                 bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
  45.             }
  46.             bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
  47.            
  48.             TessBaseAPI baseApi = new TessBaseAPI();
  49.             // DATA_PATH = Path to the storage
  50.             String lang = "eng";
  51.             String datapath = Environment.getExternalStorageDirectory() + "/tesseract/";
  52.             baseApi.init("/mnt/sdcard/tesseract/", lang);
  53.             //baseApi.init("/mnt/sdcard/tesseract/tessdata/eng.traineddata", "eng");
  54.             baseApi.setImage(bitmap);
  55.             String recognizedText = baseApi.getUTF8Text();
  56.             baseApi.end();
  57.             Context context = getApplicationContext();
  58.             //CharSequence text = "Hello toast!";
  59.             int duration = Toast.LENGTH_SHORT;

  60.             Toast toast = Toast.makeText(context, recognizedText, duration);
  61.             toast.show();
  62.             _field.setText(recognizedText);
复制代码

d. Change path
  1. //_path = Environment.getExternalStorageDirectory() + "/images/make_machine_example.jpg";
  2.         _path = "/mnt/sdcard/tesseract/images/make_machine_example.jpg";
复制代码

e. Go to your phone /mnt/sdcard/tesseract/tessdata/eng.traineddata, make sure you have the language package语言包
if no, download from here https://code.google.com/p/tesseract-ocr/downloads/list
unzip eng.traineddata.gz, then copy eng.traineddata and paste under /mnt/sdcard/tesseract/tessdata

Refer to http://gaut.am/making-an-ocr-android-app-using-tesseract/

5. Done



FAQ
1. Data path must contain subfolder tessdata!
Your path cant contain this text tessdata

2. android could not initialize tesseract api with language=eng
After Downloading, remember to unzip eng.traineddata.gz, and just take eng.traineddata

3. Crash TessBaseAPI baseApi = new TessBaseAPI
Make sure the NDK is installed/configured correctly

4. Fatal signal 11 tesseract
After Downloading, remember to unzip eng.traineddata.gz, and just take eng.traineddata

手机版|大马资讯论坛  

GMT+8, 2024-12-1 20:44 , Processed in 0.027752 second(s), 12 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表