android中ocr解決方案(tesseract)

android應用中ocr的解決方案大體有兩種,而採用最多的仍是tesseract.小弟就在這裏將我最近兩天解決思路寫下來,若有缺陷,歡迎拍磚:
  有兩種解決方案,一種是採用tesseract cloud-service,這鐘是把圖片信息發送到雲端,而後得到圖片分析數據;第二種就是不用聯網,本地化分析圖片上信息。我就說說第二種,第一種我會在最後給你們一個連接(文章很不錯)。
  搜先就是下載Tesseract native android library.這裏有兩個連接,你選哪一個連接均可以:
  a.svn checkout http://tesseract-android-tools.googlecode.com/svn/trunk/ tesseract-android-tools。(若是不能checkout到,廢話別說就到官方上下:http://code.google.com/p/tesseract-android-tools/)
  b.可能上面一個下載後編譯有些人會遇到一些問題,好比找不到jgep庫,編譯不成功。因此有了這個項目:git clone git://github.com/rmtheis/tess-two.git (這個包裏面內容太多,不過也免得下那麼多庫了)
  這裏先說採用第一個源下載:下載成功後,打開README文件,作下修改(以下):
git clone git://android.git.kernel.org/platform/external/jpeg.git libjpeg
修改成:
git clone https://android.googlesource.com/platform/external/jpeg libjpeg
n
  
  對於第二個源下載,因爲裏面沒有README文件,操做命令以下:
cd <project-directory>/tess-two
export TESSERACT_PATH=${PWD}/external/tesseract-3.01
export LEPTONICA_PATH=${PWD}/external/leptonica-1.68
export LIBJPEG_PATH=${PWD}/external/libjpeg
ndk-build
android update project --path .
ant releaseandroid

  最終兩個都獲得你想要的libs裏面的so文件和src裏面的對so文件的封裝類。這個就是咱們開發所用到的東東啦。
  而後新建工程,代碼以下:
public class MainActivity extends Activity {git

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";github

    1. TessBaseAPI service;
      @Overrideshell

    2. void onCreate(Bundle savedInstanceState) {ide

      super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         testOcr();

      }svn

    3. void testOcr(){post

      mHandler.post(new Runnable() {
            
             @Override
             public void run() {
                 Log.d(TAG, "begin>>>>>>>");
                 ocr();
                 //test();
             }
         });

      }ui

    4. void test(){google

      // First, make sure the eng.traineddata file exists.
         /*assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to "
      • EXPECTED_FILE, new File(EXPECTED_FILE).exists());*/code

      1. TessBaseAPI baseApi = new TessBaseAPI();

        baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE);
      2. 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 should 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();

        }

    5. 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 mtx = new Matrix();
                 mtx.preRotate(rotate);
      
                 // Rotating Bitmap
                 bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, 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.equalsIgnoreCase("eng")) {
             recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " ");
         }
         if (recognizedText.length() != 0) {
             ((TextView) findViewById(R.id.field)).setText(recognizedText.trim());
         }

      }

    6. Handler mHandler = new Handler(){

      public void handleMessage(android.os.Message msg) {
            
         };

      };
      }

      當你很歡喜的運行程序的時候,發現事情沒有你想象的那麼簡單。這個文件必需要用到一個語言包。否則你怎麼匹配呢?想一想也是:adb shell mkdir /mnt/sdcard/tesseractadb shell mkdir /mnt/sdcard/tesseract/tessdataadb push eng.traineddata /mnt/sdcard/tesseract/tessdata/eng.traineddataadb shell ls -l /mnt/sdcard/tesseract/tessdatals -l bin/tesseract-android-tools-test.apkadb install -r -s bin/tesseract-android-tools-test.apkadb shell am instrument -w -e class com.googlecode.tesseract.android.test.TessBaseAPITest

    相關文章
    相關標籤/搜索