Tesseract OCR整合Android Studio實現OCR識別

來源:互聯網
上載者:User

標籤:ndk   title   錯誤   groovy   沒有   工程   地址   dir   目錄   

Tesseract OCR整合Android Studio實現OCR識別介紹

Tesseract OCRGoogle開源的OCR識別引擎,支援多國文字包括中文簡體與繁體。最新的版本是3.x。可以通過安裝程式安裝在機器上然後通過命令列運行該程式識別各種圖片中的文字、同時還提供二次開發包,支援二次開發包括C、C++語言。也可以被移植到Android平台實現行動裝置 App領域的OCR識別APP。

下載

在Android平台上使用Tesseract OCR首先要下載Tess2工程,它是專門針對Android平台編譯出來的,如下 - https://github.com/rmtheis/tess-two 。下載解壓縮之後你就會看到如下目錄結構:

然後開啟Android Studio建立一個項目應用,選擇匯入Module之後選擇匯入紅色圓圈中的tess-two檔案夾,匯入之後你就會看到如下:

項目必須是支援NDK的,所以要在Project Structure中指明NDK的路徑。原因是tess-two是個NDK項目,沒有NDK支援無法完成編譯。

此外你可能還會遇到沒有android-maven的錯誤,把下面的指令碼加到build.gradle的最上面即可:

buildscript { 
repositories {
jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:2.1.2’
classpath ‘org.codehaus.groovy:groovy-backports-compat23:2.3.5’
classpath ‘com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0’
classpath ‘com.github.dcendents:android-maven-gradle-plugin:1.5’
}
}
如果一切順利,你就會完成匯入與編譯,看到匯入成功的目錄結構,第二張所示。下面就是在你的Activity中添加初始化代碼如下:
    private void initTessBaseData() { 
mTess = new TessBaseAPI();
String datapath = Environment.getExternalStorageDirectory() + “/tesseract/”;
// String language = “num”;
String language = “eng”;
File dir = new File(datapath + “tessdata/”);
if (!dir.exists())
dir.mkdirs();
mTess.init(datapath, language);
}
然後通過監聽【識別】按鈕的Click事件響應識別動作如下:
    public void onClick(View v) { 
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.textimage);
mTess.setImage(bitmap);
String result = mTess.getUTF8Text();
TextView txtView = (TextView)this.findViewById(R.id.idCard_textView);
txtView.setText(“結果為:” + result);
ImageView imgView = (ImageView)this.findViewById(R.id.imageView);
imgView.setImageBitmap(bitmap);
}
顯示結果如下:

其中上部分為識別到文字顯示,下面是對應載入的文字圖片。
這裡特別要注意的是必須在你對應目錄有相關tessdata檔案才可以,關於tessdata可以到如下地址下載:
https://github.com/tesseract-ocr/tessdata
手動或者通過程式Copy到你在初始化時候產生的目錄下面即可。
此外,還不要忘記把tess-two作為依賴添加到app中去。

Tesseract OCR整合Android Studio實現OCR識別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.