Artificial Intelligence 8 min read

Implementing OCR for ID Card and Business License Recognition in Spring Boot with Tesseract and OpenCV

This article explains how to build a Spring Boot service that uses OpenCV for image preprocessing and Tesseract OCR to extract ID numbers and business license information from photos, providing step‑by‑step guidance, required dependencies, and complete Java code examples.

Architect's Guide
Architect's Guide
Architect's Guide
Implementing OCR for ID Card and Business License Recognition in Spring Boot with Tesseract and OpenCV

This guide demonstrates how to recognize information such as ID card numbers and business license codes from images within a Spring Boot application by combining OpenCV for image preprocessing and Tesseract OCR for text extraction.

Tools and Libraries

Tesseract OCR: an open‑source engine that supports multiple languages.

OpenCV: a computer‑vision library for image manipulation.

Spring Boot: the framework used to create the backend service.

Step 1 – Add Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacv</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacpp</artifactId>
        <version>1.5.6</version>
    </dependency>
    <!-- other dependencies -->
</dependencies>

Step 2 – Image Preprocessing and OCR

import org.bytedeco.javacpp.*;
import org.bytedeco.opencv.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
import org.bytedeco.tesseract.TessBaseAPI;

public class OCRService {

    public String doOCR(String imagePath) {
        TessBaseAPI tessBaseAPI = new TessBaseAPI();
        if (tessBaseAPI.Init(".", "ENG") != 0) {
            System.err.println("Could not initialize Tesseract.");
            return null;
        }
        Mat image = opencv_imgcodecs.imread(imagePath);
        cvtColor(image, image, COLOR_BGR2GRAY);
        tessBaseAPI.SetImage(image.data(), image.cols(), image.rows(), 1, image.step());
        BytePointer outText = tessBaseAPI.GetUTF8Text();
        String result = outText.getString();
        outText.deallocate();
        tessBaseAPI.End();
        return result;
    }
}

Step 3 – Information Extraction

After obtaining the raw text, regular expressions are used to pull out ID numbers or business license codes.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InfoExtractor {

    public String extractIDNumber(String text) {
        Pattern pattern = Pattern.compile("[0-9]{18}|[0-9]{15}");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }
    // Additional methods can be added for other data such as business license numbers.
}

Step 4 – Full Document Processing Flow

public class DocumentProcessor {

    public String processDocument(String imagePath) {
        OCRService ocrService = new OCRService();
        String text = ocrService.doOCR(imagePath);
        if (text != null && !text.isEmpty()) {
            InfoExtractor extractor = new InfoExtractor();
            String idNumber = extractor.extractIDNumber(text);
            return idNumber; // returns the extracted ID number
        }
        return null;
    }
}

OpenCV Image Preprocessing Example

import org.bytedeco.opencv.opencv_core.Mat;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;

public class ImagePreprocessing {

    public Mat preprocessImage(String imagePath) {
        Mat image = imread(imagePath);
        Mat gray = new Mat();
        cvtColor(image, gray, COLOR_BGR2GRAY);
        Mat blurred = new Mat();
        GaussianBlur(gray, blurred, new Size(3, 3), 0);
        Mat edged = new Mat();
        Canny(blurred, edged, 75, 200);
        return edged;
    }
}

Integration into OCR Service

public class OCRService {

    public String doOCR(String imagePath) {
        TessBaseAPI tessBaseAPI = new TessBaseAPI();
        if (tessBaseAPI.Init(".", "ENG") != 0) {
            System.err.println("Could not initialize Tesseract.");
            return null;
        }
        ImagePreprocessing preprocessing = new ImagePreprocessing();
        Mat preprocessedImage = preprocessing.preprocessImage(imagePath);
        tessBaseAPI.SetImage(preprocessedImage.data(), preprocessedImage.cols(), preprocessedImage.rows(), 1, preprocessedImage.step());
        BytePointer outText = tessBaseAPI.GetUTF8Text();
        String result = outText.getString();
        outText.deallocate();
        tessBaseAPI.End();
        return result;
    }
}

The combined use of OpenCV for preprocessing and Tesseract OCR significantly improves text recognition accuracy for documents such as ID cards and business licenses within a Spring Boot backend service.

Javaimage processingOCRSpring BootopencvTesseractID Card Recognition
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

0 followers
Reader feedback

How this landed with the community

login 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.