1、下載tess4j依賴的jar包,maven中央庫地址:<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.1.0</version>
</dependency>
由於maven官方庫下載很慢,推薦在阿里雲的鏡像下載:配置文件中加入鏡像:<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<!-- 中央倉庫1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<!-- 中央倉庫2 -->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
2、maven工程中導入文字庫tessdata,放在當前工程的根目錄下即可,tessdata的文字庫包自己下載,如圖所示:
3、核心代碼:
File imageFile = new File("E:/test.png");
Tesseract instance = new Tesseract();
//將驗證碼圖片的內容識別為字符串
String result = instance.doOCR(imageFile);