天天看點

驗證碼識别(Tess4J初體驗)

驗證碼識别(Tess4J初體驗)

基本的步驟:1.預處理 2.灰階化 3.二值化 4.去噪 5.分割 6.識别

使用開源程式:Tesseract

下面開始正文:

Tess4J官方描述:A Java JNA wrapper for Tesseract OCR API.

1.先去官網下載下傳:​​http://tess4j.sourceforge.net/​​(我用的是目前最新版本3.1)

2.将下載下傳的檔案解壓後把下面幾個檔案夾(圖檔中選中的)複制到建立的項目中

驗證碼識别(Tess4J初體驗)

3.将lib下的jar包加到build path 中。注意:lib裡面除了jar包還有别的。

4.根據官網的樣例在剛建的項目中使用一下:

The following code example shows common usage of the library. Make sure ​

​tessdata​

​ folder are in the search path, and the ​

​.jar​

​ files are in the classpath.注意在第4步之前確定​

​tessdata​

​ 檔案夾在項目中,jar包在classpath中。前面的2,3兩步已經做了。

  1. package net.sourceforge.tess4j.example;  
  2. import java.io.File;  
  3. import net.sourceforge.tess4j.*;  
  4. public class TesseractExample {  
  5.     public static void main(String[] args) {  
  6.         File imageFile = new File("eurotext.tif");  
  7.         ITesseract instance = new Tesseract();  // JNA Interface Mapping  
  8.         // ITesseract instance = new Tesseract1(); // JNA Direct Mapping  
  9.         try {  
  10.             String result = instance.doOCR(imageFile);  
  11.             System.out.println(result);  
  12.         } catch (TesseractException e) {  
  13.             System.err.println(e.getMessage());  
  14.         }  
  15.     }  
  16. }  

我稍微改了一下,識别指定檔案夾下所有驗證碼

  1. package blog.csdn.net.dr_guo;  
  2. import net.sourceforge.tess4j.ITesseract;  
  3. import net.sourceforge.tess4j.Tesseract;  
  4. import net.sourceforge.tess4j.TesseractException;  
  5. /** 
  6.  * 驗證碼識别(圖檔名即為驗證碼數字) 
  7.  * @author drguo 
  8.  * 
  9.  */  
  10. public class VCR {  
  11.         File root = new File(System.getProperty("user.dir") + "/imgs");  
  12.         ITesseract instance = new Tesseract();  
  13.             File[] files = root.listFiles();  
  14.             for (File file : files) {  
  15.                 String result = instance.doOCR(file);  
  16.                 String fileName = file.toString().substring(file.toString().lastIndexOf("\\")+1);  
  17.                 System.out.println("圖檔名:" + fileName +" 識别結果:"+result);  
  18.             }  

直接運作就行了,但這時可能會報錯

  1. Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'libtesseract304': Native library (win32-x86-64/libtesseract304.dll) not found in resource path ([file:/G:/Eclipse/Demo/bin/, file:/G:/Eclipse/Demo/lib/commons-beanutils-1.9.2.jar, file:/G:/Eclipse/Demo/lib/commons-io-2.4.jar, file:/G:/Eclipse/Demo/lib/commons-logging-1.2.jar, file:/G:/Eclipse/Demo/lib/ghost4j-1.0.1.jar, file:/G:/Eclipse/Demo/lib/hamcrest-core-1.3.jar, file:/G:/Eclipse/Demo/lib/itext-2.1.7.jar, file:/G:/Eclipse/Demo/lib/jai-imageio-core-1.3.1.jar, file:/G:/Eclipse/Demo/lib/jna-4.2.2.jar, file:/G:/Eclipse/Demo/lib/jul-to-slf4j-1.7.19.jar, file:/G:/Eclipse/Demo/lib/junit-4.12.jar, file:/G:/Eclipse/Demo/lib/lept4j-1.1.2.jar, file:/G:/Eclipse/Demo/lib/log4j-1.2.17.jar, file:/G:/Eclipse/Demo/lib/logback-classic-1.1.6.jar, file:/G:/Eclipse/Demo/lib/logback-core-1.1.6.jar, file:/G:/Eclipse/Demo/lib/rococoa-core-0.5.jar, file:/G:/Eclipse/Demo/lib/slf4j-api-1.7.19.jar, file:/G:/Eclipse/Demo/lib/xmlgraphics-commons-1.5.jar])  

注意前面的報錯資訊,把lib下的win32-x86-64拷到項目中的bin目錄下就可以了

驗證碼識别(Tess4J初體驗)

準确率還是挺高的。

注意我的jdk版本是jdk1.8.0_74,如果你的版本低于我的版本可能會報錯~