使用autojs找图,找了很久的图发现有很多共同点于是就封装了几个方法, 方便大家的使用同时也为自己提供便利性。
1、找图点击
2、找到所有匹配的点击
3、判断是存在图不存在返回Null存在返回坐标
所有方法均区分字符串和数组,这样方便兼容多个图图一起找知道找到为止。有点效率上的浪费。
/**
* 找图,找到并点击
* @param {可以是数组也可以是字符串,传输数组可以多次找图知道找到为止} img_path_array
* @param {找图区域,默认是全屏找图,该参数可以不传输} area_region
* @param {相似度,默认是0.8,可以不传输} threshold
* @param {找到后是否继续点击} is_continue
* @returns true表示执行成功Flase表示失败
*/
Tools.clickAreaForFindImage=function(img_path_array, area_region, threshold,is_continue) {
try {
area_region = area_region || [0, 0, device.width, device.height] //默认的找图区域 全屏找图 前2位是坐标 后面是长度和宽度 千万别理解成是坐标
threshold = threshold || 0.8 // 默认的相识度0.8
is_continue=is_continue|| false
if (img_path_array instanceof Array) {
let arrayLength = img_path_array.length
for (let i = 0; i < arrayLength; i ) {
//toastLog("正在进行第" (i 1) "次找图...")
img_path = img_path_array[i] //小图地址可判断是否存在
if (!files.exists(img_path)) {
toastLog(img_path "文件不存在因此跳过")
continue
}
var little_image = images.read(img_path) //小图
var find_result_bounds = findImage(
captureScreen(), little_image, {
region: area_region,
threshold: threshold
});
if (find_result_bounds) {
toastLog(img_path "图找到,准备点击坐标:" find_result_bounds)
click(find_result_bounds.x, find_result_bounds.y)
return true
} else {
//toastLog(img_path "小图存在但是在大图中未找到图片进入下一次循环...")
}
}
return false
}
if (typeof (img_path_array) == "string") {
img_path = img_path_array//小图地址可判断是否存在
if (!files.exists(img_path)) {
toastLog(img_path "文件不存在因此跳过")
return false
}
var little_image = images.read(img_path) //小图
var find_result_bounds = findImage(
captureScreen(), little_image, {
region: area_region,
threshold: threshold
});
if (find_result_bounds) {
toastLog(img_path "图找到,准备点击坐标:" find_result_bounds)
click(find_result_bounds.x, find_result_bounds.y)
return true
} else {
//toastLog(img_path "小图存在但是在大图中未找到图片")
return false
}
}
} catch (error) {
toastLog("clickAreaForFindImage方法出现错误:" error)
return false;
}
}我是用js最新标准写的大家自己修正一下就行。
/**
* 找图,找到并点击
* @param {可以是数组也可以是字符串,传输数组可以多次找图知道找到为止} img_path_array
* @param {找图区域,默认是全屏找图,该参数可以不传输} area_region
* @param {相似度,默认是0.8,可以不传输} threshold
* @param {找到后是否继续点击} is_continue
* @returns true表示执行成功Flase表示失败
*/
Tools.clickRegionForFindImage=function(img_path_array, area_region, threshold,is_continue) {
try {
area_region = area_region || [0, 0, device.width, device.height] //默认的找图区域 全屏找图 前2位是坐标 后面是长度和宽度 千万别理解成是坐标
threshold = threshold || 0.9 // 默认的相识度0.85
is_continue=is_continue|| false
if (img_path_array instanceof Array) {
let arrayLength = img_path_array.length
for (let i = 0; i < arrayLength; i ) {
img_path = img_path_array[i] //小图地址可判断是否存在
if (!files.exists(img_path)) {
toastLog(img_path "文件不存在因此跳过")
continue
}
var little_image = images.read(img_path) //小图
var find_result_bounds = images.matchTemplate(
captureScreen(), little_image, {
region: area_region,
threshold: threshold,
max: 100
});
if (find_result_bounds) {
find_result_bounds.matches.forEach(match => {
log(img_path "point = " match.point ", similarity = " match.similarity);
click(match.point.x, match.point.y)
});
if(is_continue || is_continue=="true"){
continue;
}
return true
} else {
//toastLog(img_path "小图存在但是在大图中未找到图片进入下一次循环...")
}
}
return false
}
if (typeof (img_path_array) == "string") {
img_path = img_path_array//小图地址可判断是否存在
if (!files.exists(img_path)) {
toastLog(img_path "文件不存在因此跳过")
return false
}
var little_image = images.read(img_path) //小图
var find_result_bounds = images.matchTemplate(
captureScreen(), little_image, {
region: area_region,
threshold: threshold,
max: 100
});
toastLog(find_result_bounds)
if (find_result_bounds) {
//toastLog(img_path "图找到,准备点击坐标:" find_result_bounds)
click(find_result_bounds.x, find_result_bounds.y)
return true
} else {
//toastLog(img_path "小图存在但是在大图中未找到图片")
return false
}
}
} catch (error) {
toastLog("clickRegionForFindImage方法出现错误:" error)
return false;
}
}
这个是群点,找到多个匹配对象进行点击多个对象。根据经验来看最好是吧概率设置0.9以上。这个点击朱要是针对游戏消消乐一类的。
/**
* 找图,返回坐标
* @param {可以是数组也可以是字符串,传输数组可以多次找图知道找到为止} img_path_array
* @param {找图区域,默认是全屏找图,该参数可以不传输} area_region
* @param {相似度,默认是0.8,可以不传输} threshold
* @returns
*/
Tools.findImageCoordinate=function(img_path_array, area_region, threshold) {
//try {
let return_region=null
area_region = area_region || [0, 0, device.width, device.height] //默认的找图区域 全屏找图 前2位是坐标 后面是长度和宽度 千万别理解成是坐标
threshold = threshold || 0.8 // 默认的相识度0.8
if (img_path_array instanceof Array) {
let arrayLength = img_path_array.length
for (let i = 0; i < arrayLength; i ) {
img_path = img_path_array[i] //小图地址可判断是否存在
if (!files.exists(img_path)) {
toastLog(img_path "文件不存在因此跳过")
continue
}
var little_image = images.read(img_path) //小图
var find_result_bounds = findImage(
captureScreen(), little_image, {
region: area_region,
threshold: threshold
});
//this.toastLog(img_path ":" find_result_bounds)
if (find_result_bounds==null || find_result_bounds== undefined) {
continue
}else{
return find_result_bounds
}
}
return return_region
}
if (typeof (img_path_array) == "string") {
img_path = img_path_array //小图地址可判断是否存在
if (!files.exists(img_path)) {
toastLog(img_path "文件不存在因此跳过")
return return_region
}
var little_image = images.read(img_path) //小图
var find_result_bounds = findImage(
captureScreen(), little_image, {
region: area_region,
threshold: threshold
});
if (find_result_bounds) {
return find_result_bounds
} else {
return return_region
}
}
// } catch (error) {
// toastLog("findImageCoordinate方法出现错误:" error)
// return return_region;
// }
}
和第一个稍微有点重叠不过意图还是不一样的 这个主要就是找到了做别的操作比如保刷功能。
评论内容