[JS API]Total Control强大脚本功能之找图
totalcontrol吧
全部回复
仅看楼主
level 10
Total Control提供了灵活的找图方式,可以在全屏查找指定的图片,也可以在指定范围查找指定的图片,同时还支持在内存查找指定的图片。下面将做详细的介绍。
1. 制作bmp图片
当前仅支持搜索bmp格式的图像,因此首先先介绍如何制作将要查找的bmp图片:
1).将手机屏幕截图保存到计算机(另存为bmp格式),然后使用Windows附带的绘图工具打开图像,截取必需的图标,并将其另存为bmp格式的图片。
2).打开脚本执行器-->脚本列表-->制作BMP,点击“主控设备截屏”或打开已有的bmp图片,然后截取必需的图标,并将其另存为bmp格式的图片。
2. 内存找图
内存找图,首先得保证内存可用,因此需先保存图片到内存,我们提供了 3 个可用的内存位置,分别为 @0、@1或 @2。每个内存位置最多只能存一张图片数据,我们可以多次将图片数据存到同一个内存位置,但是内存位置 @0、@1 或 @2 只保存最后一次存储的图片数据。
内存数据的生命周期同手机端Total Control 应用程序的生命周期,也就是说当我们关闭手机端 Total Control 应用程序,保存在内存中的图片数据也就没有了。
3.接口原型
seekImage(topLeftX, topLeftY,bottomRightX, bottomRightY, imageName, sim, beGray)
seekImage(location,topLeftX, topLeftY, bottomRightX, bottomRightY, imageName,sim, beGray)
seekImage(imageName, topLeftX, topLeftY, bottomRightX, bottomRightY, options)
4.参数简介
Parameter Type Required Description
location String No 可选项,指定内存存储数据的位置;@0、@1 或 @2 表示固定的三个内存位置,"@@" 表示最后一次内存截图。
topLeftX Float No 屏幕上指定范围左上角 X 坐标
topLeftY Float No 屏幕上指定范围左上角 Y 坐标
bottomRightX Float No 屏幕上指定范围右下角 X 坐标
bottomRightY Float No 屏幕上指定范围右下角 Y 坐标
imageName String Yes 图像绝对路径,必须是bmp类型
sim Float No 相似度,取值范围为[0.0,1.0],默认值为1.0。1.0表示图像完全匹配。该值越小,对相似度的要求也越低。如果为0,则该函数返回值为与搜索图最相似区域的中心坐标。
beGray Boolean No 此值为true与false,默认为true时。当此值为true时,表示图片要先进行灰度处理,然后再进行找图。当此值为false时候,表示图片不进行任何处理就进行找图.。当然灰度处理后找图速度更快。
options object No options对象可以具有以下属性之一: sim:相似度,取值范围为[0.0,1.0]。1表示图像完全匹配。缺省值为1.0 useGray:灰度处理,其值为true或false,当为true时,在比较之前对图片进行灰度处理,当为false时,则直接比较。默认值为false。
5. 返回值:
如果执行成功,并且成功找到指定图片,则返回{x:<x> , y:<y> , rect: [x1, y1, x2, y2]}, “x”和“y”是找到图的中心坐标,rect是找到图的区域。
如果执行成功,但是没有找到指定图片,则返回null.
如果执行失败,则返回null,可通过"lastError()"获取错误信息
6.详细示例
示例一:全屏查找指定图像
//获取当前设备对象var device = Device.getMain(); //全屏查找指定图像var ret = device.seekImage("D:\\test.bmp"); if (ret == null) { print("Sorry, did not find the specified image or execute failed! The error is :"+lastError());} else { if (ret.rect) { var arr=[ret.rect[0],ret.rect[1],ret.rect[2],ret.rect[3]]; print("Image found, Center x: " + ret.x + ", Center y: " + ret.y + ", rect: " + JSON.stringify(arr)); } else { print("Image found, Center x: " + ret.x + ", Center y: " + ret.y ); }}
示例二:在指定区域查找指定图像
//获取当前设备对象var device = Device.getMain(); //在指定区域(10,10,700,1024)查找指定图像var ret = device.seekImage(10,10,700,1024, "D:\\test.bmp"); if (ret == null) { print("Sorry, did not find the specified image or execute failed! The error is :"+lastError());} else { if (ret.rect) { var arr=[ret.rect[0],ret.rect[1],ret.rect[2],ret.rect[3]]; print("Image found, Center x: " + ret.x + ", Center y: " + ret.y + ", rect: " + JSON.stringify(arr)); } else { print("Image found, Center x: " + ret.x + ", Center y: " + ret.y ); }}
示例三:在指定区域查找指定图像,指定相似度为0.6
//获取当前设备对象var device = Device.getMain(); //在指定区域(10,10,700,1024)查找指定图像,其相似度为0.6var ret = device.seekImage(10,10,700,1024, "D:\\test.bmp", 0.6); if (ret == null) { print("Sorry, did not find the specified image or execute failed! The error is :"+lastError());} else { if (ret.rect) { var arr=[ret.rect[0],ret.rect[1],ret.rect[2],ret.rect[3]]; print("Image found, Center x: " + ret.x + ", Center y: " + ret.y + ", rect: " + JSON.stringify(arr)); } else { print("Image found, Center x: " + ret.x + ", Center y: " + ret.y ); }}
示例四:在全屏查找指定图像,指定相似度为0.6,并在找图之前进行灰度处理
//获取当前设备对象var device = Device.getMain(); //在全屏查找指定图像,指定相似度为0.6,并在找图之前进行灰度处理var ret = device.seekImage("D:\\test.bmp", 0.6, true); if (ret == null) { print("Sorry, did not find the specified image or execute failed! The error is :"+lastError());} else { if (ret.rect) { var arr=[ret.rect[0],ret.rect[1],ret.rect[2],ret.rect[3]]; print("Image found, Center x: " + ret.x + ", Center y: " + ret.y + ", rect: " + JSON.stringify(arr)); } else { print("Image found, Center x: " + ret.x + ", Center y: " + ret.y ); }}
示例五:在内存图片中查找指定图像
//获取当前主控设备对象var device = Device.getMain();//截屏并保存到内存@0var ret1 = device.screenshotToMemory("@0");if (ret1 == 0){ //在内存图片中查找指定的图像 var ret = device.seekImage("@0","D:\\test.bmp"); if (ret == null) { print("Sorry, did not find the specified image or execute failed! The error is :"+lastError()); } else { if (ret.rect) { var arr=[ret.rect[0],ret.rect[1],ret.rect[2],ret.rect[3]]; print("Image found, Center x: " + ret.x + ", Center y: " + ret.y + ", rect: " + JSON.stringify(arr)); } else { print("Image found, Center x: " + ret.x + ", Center y: " + ret.y ); } }} else { print("Failed to save image to memory! The error is: "+lastError());}
示例六:在内存图片的指定区域查找指定图像,指定相似度为0.7
//获取当前主控设备对象var device = Device.getMain();//截屏并保存到内存@0var ret1 = device.screenshotToMemory("@0");if (ret1 == 0){ //在内存图片的指定区域查找指定图像,指定相似度为0.7 var ret = device.seekImage("@0", 10,10,700,1024, "D:\\test.bmp", 0.7); if (ret == null) { print("Sorry, did not find the specified image or execute failed! The error is :"+lastError()); } else { if (ret.rect) { var arr=[ret.rect[0],ret.rect[1],ret.rect[2],ret.rect[3]]; print("Image found, Center x: " + ret.x + ", Center y: " + ret.y + ", rect: " + JSON.stringify(arr)); } else { print("Image found, Center x: " + ret.x + ", Center y: " + ret.y ); } }} else { print("Failed to save image to memory! The error is: "+lastError());}
示例七:带option参数接口示例,在指定区域查找指定图像,指定相似度为0.5
var device = Device.getMain(); //获取当前设备对象var ret = device.seekImage("D:\\test.bmp",10,10,700,1024,{sim:0.5}); if (ret == null){ print("没有在手机屏幕找到该图. "+lastError());} else{ if (ret.rect) { var arr=[ret.rect[0],ret.rect[1],ret.rect[2],ret.rect[3]]; print("Image found, Center x: " + ret.x + ", Center y: " + ret.y + ", rect: " + JSON.stringify(arr)); } else { print("Image found, Center x: " + ret.x + ", Center y: " + ret.y ); }}
2019年12月04日 09点12分 1
level 1
怎么我的TC脚本列表里面没有制作BMP功能,只有录制脚本和颜色助手
2020年04月03日 10点04分 9
1