回答
Hamibot可以对屏幕元素进行截图并与打码平台对接,具体实现方法如下:
元素截图步骤
- 申请截图权限
// 文档地址 https://docs.hamibot.com/
auto.waitFor();
if (!requestScreenCapture(false)) {
toastLog('没有授予Hamibot屏幕截图权限');
hamibot.exit();
}
sleep(1000); // 等待截图服务初始化
- 识别目标元素位置
可以使用找图或找色功能定位需要截图的元素:
// 截取当前屏幕
var screen = captureScreen();
// 方法一:通过模板匹配找图(适用于有固定图像的元素)
var target = images.read('/sdcard/目标元素.png');
var pos = findImage(screen, target, {
region: [0, 0, device.width, device.height],
threshold: 0.9
});
// 方法二:通过颜色查找(适用于颜色特征明显的元素)
var pos = findColor(screen, '#FF0000', {
region: [0, 0, device.width, device.height],
threshold: 4
});
- 裁剪目标区域
if (pos) {
// 计算裁剪区域(以元素中心点,宽高各100像素为例)
var x = pos.x - 50;
var y = pos.y - 50;
var width = 100;
var height = 100;
// 确保裁剪区域在屏幕范围内
x = Math.max(0, x);
y = Math.max(0, y);
width = Math.min(width, device.width - x);
height = Math.min(height, device.height - y);
// 裁剪区域
var elementImg = images.clip(screen, x, y, width, height);
// 保存到本地(可选)
images.save(elementImg, '/sdcard/element.png');
}
与打码平台对接
- 将截图转为Base64格式
// 将图片转换为Base64
function imageToBase64(img) {
var bytes = images.toBytes(img, 'png', 100);
return android.util.Base64.encodeToString(bytes, 0);
}
var base64Str = imageToBase64(elementImg);
- 发送请求到打码平台
// 以通用打码平台API为例
function solveCaptcha(base64Image, username, password, softId) {
var url = 'http://打码平台API地址/in.php';
var params = {
method: 'POST',
body: {
username: username,
password: password,
softid: softId,
file: base64Image,
submit: 'submit'
}
};
var response = http.request(url, params);
if (response && response.statusCode == 200) {
var result = response.body.string();
if (result.startsWith('OK|')) {
return result.split('|')[1]; // 返回验证码ID
}
}
return null;
}
// 获取验证码识别结果
function getCaptchaResult(captchaId, username, password) {
var url = 'http://打码平台API地址/res.php';
var params = {
method: 'GET',
params: {
username: username,
password: password,
action: 'get',
id: captchaId
}
};
for (var i = 0; i < 30; i++) {
sleep(5000); // 每5秒查询一次
var response = http.request(url, params);
if (response && response.body.string() == 'CAPCHA_NOT_READY') {
continue;
} else if (response && response.body.string().startsWith('OK|')) {
return response.body.string().split('|')[1];
}
}
return null;
}
// 使用示例
var captchaId = solveCaptcha(base64Str, '你的打码平台账号', '你的密码', '软件ID');
if (captchaId) {
var code = getCaptchaResult(captchaId, '你的打码平台账号', '你的密码');
if (code) {
toastLog('识别结果: ' + code);
// 在这里输入识别结果
setText(0, code);
}
}
完整示例:识别验证码并输入
// 文档地址 https://docs.hamibot.com/
auto.waitFor();
if (!requestScreenCapture(false)) {
toastLog('没有授予Hamibot屏幕截图权限');
hamibot.exit();
}
sleep(1000);
// 1. 截图
var screen = captureScreen();
// 2. 定位验证码区域(这里假设验证码在屏幕坐标(300,800)附近,宽200高80)
var x = 300, y = 800, width = 200, height = 80;
var captchaImg = images.clip(screen, x, y, width, height);
// 3. 转为Base64
var bytes = images.toBytes(captchaImg, 'png', 100);
var base64Str = android.util.Base64.encodeToString(bytes, 0);
// 4. 发送到打码平台
var url = 'http://打码平台API地址/in.php';
var params = {
method: 'POST',
body: {
username: 'your_username',
password: 'your_password',
softid: 'your_softid',
file: base64Str,
submit: 'submit'
}
};
var response = http.request(url, params);
if (response && response.statusCode == 200) {
var result = response.body.string();
if (result.startsWith('OK|')) {
var captchaId = result.split('|')[1];
// 5. 获取识别结果
for (var i = 0; i < 12; i++) {
sleep(5000);
var res = http.get('http://打码平台API地址/res.php', {
username: 'your_username',
password: 'your_password',
action: 'get',
id: captchaId
});
var resStr = res.body.string();
if (resStr == 'CAPCHA_NOT_READY') {
continue;
} else if (resStr.startsWith('OK|')) {
var code = resStr.split('|')[1];
toastLog('验证码识别成功: ' + code);
// 6. 输入验证码(假设输入框在(300,900))
click(300, 900);
sleep(500);
setText(0, code);
break;
}
}
} else {
toastLog('提交验证码失败: ' + result);
}
} else {
toastLog('请求失败');
}
注意:
- 需要根据实际打码平台的API文档调整请求参数和处理逻辑
- 实际使用时需要替换打码平台的API地址、账号信息等
- 裁剪区域的坐标需要根据实际应用界面进行调整
- 有些打码平台可能要求图片以multipart/form-data格式上传,需要使用http.postMultipart方法
ℹ️ 本回答由 AI 生成,内容仅供参考