hamibot的图色检索怎么做?有例子吗?例如固定某个区域进行图色检索点击。全屏图色检索点击。

测试脚本 · 6 小时前

回答

Hamibot AI
1
· 6 小时前

文档地址 https://docs.hamibot.com/

Hamibot 的图色检索主要通过 images.findColorimages.findMultiColorscaptureScreen() 等函数实现,结合 click() 可以完成点击操作。以下是具体的使用方法和示例。


1. 固定区域图色检索并点击

在指定区域内查找某个颜色,找到后点击其坐标。

// 启用无障碍服务
auto.waitFor();

// 请求截图权限
if (!requestScreenCapture()) {
    toastLog('没有授予 Hamibot 屏幕截图权限');
    hamibot.exit();
}

sleep(1000);

// 截取当前屏幕
var img = captureScreen();

// 定义目标颜色(例如红色 #ff0000)
var targetColor = '#ff0000';

// 指定查找区域:[x, y, width, height],例如在 (500, 800) 范围内宽 200 高 300 的区域
var region = [500, 800, 200, 300];

// 执行区域找色
var point = images.findColor(img, targetColor, {
    region: region,
    threshold: 4 // 颜色相似度阈值,越小越严格
});

if (point) {
    toastLog('找到颜色,坐标:' + point.x + ', ' + point.y);
    click(point.x, point.y);
} else {
    toastLog('在指定区域未找到该颜色');
}

2. 全屏图色检索并点击

在整个屏幕上查找某一颜色,并点击。

auto.waitFor();

if (!requestScreenCapture()) {
    toastLog('没有授予 Hamibot 屏幕截图权限');
    hamibot.exit();
}

sleep(1000);

var img = captureScreen();
var targetColor = '#0000ff'; // 查找蓝色

// 全屏找色(不指定 region 即为全屏)
var point = images.findColor(img, targetColor, {
    threshold: 4
});

if (point) {
    toastLog('全屏找到颜色,坐标:' + point.x + ', ' + point.y);
    click(point.x, point.y);
} else {
    toastLog('全屏未找到该颜色');
}

3. 多点找色(更精确识别)

当单点颜色容易误判时,可以使用多点找色,确保目标位置周围的颜色也匹配。

auto.waitFor();

if (!requestScreenCapture()) {
    toastLog('没有授予 Hamibot 屏幕截图权限');
    hamibot.exit();
}

sleep(1000);

var img = captureScreen();
// 第一个点颜色
var mainColor = '#f64d30';
// 相对于第一个点的其他颜色点 [偏移x, 偏移y, 颜色]
var extraColors = [
    [5, 5, '#ffffff'],
    [10, 10, '#000000']
];

var point = images.findMultiColors(img, mainColor, extraColors, {
    region: [0, 0, 1080, 1920] // 可选:限制搜索区域
});

if (point) {
    toastLog('多点找色成功,坐标:' + point.x + ', ' + point.y);
    click(point.x, point.y);
} else {
    toastLog('未找到匹配的多点颜色');
}

注意事项:

  • 必须调用 requestScreenCapture() 并获得授权才能截图。
  • threshold 默认为 4,数值越大容错越高,但可能误识别。
  • 推荐使用 findMultiColors 提高识别准确率,避免因界面微小变化导致失败。

你可以根据实际场景调整颜色值、区域和阈值。

ℹ️ 本回答由 AI 生成,内容仅供参考