springBoot整合 Tess4J实现OCR识别文字(图片+PDF)

1. 环境准备

  • JDK 8 或更高版本
  • Maven 3.6 或更高版本
  • Spring Boot 2.4 或更高版本
  • Tesseract OCR 引擎
  • Tess4J 库

2. 安装 Tesseract OCR 引擎

下载地址: Home · UB-Mannheim/tesseract Wiki · GitHub

linux直接安装:sudo apt-get install tesseract-ocr

3. 引入pom文件

XML 复制代码
<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>4.5.4</version>
</dependency>

4. 实现 OcrService 类,目前支持png、jpeg、jpg、pdf

注意:这里需要注意配置文件:设置Tesseract的数据路径 ,案例将tess安装到了D盘,如果是linux服务器,需求配置对应的地址,到tessdata路径 ;还需要设置识别语言 ,案例里为中文识别,如需对应的语言,需要下载对应的文件到安装目录下的tessdata文件夹中 。识别语言包下载地址:GitHub - tesseract-ocr/tessdata: Trained models with fast variant of the "best" LSTM models + legacy models

java 复制代码
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.stereotype.Service;
import java.awt.image.BufferedImage;
import java.io.File;
import org.apache.commons.io.FilenameUtils;
import javax.imageio.ImageIO;
import java.io.IOException;
 
@Service
public class OcrService {
    public String recognizeText(File imageFile) {
        ITesseract instance = new Tesseract();
        instance.setDatapath("D:\\anzhuang\\ocr\\tessdata"); // 设置Tesseract的数据路径
        instance.setLanguage("chi_sim"); // 设置识别语言
 
        // 获取文件扩展名
        String extension = FilenameUtils.getExtension(imageFile.getName());
 
        // 根据文件扩展名设置Tesseract的图像类型
        if ("png".equalsIgnoreCase(extension)) {
            instance.setTessVariable("filename", "png");
        } else if ("jpg".equalsIgnoreCase(extension) || "jpeg".equalsIgnoreCase(extension)) {
            instance.setTessVariable("filename", "jpeg");
        } else if("pdf".equalsIgnoreCase(extension)){
            try {
                return processPDF(imageFile,instance);
            } catch (Exception e) {
                e.printStackTrace();
                return "Error: " + e.getMessage();
            }
        } else {
            return "Unsupported file format: " + extension;
        }
 
        try {
            return instance.doOCR(imageFile);
        } catch (TesseractException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }
 
    private String processPDF(File pdfFile, ITesseract instance) throws IOException, TesseractException {
        PDDocument document = PDDocument.load(pdfFile);
        PDFRenderer renderer = new PDFRenderer(document);
        StringBuilder result = new StringBuilder();
 
        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = renderer.renderImageWithDPI(i, 800); // 使用300 DPI渲染图像
            File tempImageFile = File.createTempFile("page" + i, ".png");
            ImageIO.write(image, "png", tempImageFile);
            result.append(instance.doOCR(tempImageFile));
            tempImageFile.delete(); // 删除临时文件
        }
 
        document.close();
        return result.toString();
    }
}

5. OcrController实现案例:

java 复制代码
package com.fan.ocr.controller;
 
import com.fan.ocr.serivce.OcrService;
import net.sourceforge.tess4j.Tesseract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
 
@RestController
@RequestMapping("/ocr")
public class OcrController {
 
    @Autowired
    private OcrService ocrService;
 
    @PostMapping("/upload")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ResponseEntity<>("File is empty", HttpStatus.BAD_REQUEST);
        }
 
        try {
            // 将文件保存到本地
            File convFile = new File(System.getProperty("java.io.tmpdir") + "/" + file.getOriginalFilename());
            file.transferTo(convFile);
 
            // 调用OCR服务识别文字
            String result = ocrService.recognizeText(convFile);
 
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (IOException e) {
            return new ResponseEntity<>("File upload error: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

注意:初步观察,解析一页pdf耗时在30-40s之间,建议不要超过一页,可能会导致无响应或者等待时间太久。

相关推荐
小Ti客栈7 小时前
Spring Boot 集成 Springdoc-OpenAPI 与 Knife4j实现接口文档与可视化调试
java·spring boot·后端
paopaokaka_luck12 小时前
基于Springboot3+Vue3的高校选课系统(AI选课助手、协同过滤算法、分享到微博、扣扣、Echarts图形化分析)
网络·spring boot·网络协议·echarts
小Ti客栈15 小时前
Spring Boot 整合 Swagger2 和 Knife4j实现接口文档与可视化调试
java·spring boot·后端
2601_9638701715 小时前
【计算机毕业设计】基于Spring Boot的社区老年大学课程报名与学习系统的设计与实现
java·spring boot·学习
weixin_BYSJ198716 小时前
springboot3家政平台小程序--附源码00904
java·javascript·spring boot·python·django·flask·php
蓝创工坊Blue Foundry16 小时前
PaddleOCR 本地部署教程:小模型 OCR 如何完成字段提取到 Excel
pdf·自动化·ocr·excel·paddlepaddle·paddle
凤山老林17 小时前
SpringBoot 3 启用 spring.factories 后如何升级替换原有的 spring.factories ?
spring boot·后端·spring
啊啊啊迈 旋棍18 小时前
生态整合与实战篇:Spring Boot 整合 RocketMQ 完全指南
spring boot·rocketmq·java-rocketmq
减瓦19 小时前
Jackson 使用指南
java·spring boot·json
旺仔学长 哈哈20 小时前
基于SpringBoot的在线招聘测评系统的设计与实现----附源码35253+数据库文档
数据库·spring boot·后端·在线招聘