Building a License Plate Recognition Service with Spring Boot 3.x and OCR
This article walks through creating a server‑side license‑plate recognition system using Spring Boot 3.x, the open‑source Tesseract OCR library, and OpenCV for image preprocessing, covering project goals, Maven dependencies, core service implementations, special‑plate handling, and a REST API controller.
In this tutorial the author demonstrates how to build a Java‑based license‑plate recognition system on top of the Spring Boot 3.x framework, targeting high accuracy, fast response, and high availability for security, logistics, and traffic applications.
The project starts by defining the functional goals and requirements: automatic extraction of plate text from images or video, a user‑friendly upload interface, support for image enhancement (denoising, contrast adjustment), and the ability to handle large concurrent requests.
To enable OCR, the Maven dependencies for
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>leptonica-platform</artifactId>
<version>1.78.0-1.5.3</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>tesseract-platform</artifactId>
<version>4.1.1-1.5.3</version>
</dependency>are added to the project.
Image preprocessing is performed with OpenCV. The LicensePlateRecognitionService creates a TessBaseAPI instance, initializes it with the path to the tessdata directory, loads the input image via pixRead, sets the image for OCR, retrieves the UTF‑8 text, and finally releases resources.
import org.bytedeco.javacpp.*;
import org.bytedeco.leptonica.*;
import org.bytedeco.tesseract.*;
import java.io.File;
@Service
public class LicensePlateRecognitionService {
public String recognizeLicensePlate(String imagePath) {
TessBaseAPI api = new TessBaseAPI();
if (api.Init("/path/to/tesseract/tessdata", "eng") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}
PIX image = pixRead(imagePath);
if (image == null) {
System.err.println("Could not open input image.");
System.exit(1);
}
api.SetImage(image);
String result = api.GetUTF8Text().getString();
api.End();
pixDestroy(image);
return result;
}
}Special or abnormal plates (non‑standard colors, fonts, damaged or blurred plates) are pre‑processed by the SpecialLicensePlateService. The service loads the image in grayscale, applies fast non‑local means denoising, performs Otsu thresholding, enhances contrast with CLAHE, and writes the processed image to a specified path.
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@Service
public class SpecialLicensePlateService {
static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
public String preprocess(String imagePath) {
Mat src = Imgcodecs.imread(imagePath, Imgcodecs.IMREAD_GRAYSCALE);
Mat dst = new Mat();
Imgproc.fastNlMeansDenoising(src, dst, 10, 7, 21);
Imgproc.threshold(dst, dst, 0, 255, Imgproc.THRESH_BINARY + Imgproc.THRESH_OTSU);
CLAHE clahe = Imgproc.createCLAHE();
clahe.setClipLimit(4.0);
clahe.apply(dst, dst);
String processedImagePath = "/path/to/processed/image";
Imgcodecs.imwrite(processedImagePath, dst);
return processedImagePath;
}
}The complete request flow is exposed through a Spring MVC controller. The LicensePlateController autowires both services, preprocesses the uploaded image if needed, invokes the OCR service, and returns the recognized plate string.
@RestController
public class LicensePlateController {
@Autowired
private SpecialLicensePlateService specialLicensePlateService;
@Autowired
private LicensePlateRecognitionService licensePlateRecognitionService;
@RequestMapping("/recognize")
public String recognize(@RequestParam("image") String imagePath) {
String processedImagePath = specialLicensePlateService.preprocess(imagePath);
String licensePlate = licensePlateRecognitionService.recognizeLicensePlate(processedImagePath);
return licensePlate;
}
}The article concludes that the presented solution combines open‑source OCR and image‑processing libraries with a Spring Boot backend to deliver an end‑to‑end license‑plate recognition service that can be integrated into real‑world applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
LuTiao Programming
LuTiao Programming is a friendly community offering free programming lessons. We inspire learners to explore new ideas and technologies and quickly acquire job-ready skills.
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.
