Artificial Intelligence 10 min read

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

This tutorial demonstrates how to build a Spring Boot application that extracts ID numbers and business license information from images by preprocessing them with OpenCV and recognizing text with Tesseract OCR, covering the full workflow from image handling to regex-based data extraction.

Top Architect
Top Architect
Top Architect
Implementing ID Card and Business License Recognition in Spring Boot Using OpenCV and Tesseract OCR

In this tutorial, a top‑level architect explains how to build a Spring Boot application that extracts ID numbers and business‑license information from images using OpenCV for preprocessing and Tesseract OCR for text recognition.

The process is divided into four main steps: image preprocessing (adjust size, contrast, brightness, etc.), text detection, OCR conversion, and information extraction using regular expressions.

Tools and libraries used include Tesseract OCR (open‑source optical character recognition engine), OpenCV (computer‑vision library for image processing), and Spring Boot (framework for creating Java web applications).

Code example – 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>

Code example – OCRService :

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;
        }
        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;
    }
}

Code example – ImagePreprocessing (OpenCV) :

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) {
        // Read image
        Mat image = imread(imagePath);
        // Convert to grayscale
        Mat gray = new Mat();
        cvtColor(image, gray, COLOR_BGR2GRAY);
        // Apply Gaussian blur
        Mat blurred = new Mat();
        GaussianBlur(gray, blurred, new Size(3, 3), 0);
        // Edge detection
        Mat edged = new Mat();
        Canny(blurred, edged, 75, 200);
        return edged;
    }
}

Code example – InfoExtractor (regex extraction) :

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 extracting business‑license numbers, etc.
}

Code example – DocumentProcessor (orchestrates the workflow) :

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 extracted ID number
        }
        return null;
    }
}

The OpenCV preprocessing steps—grayscale conversion, Gaussian blur, and Canny edge detection—enhance the visibility of text regions, which improves the accuracy of the subsequent Tesseract OCR step.

By integrating OpenCV preprocessing with Tesseract OCR inside a Spring Boot service, the application can reliably recognize and extract required fields such as ID numbers or business‑license codes from scanned documents.

Javaimage processingOCRSpring BootopencvTesseract
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.