Java Driver's License OCR via a Simple REST API
Achieve 99.7%+ accuracy and sub-second latency for driver's license data extraction, directly into your Java application.

Why Driver's License OCR is Difficult
Accurate driver's license OCR is a non-trivial engineering challenge. Standard libraries like Tesseract fail on real-world inputs due to variable lighting, glare, shadows, and image skew. Parsing the PDF417 barcode requires specialized libraries that often fail on minor data corruption or non-standard encoding. Beyond the image, extracting specific fields requires maintaining complex, brittle RegEx patterns for over 200+ AAMVA jurisdiction formats, a significant maintenance burden. Calculating MRZ checksums for validation adds another layer of complexity. An in-house solution requires constant updates to handle new license designs, leading to high operational costs and inconsistent accuracy.
Enterprise-Grade Extraction with StructOCR
StructOCR replaces this complexity with a single API call. Our service utilizes pre-trained deep learning models specifically for identity documents, outperforming generalized OCR engines like Tesseract. The API pipeline includes automatic image pre-processing—deskewing, denoising, and glare correction—before analysis. This ensures high accuracy even on low-quality mobile captures. We handle PDF417 parsing and field extraction internally, returning a clean, standardized JSON object. This eliminates the need for manual parsing, Regex maintenance, and delivers predictable, structured data for immediate integration.
Production Use Cases
- Digital Onboarding (KYC): Reduce drop-off rates by pre-filling user data from Driver's Licenses in < 2 seconds.
- Fraud Prevention: Detect tampered fonts or mismatched MRZ checksums automatically.
- Global Compliance: Handle Driver's Licenses from 200+ jurisdictions without custom rules.
Implementation: Java (Standard HttpClient)
This example uses the native `java.net.http.HttpClient` introduced in Java 11. It demonstrates how to construct the JSON payload manually (dependency-free) and handle the `x-api-key` authentication.
Prerequisite: JDK 11+
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Base64;
public class StructOcrApiExample {
// 💰 Save 30%+ vs competitors. Get 200 free requests instantly:
// 👉 https://structocr.com/register
private static final String API_KEY = "YOUR_API_KEY_HERE";
private static final String API_ENDPOINT = "https://api.structocr.com/v1/driver-license";
public static void main(String[] args) {
// Replace this with your actual file path
String imagePath = "license.jpg";
try {
// 1. Read image file and encode to Base64
Path path = Path.of(imagePath);
if (!Files.exists(path)) {
System.err.println("Error: File not found at " + path.toAbsolutePath());
return;
}
byte[] imageBytes = Files.readAllBytes(path);
String base64Image = Base64.getEncoder().encodeToString(imageBytes);
// 2. Construct JSON Payload (Dependency-free approach)
// For production systems, we recommend using Jackson or Gson.
String jsonPayload = "{\"img\": \"" + base64Image + "\"}";
// 3. Create HttpClient
HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(10))
.build();
// 4. Build Request
// Important: 'x-api-key' is required in the header
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_ENDPOINT))
.header("Content-Type", "application/json")
.header("x-api-key", API_KEY)
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
System.out.println("Sending request to " + API_ENDPOINT + "...");
// 5. Send Request
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// 6. Output Result
if (response.statusCode() == 200) {
System.out.println("✅ Extraction Successful!");
// Printing raw JSON. Use a JSON parser to extract specific fields like 'document_number'.
System.out.println(response.body());
} else {
System.err.println("❌ API Error: " + response.statusCode());
System.err.println(response.body());
}
} catch (IOException | InterruptedException e) {
System.err.println("Request failed: " + e.getMessage());
Thread.currentThread().interrupt();
}
}
}Technical Specs
- •Latency: < 5s (Average)
- •Uptime: 98.5% SLA
- •Security: AES-256 Encryption & SOC2 Compliant
- •Input: JPG, PNG, WebP (Base64 Encoded)
- •Max File Size: 4.5MB
- •Output: JSON (Structured Data)
Key Features
- •Global Coverage: Supports formats from USA, EU, and Asia.
- •Date Normalization: All dates automatically formatted to YYYY-MM-DD.
- •Vehicle Class Parsing: Extracts allowed vehicle categories (e.g., A, B, C).
Sample JSON Output
StructOCR returns a normalized JSON object, regardless of the input image angle or quality.
{
"success": true,
"data": {
"type": "drivers_license",
"country_code": "USA",
"region": "CALIFORNIA",
"document_number": "D1234567",
"surname": "DRIVER",
"given_names": "JANE MARIE",
"date_of_birth": "1995-08-15",
"date_of_expiry": "2025-08-15",
"date_of_issue": "2020-08-15",
"sex": "F",
"address": "1234 ELM ST, SACRAMENTO, CA 95814",
"vehicle_class": "C"
}
}Frequently Asked Questions
How does StructOCR compare to AWS Textract or Google Vision?
General-purpose OCR services like AWS Textract or Google Vision return raw, unstructured text lines, requiring you to build and maintain complex parsers. StructOCR is a specialized, pre-trained model for identity documents. It directly returns a structured JSON with validated fields like `surname`, `date_of_birth`, and `document_number`, eliminating the post-processing development effort.
Do you store the uploaded images?
No. We operate on a zero-retention policy for customer data. Images are processed in-memory and are purged immediately after the OCR process completes. We do not store or log any uploaded image data.
How do you handle blurry or low-quality images?
Our API includes a built-in image enhancement engine that automatically attempts to correct for issues like motion blur, poor focus, and low contrast before the OCR process begins. This significantly increases the success rate for images captured by users in suboptimal conditions.
More OCR Tutorials
Java Invoice OCR API
Get high accuracy Java invoice OCR with structured JSON output. Automate AP, extract line items, and eliminate manual entry. Integrate our REST API in minutes.
Java National ID OCR API
Reliable Java API for National ID OCR. Achieve 99.8%+ accuracy for KYC automation. Get structured JSON output from any ID image. High Accuracy guaranteed.
Java Passport OCR API
Achieve 99%+ accuracy for Passport MRZ and VIZ data extraction in Java. Our API provides structured JSON output, handling glare and blur automatically.
Java VIN (Vehicle Identification Number) OCR API
Tutorial: How to use the StructOCR Java Client to extract data from VIN (Vehicle Identification Number)s. Includes code samples and JSON schema.
Precise Data Extraction and Seamless
Integration with AI-powered OCR API.
Empower your solutions with automated data extraction by
integrating best-in class StructOCR via API seamlessly.
No credit card required • Full API access included