auto.waitFor();
console.show();
console.setSize(device.width, 400);
if (!requestScreenCapture()) {
toastLog("⚠️ 未获取截图权限!");
exit();
}
toastLog("✅ 已请求截图权限,准备截图");
sleep(1000); // 等待稳定
try {
log("📸 正在截图...");
let img = captureScreen();
if (img) {
toastLog("✅ 截图成功!");
} else {
toastLog("❌ 截图失败,返回空图像!");
}
} catch (e) {
toastLog("❌ 报错:" + e);
}
hamibot.exit();
代码停止在正在截图,一直卡住,后面就没有结果
第 1 条补充
· 2 个月前
回答
这个问题通常出现在截图权限不稳定或截图操作超时的情况。根据知识库中的说明,这里有几个可能的原因和解决方案:
- 截图需要时间初始化:
// 文档地址:https://docs.hamibot.com/
auto.waitFor();
console.show();
console.setSize(device.width, 400);
if (!requestScreenCapture()) {
toastLog("⚠️ 未获取截图权限!");
hamibot.exit();
}
toastLog("✅ 已请求截图权限,准备截图");
// 增加等待时间,让截图系统完全初始化
sleep(3000); // 从原来的1秒增加到3秒
try {
log("📸 正在截图...");
let img = captureScreen();
if (img) {
toastLog("✅ 截图成功!");
} else {
toastLog("❌ 截图失败,返回空图像!");
}
} catch (e) {
toastLog("❌ 报错:" + e);
}
hamibot.exit();
- 检查截图方向:
如果设备方向与截图请求方向不匹配也会卡住,可以明确指定方向:
requestScreenCapture(true); // 横屏截图
// 或
requestScreenCapture(false); // 竖屏截图
- 其他建议:
- 确保设备屏幕已解锁且稳定
- 某些设备可能需要更长的初始化时间,尝试sleep(5000)
- 检查Hamibot的截图权限是否被系统回收了
如果问题仍然存在,可以尝试重启设备或重新安装Hamibot。
补充回答 1
// 文档地址 https://docs.hamibot.com/
auto.waitFor();
if (!requestScreenCapture()) {
toastLog('没有授予 Hamibot 屏幕截图权限');
hamibot.exit();
}
sleep(1000);
// 截取当前屏幕
var screenshot = captureScreen();
// 在屏幕中查找图片(图片路径需替换为实际图片)
var targetImg = images.read('/sdcard/11.png'); // 假设图片已保存到设备
var result = images.findImage(screenshot, targetImg, {
threshold: 0.8
});
if (result) {
toastLog('找到匹配图片,位置:' + JSON.stringify(result));
} else {
toastLog('未找到匹配图片');
}
hamibot.exit();
说明:
- 需要提前将图片保存到设备存储(如
/sdcard/11.png
) threshold
参数控制匹配精度(0-1),值越大匹配越严格- 找到图片会返回坐标位置{x,y}
- 需要授予无障碍服务和屏幕截图权限
注意:图片匹配可能受分辨率、颜色变化影响
ℹ️ 本回答由 AI 生成,内容仅供参考