OCR識別的Android端實現


1.OCR簡介
OCR (Optical Character Recognition,光學字符識別)是指電子設備(例如掃描儀或數碼相機)檢查紙上打印的字符,通過檢測暗、亮的模式確定其形狀,然后用字符識別方法將形狀翻譯成計算機文字的過程;

2.Tesseract簡介
Tesseract是Ray Smith於1985到1995年間在惠普布里斯托實驗室開發的一個OCR引擎,曾經在1995 UNLV精確度測試中名列前茅。但1996年后基本停止了開發。2006年,Google邀請Smith加盟,重啟該項目。目前項目的許可證是Apache 2.0。該項目目前支持Windows、Linux和Mac OS等主流平台。但作為一個引擎,它只提供命令行工具。
現階段的Tesseract由Google負責維護,是最好的開源OCR Engine之一,並且支持中文。

主頁地址:https://github.com/tesseract-ocr

在Tesseract的主頁中,我們可以下載到Tesseract的源碼及語言包,常用的語言包為

中文:chi-sim.traineddata

英文:eng.traineddata

3.Tess-two
因為Tesseract使用C++實現的,在Android中不能直接使用,需要封裝JavaAPI才能在Android平台中進行調用,這里我們直接使用TessTwo項目,tess-two是TesseraToolsForAndroid的一個git分支,使用簡單,切集成了leptonica,在使用之前需要先從git上下載源碼進行編譯。

3.1.1 項目地址
Tess-two在git上地址為:https://github.com/rmtheis/tess-two

3.1.2 使用

在你的Android項目中,修改build.gradle 文件,添加如下依賴,即可使用了

dependencies {
    implementation 'com.rmtheis:tess-two:9.0.0'
}

Android 代碼如下:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;

import com.googlecode.tesseract.android.TessBaseAPI;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * ocr 識別截圖文本
 *
 */
public class Imagett {
    private static String TAG = "IMAGETT";
    private static final String DEFAULT_LANGUAGE = "chi_sim";
    private static String text;

    /**
     *
     * @param imageFile 識別的圖片文件
     * @param language 識別的語言 chi_sim : 中文, eng:英文
     * @param refresh 是否重新獲取圖片
     * @return
     */
    public static String imageToText(final String imageFile, final String language, boolean refresh){ //language :簡體中文 chi_sim, 英文 eng
        if (!refresh){
            try {
                return MyFile.readFile(CONST.TESSDATA + File.separator + "text.txt");  //文件讀取操作
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = BitmapFactory.decodeFile(imageFile);
                TessBaseAPI tessBaseAPI = new TessBaseAPI();
                tessBaseAPI.init(CONST.LOGPATH, language);
                tessBaseAPI.setImage(bitmap);
                text = tessBaseAPI.getUTF8Text();
//                logUtil.i(TAG, "run: text " + System.currentTimeMillis() + text);
                //識別的文本內容寫入的文件中
                try {
                    MyFile.writeFile(CONST.TESSDATA + File.separator + "text.txt", text, false); //文件寫操作
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                tessBaseAPI.end();
            }
        });
        t.start();
        //等待識別完成
        while (t.isAlive()){
            SystemClock.sleep(100);
        }
        return text;
    }
    }

實現的功能,將指定圖片內的文字識別后輸出的txt文件內

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM