How To

Use Java & JVM

Using the native RustO! OCR engine in Java, Kotlin, and Scala applications via JNA.

The rusto-java package provides high-performance JVM bindings using Java Native Access (JNA). It implements AutoCloseable for clean resource reclamation in try-with-resources blocks and encapsulates native pointers behind type-safe Java classes.

Installation

Gradle (Kotlin DSL)

repositories {
    mavenCentral()
    maven { url = uri("https://jitpack.io") }
}

dependencies {
    implementation("com.github.byrizki.rusto-rs:rusto-java:0.3.2")
}

Maven (pom.xml)

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.github.byrizki.rusto-rs</groupId>
    <artifactId>rusto-java</artifactId>
    <version>0.3.2</version>
</dependency>

Ensure librusto is present in jna.library.path or java.library.path.


1. Engine Initialization

import com.byrizki.rusto.RustO;
import com.byrizki.rusto.TextResult;

public class Main {
    public static void main(String[] args) {
        String config = """
            {
                "detection": { "model_path": "models/PPOCR_v6/det.rten" },
                "recognition": { "model_path": "models/PPOCR_v6/rec.rten", "dict_path": "models/PPOCR_v6/dict.txt" }
            }
            """;

        try (RustO engine = RustO.initialize(config)) {
            // Engine is ready for recognition
        }
    }
}

2. Text Recognition

Structured Line Results

try (RustO engine = RustO.initialize(config)) {
    List<TextResult> results = engine.detectTextFile("receipt.jpg", "{}");

    for (TextResult r : results) {
        System.out.printf("[%0.2f] %s%n", r.score(), r.text());
        System.out.printf("  Frame: left=%0.1f, top=%0.1f, width=%0.1f, height=%0.1f%n",
            r.frame().left(), r.frame().top(), r.frame().width(), r.frame().height());
    }
}

From In-Memory Bytes

byte[] imageBytes = java.nio.file.Files.readAllBytes(java.nio.file.Path.of("invoice.png"));
List<TextResult> results = engine.detectTextData(imageBytes, "{}");

3. Spatial Layout Reconstruction

String spatial = engine.detectTextFileSpatial("invoice.png", "{}");
System.out.println("--- Reconstructed Spatial Document ---");
System.out.println(spatial);