This module provides OCR (Optical Character Recognition) functionality using Tesseract engine.
It enables text recognition from screen regions on Android devices, supporting multiple languages and layout modes.
Key Features:
- Analyze screen content with support for English, Chinese, numbers, and multi-column layouts.
- Upload
.traineddatafiles for various languages. - Multiple recognition modes: single line, multiline, word, character, number, and unordered layout.
Basic Usage:
- Download
.traineddatafiles from Tesseract tessdata GitHub. - Upload the data using
uploadTessData(filePath). - Perform OCR using
analyzeText(x1, y1, x2, y2, lang, mode).
Supported Modes (mode parameter in analyzeText):
singleline: Single-line textmultiline: Multi-line paragraphsingleword: Single wordsinglechar: Single characternumber: Numeric valuessinglecolumn: Column of equally sized textmulticolumn: Multiple text columnsnoorder: Text with no positional order
This module attaches its methods to the Device.prototype, allowing direct use via device instances.
Functions
(inner) analyzeText(x1, y1, x2, y2, lang, mode) → {string|null}
Optical Character Recognition(OCR), tesseract is used for text detection on mobile devices, Non-English language texts are supported.
General steps:
-
Download [lang].traineddata file, download address.
If you want to parse English text, you can download "eng.traineddata" file;
If you want to parse Chinese text, you can download "chi_sim.traineddata" file; -
Upload the "*.traineddata" file.
Upload this file using the JS API "uploadTessData(fileName)".
Examples
var { Device } = require("sigma/device");
var sigmaDevice = Device.getMain();
var ret = sigmaDevice.uploadTessData("E:/File/ocr/eng.traineddata");
3. Using this interface, analyze text.
// Example: Analyze text on the current screen in region (7, 355, 680, 622)
var { Device } = require("sigma/device");
var ocr = require("sigma/ocr");
var sigmaDevice = Device.getMain();
if (sigmaDevice != null) {
const ret = sigmaDevice.analyzeText(7, 355, 680, 622, "eng", "singleline");
if (ret != null) {
print("Congratulations, this API executes successfully.\nReturn value: " + ret);
} else {
print("Sorry! " + lastError() + "\nReturn value: " + ret);
}
} else {
print("Failed to get the master device object");
}
// Operation Result:
Return value:
RSTA offers elite competitive
tennis training integrated With
Ross School’s unique academic
curriculum.
Parameters:
| number | x1 |
X coordinate of the upper left corner of the screen. |
| number | y1 |
Y coordinate of the upper left corner of the screen. |
| number | x2 |
X coordinate of the lower right corner of the screen. |
| number | y2 |
Y coordinate of the lower left corner of the screen. |
| string | lang |
Text detection on mobile devices, choose the corresponding language as needed; For example, [lang].traineddata, if you use the eng.traineddata file, The value of this lang is eng. |
| string | mode |
Analyze text mode: |
Returns:
| string | null |
Parsed content, parsing the text fails, you can get the specific error information through the lastError() function. |
| string | fileName |
The absolute path of the |
Returns:
| boolean |
This function returns TRUE on success or FALSE on failure, you can get the specific error information through the lastError() function. |