在maven項目中,使用Java代碼實現Tesseract_ocr技術非常的簡單。只需要引入一個依賴就行。
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.3.1</version>
</dependency>
版本的選擇,可以根據自己的需要自行去maven倉庫中下載。下面咱們來看代碼如何實現。
首先聲明我是用idea寫的代碼。
代碼就這么多。
public class Tesseract_ocr {
public static void main(String[] args){
// 圖片和語言庫的存放路徑
String path = "E://com//dream//begin//Tesseract_OCR";
// 圖片路徑
File file = new File(path + "//image//jpg//bank//1_3.jpg");
System.out.println("本地文件路徑:".concat(file.getPath()));
// 創建ITesseract對象
ITesseract instance = new Tesseract();
// 設置訓練庫的位置
instance.setDatapath(path + "//tessdata");
// 根據需求選擇語言庫 chi_sim :簡體中文, eng
instance.setLanguage("chi_sim");
String result = null;
try {
// 識別開始獲取時間戳
long startTime = System.currentTimeMillis();
// 圖片識別
result = instance.doOCR(file);
// 識別結束時間戳
long endTime = System.currentTimeMillis();
System.out.println("Time is:" + (endTime - startTime) + " 毫秒");
} catch (TesseractException e) {
e.printStackTrace();
}
// 識別信息
System.out.println("result: ".concat(result));
}
}
希望能幫助大家
限知識有限,如有問題,請糾正。