通过OCR,识别文字所在区域,并点击,代码怎么写??

crv3yukjc8 · 2 小时前
第 1 条补充
·
1 小时前

hamibot配置模式中的文本文档输入提取是带“”的文本,如过我输入的是数字,如何提出出不带“”的数字?代码怎么写?

第 2 条补充
·
1 小时前

如何实现在特定的父组件里面搜索需要的子组件?代码应该怎么写?

回答

Hamibot AI
1
· 2 小时前

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

要通过 OCR 识别文字并点击其所在区域,你需要结合 ocr.recognize(返回带坐标的结果)和 click(x, y) 函数来实现。以下是完整的示例代码:

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

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

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

// 使用 OCR 识别图片中的所有文字及其位置
const result = ocr.recognize(img);

if (!result || result.length === 0) {
    toastLog('未识别到任何文字');
    hamibot.exit();
}

// 查找目标文字,例如“确认”
const targetText = '确认';
let found = false;

for (let i = 0; i < result.length; i++) {
    const item = result[i];
    if (item.text.includes(targetText)) {
        // 获取文字的中心坐标
        const bounds = item.bounds;
        const x = (bounds.left + bounds.right) / 2;
        const y = (bounds.top + bounds.bottom) / 2;

        // 点击该位置
        click(x, y);
        toastLog('已点击: ' + targetText + ' 位置: (' + x + ', ' + y + ')');
        found = true;
        break;
    }
}

if (!found) {
    toastLog('未找到包含 "' + targetText + '" 的文字');
}

说明:

  • ocr.recognize(img) 返回的是一个数组,每个元素包含 textbounds(坐标范围)。
  • bounds 包含 left, top, right, bottom,你可以根据这些计算中心点进行点击。
  • click(x, y) 模拟点击屏幕坐标。

注意事项:

  • 确保设备已开启无障碍服务和截图权限。
  • OCR 识别受字体、背景、清晰度影响,复杂场景建议裁剪区域或预处理图像(如灰度化)提升准确率。
  • 如果目标文字不明确,可添加更多判断逻辑或容错匹配。

参考文档:https://docs.hamibot.com/reference/ocr


补充回答 1

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

在 Hamibot 的配置模式中,所有输入都会以字符串形式保存在 hamibot.env 中。即使你输入的是数字,它仍然是带引号的字符串。

如果你需要将其作为真正的数字(不带引号的 number 类型)使用,可以通过 JavaScript 的类型转换函数来实现。

正确写法:

// 从配置中读取值
const { myNumber } = hamibot.env;

// 转换为数字(推荐使用 Number())
const num = Number(myNumber);

// 或者使用 parseInt()(适用于整数)
// const num = parseInt(myNumber, 10);

// 使用示例
if (!isNaN(num)) {
    toastLog('转换成功: ' + num + ', 类型是: ' + typeof num);
    // 现在可以用于数学运算
    log(num + 10);
} else {
    toastLog('转换失败,输入不是有效数字');
}

示例场景:

假设你在脚本配置中定义了一个字段:

{
  "type": "text",
  "key": "myNumber",
  "label": "请输入一个数字"
}

你在表单里输入 123,虽然它是数字内容,但实际获取到的是字符串 "123"

通过 Number(hamibot.env.myNumber) 就能正确转成数值 123(无引号、number 类型)。

注意事项:

  • Number() 可处理整数和小数。
  • 如果输入为空或非数字字符(如 abc),转换结果为 NaN,务必用 isNaN() 判断。
  • 不要直接使用 + 拼接或参与数学运算而不转换,否则可能进行字符串拼接而非数值计算。

这样即可提取出“不带引号”的真正数字用于后续逻辑。


补充回答 2

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

你可以通过 findOne() 获取父组件,然后在其子控件中查找目标子组件。以下是几种常用方法:

方法一:使用 child()children() 遍历子控件

// 找到父控件
var parent = id('parent_id').findOne();

// 遍历所有子控件,查找特定子控件
parent.children().forEach(function(child) {
    if (child.text() === '目标文字') {
        child.click();
        log('找到并点击');
    }
});

方法二:使用 find() 在父控件内搜索子控件

// 找到父控件
var parent = id('parent_id').findOne();

// 在该父控件的所有后代中查找符合条件的子控件
var target = parent.find(id('target_id')).findOne();

if (target) {
    target.click();
    log('子控件已点击');
}

方法三:结合类名和文本等条件筛选

// 找到父控件
var parent = className('android.widget.ListView').findOne();

// 查找其子控件中包含特定文本的 TextView
var textChild = parent.findOne(text('设置') && className('android.widget.TextView'));

if (textChild) {
    textChild.click();
    log('找到指定文本的子控件');
}

示例场景(微信朋友圈动态点赞判断):

// 找到动态列表
var list = id('recycler_view').findOne();

// 遍历每个动态项(即每个父控件)
list.children().forEach(function(item) {
    // 在当前 item 内查找点赞图标和点赞数
    var likeIcon = item.findOne(id('feed_action_view_like'));
    var likeCountView = item.findOne(id('text_view'));

    if (likeIcon && likeCountView) {
        var likeCount = parseInt(likeCountView.text());
        if (!isNaN(likeCount) && likeCount < 10) {
            likeIcon.click();
        }
    }
});

注意事项:

  • 使用 parent.findOne(...)parent.find(...) 可以限定搜索范围为该父控件内部。
  • children() 返回直接子控件集合,find() 支持递归查找后代控件。
  • 建议优先使用 iddesc 定位,避免因布局变化导致失败。

这种方式能有效缩小查找范围,提高准确性和执行效率。

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