找回密码
 立即注册
首页 业界区 业界 Spring Boot 集成 tess4j 实现图片识别文本

Spring Boot 集成 tess4j 实现图片识别文本

澹台吉星 2025-6-29 18:41:36
前言

Tesseract 是世界上最知名、应用最广泛的开源 OCR 引擎。它由 Google 积极维护,功能强大,支持多种语言和平台。虽然它在处理理想条件下的印刷文本时表现出色,但其精度会受到图像质量和复杂性的影响。它通常作为核心引擎被集成到各种应用程序、脚本和更大型的系统中,是许多需要文本提取功能的项目的首选开源解决方案。
 
一、安装 tesseract (OCR)

安装链接:Index of /tesseract (uni-mannheim.de)
1.png

二、下载训练数据

通过网盘分享的文件:tessdata各语言集合包.zip
链接: https://pan.baidu.com/s/13oPR2r7qOE6lt6SgbpWOQA 提取码: uaaw 
三、创建springboot项目

 1、导入依赖
  1. <dependency>
  2.             <groupId>net.sourceforge.tess4j</groupId>
  3.             tess4j</artifactId>
  4.             <version>5.3.0</version>
  5. </dependency>
复制代码
2、编写配置类
  1. package com.songwp.config;
  2. import net.sourceforge.tess4j.Tesseract;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. /**
  7. * @ClassName:TesseractOcrConfig
  8. * @Description: ocr配置类
  9. * @Author: songwp
  10. * @Date: 2025/6/29 19:24
  11. */
  12. @Configuration
  13. public class TesseractOcrConfig {
  14.     @Value("${tess4j.data-path}")
  15.     private String dataPath;
  16.     @Value("${tess4j.language}")
  17.     private String language;
  18.     @Bean
  19.     public Tesseract tesseract() {
  20.         Tesseract tesseract = new Tesseract();
  21.         // 设置训练数据文件夹路径
  22.         tesseract.setDatapath(dataPath);
  23.         // 设置为中文简体
  24.         tesseract.setLanguage(language);
  25.         return tesseract;
  26.     }
  27. }
复制代码
3、编写controller
  1. package com.songwp.controller;
  2. import com.songwp.service.OcrService;
  3. import lombok.extern.slf4j.Slf4j;
  4. import net.sourceforge.tess4j.TesseractException;
  5. import org.springframework.http.MediaType;
  6. import org.springframework.web.bind.annotation.PostMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import java.io.IOException;
  12. /**
  13. * @ClassName:OcrController
  14. * @Description: ocr识别controller
  15. * @Author: songwp
  16. * @Date: 2025/6/29 18:40
  17. */
  18. @RestController
  19. @RequestMapping("/ocr")
  20. @Slf4j
  21. public class OcrController {
  22.     private final OcrService ocrService;
  23.     public OcrController(OcrService ocrService) {
  24.         this.ocrService = ocrService;
  25.     }
  26.     @PostMapping(value = "/recognize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  27.     public String recognizeImage(@RequestParam("file") MultipartFile file) throws TesseractException, IOException {
  28.         log.info(ocrService.recognizeText(file));
  29.         // 调用OcrService中的方法进行文字识别
  30.         return ocrService.recognizeText(file);
  31.     }
  32. }
复制代码
5、编写service
  1. package com.songwp.service;
  2. import net.sourceforge.tess4j.TesseractException;
  3. import org.springframework.web.multipart.MultipartFile;
  4. import java.io.IOException;
  5. /**
  6. * @ClassName:OcrService
  7. * @Description: ocr识别接口
  8. * @Author: songwp
  9. * @Date: 2025/6/29 19:27
  10. */
  11. public interface OcrService {
  12.     public String recognizeText(MultipartFile imageFile) throws IOException, TesseractException;
  13. }
复制代码
5、编写service实现类
  1. package com.songwp.service.impl;
  2. import com.songwp.service.OcrService;
  3. import net.sourceforge.tess4j.Tesseract;
  4. import net.sourceforge.tess4j.TesseractException;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import javax.imageio.ImageIO;
  8. import java.awt.image.BufferedImage;
  9. import java.io.ByteArrayInputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. /**
  13. * @ClassName:OcrServiceImpl
  14. * @Description: ocr识别实现类
  15. * @Author: songwp
  16. * @Date: 2025/6/29 19:28
  17. */
  18. @Service
  19. public class OcrServiceImpl implements OcrService {
  20.     private final Tesseract tesseract;
  21.     public OcrServiceImpl(Tesseract tesseract) {
  22.         this.tesseract = tesseract;
  23.     }
  24.     /**
  25.      *
  26.      * @param imageFile 要识别的图片
  27.      * @return
  28.      */
  29.     @Override
  30.     public String recognizeText(MultipartFile imageFile) throws IOException, TesseractException {
  31.         // 转换
  32.         InputStream sbs = new ByteArrayInputStream(imageFile.getBytes());
  33.         BufferedImage bufferedImage = ImageIO.read(sbs);
  34.         // 对图片进行文字识别
  35.         return tesseract.doOCR(bufferedImage);
  36.     }
  37. }
复制代码
6、运行调试

2.png

3.png

 注:图片颜色比较多的时候有有点识别不清楚了以及一些带字体的文本
参考链接:Spring Boot 集成 tess4j 实现图片识别文本_springboot tesseract-CSDN博客

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册