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 "";
        }
    }
}

亲测,可用

相关推荐
Sour14 天前
PDF翻译卡住不动怎么办?扫描件、OCR 和大文件排查清单
前端·pdf·ocr
旗讯数字14 天前
旗讯 OCR 工业手写识别解决方案|破解车间纸质表单录入难题,加速生产数字化转型
大数据·ocr
XTIOT66614 天前
多形态护照 OCR 读取器传输机制、识别算法与行业落地技术对比
大数据·人工智能·嵌入式硬件·物联网·ocr
天天代码码天天14 天前
用 TensorRT 加速 PP-OCR:一套 C++ DLL + C# 调用的高性能 OCR 推理方案
c++·c#·ocr
2401_8856651915 天前
基于OpenCV的模板匹配OCR实战:银行卡与身份证数字识别完整教程
人工智能·python·opencv·计算机视觉·ocr
东集Seuic15 天前
食品标签新规 GB 7718-2025 倒计时:产线“首件检验”如何用东集小码哥CRUISE Ge2-M跑通 OCR 智能核对?
大数据·人工智能·ocr
小鹏linux15 天前
鸿蒙PC迁移:Tesseract OCR C++ 三方库鸿蒙适配全记录
c++·ocr·harmonyos
开开心心就好15 天前
自动生成小学数学题库支持导出Word
人工智能·安全·leetcode·贪心算法·ocr·音视频·语音识别
FL162386312916 天前
基于C#winform使用纯opencv部署ppocrv5和ppocrv6的onnx模型进行OCR文件检测识别
opencv·c#·ocr
AI人工智能+17 天前
智能文档抽取系统以专业的文档解析底座和大模型智能语义理解能力为核心,洞察文档的语义内涵与逻辑结构
深度学习·自然语言处理·ocr·文档抽取