什么是OCR?

OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程。

第一步: 创建项目导入tess4j对应的依赖

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

第二步: 导入中文字体库, 把资料中的tessdata文件夹拷贝到自己的工作空间下。资料

第三步: 编写工具类

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
 * OcrUtils OCR工具类
 * @author longshao
 */
@Slf4j
@Configuration
public class OcrUtils {
    /**
     * 语言库的路径
     */
    @Value("${tess4j.datapath}")
    private String dataPath;
    /**
     * 设置语言
     * chi_sim为中文, eng为英文
     */
    @Value("${tess4j.language}")
    private   String language;


    /**
     * 转换图片为png格式
     * @param imgUrl 图片地址
     * @return 图片地址
     */
    public  String convertPng(String imgUrl) {
        String tarFilePath = imgUrl.substring(0, imgUrl.lastIndexOf(".")) + ".png";
        try {
            BufferedImage bufferedImage = ImageIO.read(new File(imgUrl));
            BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
            newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.white, null);
            ImageIO.write(newBufferedImage, "png", new File(tarFilePath));
        } catch (IOException e) {
            return "";
        }
        return tarFilePath;
    }
    /**
     * 图片文字识别判断
     * 判断图片是否包含指定内容
     * @param content 指定内容
     * @param imgUrl 图片地址
     * @return true包含 false 不包含
     */
    public  boolean  identify(String content, String imgUrl) {
        //图片格式转化
        File imageFile = new File(convertPng(imgUrl));
        //创建Tesseract对象
        ITesseract instance = new Tesseract();
        // 设置语言库路径
        instance.setDatapath(dataPath);
        // 设置语言
        instance.setLanguage(language);
        try {
            // 执行OCR操作
            String result = instance.doOCR(imageFile);
            if (result.contains(content)) {
                return true;
            } else {
                return false;
            }
        } catch (TesseractException e) {
            log.error(e.getMessage());
        }
        return false;
    }
    /**
     * 图片文字识
     * @param imgUrl 图片地址
     * @return 识别到文字
     */
    public  String identify(String imgUrl) {
        //图片格式转化
        File imageFile = new File(convertPng(imgUrl));
        //创建Tesseract对象
        ITesseract instance = new Tesseract();
        // 设置语言库路径
        instance.setDatapath(dataPath);
        // 设置语言
        instance.setLanguage(language);
        try {
            // 执行OCR操作
            return instance.doOCR(imageFile);
        } catch (TesseractException e) {
            log.error(e.getMessage());
        }
        return null;
    }
}

第四步:配置 yml

#OCR配置
tess4j:
  data-path: D:\workspace\tessdata
  language: chi_sim

更多推荐