Implementing ID Card and Business License Recognition in Spring Boot Using Tesseract OCR and OpenCV
This article explains how to build a Spring Boot service that preprocesses images with OpenCV, extracts text using Tesseract OCR, and then parses identification numbers such as ID cards and business licenses via regular expressions, providing complete code examples and dependency details.
In Spring Boot, recognizing information like ID numbers and business license codes from images can be achieved through a series of steps: image preprocessing, text detection, OCR recognition, and information extraction.
Steps:
Image preprocessing: Adjust size, contrast, brightness, convert to grayscale, apply Gaussian blur, and edge detection using OpenCV to improve OCR accuracy.
Text detection: Locate text regions in the image with image‑processing algorithms.
Text recognition: Feed the detected regions to an Optical Character Recognition engine (Tesseract) to obtain raw text.
Information extraction: Use regular expressions to pull out ID numbers, business license numbers, etc., from the recognized text.
Tools and libraries:
Tesseract OCR: Open‑source OCR engine supporting multiple languages.
OpenCV: Computer‑vision library for image processing.
Spring Boot: Framework for building the backend service.
Dependency configuration (pom.xml):
<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>OCR service implementation:
public class OCRService {
public String doOCR(String imagePath) {
TessBaseAPI tessBaseAPI = new TessBaseAPI();
// Initialize Tesseract
if (tessBaseAPI.Init(".", "ENG") != 0) {
System.err.println("Could not initialize Tesseract.");
return null;
}
// Read image
Mat image = opencv_imgcodecs.imread(imagePath);
// Preprocess (e.g., convert to gray)
cvtColor(image, image, COLOR_BGR2GRAY);
// Set image for Tesseract
tessBaseAPI.SetImage(image.data(), image.cols(), image.rows(), 1, image.step());
// Perform OCR
BytePointer outText = tessBaseAPI.GetUTF8Text();
String result = outText.getString();
outText.deallocate();
tessBaseAPI.End();
return result;
}
}Information extraction example:
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 for business license, etc.
}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 extracted ID number
}
return null;
}
}Image preprocessing class (OpenCV):
public class ImagePreprocessing {
public Mat preprocessImage(String imagePath) {
// Read image
Mat image = imread(imagePath);
// Convert to gray
Mat gray = new Mat();
cvtColor(image, gray, COLOR_BGR2GRAY);
// 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;
}
}Finally, integrate the preprocessing step into OCRService by creating an ImagePreprocessing instance, obtaining the preprocessed Mat , and passing its data to Tesseract. This combination of OpenCV and Tesseract within a Spring Boot backend improves the accuracy of text recognition for ID cards and business licenses.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.