Optical Character Recognition (OCR) converts printed text from images in to machine‑editable strings using image processing and pattern recognition. It enables digitization of documents and automated data entry, improving efficiency across many domains. Modern OCR benefits from AI advances, delivering higher accuracy and speed for digital transformation.
Tess4j provides a Java wrapper around the Tesseract OCR engine. To use it, declare the dependancy:
Gradle (Groovy)
implementation 'net.sourceforge.tess4j:tess4j:5.8.0'
Gradle (Kotlin)
implementation("net.sourceforge.tess4j:tess4j:5.8.0")
Maven
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>5.8.0</version>
</dependency>
Use the latest version to take advantage of improved recognition rates.
Chinese language support requires the chi_sim.traineddata file, available from the Tesseract tessdata repository.
Basic usage involves:
- Instantiating the OCR engine.
- Pointing to the folder containing trained data.
- Selecting the language.
- Executing recognition.
Example:
Tesseract ocrEngine = new Tesseract();
ocrEngine.setDatapath("./src/main/resources/traineddata");
ocrEngine.setLanguage("chi_sim");
String resultText = ocrEngine.doOCR(new File("./src/main/resources/static/sample.png"));
Output demonstrates reasonable accuracy for Chinese text, though clarity of source imagery affects results.
In a Spring Boot application, separate responsibilities into controller, service, and configuration layers.
Configuration – OCRConfig.java
package com.example.ocr.config;
import net.sourceforge.tess4j.Tesseract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
@Configuration
public class OCRConfig {
private final ResourceLoader loader;
public OCRConfig(ResourceLoader resLoader) {
this.loader = resLoader;
}
@Bean
public Tesseract createOCR() throws Exception {
String dataDir = loader.getResource("classpath:/traineddata").getFile().getPath();
Tesseract engine = new Tesseract();
engine.setDatapath(dataDir);
engine.setLanguage("chi_sim");
return engine;
}
}
The bean initializes the OCR engine with the location of language packs stored under resources/traineddata.
Service – OCRService.java
package com.example.ocr.service;
import net.sourceforge.tess4j.Tesseract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
@Service
public class OCRService {
private final Tesseract ocr;
@Autowired
public OCRService(Tesseract engine) {
this.ocr = engine;
}
public String recognizeText(MultipartFile upload) throws Exception {
BufferedImage img = ImageIO.read(upload.getInputStream());
return ocr.doOCR(img);
}
}
The service receives an uploaded image, converts it to a BufferedImage, and returns recognized text.
Controller – OCRController.java
package com.example.ocr.controller;
import com.example.ocr.service.OCRService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class OCRController {
private final OCRService processor;
@Autowired
public OCRController(OCRService svc) {
this.processor = svc;
}
@PostMapping("/ocr")
public String extractText(@RequestParam("image") MultipartFile imageFile) throws Exception {
return processor.recognizeText(imageFile);
}
}
The endpoint accepts a multipart file parameter named image and responds with the extracted string.
Testing via HTTP client confirms correct operation of the OCR endpoint.