有一个名为TP的文件夹,里面有用于图像识别的模板图片,但是现在似乎没办法按照预期点击模板为0.png的按钮,另外我想让运行状态都输出到控制台:
// ===== 主程序 =====
console.show(); // 显示控制台窗口
toast("企业微信启动中..."); // 底部弹出提示

// 启动企业微信
const wecomPackage = "com.tencent.wework";
app.launch(wecomPackage);

// 创建状态信息
const statusMsg = ✅ 企业微信已打开 📱 设备: ${device.brand} ${device.model} 🕒 时间: ${new Date().toLocaleTimeString()} 📦 包名: ${wecomPackage};

console.info(statusMsg); // 控制台输出

sleep(5000)

auto.waitFor();
if (!requestScreenCapture()) {
toast("需要开启截图权限");
hamibot.exit();
}

// 读取模板图片
var template = images.read("/TP/0.png");

// 设置匹配参数
var options = {
threshold: 0.9, // 相似度阈值,0-1之间,数值越接近1匹配越严格
region: [0, 0, device.width, device.height] // 全屏范围查找
};

// 循环查找按钮
while(true){
var screen = captureScreen(); // 截图
var point = images.findImage(screen, template, options);

if(point){
    // 找到按钮中心位置并点击
    var centerX = point.x + template.getWidth()/2;
    var centerY = point.y + template.getHeight()/2;
    click(centerX, centerY);
    toast("按钮已点击");
    break;
} else {
    toast("未找到目标按钮");
    sleep(1000);  // 1秒后重试
}

}

已存档
AOS3114 · 17 天前

回答

Hamibot AI
1
· 17 天前

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

你的代码存在几个关键问题需要修正:

  1. 图片路径错误
    Android系统读取图片需使用完整绝对路径:
// 修改路径为SD卡路径
var template = images.read("/sdcard/TP/0.png");
// 添加加载失败判断
if(!template){
    toast("模板图片加载失败");
    hamibot.exit();
}
  1. 添加坐标输出调试
// 在点击逻辑添加坐标输出
console.log("找到坐标:", point.x, point.y);
console.log("图片尺寸:", template.getWidth(), template.getHeight());
  1. 优化匹配参数
// 调整阈值到0.8-0.95之间
var options = {
    threshold: 0.85,  // 降低匹配精度要求
    region: [0, 0, device.width, device.height]
};
  1. 添加启动完成检测
// 替换原有sleep(5000)
var appName = "企业微信";
if(!waitFor(() => id("com.tencent.wework").exists(), 10000)){
    toast("等待超时");
    hamibot.exit();
}
console.info(`✅ ${appName}启动成功`);
  1. 完整控制台优化版本
console.show();
ui.run(() => {
    // 隐藏输入框
    runtime.console.findViewById(com.stardust.autojs.R.id.input_container)
        .setVisibility(android.view.View.GONE);
});

toast("企业微信启动中...");
console.info("📱 设备型号:", device.brand, device.model);
console.info("🕒 启动时间:", new Date().toLocaleTimeString());

// 启动企业微信
const APP_PACKAGE = "com.tencent.wework";
app.launch(APP_PACKAGE);

// 权限检测
if(!requestScreenCapture()){
    toast("请开启截图权限");
    hamibot.exit();
}

// 图片加载
var template = images.read("/sdcard/TP/0.png");
if(!template){
    toast("模板图片加载失败");
    hamibot.exit();
}

// 匹配参数
var options = {
    threshold: 0.85,
    region: [0, 0, device.width, device.height]
};

// 主循环
var maxRetries = 10;
for(var i=0; i<maxRetries; i++){
    var screen = captureScreen();
    var point = images.findImage(screen, template, options);
    
    if(point){
        var centerX = point.x + template.getWidth()/2;
        var centerY = point.y + template.getHeight()/2;
        click(centerX, centerY);
        console.info("🎯 按钮点击成功:", centerX, centerY);
        break;
    }
    
    console.warn(`🔍 第${i+1}次查找失败`);
    sleep(1500);
}

if(!point) console.error("❌ 达到最大重试次数仍未找到");

建议:

  1. 使用 images.detectsColor() 检测按钮颜色特征
  2. 在企业微信启动后添加 auto.waitFor()
  3. 使用 device.widthdevice.height 添加屏幕比例适配逻辑
ℹ️ 本回答由 AI 生成,内容仅供参考