如何通過Tesseract開源OCR引擎創建Android OCR應用
Tesseract是遵守 Apache License 2.0協議的開源OCR引擎。這里介紹下如何在Android平臺編譯Tesseract,以及如何快速創建一個簡單的OCR應用。
參考原文:Making an Android OCR Application with Tesseract
Tesseract Android Tools
要編譯Android平臺的Tesseract,需要使用Google提供的tesseract-android-tools。
代碼獲取方式:
git clone https://code.google.com/p/tesseract-android-tools/
打開README,在命令行工具中執行下面的步驟:
cd <project-directory> curl -O https://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.02.tar.gz curl -O http://leptonica.googlecode.com/files/leptonica-1.69.tar.gz tar -zxvf tesseract-ocr-3.02.02.tar.gz tar -zxvf leptonica-1.69.tar.gz rm -f tesseract-ocr-3.02.02.tar.gz rm -f leptonica-1.69.tar.gz mv tesseract-3.02.02 jni/com_googlecode_tesseract_android/src mv leptonica-1.69 jni/com_googlecode_leptonica_android/src ndk-build -j8 android update project --target 1 --path . ant debug (release)
注意:如果你在使用NDK r9,編譯的時候會出現錯誤:
format not a string literal and no format arguments [-Werror=format-security]
解決的方法就是在Application.mk中加入一行:
APP_CFLAGS += -Wno-error=format-security
編譯之后會生成class.jar和一些*.so。
Android OCR Application
創建一個Android應用,把生成的jar和so導入進來。
創建TessOCR:
public class TessOCR { private TessBaseAPI mTess; public TessOCR() { // TODO Auto-generated constructor stub mTess = new TessBaseAPI(); String datapath = Environment.getExternalStorageDirectory() + "/tesseract/"; String language = "eng"; File dir = new File(datapath + "tessdata/"); if (!dir.exists()) dir.mkdirs(); mTess.init(datapath, language); } public String getOCRResult(Bitmap bitmap) { mTess.setImage(bitmap); String result = mTess.getUTF8Text(); return result; } public void onDestroy() { if (mTess != null) mTess.end(); } }
構造函數中需要在存儲卡上創建一個目錄tessdata,如果不創建程序運行就會出錯。因為源碼中會檢測這個目錄,不存在就拋出異常:
public boolean init(String datapath, String language) { if (datapath == null) { throw new IllegalArgumentException("Data path must not be null!"); } if (!datapath.endsWith(File.separator)) { datapath += File.separator; } File tessdata = new File(datapath + "tessdata"); if (!tessdata.exists() || !tessdata.isDirectory()) { throw new IllegalArgumentException("Data path must contain subfolder tessdata!"); } return nativeInit(datapath, language); }
就這么簡單。現在通過三種方式獲取圖片做OCR:
在圖庫中選取一張圖,選擇發送或者分享,選擇OCR應用
在AndroidManifest.xml中加入IntentFilter,讓OCR應用出現在圖庫的分享列表中:
<intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> <data android:mimeType="image/*" /> </intent-filter>
獲得URI之后,對URI解碼,獲取bitmap:
if (Intent.ACTION_SEND.equals(intent.getAction())) { Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); uriOCR(uri); } private void uriOCR(Uri uri) { if (uri != null) { InputStream is = null; try { is = getContentResolver().openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(is); mImage.setImageBitmap(bitmap); doOCR(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
啟動OCR應用,從圖庫中選擇一張圖做OCR
發送Intent調用圖庫,在onActivityResult中獲取返回的URI做OCR:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, REQUEST_PICK_PHOTO);
啟動OCR應用,拍照之后做OCR
為了獲取高質量的圖片,在Intent中加入圖片路徑。返回之后就可以直接使用這個圖片路徑解碼:
private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // Create the File where the photo should go File photoFile = null; try { photoFile = createImageFile(); } catch (IOException ex) { // Error occurred while creating the File } // Continue only if the File was successfully created if (photoFile != null) { takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); } } }
最后不要忘記下載語言包,并push到存儲卡的tessdata目錄下。
源碼
https://github.com/DynamsoftRD/android-tesseract-ocr
git clone https://github.com/DynamsoftRD/android-tesseract-ocr.git
原文地址:http://www.codepool.biz/ocr/making-an-android-ocr-application-with-tesseract.html