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.
Methods
(static) 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:
| Name | Type | Description |
|---|---|---|
x1 |
number | X coordinate of the upper left corner of the screen. |
y1 |
number | Y coordinate of the upper left corner of the screen. |
x2 |
number | X coordinate of the lower right corner of the screen. |
y2 |
number | Y coordinate of the lower left corner of the screen. |
lang |
string | 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. |
mode |
string | Analyze text mode: |
Returns:
Parsed content, parsing the text fails, you can get the specific error information through the lastError() function.
- Type
- string | null
(static) uploadTessData(fileName) → {boolean}
Upload the traineddata file. As for uploading the traineddata file, you can parse the text on the screen with the JS API "analyzeText".
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;
Example
// Example: Upload the English OCR training data and perform text recognition
var { Device } = require("sigma/device");
var ocr = require("sigma/ocr");
var sigmaDevice = Device.getMain();
if (sigmaDevice != null) {
const res = sigmaDevice.uploadTessData("E:/File/ocr/eng.traineddata");
if (res == true) {
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("Sorry! Uploading the file failed. " + lastError() + "\nReturn value: " + res);
}
} else {
print("Failed to get the master device object");
}
// Operation Result (if successful):
Congratulations, this API executes successfully.
Return value:
RSTA offers elite competitive
tennis training integrated With
Ross School’s unique academic
curriculum.
Parameters:
| Name | Type | Description |
|---|---|---|
fileName |
string | The absolute path of the |
Returns:
This function returns TRUE on success or FALSE on failure, you can get the specific error information through the lastError() function.
- Type
- boolean