天天看點

利用Tess4J實作圖檔識别

一、下載下傳

1.進入官網下載下傳頁面

https://sourceforge.net/projects/tess4j/ 2.點選download
利用Tess4J實作圖檔識别
3.下載下傳後解壓,目錄如下,圈出的三個檔案夾是需要用到的
利用Tess4J實作圖檔識别

二、使用Tess4J

1.将dist和lib下的包導入java項目

利用Tess4J實作圖檔識别
利用Tess4J實作圖檔識别
2.将 tessdata 檔案夾複制進項目的根目錄
利用Tess4J實作圖檔識别
3.示範代碼如下

public class OCRDemo {

    public static void main(String[] args) {
        try {
            double start=System.currentTimeMillis();
            File imageFile = new File("C:\\Users\\dan\\Desktop\\12345.png");//圖檔位置
            ITesseract instance = new Tesseract();
            //instance.setDatapath("");//設定tessdata位置
            instance.setLanguage("chi_sim");//選擇字庫檔案
            String result = instance.doOCR(imageFile);//開始識别
            double end=System.currentTimeMillis();
            System.out.println(result);//列印圖檔内容
            System.out.println("耗時"+(end-start)/1000+" s");
        } catch (TesseractException e) {
            e.printStackTrace();
        }
    }

}      

注意事項:

①如果tessdata沒有放入根目錄,務必設定teedata的位置

instance.setDatapath("");//設定tessdata位置      

②選擇字庫檔案不需要寫上字尾,預設 tessdata 包中可能沒有 chi_sim 這個中文包,需要自己下載下傳

https://github.com/tesseract-ocr/tessdata

三、運作結果

利用Tess4J實作圖檔識别
利用Tess4J實作圖檔識别

官方字庫識别率還是偏低的,如果對精度要求高的話需要自己訓練字庫了