Spring Boot 集成 tess4j 實現圖片識別文本
前言
Tesseract 是世界上最知名、應用最廣泛的開源 OCR 引擎。它由 Google 積極維護,功能強大,支持多種語言和平臺。雖然它在處理理想條件下的印刷文本時表現出色,但其精度會受到圖像質量和復雜性的影響。它通常作為核心引擎被集成到各種應用程序、腳本和更大型的系統中,是許多需要文本提取功能的項目的首選開源解決方案。
一、安裝 tesseract (OCR)
安裝鏈接:Index of /tesseract (uni-mannheim.de)

二、下載訓練數據
通過網盤分享的文件:tessdata各語言集合包.zip
鏈接: https://pan.baidu.com/s/13oPR2r7qOE6lt6SgbpWOQA 提取碼: uaaw
三、創建springboot項目
1、導入依賴
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>5.3.0</version>
</dependency>
2、編寫配置類
package com.songwp.config;
import net.sourceforge.tess4j.Tesseract;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName:TesseractOcrConfig
* @Description: ocr配置類
* @Author: songwp
* @Date: 2025/6/29 19:24
*/
@Configuration
public class TesseractOcrConfig {
@Value("${tess4j.data-path}")
private String dataPath;
@Value("${tess4j.language}")
private String language;
@Bean
public Tesseract tesseract() {
Tesseract tesseract = new Tesseract();
// 設置訓練數據文件夾路徑
tesseract.setDatapath(dataPath);
// 設置為中文簡體
tesseract.setLanguage(language);
return tesseract;
}
}
3、編寫controller
package com.songwp.controller;
import com.songwp.service.OcrService;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* @ClassName:OcrController
* @Description: ocr識別controller
* @Author: songwp
* @Date: 2025/6/29 18:40
*/
@RestController
@RequestMapping("/ocr")
@Slf4j
public class OcrController {
private final OcrService ocrService;
public OcrController(OcrService ocrService) {
this.ocrService = ocrService;
}
@PostMapping(value = "/recognize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String recognizeImage(@RequestParam("file") MultipartFile file) throws TesseractException, IOException {
log.info(ocrService.recognizeText(file));
// 調用OcrService中的方法進行文字識別
return ocrService.recognizeText(file);
}
}
5、編寫service
package com.songwp.service;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* @ClassName:OcrService
* @Description: ocr識別接口
* @Author: songwp
* @Date: 2025/6/29 19:27
*/
public interface OcrService {
public String recognizeText(MultipartFile imageFile) throws IOException, TesseractException;
}
5、編寫service實現類
package com.songwp.service.impl;
import com.songwp.service.OcrService;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @ClassName:OcrServiceImpl
* @Description: ocr識別實現類
* @Author: songwp
* @Date: 2025/6/29 19:28
*/
@Service
public class OcrServiceImpl implements OcrService {
private final Tesseract tesseract;
public OcrServiceImpl(Tesseract tesseract) {
this.tesseract = tesseract;
}
/**
*
* @param imageFile 要識別的圖片
* @return
*/
@Override
public String recognizeText(MultipartFile imageFile) throws IOException, TesseractException {
// 轉換
InputStream sbs = new ByteArrayInputStream(imageFile.getBytes());
BufferedImage bufferedImage = ImageIO.read(sbs);
// 對圖片進行文字識別
return tesseract.doOCR(bufferedImage);
}
}
6、運行調試


注:圖片顏色比較多的時候有有點識別不清楚了以及一些帶字體的文本
參考鏈接:Spring Boot 集成 tess4j 實現圖片識別文本_springboot tesseract-CSDN博客
古今成大事者,不唯有超世之才,必有堅韌不拔之志!

浙公網安備 33010602011771號