OCRResult
OCRResult
ocr.OCRResult
One element of the array returned by OCR.detect: recognized text, confidence, axis-aligned bounds, optional rotation / rotationConfidence, and a helper click() that taps the box center via accessibility.
Table of contents
Constructors
Properties
Methods
Constructors
constructor
• new OCRResult(javaObject, bounds, text, confidence, rotation, rotationConfidence)
Internal constructor—normally you only receive instances from detect.
Parameters
| Name | Type | Description |
|---|---|---|
javaObject | JavaObject | Native OCR result handle. |
bounds | Rect | Bounding box in image coordinates. |
text | string | Recognized UTF-8 text. |
confidence | number | Quality score 0…1. |
rotation | number | Clockwise angle degrees when rotation detection is enabled. |
rotationConfidence | number | Confidence for rotation, 0…1. |
Properties
javaObject
• Readonly javaObject: JavaObject
Underlying JNI object. Rarely needed from script; some engines may expose extra fields (lines, tokens, etc.).
bounds
• Readonly bounds: Rect
Bounding quadrilateral projected to a Rect: left, top, right, bottom, plus derived centerX, centerY, width, height.
Field cheat sheet
bounds.left— left edge Xbounds.top— top edge Ybounds.right— right edge Xbounds.bottom— bottom edge Ybounds.centerX,bounds.centerY— centroid (read-only accessors)bounds.width,bounds.height— span in pixels (read-only accessors)
Example
"nodejs";
const { createOCR } = require("ocr");
const { requestScreenCapture } = require("media_projection");
async function main() {
const ocr = await createOCR();
const capturer = await requestScreenCapture();
const img = await capturer.nextImage();
const results = await ocr.detect(img);
for (const result of results) {
const bounds = result.bounds;
console.log(`text: ${result.text}`);
console.log(`origin: (${bounds.left}, ${bounds.top})`);
console.log(`size: ${bounds.width} x ${bounds.height}`);
console.log(`center: (${bounds.centerX}, ${bounds.centerY})`);
console.log(`corner: (${bounds.right}, ${bounds.bottom})`);
}
ocr.release();
capturer.stop();
}
main();text
• Readonly text: string
Recognized string for this region.
Example
"nodejs";
const { createOCR } = require("ocr");
const { readImage } = require("image");
async function main() {
const ocr = await createOCR();
const img = await readImage("./sample.png");
const results = await ocr.detect(img);
for (const result of results) {
console.log(`recognized: ${result.text}`);
}
ocr.release();
}
main();confidence
• Readonly confidence: number
Model confidence ∈ [0, 1]; higher is better.
Rough bands
- 0.9 – 1.0 — very likely correct
- 0.7 – 0.9 — usually reliable
- 0.5 – 0.7 — verify visually
- 0.0 – 0.5 — suspect / noisy
Example
"nodejs";
const { createOCR } = require("ocr");
const { requestScreenCapture } = require("media_projection");
async function main() {
const ocr = await createOCR();
const capturer = await requestScreenCapture();
const img = await capturer.nextImage();
const results = await ocr.detect(img);
const highConfidenceResults = results.filter((r) => r.confidence > 0.8);
console.log(`high-confidence count: ${highConfidenceResults.length}`);
for (const result of highConfidenceResults) {
console.log(
`text: ${result.text}, confidence: ${(result.confidence * 100).toFixed(1)}%`,
);
}
const sortedResults = results.sort((a, b) => b.confidence - a.confidence);
console.log("top text:", sortedResults[0]?.text);
ocr.release();
capturer.stop();
}
main();rotation
• Readonly rotation: number
Clockwise rotation in degrees ([0, 360)). Meaningful only when OCRDetectionOptions.detectRotation is true; otherwise often 0 or stale.
Notes
- With
detectRotation: false(default), ignore this field for geometry. - 0° = upright reading direction; 180° = upside-down text.
Example
"nodejs";
const { createOCR } = require("ocr");
const { requestScreenCapture } = require("media_projection");
async function main() {
const ocr = await createOCR();
const capturer = await requestScreenCapture();
const img = await capturer.nextImage();
const results = await ocr.detect(img, {
detectRotation: true,
});
for (const result of results) {
if (result.rotation !== 0) {
console.log(`text: ${result.text}`);
console.log(`rotation: ${result.rotation} deg`);
console.log(`rotationConfidence: ${result.rotationConfidence}`);
}
}
ocr.release();
capturer.stop();
}
main();rotationConfidence
• Readonly rotationConfidence: number
Confidence for rotation, 0…1, meaningful only with detectRotation: true.
Notes
- With
detectRotation: false, values may be 0 or meaningless. - Closer to 1 means the angle estimate is trustworthy.
Methods
click
▸ click(): Promise<boolean>
Taps (bounds.centerX, bounds.centerY) on the live device using the accessibility service—equivalent to manually calling click(x, y) at the box center.
Notes
- Requires an enabled accessibility / automation service.
- If the on-screen UI has changed since the screenshot, the tap can miss.
- Prefer capture → detect → click in quick succession.
Example
"nodejs";
const { createOCR } = require("ocr");
const { requestScreenCapture } = require("media_projection");
const { delay } = require("lang");
async function main() {
const ocr = await createOCR();
const capturer = await requestScreenCapture();
await capturer.awaitForImageAvailable();
const img = capturer.latestImage();
const results = await ocr.detect(img);
for (const result of results) {
if (result.text.includes("OK") || result.text.includes("Confirm")) {
console.log(`button: ${result.text}`);
console.log(`center: (${result.bounds.centerX}, ${result.bounds.centerY})`);
const success = await result.click();
console.log(`click: ${success ? "ok" : "failed"}`);
if (success) {
await delay(1000);
}
break;
}
}
ocr.release();
capturer.stop();
}
main();More patterns
"nodejs";
const { createOCR } = require("ocr");
const { requestScreenCapture } = require("media_projection");
const { delay } = require("lang");
async function main() {
const ocr = await createOCR();
const capturer = await requestScreenCapture();
const img = await capturer.nextImage();
const results = await ocr.detect(img);
const targetText = "Sign in";
const targetResult = results.find((r) => r.text === targetText);
if (targetResult && targetResult.confidence > 0.8) {
await targetResult.click();
}
const highConfidenceResults = results.filter((r) => r.confidence > 0.9);
for (const result of highConfidenceResults) {
if (result.text.match(/^\d+$/)) {
await result.click();
await delay(500);
}
}
const sortedResults = results.sort((a, b) => {
if (a.bounds.top !== b.bounds.top) {
return a.bounds.top - b.bounds.top;
}
return a.bounds.left - b.bounds.left;
});
for (const result of sortedResults) {
console.log(`tap: ${result.text}`);
await result.click();
await delay(300);
}
ocr.release();
capturer.stop();
}
main();Returns
Promise<boolean>
Resolves to true if the accessibility tap succeeded, false otherwise.
