Enterprise Java API for Receipt OCR & Expense Parsing

Ditch fragile JNI wrappers and legacy vision libraries. Convert messy thermal receipts into strongly-typed Java objects instantly.

Java Spring Boot architecture diagram showing mobile receipt image upload converting to structured JSON data
StructOCR connects your Java backends directly to our finance-optimized OCR engine, returning ERP-ready data instantly.

The Memory and Maintenance Overhead in Java

Embedding traditional OCR engines like Tesseract into a Java application requires clunky JNI (Java Native Interface) bridges, which often lead to severe JVM memory leaks and deployment headaches. Even if you get the native libraries running, you are left with a raw text dump. Writing and maintaining thousands of regex patterns in Java to identify a 'Total', 'Tax', or a handwritten 'Tip' across infinite retail POS formats is an unscalable nightmare for enterprise development teams. When users upload crumpled, shadowed receipts from their phones, standard Java text-parsing utilities break completely.

The StructOCR Enterprise Solution

StructOCR offloads the intense computer vision processing from your JVM to our specialized infrastructure. By calling our receipt ocr api, your application receives a predictable, normalized JSON response. Our deep learning models inherently understand the spatial layout of retail bills, isolating line items, vendor details, and financial hierarchies automatically. This allows Spring Boot developers to rapidly deploy expense management automation workflows directly into corporate ERPs or banking platforms without writing a single line of image-processing code.

Ideal for Enterprise Architectures

  • Corporate ERP Integration: Automatically ingest employee reimbursement receipts directly into systems like SAP, Oracle, or NetSuite with validated financial fields.
  • Digital Banking Enrichment: Enhance mobile banking transaction feeds by allowing users to attach and parse receipts, linking line-item data to credit card charges.
  • Audit & Fraud Detection: Programmatically cross-reference extracted dates, merchant IDs, and totals to flag duplicate expense submissions at scale.

Implementation: Java 11+ HttpClient Request

A production-ready Java snippet utilizing the native `java.net.http.HttpClient` and Google's `Gson` library to parse the complex receipt response into accessible variables.

Prerequisite: JDK 11+ and Gson dependency

CODE EXAMPLE
// 💰 Skip the JVM memory leaks. Get 20 free credits instantly:
// 👉 https://structocr.com/register

package com.structocr.examples;

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.util.Base64;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class ReceiptOcrClient {

    public static void main(String[] args) throws IOException, InterruptedException {
        String apiKey = "YOUR_API_KEY"; // Replace with your StructOCR API key
        String imagePath = "path/to/uber_receipt.jpg"; // Path to the uploaded receipt

        // 1. Read the image file and encode it to Base64
        String base64Image = encodeFileToBase64(imagePath);

        // 2. Construct the JSON payload
        JsonObject payload = new JsonObject();
        payload.addProperty("img", base64Image);
        String jsonPayload = payload.toString();

        // 3. Create the modern HTTP client
        HttpClient client = HttpClient.newHttpClient();

        // 4. Build the POST request to the StructOCR Receipt endpoint
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://api.structocr.com/v1/receipt"))
                .header("x-api-key", apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
                .build();

        // 5. Execute request
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        // 6. Parse the deeply structured JSON response
        Gson gson = new Gson();
        JsonObject jsonResponse = gson.fromJson(response.body(), JsonObject.class);

        // 7. Extract the financial data safely
        if (jsonResponse.has("success") && jsonResponse.get("success").getAsBoolean()) {
            JsonObject data = jsonResponse.getAsJsonObject("data");
            
            String merchant = data.getAsJsonObject("merchant").get("name").getAsString();
            String total = data.getAsJsonObject("financials").get("total_amount").getAsString();
            String currency = data.get("currency").getAsString();

            System.out.println("✅ Extracted Receipt Data");
            System.out.println("Merchant: " + merchant);
            System.out.println("Total Paid: " + total + " " + currency);

            // Loop through line items
            System.out.println("\n--- Line Items ---");
            JsonArray items = data.getAsJsonArray("line_items");
            for (JsonElement itemElement : items) {
                JsonObject item = itemElement.getAsJsonObject();
                String desc = item.get("description").getAsString();
                String amount = item.get("amount").getAsString();
                System.out.println("- " + desc + " : " + amount);
            }

        } else {
            String error = jsonResponse.has("error") && !jsonResponse.get("error").isJsonNull() 
                ? jsonResponse.get("error").getAsString() 
                : "Unknown extraction error.";
            System.out.println("❌ Failed: " + error);
        }
    }

    private static String encodeFileToBase64(String filePath) throws IOException {
        Path path = Path.of(filePath);
        byte[] fileContent = Files.readAllBytes(path);
        return Base64.getEncoder().encodeToString(fileContent);
    }
}

Technical Specs

  • Latency: < 3s (Optimized for synchronous UI responses)
  • Uptime: 99.9% SLA
  • Security: Zero Data Retention (GDPR & SOC2 Compliant)
  • Input: JPG, PNG, WebP (Max 4.5MB)
  • Output: ERP-Ready Nested JSON Object

Key Features

  • Spring Boot Ready: A stateless, RESTful architecture that integrates effortlessly into Spring WebMvc or WebFlux (Reactive) paradigms.
  • Advanced Gratuity Parsing: Intelligently separates base subtotals from localized taxes and handwritten tips on hospitality bills.
  • Mobile-First Enhancement: Server-side algorithms automatically fix rotation, warp, and shadow issues common in employee mobile uploads.

Sample JSON Response

By receiving a highly predictable structure, you can bind the JSON payload directly to your Go interfaces without messy intermediary parsing.

{
  "success": true,
  "data": {
    "is_valid": true,
    "confidence": "high",
    "merchant_name": "Blue Bottle Coffee",
    "date": "2026-04-22",
    "time": "08:45 AM",
    "currency": "USD",
    "total_amount": 14.5,
    "tax_amount": 1.25,
    "items": [
      {
        "name": "Caffe Latte - Large",
        "quantity": 2,
        "price": "11.00"
      },
      {
        "name": "Butter Croissant",
        "quantity": 1,
        "price": "3.50"
      }
    ],
    "validation_error": null
  }
}

Frequently Asked Questions

Can I use Spring WebClient instead of Java's HttpClient?

Absolutely. Our API endpoints are completely standard REST. You can use `WebClient` for reactive, non-blocking calls, `RestTemplate` for older Spring projects, or libraries like `OkHttp` and `Retrofit`.

How does the API handle non-English receipts and foreign currencies?

The OCR engine provides extensive multi-language support and automatically detects and normalizes currency symbols (e.g., £, €, ¥, $) into standard ISO currency codes in the JSON output.

Does the API store the receipts my users upload?

No. StructOCR enforces a strict zero data retention policy. Images are processed entirely in memory to generate the JSON and are instantly purged from our servers to maintain financial compliance.

More OCR Tutorials

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