SpringBoot/Java中OCR实现,集成Tess4J实现图片文字识别

公众号:霸道的程序猿 / 2024-02-27 / 原文

场景

Tesseract

Tesseract是一个开源的光学字符识别(OCR)引擎,它可以将图像中的文字转换为计算机可读的文本。

支持多种语言和书面语言,并且可以在命令行中执行。它是一个流行的开源OCR工具,可以在许多不同的操作系统上运行。

https://github.com/tesseract-ocr/tesseract

Tess4J

Tess4J是一个基于Tesseract OCR引擎的Java接口,可以用来识别图像中的文本,说白了,就是封装了它的API,让Java可以直接调用。

中文文字训练集下载

Tesseract引擎默认是无法识别中文的,只能识别数字或者英文。如果我们想实现中文的识别就得去下载对应的训练集。

https://gitcode.com/tesseract-ocr/tessdata/tree/main

下载之后找到中文简体训练集文件

 

将chi_sim.traineddata复制到某磁盘路径下,这里放在D盘tessdata目录下

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi

实现

1、搭建SpringBoot项目后,添加Tess4J依赖

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

2、在配置文件application.yml中添加训练集文件夹的路径

# 训练数据文件夹的路径
tess4j:
  datapath: D:/tessdata

3、新增配置类,读取配置文件内容,并初始化Tesseract类,交给Spring管理

import net.sourceforge.tess4j.Tesseract;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TesseractOcrConfiguration {

    @Value("${tess4j.datapath}")
    private String dataPath;

    @Bean
    public Tesseract tesseract() {

        Tesseract tesseract = new Tesseract();
        // 设置训练数据文件夹路径
        tesseract.setDatapath(dataPath);
        // 设置为中文简体
        tesseract.setLanguage("chi_sim");
        return tesseract;
    }
}

4、编写Service接口层

import java.io.InputStream;

public interface IOcrService {
    String recognizeText(InputStream sbs);
}

5、编写ServiceImpl

import com.ruoyi.system.service.IOcrService;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

@Service
public class OcrServiceImpl implements IOcrService {

    @Autowired
    private Tesseract tesseract;

    @Override
    public String recognizeText(InputStream sbs) {
        // 转换
        try {
            BufferedImage bufferedImage = ImageIO.read(sbs);
            // 对图片进行文字识别
            return tesseract.doOCR(bufferedImage);
        } catch (IOException | TesseractException e) {
            e.printStackTrace();
            return null;
        }
    }
}

6、编写单元测试

import com.ruoyi.system.service.IOcrService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Tess4JOcrTest {

    @Autowired
    private IOcrService iOcrService;

    @Test
    public void ocrLocalPng() {
        try {
            InputStream inputStream=new FileInputStream("D://tess4j.png");
            String result = iOcrService.recognizeText(inputStream);
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

7、这里的png文件随便截图并放在磁盘路径下,运行单元测试

 

识别效果可能存在不准确的情况。

其它使用场景,比如前端上传照片,后台识别返回结果等可自己进行扩展。