hamibot的截图ocr要用无障碍吗?代码例子怎么写?

090909 · 3 天前
第 1 条补充
·
3 天前

ocr后,那我要找到屏幕有提交两个字,怎么写?

第 2 条补充
·
3 天前

怎么增加文字大小,位置,颜色判断?让识别更准确

回答

Hamibot AI
1
· 3 天前

是的,Hamibot的截图OCR功能需要两个权限:

  1. 无障碍服务(必要权限)
  2. 屏幕截图权限

无障碍服务是Hamibot运行的基础权限,必须开启才能执行脚本;屏幕截图权限则是专门用于截取屏幕内容进行OCR识别的权限。

以下是完整的OCR识别代码示例:

// 文档地址 https://docs.hamibot.com/
auto.waitFor();
if (!requestScreenCapture()) {
    toastLog('没有授予 Hamibot 屏幕截图权限');
    hamibot.exit();
}
sleep(1000);
const img = captureScreen();
toastLog('开始识别');

// 方法一:获取完整识别结果(包含文字位置等信息)
const res = ocr.recognize(img);
log(res);
// res是一个对象数组,每个对象包含text、bounds等属性

// 方法二:只获取纯文本结果(更简洁)
// const text = ocr.recognizeText(img);
// toastLog(text);

hamibot.exit();

说明:

  • 必须先调用auto.waitFor()确保无障碍服务已启用
  • requestScreenCapture()会弹出屏幕截图权限请求
  • 有两种OCR识别方法:
    • ocr.recognize(img):返回完整识别结果,包含每个文字的位置信息
    • ocr.recognizeText(img):只返回纯文本结果,适合只需要文字内容的场景

首次运行时,系统会弹出屏幕截图权限请求,建议勾选"记住选择"避免每次运行都需确认。如果已开启权限但仍无法使用,可以尝试重启设备。


补充回答 1

要找到屏幕上的"提交"文字并进行操作,你需要使用OCR识别后遍历结果。以下是完整的代码示例:

// 文档地址 https://docs.hamibot.com/
auto.waitFor();
if (!requestScreenCapture()) {
    toastLog('没有授予 Hamibot 屏幕截图权限');
    hamibot.exit();
}
sleep(1000);
const img = captureScreen();
toastLog('开始识别');

// 识别屏幕上的文字
const res = ocr.recognize(img);

// 查找"提交"文字
let submitFound = false;
for (let i = 0; i < res.length; i++) {
    if (res[i].text.includes('提交')) {
        // 获取文字区域的中心点坐标
        let bounds = res[i].bounds;
        let x = (bounds.left + bounds.right) / 2;
        let y = (bounds.top + bounds.bottom) / 2;
        
        toastLog('找到"提交"按钮,坐标:(' + x + ', ' + y + ')');
        click(x, y);
        submitFound = true;
        break;
    }
}

if (!submitFound) {
    toastLog('未找到"提交"按钮');
}

hamibot.exit();

如果你发现"提交"被识别为两个单独的字("提"和"交"),可以使用以下改进版代码:

// 文档地址 https://docs.hamibot.com/
auto.waitFor();
if (!requestScreenCapture()) {
    toastLog('没有授予 Hamibot 屏幕截图权限');
    hamibot.exit();
}
sleep(1000);
const img = captureScreen();
const res = ocr.recognize(img);

// 查找连续的"提"和"交"字
let submitIndex = -1;
for (let i = 0; i < res.length - 1; i++) {
    if (res[i].text.trim() === '提' && res[i+1].text.trim() === '交') {
        submitIndex = i;
        break;
    }
}

if (submitIndex !== -1) {
    // 获取"提交"组合的中心位置
    let bounds1 = res[submitIndex].bounds;
    let bounds2 = res[submitIndex+1].bounds;
    let x = (bounds1.left + bounds2.right) / 2;
    let y = (bounds1.top + bounds1.bottom) / 2;
    
    toastLog('找到"提交"文字组合');
    click(x, y);
} else {
    toastLog('未找到"提交"文字');
}

hamibot.exit();

注意:

  1. OCR识别可能受屏幕分辨率、字体大小和背景颜色影响
  2. 如果"提交"是按钮的一部分文字,可能需要调整查找逻辑
  3. 可以在ocr.recognize(img)后添加log(JSON.stringify(res))查看完整的识别结果结构
  4. 如需更精确的匹配,可以增加文字大小、位置等额外条件判断

补充回答 2

要提高OCR识别的准确性,可以通过增加文字大小、位置和颜色等条件进行筛选。以下是增强版的代码实现:

// 文档地址 https://docs.hamibot.com/
auto.waitFor();
if (!requestScreenCapture()) {
    toastLog('没有授予 Hamibot 屏幕截图权限');
    hamibot.exit();
}
sleep(1000);
const img = captureScreen();
const res = ocr.recognize(img);

// 查找符合特定条件的"提交"文字
function findSubmitText() {
    for (var i = 0; i < res.length; i++) {
        var textItem = res[i];
        var bounds = textItem.bounds;
        
        // 1. 文字大小判断 (宽度>50像素且高度>30像素)
        var width = bounds.right - bounds.left;
        var height = bounds.bottom - bounds.top;
        if (width < 50 || height < 30) {
            continue;
        }
        
        // 2. 位置判断 (只在屏幕下半部分查找)
        var centerY = (bounds.top + bounds.bottom) / 2;
        if (centerY < device.height * 0.5) {
            continue;
        }
        
        // 3. 颜色判断 (获取文字区域中心点颜色)
        var centerX = (bounds.left + bounds.right) / 2;
        var color = images.pixel(img, centerX, centerY);
        // 检查颜色是否接近白色 (#FFFFFF),容差为40
        if (!isColorClose(color, "#FFFFFF", 40)) {
            continue;
        }
        
        // 4. 文字内容精确匹配
        if (textItem.text.trim() === '提交') {
            return {
                x: centerX,
                y: centerY,
                bounds: bounds,
                color: color
            };
        }
    }
    return null;
}

// 判断颜色是否接近目标颜色 (容差threshold)
function isColorClose(color, target, threshold) {
    var c1 = colors.parseColor(color);
    var c2 = colors.parseColor(target);
    return colors.diff(c1, c2) <= threshold;
}

// 查找并点击"提交"按钮
var submitPos = findSubmitText();
if (submitPos) {
    toastLog('找到符合条件的"提交"按钮,颜色:' + submitPos.color);
    click(submitPos.x, submitPos.y);
} else {
    toastLog('未找到符合条件的"提交"按钮');
    
    // 备用方案:查找包含"提交"的文本
    for (var i = 0; i < res.length; i++) {
        if (res[i].text.includes('提交')) {
            var bounds = res[i].bounds;
            var x = (bounds.left + bounds.right) / 2;
            var y = (bounds.top + bounds.bottom) / 2;
            toastLog('使用备用方案找到"提交"');
            click(x, y);
            break;
        }
    }
}

hamibot.exit();

关键增强点说明:

  1. 文字大小筛选

    var width = bounds.right - bounds.left;
    var height = bounds.bottom - bounds.top;
    if (width < 50 || height < 30) {
        continue;
    }
    
    • 根据实际需求调整最小尺寸阈值
    • 可改为相对屏幕尺寸的比例:width > device.width * 0.1
  2. 位置筛选

    var centerY = (bounds.top + bounds.bottom) / 2;
    if (centerY < device.height * 0.5) {
        continue;
    }
    
    • 可指定精确区域:bounds.left > 200 && bounds.right < 800
    • 对于底部按钮:centerY > device.height * 0.7
  3. 颜色筛选

    var color = images.pixel(img, centerX, centerY);
    if (!isColorClose(color, "#FFFFFF", 40)) {
        continue;
    }
    
    • 使用images.pixel()获取指定坐标颜色
    • colors.diff()计算颜色差异,阈值越小匹配越严格
    • 可针对不同背景设置不同目标颜色
  4. 高级颜色处理

    • 如果需要更精确的颜色匹配,可以取多个点的平均颜色
    • 对于渐变色按钮,可以检查边缘颜色差异
  5. 多重匹配策略

    • 先尝试精确匹配"提交"文字
    • 失败后尝试包含"提交"的文本
    • 可添加文字间距判断处理分开的"提"和"交"

提示:实际使用时,建议先运行一次只做日志记录的脚本:

log(JSON.stringify(res));

查看完整的OCR识别结果,了解目标文字的具体属性,然后针对性地设置筛选条件。

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