Implement OCR in Java with Tess4j and SpringBoot in Just a Few Lines

This tutorial walks you through adding optical character recognition to a Java SpringBoot project using the Tess4j library, covering prerequisites, dependency setup, engine initialization, RESTful API creation, and tips for improving accuracy with custom training data or third‑party services.

Architect
Architect
Architect
Implement OCR in Java with Tess4j and SpringBoot in Just a Few Lines

Introduction

Ever needed to copy text from an image but had to type it manually? Java can perform OCR (optical character recognition) easily without heavyweight external tools. By using the Tess4j library, you can integrate OCR into your Java applications with just a few lines of code.

Demo

The final OCR result looks like this:

OCR demo result
OCR demo result

Implementation

1. Description

We will use SpringBoot together with Tess4j, a Java wrapper for the Tesseract OCR engine. Tess4j works for scanned documents, image text extraction, and automated screenshot reading, and can be exposed as a lightweight RESTful service.

2. Code

2.1 Add Dependency

<dependency>
  <groupId>net.sourceforge.tess4j</groupId>
  <artifactId>tess4j</artifactId>
</dependency>

2.2 Initialize Tesseract Engine

/**
 * TesseractOcr model loader
 */
@Slf4j
@Getter
@Component
public class TesseractOcrModelService {
    private final Tesseract tesseract = new Tesseract();
    public TesseractOcrModelService() {
        try {
            // Path to trained data (may need custom handling when packaged as a jar)
            String folderPath = new ClassPathResource("tess_data").getFile().getAbsolutePath();
            tesseract.setDatapath(folderPath);
            tesseract.setPageSegMode(OEM_TESSERACT_LSTM_COMBINED);
            tesseract.setLanguage("chi_sim"); // Simplified Chinese
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

2.3 RESTful API

/**
 * OCR 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();
            return Result.success(tesseract.doOCR(ImageIO.read(file.getInputStream())));
        } catch (Exception e) {
            throw new RuntimeException("ImageIO.read(file.getInputStream()) parsing error");
        }
    }
}

Source Code

The complete source can be found at the repository link below.

https://gitee.com/fateyifei/yf

Conclusion

Tess4j works well for IDs, phone numbers, and English words, but its free training data may struggle with Chinese. For higher accuracy, consider custom training, or use third‑party OCR services such as Google Cloud Vision, Microsoft Azure OCR, or Amazon Textract. OCR can also be applied to document digitization, automated data entry, license‑plate recognition, and handwriting conversion.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaartificial intelligenceImage ProcessingOCRSpringBoottess4j
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.