1、下載java
1.進入官網下載頁面git
https://sourceforge.net/projects/tess4j/github
2.點擊downloadspa
3.下載後解壓,目錄以下,圈出的三個文件夾是須要用到的.net
2、使用Tess4Jcode
1.將dist和lib下的包導入java項目blog
2.將 tessdata 文件夾複製進項目的根目錄圖片
3.示範代碼以下get
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(); } } }
注意事項:it
①若是tessdata沒有放入根目錄,務必設置teedata的位置
instance.setDatapath("");//設置tessdata位置
②選擇字庫文件不須要寫上後綴,默認 tessdata 包中可能沒有 chi_sim 這個中文包,須要本身下載
https://github.com/tesseract-ocr/tessdata
3、運行結果
官方字庫識別率仍是偏低的,若是對精度要求高的話須要本身訓練字庫了