回答
文档地址 https://docs.hamibot.com/
要识别非灰色和白色区域并随机点击,你可以使用以下代码:
// 判断颜色是否为灰色(RGB值相近)
function isGray(color) {
var r = (color >> 16) & 0xFF;
var g = (color >> 8) & 0xFF;
var b = color & 0xFF;
// 计算RGB之间的差异
var diffRG = Math.abs(r - g);
var diffRB = Math.abs(r - b);
var diffGB = Math.abs(g - b);
// 如果差异都很小,认为是灰色
return diffRG < 20 && diffRB < 20 && diffGB < 20;
}
// 判断颜色是否为白色
function isWhite(color) {
var r = (color >> 16) & 0xFF;
var g = (color >> 8) & 0xFF;
var b = color & 0xFF;
// 白色是RGB都接近255
return r > 240 && g > 240 && b > 240;
}
// 请求截图权限
auto.waitFor();
if (!requestScreenCapture()) {
toastLog('没有授予 Hamibot 屏幕截图权限');
hamibot.exit();
}
sleep(1000);
// 截取屏幕
var img = captureScreen();
var width = img.getWidth();
var height = img.getHeight();
// 采样间隔,避免遍历所有像素点
var sampleStep = 20;
var validPoints = [];
// 遍历屏幕像素点(采样)
for (var y = 0; y < height; y += sampleStep) {
for (var x = 0; x < width; x += sampleStep) {
var color = images.pixel(img, x, y);
if (!isGray(color) && !isWhite(color)) {
validPoints.push({x: x, y: y});
}
}
}
// 如果找到符合条件的区域
if (validPoints.length > 0) {
// 随机选择一个点
var randomIndex = Math.floor(Math.random() * validPoints.length);
var point = validPoints[randomIndex];
toastLog('找到目标区域,点击位置: ' + point.x + ', ' + point.y);
click(point.x, point.y);
sleep(500);
} else {
toastLog('没有找到非灰色非白色区域');
}
// 释放图片资源
img.recycle();
hamibot.exit();
注意事项:
- 灰色判断阈值(diff < 20)和白色判断阈值(RGB > 240)可以根据实际应用调整
- 采样间隔(sampleStep = 20)影响识别速度和准确性,值越大速度越快但可能遗漏小区域
- 如果目标应用有透明区域,可能需要额外处理
- 对于高分辨率屏幕,可以适当增大采样间隔以提高执行速度
如果需要更精确的识别,可以调整算法参数或缩小找色区域。例如,在options中指定region参数限制搜索范围:
// 限制在屏幕下半部分搜索
var region = [0, height/2, width, height/2];
补充回答 1
文档地址 https://docs.hamibot.com/
可以修改代码,让识别从屏幕中心开始向外进行。以下是优化后的代码:
function isGray(color) {
var r = (color >> 16) & 0xFF;
var g = (color >> 8) & 0xFF;
var b = color & 0xFF;
var diffRG = Math.abs(r - g);
var diffRB = Math.abs(r - b);
var diffGB = Math.abs(g - b);
return diffRG < 20 && diffRB < 20 && diffGB < 20;
}
function isWhite(color) {
var r = (color >> 16) & 0xFF;
var g = (color >> 8) & 0xFF;
var b = color & 0xFF;
return r > 240 && g > 240 && b > 240;
}
auto.waitFor();
if (!requestScreenCapture()) {
toastLog('没有授予 Hamibot 屏幕截图权限');
hamibot.exit();
}
sleep(1000);
var img = captureScreen();
var width = img.getWidth();
var height = img.getHeight();
// 计算屏幕中心点
var centerX = Math.round(width / 2);
var centerY = Math.round(height / 2);
// 采样间隔
var sampleStep = 20;
var points = [];
// 收集所有采样点并计算到中心点的距离
for (var y = 0; y < height; y += sampleStep) {
for (var x = 0; x < width; x += sampleStep) {
var distance = Math.sqrt(Math.pow(x - centerX, 2) + Math.pow(y - centerY, 2));
points.push({x: x, y: y, distance: distance});
}
}
// 按距离从近到远排序
points.sort(function(a, b) {
return a.distance - b.distance;
});
var foundPoint = null;
// 按距离从近到远检查每个点
for (var i = 0; i < points.length; i++) {
var point = points[i];
var color = images.pixel(img, point.x, point.y);
if (!isGray(color) && !isWhite(color)) {
foundPoint = point;
break;
}
}
if (foundPoint) {
toastLog('找到目标区域,点击位置: ' + foundPoint.x + ', ' + foundPoint.y);
click(foundPoint.x, foundPoint.y);
sleep(500);
} else {
toastLog('没有找到非灰色非白色区域');
}
img.recycle();
hamibot.exit();
这段代码的核心改进是:
- 计算每个采样点到屏幕中心的距离
- 按照距离从小到大对所有点进行排序
- 优先检查距离中心点最近的点,然后逐步向外扩散
这样就能确保先识别屏幕中心区域,如果没有找到符合条件的区域,再逐渐扩大搜索范围到边缘区域。
如果需要更精细的控制,可以调整以下参数:
sampleStep: 减小这个值会增加检测点数量,提高准确性但降低速度- 灰色判断阈值
20: 可以根据实际应用调整 - 白色判断阈值
240: 可以根据实际需要调整
这个实现方法简单高效,特别适合希望优先点击屏幕中心区域的场景。
ℹ️ 本回答由 AI 生成,内容仅供参考