Tesseract是遵照 Apache License 2.0協議的開源OCR引擎。這裏介紹下如何在Android平臺編譯Tesseract,以及如何快速建立一個簡單的OCR應用。
html
參考原文:Making an Android OCR Application with Tesseractjava
要編譯Android平臺的Tesseract,須要使用Google提供的tesseract-android-tools。
android
代碼獲取方式:git
git clone https://code.google.com/p/tesseract-android-tools/
打開README,在命令行工具中執行下面的步驟:github
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,編譯的時候會出現錯誤:
apache
format not a string literal and no format arguments [-Werror=format-security]
解決的方法就是在Application.mk中加入一行:
app
APP_CFLAGS += -Wno-error=format-security
編譯以後會生成class.jar和一些*.so。
curl
建立一個Android應用,把生成的jar和so導入進來。ide
建立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:
在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(); } } } } }
發送Intent調用圖庫,在onActivityResult中獲取返回的URI作OCR:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, REQUEST_PICK_PHOTO);
爲了獲取高質量的圖片,在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