level 10
TC资深用户😄
楼主
Total Control提供了强大的找色功能,详情如下:
1. 原型seekColor(topLeftX, topLeftY, bottomRightX, bottomRightY, color, secondary colors, dir, sim)seekColor(location,topLeftX, topLeftY, bottomRightX, bottomRightY, color, secondary colors, dir, sim)2. 描述
在全屏或指定区域内查找指定的颜色,支持屏幕找色与内存找色,同时支持单点找色与多点找色。当仅给定color时,表示单点找色,找到后返回找到点的x、y坐标;当同时给定color与secondarycolors,表示多点找色,在找色的过程中,首先查找主色( color ),如果主色匹配,再查找辅色( secondary colors ),如果辅色也匹配,则返回查找到的主色的x、y坐标与主色和辅色组成的区域。该API仅支持16进制颜色,因此指定颜色时0x可以省略。如"0x001122" 与"001122"等同。
内存查找,首先需要把图片存储到内存指定位置,可用的内存为@0、@1或 @2,当我们找图、找色、文字识别时,就可以重复使用保存在内存位置的图片数据,找图、找色、文字识别就更快。如何保存图片到内存,请参照screenshotToMemory API。保存到内存的图片并不是一直存在,其生命周期为应用程序的生命周期。也就说只要我们没有关闭手机端 Total Control 应用程序,保存到内存的图片数据一直可用。
当我们需要使用该 API 操作指定内存位置中存储的图片数据,就使用带有"location"参数的 API
3. 参数简介
Parameter Type Required Description
location String N 可选项,指定内存存储数据的位置;@0、@1 或 @2 表示固定的三个内存位置,"@@" 表示最后一次内存截图。@@使用时注意事项如下: 当使用 @@ 指定位置时,首先查找最后一次内存截图是否可用,如果可用,则直接使用;如果最后一次内存截图为空,不可用,则查找@0、@1、@2这三个内存位置,使用第一个非空缓冲区;如果所有屏幕缓冲区均为空,则调用screenshotToMemory(“ @ 0”)截屏到的@0,并使用 "@0" 作为最后一个缓冲区。
topLeftX, topLeftY, bottomRightX,bottomRightY Float N 指定查找区域,支持绝对与相对坐标,缺省时表示全屏查找。其中 - topLeftX: 屏幕上指定范围左上角 X 坐标 - topLeftY: 屏幕上指定范围左上角 Y 坐标 - bottomRightX: 屏幕上指定范围右下角 X 坐标 - bottomRightY: 屏幕上指定范围右下角 Y 坐标
color Array/String Y 要对比的颜色,仅支持16 进制颜色(0x可省略), 多个颜色用"|"号分隔, 如果需要对比颜色范围则中间用"-"号分隔 示例: Array格式: ["0x0000ff", "0x00ff00", "0x0000dd-0x0000ff"] ["0000ff", "00ff00", "0x0000dd-0000ff"] String格式: "0x0000ff|0x00ff00|0x0000dd-0x0000ff" "0000ff|00ff00|0x0000dd-0000ff"
secondary colors Array/String N 要对比的辅色,可同时给定多个辅色,多个辅色间用“,”分隔。 辅色格式:距离主色的x偏移量|距离主色的y偏移量|16 进制颜色。 16进制颜色(0x可省略),多个颜色用"|"号分隔, 如果需要对比颜色范围则中间用"-"号分隔 示例: Array格式: ["-10|5|0x05beef","5|-10|0xaaaaaa-0xbbbbbb"] ["5|0|aabbcc", "-5|0|aabbdd|aaaaaa-cccccc", "0|5|bbccdd|000000-111111"] String格式: "-10|5|0xccddee|0xaabbcc-0xaabbcc" "-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb" "5|0|aabbcc, -5|0|aabbdd|aaaaaa-cccccc, 0|5|bbccdd|000000-111111"
dir Int Y 查找方向,取值为0-4,其代表的意义如下: - 0: 表示从左上向右下查找 - 1: 表示从中心往四周查找 - 2: 表示从右下向左上查找 - 3: 表示从左下向右上查找 - 4:表示从右上向左下查找
sim Float/Int Y 相似度. 单点找色:相似度取值范围为[0.0, 1.0]。1.0表示完全匹配,该值越小,对相似度的要求也越低。 多点找色:相似度为十六进制数字, 表示误差值。
4. 返回值
国内版与国际版,返回值有一点区别,详情如下。如果不清楚版本类别,可通过get(tcConst.BuildCountryCode)查看,其中cn表示国内版,us表示国际版。
国内版:
如果执行成功,并且成功找到指定颜色,则返回 js 对象,第一个与指定颜色相同的像素的坐标,例如:{x:<x> , y:<y>},如果是多点找色,则返回坐标与区域。例如{x:<x> , y:<y> , rect: [x1, y1, x2, y2]}。
如果执行成功,但是没有找到指定颜色,则返回null.
如果执行失败,则返回null,可通过"lastError()"获取错误信息
国际版:
如果执行成功,并且成功找到指定颜色,则返回 js 对象,第一个与指定颜色相同的像素的坐标,例如:{x:<x> , y:<y>},如果是多点找色,则返回坐标与区域。例如{x:<x> , y:<y> , rect: [x1, y1, x2, y2]}。
如果执行成功,但是没有找到指定颜色,则返回false.
如果执行失败,则返回null,可通过"lastError()"获取错误信息
5. 详细示例
示例一: (单点找色)全屏查找
// 获取当前主控设备对象var device = Device.getMain();//在全屏查找指定的颜色"0x000000-0xffffff"coor = device.seekColor("0x000000-0xffffff", 4, 1);if (coor == null) { print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else { print("Congratulations, find the specified color.\nReturn value: (" + coor.x + "," + coor.y+ ")");}
示例二: (单点找色)在指定区域查找指定的颜色
// 获取当前主控设备对象var device = Device.getMain();//在指定区域(0, 0, 719, 1110)内查找指定的颜色"0x0000ff|0x00ff00|0x0000dd-0x0000ff"coor = device.seekColor(0, 0, 719, 1110, "0x0000ff|0x00ff00|0x0000dd-0x0000ff", 4, 1);if (coor == null) { print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else {print("Congratulations, find the specified color.\nReturn value: (" + coor.x + "," + coor.y+ ")");}
示例三: (单点找色)在内存图片的指定区域查找指定的颜色,以数组的格式指定颜色
//获取当前主控设备对象var device = Device.getMain();//指定内存位置 "@0" 来存储数据var ret1 = device.screenshotToMemory("@0");if (ret1 == 0){//内存位置"@0"存储的图片上找色,在指定区域(0, 0, 719, 1110)内查找指定的颜色["0x0000ff", "0x00ff00", "0x0000dd-0x0000ff"]coor = device.seekColor("@0",0, 0, 719, 1110, ["0x0000ff", "0x00ff00", "0x0000dd-0x0000ff"], 4, 1);if (coor == null) {print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else {print("Congratulations, find the specified color.\nReturn value: (" + coor.x + "," + coor.y+ ")");}} else {print("Failed to save image to memory! The error is: "+lastError());}
示例四:(多点找色)全屏查找
// 获取当前主控设备对象var device = Device.getMain();//在全屏查找指定的颜色,主色为"0x000000-0xffffff",辅色为"-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb"coor = device.seekColor("0x000000-0xffffff","-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb", 4, 1);if (coor == null) {print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else { var arr=[coor.rect[0],coor.rect[1],coor.rect[2],coor.rect[3]];print("Congratulations, find the specified color.Return value: \ncoordinate: (" + coor.x + "," + coor.y+ ")"+", rect: "+JSON.stringify(arr));}
示例五: (多点找色)在指定区域查找指定的颜色
// 获取当前主控设备对象var device = Device.getMain();//在指定区域(0, 0, 719, 1110)内查找指定的颜色,主色为"0x000000-0xffffff",辅色为"-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb"coor = device.seekColor(0, 0, 719, 1110, "0x000000-0xffffff", "-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb", 4, 1);if (coor == null) {print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else {var arr=[coor.rect[0],coor.rect[1],coor.rect[2],coor.rect[3]]; print("Congratulations, find the specified color.Return value: \ncoordinate: (" + coor.x + "," + coor.y+ ")"+", rect: "+JSON.stringify(arr));}
示例六: (多点找色)在内存图片的指定区域查找指定的颜色,以数组的格式指定颜色
//获取当前主控设备对象var device = Device.getMain();//指定内存位置 "@0" 来存储数据var ret1 = device.screenshotToMemory("@0");if (ret1 == 0){//内存位置"@0"存储的图片上找色,在指定区域(0, 0, 719, 1110)内查找指定的颜色,主色为"0x000000-0xffffff",辅色为"-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb"coor = device.seekColor("@0",0, 0, 719, 1110, "000000-ffffff", ["-10|5|0x05beef","5|-10|0xaaaaaa-0xbbbbbb"], 4, 1);if (coor == null) {print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else {var arr=[coor.rect[0],coor.rect[1],coor.rect[2],coor.rect[3]]; print("Congratulations, find the specified color.Return value: \ncoordinate: (" + coor.x + "," + coor.y+ ")"+", rect: "+JSON.stringify(arr));}} else { print("Failed to save image to memory! The error is: "+lastError());}运行结果:
执行成功,单点找色,则返回:
Congratulations, find the specified color.Return value: (719,50)
执行成功,多点找色,则返回:
Congratulations, find the specified color.Return value:coordinate: (719,50), rect: [685,26,733,74]
温馨提示:
如果内存找色返回值为 "memory data is null!",则表示我们的内存位置不可用,这时我们需要使用 screenshotToMemory 来指定我们需要存储数据的内存位置。
2020年02月05日 06点02分
1
1. 原型seekColor(topLeftX, topLeftY, bottomRightX, bottomRightY, color, secondary colors, dir, sim)seekColor(location,topLeftX, topLeftY, bottomRightX, bottomRightY, color, secondary colors, dir, sim)2. 描述
在全屏或指定区域内查找指定的颜色,支持屏幕找色与内存找色,同时支持单点找色与多点找色。当仅给定color时,表示单点找色,找到后返回找到点的x、y坐标;当同时给定color与secondarycolors,表示多点找色,在找色的过程中,首先查找主色( color ),如果主色匹配,再查找辅色( secondary colors ),如果辅色也匹配,则返回查找到的主色的x、y坐标与主色和辅色组成的区域。该API仅支持16进制颜色,因此指定颜色时0x可以省略。如"0x001122" 与"001122"等同。
内存查找,首先需要把图片存储到内存指定位置,可用的内存为@0、@1或 @2,当我们找图、找色、文字识别时,就可以重复使用保存在内存位置的图片数据,找图、找色、文字识别就更快。如何保存图片到内存,请参照screenshotToMemory API。保存到内存的图片并不是一直存在,其生命周期为应用程序的生命周期。也就说只要我们没有关闭手机端 Total Control 应用程序,保存到内存的图片数据一直可用。
当我们需要使用该 API 操作指定内存位置中存储的图片数据,就使用带有"location"参数的 API
3. 参数简介
Parameter Type Required Description
location String N 可选项,指定内存存储数据的位置;@0、@1 或 @2 表示固定的三个内存位置,"@@" 表示最后一次内存截图。@@使用时注意事项如下: 当使用 @@ 指定位置时,首先查找最后一次内存截图是否可用,如果可用,则直接使用;如果最后一次内存截图为空,不可用,则查找@0、@1、@2这三个内存位置,使用第一个非空缓冲区;如果所有屏幕缓冲区均为空,则调用screenshotToMemory(“ @ 0”)截屏到的@0,并使用 "@0" 作为最后一个缓冲区。
topLeftX, topLeftY, bottomRightX,bottomRightY Float N 指定查找区域,支持绝对与相对坐标,缺省时表示全屏查找。其中 - topLeftX: 屏幕上指定范围左上角 X 坐标 - topLeftY: 屏幕上指定范围左上角 Y 坐标 - bottomRightX: 屏幕上指定范围右下角 X 坐标 - bottomRightY: 屏幕上指定范围右下角 Y 坐标
color Array/String Y 要对比的颜色,仅支持16 进制颜色(0x可省略), 多个颜色用"|"号分隔, 如果需要对比颜色范围则中间用"-"号分隔 示例: Array格式: ["0x0000ff", "0x00ff00", "0x0000dd-0x0000ff"] ["0000ff", "00ff00", "0x0000dd-0000ff"] String格式: "0x0000ff|0x00ff00|0x0000dd-0x0000ff" "0000ff|00ff00|0x0000dd-0000ff"
secondary colors Array/String N 要对比的辅色,可同时给定多个辅色,多个辅色间用“,”分隔。 辅色格式:距离主色的x偏移量|距离主色的y偏移量|16 进制颜色。 16进制颜色(0x可省略),多个颜色用"|"号分隔, 如果需要对比颜色范围则中间用"-"号分隔 示例: Array格式: ["-10|5|0x05beef","5|-10|0xaaaaaa-0xbbbbbb"] ["5|0|aabbcc", "-5|0|aabbdd|aaaaaa-cccccc", "0|5|bbccdd|000000-111111"] String格式: "-10|5|0xccddee|0xaabbcc-0xaabbcc" "-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb" "5|0|aabbcc, -5|0|aabbdd|aaaaaa-cccccc, 0|5|bbccdd|000000-111111"
dir Int Y 查找方向,取值为0-4,其代表的意义如下: - 0: 表示从左上向右下查找 - 1: 表示从中心往四周查找 - 2: 表示从右下向左上查找 - 3: 表示从左下向右上查找 - 4:表示从右上向左下查找
sim Float/Int Y 相似度. 单点找色:相似度取值范围为[0.0, 1.0]。1.0表示完全匹配,该值越小,对相似度的要求也越低。 多点找色:相似度为十六进制数字, 表示误差值。
4. 返回值
国内版与国际版,返回值有一点区别,详情如下。如果不清楚版本类别,可通过get(tcConst.BuildCountryCode)查看,其中cn表示国内版,us表示国际版。
国内版:
如果执行成功,并且成功找到指定颜色,则返回 js 对象,第一个与指定颜色相同的像素的坐标,例如:{x:<x> , y:<y>},如果是多点找色,则返回坐标与区域。例如{x:<x> , y:<y> , rect: [x1, y1, x2, y2]}。
如果执行成功,但是没有找到指定颜色,则返回null.
如果执行失败,则返回null,可通过"lastError()"获取错误信息
国际版:
如果执行成功,并且成功找到指定颜色,则返回 js 对象,第一个与指定颜色相同的像素的坐标,例如:{x:<x> , y:<y>},如果是多点找色,则返回坐标与区域。例如{x:<x> , y:<y> , rect: [x1, y1, x2, y2]}。
如果执行成功,但是没有找到指定颜色,则返回false.
如果执行失败,则返回null,可通过"lastError()"获取错误信息
5. 详细示例
示例一: (单点找色)全屏查找
// 获取当前主控设备对象var device = Device.getMain();//在全屏查找指定的颜色"0x000000-0xffffff"coor = device.seekColor("0x000000-0xffffff", 4, 1);if (coor == null) { print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else { print("Congratulations, find the specified color.\nReturn value: (" + coor.x + "," + coor.y+ ")");}
示例二: (单点找色)在指定区域查找指定的颜色
// 获取当前主控设备对象var device = Device.getMain();//在指定区域(0, 0, 719, 1110)内查找指定的颜色"0x0000ff|0x00ff00|0x0000dd-0x0000ff"coor = device.seekColor(0, 0, 719, 1110, "0x0000ff|0x00ff00|0x0000dd-0x0000ff", 4, 1);if (coor == null) { print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else {print("Congratulations, find the specified color.\nReturn value: (" + coor.x + "," + coor.y+ ")");}
示例三: (单点找色)在内存图片的指定区域查找指定的颜色,以数组的格式指定颜色
//获取当前主控设备对象var device = Device.getMain();//指定内存位置 "@0" 来存储数据var ret1 = device.screenshotToMemory("@0");if (ret1 == 0){//内存位置"@0"存储的图片上找色,在指定区域(0, 0, 719, 1110)内查找指定的颜色["0x0000ff", "0x00ff00", "0x0000dd-0x0000ff"]coor = device.seekColor("@0",0, 0, 719, 1110, ["0x0000ff", "0x00ff00", "0x0000dd-0x0000ff"], 4, 1);if (coor == null) {print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else {print("Congratulations, find the specified color.\nReturn value: (" + coor.x + "," + coor.y+ ")");}} else {print("Failed to save image to memory! The error is: "+lastError());}
示例四:(多点找色)全屏查找
// 获取当前主控设备对象var device = Device.getMain();//在全屏查找指定的颜色,主色为"0x000000-0xffffff",辅色为"-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb"coor = device.seekColor("0x000000-0xffffff","-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb", 4, 1);if (coor == null) {print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else { var arr=[coor.rect[0],coor.rect[1],coor.rect[2],coor.rect[3]];print("Congratulations, find the specified color.Return value: \ncoordinate: (" + coor.x + "," + coor.y+ ")"+", rect: "+JSON.stringify(arr));}
示例五: (多点找色)在指定区域查找指定的颜色
// 获取当前主控设备对象var device = Device.getMain();//在指定区域(0, 0, 719, 1110)内查找指定的颜色,主色为"0x000000-0xffffff",辅色为"-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb"coor = device.seekColor(0, 0, 719, 1110, "0x000000-0xffffff", "-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb", 4, 1);if (coor == null) {print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else {var arr=[coor.rect[0],coor.rect[1],coor.rect[2],coor.rect[3]]; print("Congratulations, find the specified color.Return value: \ncoordinate: (" + coor.x + "," + coor.y+ ")"+", rect: "+JSON.stringify(arr));}
示例六: (多点找色)在内存图片的指定区域查找指定的颜色,以数组的格式指定颜色
//获取当前主控设备对象var device = Device.getMain();//指定内存位置 "@0" 来存储数据var ret1 = device.screenshotToMemory("@0");if (ret1 == 0){//内存位置"@0"存储的图片上找色,在指定区域(0, 0, 719, 1110)内查找指定的颜色,主色为"0x000000-0xffffff",辅色为"-10|5|0x05beef, 5|-10|0xaaaaaa-0xbbbbbb"coor = device.seekColor("@0",0, 0, 719, 1110, "000000-ffffff", ["-10|5|0x05beef","5|-10|0xaaaaaa-0xbbbbbb"], 4, 1);if (coor == null) {print("Sorry, did not find the specified color or execute failed! The error is :"+lastError());} else {var arr=[coor.rect[0],coor.rect[1],coor.rect[2],coor.rect[3]]; print("Congratulations, find the specified color.Return value: \ncoordinate: (" + coor.x + "," + coor.y+ ")"+", rect: "+JSON.stringify(arr));}} else { print("Failed to save image to memory! The error is: "+lastError());}运行结果:
执行成功,单点找色,则返回:
Congratulations, find the specified color.Return value: (719,50)
执行成功,多点找色,则返回:
Congratulations, find the specified color.Return value:coordinate: (719,50), rect: [685,26,733,74]
温馨提示:
如果内存找色返回值为 "memory data is null!",则表示我们的内存位置不可用,这时我们需要使用 screenshotToMemory 来指定我们需要存储数据的内存位置。