Integrating Tess4j with SpringBoot: Low‑Cost OCR Image Recognition
This tutorial shows how to add OCR capabilities to a SpringBoot application using the Tess4j library, covering dependency setup, Tesseract engine initialization, RESTful endpoint implementation, training data choices, and practical tips for handling resources and deployment.
Overview
SpringBoot can expose OCR functionality through a lightweight REST service by using tess4j, a Java wrapper for the Tesseract engine. The service processes image files and returns the recognized text.
Demo
The expected OCR result is shown in the following image.
Implementation
1. Maven Dependency
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
</dependency>2. Tesseract Engine Initialization
When the application is packaged as a JAR,
new ClassPathResource("tess_data").getFile().getAbsolutePath()may fail. The article recommends copying the resource to a temporary location (e.g., via a TensorflowUtil helper) before passing the path to Tesseract.
On Linux, the native library net.sourceforge.tess4j.TessAPI must be loadable; all required system dependencies need to be installed.
3. Service Class
public class TesseractOcrModelService {
private final Tesseract tesseract = new Tesseract();
public TesseractOcrModelService() {
try {
String folderPath = new ClassPathResource("tess_data").getFile().getAbsolutePath();
// OEM_TESSERACT_LSTM_COMBINED = 2 – run both Tesseract and LSTM, fallback to Tesseract when needed
tesseract.setPageSegMode(OEM_TESSERACT_LSTM_COMBINED);
tesseract.setDatapath(folderPath);
// Page segmentation mode 6 – assume a single uniform block of text
tesseract.setPageSegMode(6);
tesseract.setLanguage("chi_sim"); // Simplified Chinese
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Tesseract getTesseract() { return tesseract; }
}4. RESTful Controller
@RestController
@RequestMapping("ocr")
@RequiredArgsConstructor
public class OcrController {
private final TesseractOcrModelService tesseractOcrModelService;
@PostMapping("/detection")
public Result<String> ocrDetection(MultipartFile file) {
try {
Tesseract tesseract = tesseractOcrModelService.getTesseract();
// Recommended preprocessing: binarization, denoising, rotation correction (not implemented here)
return Result.success(tesseract.doOCR(ImageIO.read(file.getInputStream())));
} catch (Exception e) {
throw new RuntimeException("ImageIO.read(file.getInputStream()) parsing error");
}
}
}5. Training Data Options
tessdata_best– highest accuracy, slower recognition. tessdata – balanced speed and accuracy, suitable for general OCR. tessdata_fast – optimized for speed, lower accuracy.
Source Code
https://gitee.com/fateyifei/yf
Conclusion
Tess4j reliably extracts ID numbers, phone numbers, and English words. Using the free Chinese models ( chi_sim) yields lower accuracy for Chinese text. Higher quality can be achieved by training custom data sets or by invoking third‑party OCR APIs such as Google Cloud Vision, Microsoft Azure OCR, or Amazon Textract.
Potential application scenarios include document digitization, automatic data entry from scanned tables, license‑plate recognition, and handwritten text conversion.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer XiaoFu
xiaofucode.com – a programmer learning guide driven by the pursuit of profit
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
