ocr

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 .traineddata files for various languages.
  • Multiple recognition modes: single line, multiline, word, character, number, and unordered layout.

Basic Usage:

  1. Download .traineddata files from Tesseract tessdata GitHub.
  2. Upload the data using uploadTessData(filePath).
  3. Perform OCR using analyzeText(x1, y1, x2, y2, lang, mode).

Supported Modes (mode parameter in analyzeText):

  • singleline: Single-line text
  • multiline: Multi-line paragraph
  • singleword: Single word
  • singlechar: Single character
  • number: Numeric values
  • singlecolumn: Column of equally sized text
  • multicolumn: Multiple text columns
  • noorder: Text with no positional order

This module attaches its methods to the Device.prototype, allowing direct use via device instances.

(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:

  1. 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;

  2. 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:
-singleline: A single-line text in the same line
-multiline: The multi-line text
-singlechar: Single char
-singleword: Single word
-singlecolumn: The text of the same size in the same column
-multicolumn: The text are spread multiple column
-number: number
-noorder: The text has no order.

Returns:
string | null

Parsed content, parsing the text fails, you can get the specific error information through the lastError() function.

(inner) 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:
string fileName

The absolute path of the .traineddata file on the PC (e.g., E:/File/ocr/eng.traineddata).

Returns:
boolean

This function returns TRUE on success or FALSE on failure, you can get the specific error information through the lastError() function.