2014年11月5日 星期三

OCR - tessract

環境 : Android 4.2 + leptonica-1.71.tar.gz
  • Android 版 tesseract
    必備 : leptonica
    ( compile方法參考README )
  • tesseract - 語言包
    ( 語言包下載後,路徑如 : /mnt/sdcard/tesseract/tessdata/eng.traineddata)
  • 建立一個專案,接者把compile好的jar檔加入專案:
    專案上右鍵 > Properties > Android > 下方的Library加入 tess-two和eyes-two
import java.io.File;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import com.googlecode.tesseract.android.TessBaseAPI;

public class OCR
{
 private String filepath=null;
 private String language=null;
 TessBaseAPI baseApi;
 public OCR (String filepath, String language)
 {
  this.filepath=filepath;
  this.language=language;
 }
 public boolean ocr ()
 {
  Log.i("tess", "into OCR function");
  
  baseApi = new TessBaseAPI();
  baseApi.init("/mnt/sdcard/tesseract/", "eng");  
  
  baseApi.setImage(getDiskBitmap(filepath));
  String text1 = baseApi.getUTF8Text();
  baseApi.clear();
  baseApi.end();
  //text.setText(text1);
  Log.i("tess", "text1="+text1);
  return true;
 }
 private Bitmap getDiskBitmap(String pathString)
 {
  Bitmap bitmap = null;
  try
  {
   File file = new File(pathString);
   if (file.exists())
   {
    bitmap = BitmapFactory.decodeFile(pathString);
   }
  }
  catch (Exception e)
  {
  }
  return bitmap;
 }
}

ref :
不可一日无烟卷
Google Glass OCR Tutorial using Eclipse

2 則留言:

  1. 請問我參考你的文章且我也照著下列的方法去做,但是用手機執行後,拍攝完進入ocr的時候就停止了 想請問這個問題可能是卡在哪裡呢??
    Download the NDK from the Android Development site and extract it to any folder.

    Go to Preferences-->Android -->NDK and select the path for Android-Ndk directory ex-->D:\Work\Android\Android Tools\android-ndk-r9

    Import tess-two project. File -> Import -> Existing Projects into workspace -> tess-two directory.

    Right Click on Tess-two--> Properties-->Builders then give any name for Builder like Ndk tess Builder And add location for ndk-build.cmd ex-->:D:\Work\Android\Android Tools\android-ndk-r9\ndk-build.cmd. select working directory as ${Workspace/tess-two}

    Refresh tess-two, It will take time to build

    After complete build Add it as library:Right click the project, Android Tools -> Fix Project Properties. Right click -> Properties -> Android -> Check Is Library.

    回覆刪除
  2. 嗨,你好
    我是用linux環境去compile的, 然後也是在linux下使用eclipse完成的
    使用過程都沒問題 : )

    回覆刪除