public final class TextRecognizer

  1. Object
  2. TextRecognizer

ImplementsAutoCloseable, VisionAnalyzer<TextRecognitionResult>

Reads the text in an image. The default recognizes the Latin script; select VisionOptions.textScript(TextScript) to read Chinese, Devanagari, Japanese or Korean text.

Reading a photographed receipt or label:

TextRecognizer recognizer = new TextRecognizer();
recognizer.process(VisionImage.fromFile(photoPath)).ready(result -> {
    textArea.setText(result.getText());
    recognizer.close();
}).except(error -> {
    Log.e(error);
    recognizer.close();
});

Reading text out of the live camera and stopping at the first line that looks like the value you want:

VisionCameraView<TextRecognitionResult> view =
        new VisionCameraView<TextRecognitionResult>(new TextRecognizer());
view.setListener(new VisionPipelineListener<TextRecognitionResult>() {
    public void result(TextRecognitionResult text, VisionImage source) {
        for (TextRecognitionResult.TextBlock block : text.getBlocks()) {
            if (block.getText().startsWith("SN-")) {
                accept(block.getText());
                return;
            }
        }
    }
    public void error(Throwable error) {
        Log.e(error);
    }
});

TextRecognitionResult.getText() is the whole page in reading order; TextRecognitionResult.getBlocks() adds per-region text, confidence, and normalized bounds. Recognize one script per analyzer – create a second analyzer when a screen needs two.

Constructors

public TextRecognizer()Creates an analyzer using the platform default backend and options, reading the Latin script.
public TextRecognizer(VisionOptions options)Creates a reusable analyzer with explicit backend and result options.

Methods

public final synchronized boolean isSupported()Tests the exact feature/backend pair configured for this analyzer.
public final synchronized AsyncResource<TextRecognitionResult> process(VisionImage image)Starts one analysis using the retained native backend.
public final synchronized void close()Idempotently releases the retained native backend.

Inherited methods

Constructor details

TextRecognizer

public TextRecognizer()
Creates an analyzer using the platform default backend and options, reading the Latin script.

TextRecognizer

public TextRecognizer(VisionOptions options)
Creates a reusable analyzer with explicit backend and result options. The writing system comes from VisionOptions.textScript(TextScript).

Parameters

options VisionOptions
configuration captured by this analyzer; null uses defaults

Method details

isSupported

public final synchronized boolean isSupported()
Tests the exact feature/backend pair configured for this analyzer.

Returns

whether this analyzer’s feature/backend is available and open

process

public final synchronized AsyncResource<TextRecognitionResult> process(VisionImage image)
Starts one analysis using the retained native backend.

Parameters

image VisionImage
immutable encoded or raw input

Returns

asynchronous typed result

close

public final synchronized void close()
Idempotently releases the retained native backend.