验证码识别(Tess4J初体验)

遇到一道机试题

当时就懵逼了0.0查了好多资料,大体知道了基本的步骤: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 java.io.File;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
/**
 * 验证码识别(图片名即为验证码数字)
 * @author drguo
 *
 */
public class VCR {
	public static void main(String[] args) {
		File root = new File(System.getProperty("user.dir") + "/imgs");
		ITesseract instance = new Tesseract();

		try {
			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);
			}
		} catch (TesseractException e) {
			System.err.println(e.getMessage());
		}
    }
}

直接运行就行了,但这时可能会报错

 

 

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目录下就可以了

 


准确率还是挺高的。

 

如果你还觉得麻烦,直接把我的项目下载下来改吧:https://git.oschina.net/drguo/VerificationCodeRecognition.git

注意我的jdk版本是jdk1.8.0_74,如果你的版本低于我的版本可能会报错~

 

 

  • 11
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 20
    评论
在使用Tess4J进行OCR识别时,可以设置识别参数以达到更好的识别效果。具体设置方法如下: 1. 创建Tesseract对象,可以通过Tesseract.getInstance()方法创建默认的Tesseract对象,也可以通过Tesseract构造函数传入参数来设置Tesseract的语言、OCR引擎等参数。 2. 通过setDatapath()方法设置tessdata目录的路径,该目录包含Tesseract的语言数据和配置文件。 3. 通过setLanguage()方法设置识别语言,可以同时设置多个语言,例如“chi_sim+eng”。 4. 通过setParameter()方法设置其他参数,常见的参数包括: - tessedit_char_whitelist:限定识别字符集,例如“0123456789”表示只识别数字; - tessedit_char_blacklist:忽略识别字符集,例如“$%&*”表示不识别这些字符; - tessedit_pageseg_mode:设置分页模式,例如PSM_AUTO、PSM_SINGLE_BLOCK等; - tessedit_ocr_engine_mode:设置OCR引擎模式,例如OEM_TESSERACT_ONLY、OEM_LSTM_ONLY等。 下面是一个示例代码,展示了如何设置Tess4J的识别参数: ``` Tesseract tesseract = new Tesseract(); tesseract.setDatapath("tessdata"); tesseract.setLanguage("chi_sim+eng"); tesseract.setParameter("tessedit_char_whitelist", "0123456789"); tesseract.setParameter("tessedit_pageseg_mode", "PSM_AUTO"); tesseract.setParameter("tessedit_ocr_engine_mode", "OEM_TESSERACT_ONLY"); ``` 以上代码将设置Tesseract的语言为中文和英文,限定只识别数字字符,自动分页,使用Tesseract引擎进行OCR识别。
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光于前裕于后

您的打赏将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值