字庫下載參考:https://github.com/tesseract-ocr/tessdata/tree/3.04.00 解壓后在項目根目錄創建tessdata目錄,將內容復制進去。 如圖:
測試:
public static void main(String[] args) throws TesseractException { ITesseract instance = new Tesseract(); //如果未將tessdata放在根目錄下需要指定絕對路徑 //instance.setDatapath("the absolute path of tessdata"); //如果需要識別英文之外的語種,需要指定識別語種,並且需要將對應的語言包放進項目中 instance.setLanguage("chi_sim"); // 指定識別圖片 File imgDir = new File("D:\\t.jpg"); long startTime = System.currentTimeMillis(); String ocrResult = instance.doOCR(imgDir); // 輸出識別結果 System.out.println("OCR Result: \n" + ocrResult + "\n 耗時:" + (System.currentTimeMillis() - startTime) + "ms"); }
依賴:
· <dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>3.2.1</version> </dependency