欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 验证码识别(1)使用 Tess4J 进行 OCR 识别

程序员文章站 2022-07-06 10:37:10
...

Java 验证码识别(1)使用 Tess4J 进行 OCR 识别

1、maven依赖

        <dependency>
            <groupId>net.sourceforge.tess4j</groupId>
            <artifactId>tess4j</artifactId>
            <version>4.3.1</version>
        </dependency>

2、训练库下载

https://github.com/tesseract-ocr/tesseract 或者 https://download.csdn.net/download/u014644574/68196205

下载解压后,将 tessreact 项目里面的 tessdata 文件夹剪切到 "C:/verify/tessdata"待用。

3、语言包下载

https://github.com/tesseract-ocr/tessdata 或者 https://download.csdn.net/download/u014644574/68197295

如果是简单的英文数字验证码识别,解压后将 eng.traineddata 放到上面 tessdata 文件夹里即可。
要识别中文,还需要将 chi 开头的 traineddata 复制到上面 tessdata 文件夹里。

4、测试代码

package com.demo.verify;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.io.File;

public class Test {
    public static void main(String[] args) {
        //验证码图片存储地址
        File imageFile = new File("C:/verify/3esg.jpg");
        Tesseract tessreact = new Tesseract();
        tessreact.setDatapath("C:/verify/tessdata");
        String result;
        try {
            result = "识别结果:" + tessreact.doOCR(imageFile);
            System.out.println(result);
        } catch (TesseractException e) {
            e.printStackTrace();
        }
    }
}

Java 验证码识别(1)使用 Tess4J 进行 OCR 识别

  

像这样一些无干扰线的,可以直接识别,但是有干扰线的情况下就识别不了。