脚本引擎 API

视觉检测 API

runtime.vision.* 文字识别、图片模板匹配、颜色检测。支持区域限定、等待出现/消失、点击匹配目标。

当前:视觉检测 API
脚本引擎 API

视觉检测 API

runtime.vision.* 文字识别、图片模板匹配、颜色检测。支持区域限定、等待出现/消失、点击匹配目标。

概述

基于屏幕截图的文字识别(OCR)、图片模板匹配和像素颜色检测。区域参数支持两种格式:① 4 个独立整数 (left, top, right, bottom),全部为 0 时使用全屏;② 区域对象 {left: L, top: T, right: R, bottom: B},可省略不需要的字段(默认 0)。图片匹配使用 OpenCV 模板匹配算法,阈值范围 0~1。颜色值使用 0xRRGGBB 格式的十六进制整数。

文字检测方法

  • runtime.vision.textExists(text, [regionOrLeft?, top?, right?, bottom?]): boolean — 检测指定区域是否存在指定文字。区域可用 {left, top, right, bottom} 对象或 4 个独立整数。
  • runtime.vision.waitForText(text, timeoutMs?: 10000, [regionOrLeft?, top?, right?, bottom?]): boolean — 等待文字出现,轮询间隔 200ms。
  • runtime.vision.waitForTextGone(text, timeoutMs?: 10000, [regionOrLeft?, top?, right?, bottom?]): boolean — 等待文字消失,轮询间隔 200ms。
  • runtime.vision.clickText(text, [regionOrLeft?, top?, right?, bottom?]): boolean — 先检测文字是否存在(区域),存在则点击该文字。返回是否点击成功。

图片模板匹配方法

图片模板需要先截取目标区域的截图,转换为 Base64 编码字符串传入。threshold 控制匹配严格度,0.8~0.95 为常用范围。

  • runtime.vision.imageExists(templateBase64, threshold?: 0.8): boolean — 检测屏幕中是否包含指定图片模板。
  • runtime.vision.clickImage(templateBase64, threshold?: 0.8, [regionOrLeft?, top?, right?, bottom?]): boolean — 检测图片模板并点击匹配位置中心。区域可选。
  • runtime.vision.waitForImage(templateBase64, timeoutMs?: 10000, threshold?: 0.8): boolean — 等待图片出现,轮询间隔 200ms。
  • runtime.vision.imageGone(templateBase64, timeoutMs?: 10000, threshold?: 0.8): boolean — 等待图片消失,轮询间隔 200ms。

颜色检测方法

颜色值格式为 0xRRGGBB 十六进制整数,例如红色 = 0xFF0000。tolerance 为颜色容差(0~255),值越大越宽松。

  • runtime.vision.colorAt(x, y, color, tolerance?: 10): boolean — 检测屏幕指定坐标点的像素颜色是否匹配。
  • runtime.vision.clickColor(x, y, color, tolerance?: 10): boolean — 检测坐标点颜色并点击该点(颜色匹配时 tap 坐标,等效于 ClickColor 节点)。
  • runtime.vision.colorRegion([regionOrLeft?, top?, right?, bottom?], color, tolerance?: 10, matchRatio?: 1.0): boolean — 检测指定区域内是否包含目标颜色。区域支持对象格式 {left, top, right, bottom}
  • runtime.vision.waitForColor(x, y, color, timeoutMs?: 10000, tolerance?: 10): boolean — 等待坐标点出现目标颜色,轮询间隔 200ms。

其他方法

  • runtime.vision.checkVision(): string — 检查当前视觉帧的状态信息,返回描述字符串。

调用示例

// 全屏文字检测 + 点击
if (runtime.vision.textExists("确认")) {
    runtime.process.log("确认按钮存在");
    runtime.vision.clickText("确认");  // 等效 findAndClick
}

// 区域文字检测 + 等待
if (runtime.vision.waitForText("领取奖励", 10000, 0, 0, 540, 400)) {
    runtime.process.log("顶部区域出现领取奖励");
    runtime.vision.clickText("领取奖励", 0, 0, 540, 400);
}

// 等待文字消失
if (runtime.vision.waitForTextGone("加载中", 15000)) {
    runtime.process.log("加载完成");
}
// 图片模板匹配
var base64 = "iVBORw0KGgo...";  // 截图的 Base64 编码
if (runtime.vision.imageExists(base64, 0.85)) {
    runtime.process.log("图片匹配成功");
}

// 点击图片(自动计算匹配框中心坐标)
runtime.vision.clickImage(base64, 0.9);

// 等待图片出现
if (runtime.vision.waitForImage(base64, 5000, 0.85)) {
    runtime.process.log("图片已出现");
    runtime.vision.clickImage(base64, 0.85);
}

// 等待图片消失
if (runtime.vision.imageGone(base64, 10000, 0.85)) {
    runtime.process.log("图片已消失");
}
// 颜色检测
var red = 0xFF0000;
if (runtime.vision.colorAt(500, 800, red, 15)) {
    runtime.process.log("坐标 (500,800) 处为红色");
}

// 区域颜色检测
if (runtime.vision.colorRegion(0, 0, 200, 200, red, 20, 0.5)) {
    runtime.process.log("左上角区域 50% 以上为红色");
}

// 等待颜色
if (runtime.vision.waitForColor(540, 1100, 0x00FF00, 5000)) {
    runtime.process.log("绿色按钮已出现 (5秒内)");
    runtime.touch.tap(540, 1100);
}
// 综合示例:等待目标出现并操作
var targetText = "立即领取";
var targetColor = 0xFFCC00;

runtime.process.log("等待目标文字或颜色出现");
var found = runtime.vision.waitForText(targetText, 8000);
if (found) {
    runtime.vision.clickText(targetText);
} else if (runtime.vision.waitForColor(540, 1000, targetColor, 5000)) {
    runtime.touch.tap(540, 1000);
} else {
    runtime.process.error("目标未出现");
    runtime.process.stop();
}
// 区域对象格式示例(支持对象或 4 个独立整数混用)
// 区域对象可省略不需要的字段(默认 0)
runtime.vision.textExists("确认", {left: 100, top: 200, right: 500, bottom: 400});
runtime.vision.clickText("领取奖励", {left: 0, right: 540}); // 只限制左右
runtime.vision.colorRegion({left: 100, top: 200, right: 300, bottom: 400}, 0xFF0000, 15, 0.5);

// 图片检测 + 区域限定
var b64 = "iVBORw0KGgo...";
runtime.vision.clickImage(b64, 0.85, {left: 50, top: 50, right: 400, bottom: 700});