Use Aspose.OCR for Java on-premise API

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Redaction MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-redaction -- dnx GroupDocs.Redaction.Mcp --yes
Folders, licensing and more →

Note: The samples on this page are Java classes from the GroupDocs.Redaction for Java API. Use them from Node.js via Java by placing the classes on the JVM classpath, or by wrapping the same interfaces with java.newProxy.

This implementation is based on Aspose.OCR for Java on-premise API. Although it requires a valid license file, you can always request a temporary license for a month to evaluate.

Add the Aspose Maven repository and the aspose-ocr dependency to the Java side of your project (or include the JAR on the classpath used by the Node–Java bridge):

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>https://releases.aspose.com/java/repo/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-ocr</artifactId>
        <version>26.9.0</version>
    </dependency>
</dependencies>

Java

import com.aspose.ocr.AsposeOCR;
import com.aspose.ocr.License;
import com.aspose.ocr.OcrInput;
import com.aspose.ocr.RecognitionResult;
import com.aspose.ocr.RecognitionSettings;
import com.aspose.ocr.models.AreasType;
import com.aspose.ocr.models.DetectAreasMode;
import com.aspose.ocr.models.InputType;
import com.aspose.ocr.models.RectangleOutput;
import com.groupdocs.redaction.integration.ocr.IOcrConnector;
import com.groupdocs.redaction.integration.ocr.RecognizedImage;
import com.groupdocs.redaction.integration.ocr.TextFragment;
import com.groupdocs.redaction.integration.ocr.TextLine;

import javax.imageio.ImageIO;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class AsposeOCRStandaloneConnector implements IOcrConnector
{
    public AsposeOCRStandaloneConnector(String licenseFile) throws Exception
    {
        License lic = new License();
        lic.setLicense(licenseFile);
    }

    public RecognizedImage recognize(InputStream imageStream)
    {
        try
        {
            AsposeOCR api = new AsposeOCR();
            OcrInput ocrInput = new OcrInput(InputType.SingleImage);

            BufferedImage image = ImageIO.read(imageStream);
            ocrInput.add(image);

            RectangleOutput detectedRectangles = api.DetectRectangles(ocrInput, AreasType.LINES, false).get(0);

            RecognitionSettings recognitionSettings = new RecognitionSettings();
            recognitionSettings.setDetectAreasMode(DetectAreasMode.COMBINE);
            recognitionSettings.setRecognitionAreas(detectedRectangles.Rectangles);

            RecognitionResult result = api.Recognize(ocrInput, recognitionSettings).get(0);
            return createDtoFromResponse(result);
        }
        catch (Exception ex)
        {
            System.out.println("Aspose.OCR on-premise recognition failed: " + ex.toString());
        }
        return new RecognizedImage(new TextLine[0]);
    }

    protected RecognizedImage createDtoFromResponse(RecognitionResult result)
    {
        List<TextLine> lines = new ArrayList<TextLine>();
        for (RecognitionResult.LinesResult line : result.recognitionLinesResult)
        {
            String text = line.textInLine.trim();
            Rectangle rectangle = line.line;
            TextFragment fragment = new TextFragment(text, rectangle);
            lines.add(new TextLine(new TextFragment[] { fragment }));
        }
        return new RecognizedImage(lines.toArray(new TextLine[0]));
    }
}

First, identify rectangle areas of text using DetectRectangles() with AreasType.LINES. With this option you get a rectangle for every line of text. For word-level redaction, split each line into word fragments with their own rectangles before creating TextFragment instances.

From Node.js, pass an instance of the connector to RedactorSettings when you open a document (after the Java class is available on the classpath):

const redaction = require('@groupdocs/groupdocs.redaction');
const java = require('java');
const Color = java.import('java.awt.Color');

const connector = java.newInstanceSync('AsposeOCRStandaloneConnector', 'Aspose.OCR.lic');
const settings = new redaction.RedactorSettings(connector);
const redactor = new redaction.Redactor('sample.docx', new redaction.LoadOptions(), settings);
try {
  redactor.apply(new redaction.ExactPhraseRedaction('John Doe', new redaction.ReplacementOptions(Color.BLACK)));
  redactor.save();
} finally {
  redactor.close();
}

You can find related OCR examples here: GroupDocs.Redaction for Java examples.

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document redaction App

Along with full featured Node.js via Java library we provide simple, but powerful free Apps.

You are welcome to perform redactions for various document formats like PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Redaction App.