tesseract ocr 文字识别

windows 版

下载地址:https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w64-setup-v5.3.0.20221214.exe

中文简体语言包下载:https://raw.githubusercontent.com/tesseract-ocr/tessdata/main/chi_sim.traineddata

操作

1、下载两个文件;

2、双击安装exe文件;

3、把安装根目录配置到 环境变量 Path中 ;

4、语言包 chi_sim.traineddata 放到安装目录的 tessdata 文件夹下;

5、cmd tesseract -v 验证安装结果;

6、系统编码集成:

java maven项目 引入 包

复制代码
        <dependency>
            <groupId>net.sourceforge.tess4j</groupId>
            <artifactId>tess4j</artifactId>
            <version>5.13.0</version> <!-- 建议使用最新稳定版 -->
        </dependency>

编码:

复制代码
import jakarta.annotation.PostConstruct;
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.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 图片文字识别工具类
 */
@Component
public class OcrUtils {

    @Value("${tesseract.datapath}")
    private String dataPathConfig;

    private static String dataPath;

    @PostConstruct
    private void init() {
        dataPath = dataPathConfig;
    }

    /**
     * 识别图片文件中的文字(MultipartFile 入参)
     *
     * @param file 上传的图片文件
     * @return 识别出的文字内容
     * @throws IOException 图片读取失败时抛出
     */
    public static String recognizeText(MultipartFile file) throws IOException {
        try (InputStream is = file.getInputStream()) {
            BufferedImage image = ImageIO.read(is);
            if (image == null) {
                throw new IOException("无法解析图片,请确认文件格式正确");
            }
            ITesseract instance = new Tesseract();
            instance.setDatapath(dataPath);
            instance.setLanguage("chi_sim");
            return instance.doOCR(image).replace(" ", "").replace("\n", "");
        } catch (TesseractException e) {
            System.err.println("OCR识别失败: " + e.getMessage());
            return "";
        }
    }

    /**
     * 识别 BufferedImage 图片对象中的文字
     */
    public static String recognizeImage(File image, String language) {
        ITesseract instance = new Tesseract();
        instance.setDatapath(dataPath);
        instance.setLanguage(language);
        try {
            return instance.doOCR(image);
        } catch (TesseractException e) {
            System.err.println("OCR识别失败: " + e.getMessage());
            return "";
        }
    }
}

亲测,可用

相关推荐
胡琦博客16 小时前
HarmonyOS 智能工具箱(二):OCR 文字识别工具
华为·ocr·harmonyos
云间月131418 小时前
搭一套截图识别通知机器人:OCR、飞书Webhook与远程触发实战
机器人·ocr·飞书
蓝创工坊Blue Foundry20 小时前
PDF 批量提取指定内容到 Excel:按字段整理多个 PDF 的方法
pdf·ocr·excel·文心一言·paddlepaddle·paddle
zyplayer-doc1 天前
研发接口文档怎么长期维护:zyplayer-doc把API、Markdown和变更记录放进同一个知识库
大数据·数据库·人工智能·笔记·pdf·ocr
AI人工智能+1 天前
高精度表格识别技术通过深度学习与计算机视觉融合,突破传统OCR局限,实现表格结构的智能解析与还原
深度学习·ocr·表格识别
蓝创工坊Blue Foundry1 天前
本地 PDF、图片字段提取到 Excel:用文档工作台的完整流程
python·ocr·vim·paddlepaddle
小程故事多_801 天前
边缘端OCR+RAG文档智能问答系统,落地实践与全场景解析
ocr·rag
去码头整点薯条ing2 天前
某当网登录滑块【协议+OCR】
爬虫·python·ocr
梦远青城2 天前
Docker 部署python的paddle进行OCR文字识别身份证
python·docker·ocr·paddle·身份证识别
AI人工智能+2 天前
银行回单识别技术通过AI驱动的智能文档理解方案,实现财务处理的革命性升级
深度学习·计算机视觉·自然语言处理·ocr·银行回单识别