Spring Boot + Tess4j: Build OCR REST API Step by Step
This tutorial demonstrates how to integrate Tess4j with Spring Boot to create an OCR REST API, covering dependency setup, Tesseract engine initialization with language data configuration, image preprocessing tips, and a working controller example, plus a comparison of popular OCR engines and alternatives for improved Chinese text recognition.
Introduction
Java can implement OCR (Optical Character Recognition) to automatically extract text from images using Spring Boot and Tess4j, a Java wrapper for the Tesseract engine. This enables a lightweight RESTful service for various OCR needs without bulky external tools.
OCR Engine Comparison
Four popular OCR engines are compared:
Tesseract (Google) : 100+ languages, average Chinese optimization, good accuracy, medium speed, medium model size, simple Spring Boot integration, high maintenance activity.
PaddleOCR (Baidu) : 80+ languages, excellent Chinese optimization, excellent accuracy, fast speed, larger model size, medium Spring Boot integration, very high maintenance activity.
EasyOCR (aided AI) : 80+ languages, good Chinese optimization, good accuracy, fast speed, medium model size, simple Spring Boot integration, high maintenance activity.
TrOCR (Microsoft) : Multi-language, good Chinese optimization, excellent accuracy, medium speed, medium model size, complex Spring Boot integration, medium maintenance activity.
Function Demonstration
Result of OCR detection on a sample image:
Implementation
1. Description
Spring Boot combined with Tess4j provides a Java-based OCR solution. Tess4j wraps Tesseract, allowing easy integration into Java applications. The stack yields a lightweight RESTful service for OCR tasks.
2. Coding Implementation
2.1 Add Dependency
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
</dependency>2.2 Initialize Tesseract Engine
Deployment considerations:
Using
new ClassPathResource("tess_data").getFile().getAbsolutePath()may fail after packaging as a JAR. Refer to open-source projects like TensorflowUtil to copy resource files before loading.
In Linux, resolve net.sourceforge.tess4j.TessAPI initialization issues by ensuring all native dependencies and system configurations are correct.
Training data: Different training data affects accuracy and speed. Free training data sets include: tessdata_best: High accuracy, slower recognition. tessdata: Standard balance of speed and accuracy. tessdata_fast: Faster recognition, slightly lower accuracy.
/** TesseractOcr model loading */
@Slf4j
@Getter
@Component
public class TesseractOcrModelService {
private final Tesseract tesseract = new Tesseract();
public TesseractOcrModelService() {
try {
// Get training model folder (this method has issues when packaged as JAR; use TensorflowUtil)
String folderPath = new ClassPathResource("tess_data").getFile().getAbsolutePath();
/* OEM modes:
* OEM_TESSERACT_ONLY = 0: Tesseract only
* OEM_LSTM_ONLY = 1: LSTM only
* OEM_TESSERACT_LSTM_COMBINED = 2: Combined
* OEM_DEFAULT = 3: Auto
*/
tesseract.setOcrEngineMode(2); // OEM_TESSERACT_LSTM_COMBINED
// Set training data path
tesseract.setDatapath(folderPath);
tesseract.setPageSegMode(6);
// Set language to Simplified Chinese
tesseract.setLanguage("chi_sim");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}2.3 Write RESTful Interface
/** OCR Controller */
@RestController
@RequestMapping("/ocr")
@RequiredArgsConstructor
public class OcrController {
private final TesseractOcrModelService tesseractOcrModelService;
@PostMapping("/detection")
public Result<String> ocrDetection(MultipartFile file) {
try {
/* Image preprocessing recommendations:
* Binarization: convert to black/white for contrast
* Denoising: remove noise
* Rotation correction: ensure text is horizontal
*/
Tesseract tesseract = tesseractOcrModelService.getTesseract();
return Result.success(tesseract.doOCR(ImageIO.read(file.getInputStream())));
} catch (Exception e) {
throw new RuntimeException("ImageIO.read(file.getInputStream()) parsing error");
}
}
}Source Code
https://gitee.com/fateyifei/yf
Conclusion
Tess4j performs well for ID numbers, phone numbers, and English words, but Chinese recognition with free training data is relatively poor. For higher quality requirements, consider:
Custom training: Train with custom datasets to improve accuracy for specific text types or languages.
Third-party APIs: Use professional OCR services like Google Cloud Vision, Microsoft Azure OCR, or Amazon Textract for higher accuracy and more features.
Additional applications of Tess4j include:
Document digitization: Convert paper documents to editable electronic text.
Automated data entry: Extract data from scanned forms, invoices, etc.
License plate recognition: Identify plate numbers from traffic camera images.
Handwriting recognition: Convert handwritten content to digital text.
Although these approaches may involve extra cost and setup, they significantly improve recognition results to meet higher demands.
Reference
juejin.cn/post/7408619644195766272
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.
SpringMeng
Focused on software development, sharing source code and tutorials for various systems.
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.
