![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLi0zaHRGcWdUYuVzVa9GczoVdG1mWfVGc5RHLwkzX39GZhh2csATMflHLwEzX4xSZz91ZsADMx8FdsYkRGZkRG9lcvx2bjxSa2EWNhJTW1AlUxEFeVRUUfRHelRHL2EzXlpXazxyayFWbyVGdhd3LcV2Zh1Wa9M3clN2byBXLzN3btg3PnVGcq5yN1MTYllDZ2ImZ2czN1UmN0QWNyImMyITO2cDMjFDZ18CX0AzLchDMxIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjL1M3Lc9CX6MHc0RHaiojIsJye.jpeg)
基本的步驟:1.預處理 2.灰階化 3.二值化 4.去噪 5.分割 6.識别
使用開源程式:Tesseract
下面開始正文:
Tess4J官方描述:A Java JNA wrapper for Tesseract OCR API.
1.先去官網下載下傳:http://tess4j.sourceforge.net/(我用的是目前最新版本3.1)
2.将下載下傳的檔案解壓後把下面幾個檔案夾(圖檔中選中的)複制到建立的項目中
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兩步已經做了。
- package net.sourceforge.tess4j.example;
- import java.io.File;
- import net.sourceforge.tess4j.*;
- public class TesseractExample {
- public static void main(String[] args) {
- File imageFile = new File("eurotext.tif");
- ITesseract instance = new Tesseract(); // JNA Interface Mapping
- // ITesseract instance = new Tesseract1(); // JNA Direct Mapping
- try {
- String result = instance.doOCR(imageFile);
- System.out.println(result);
- } catch (TesseractException e) {
- System.err.println(e.getMessage());
- }
- }
- }
我稍微改了一下,識别指定檔案夾下所有驗證碼
- package blog.csdn.net.dr_guo;
- import net.sourceforge.tess4j.ITesseract;
- import net.sourceforge.tess4j.Tesseract;
- import net.sourceforge.tess4j.TesseractException;
- /**
- * 驗證碼識别(圖檔名即為驗證碼數字)
- * @author drguo
- *
- */
- public class VCR {
- File root = new File(System.getProperty("user.dir") + "/imgs");
- ITesseract instance = new Tesseract();
- File[] files = root.listFiles();
- for (File file : files) {
- String result = instance.doOCR(file);
- String fileName = file.toString().substring(file.toString().lastIndexOf("\\")+1);
- System.out.println("圖檔名:" + fileName +" 識别結果:"+result);
- }
直接運作就行了,但這時可能會報錯
- 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目錄下就可以了
準确率還是挺高的。
注意我的jdk版本是jdk1.8.0_74,如果你的版本低于我的版本可能會報錯~