【JavaScript】在全屏或指定區域範圍內尋找指定圖片的座標(找圖)
原型:
seekImage(imageName)
seekImage(imageName, sim)
seekImage(topLeftX, topLeftY, bottomRightX, bottomRightY, imageName)
seekImage(topLeftX, topLeftY, bottomRightX, bottomRightY, imageName, sim)
seekImage(imageInfo)
seekImage(imageInfo, sim)
注,在版本1.8.1之後,又新增了兩個seekImage函式,其原型如下:
seekImage(topLeftX, topLeftY, bottomRightX, bottomRightY, imageName, sim, beGray)
seekImage(imageName, sim, beGray)
是否支援多裝置:
不支援
對應的 REST API:
描述:
在全屏或指定區域範圍內尋找指定圖片的座標,支援相似查詢。如找到目標圖,則返回找到位置的中間點座標。該函式有以上原型方法,對應不同的引數。
注意:
儘管我們支援在一臺手機上截圖,而在另一臺手機上搜圖,但這兩臺手機的高寬比例必須一致。此外,如果手機截圖是在橫屏時獲取的,找圖時也必須保證手機是橫屏的;同理,縱屏截圖也必須在縱屏時查詢。
目前這個版本僅支援bmp,在未來的開發計劃中,我們將逐步提供各種影象格式的支援。
更多資訊請看seekImage快速入門
引數:
-
seekImage(imageName)
-
seekImage(imageName, sim)
imageName:影象絕對路徑,必須是bmp型別
sim: 相似度,取值範圍為[0.0,1.0]。1表示影象完全匹配。該值越小,對相似度的要求也越低。如果為0,則該函式返回值為與搜尋圖最相似區域的中心座標。 不填sim表示使用預設的相似度1.0. -
seekImage(topLeftX, topLeftY, bottomRightX, bottomRightY, imageName)
-
seekImage(topLeftX, topLeftY, bottomRightX, bottomRightY, imageName, sim)
topLeftX:螢幕上指定範圍左上角X座標
topLeftY:螢幕上指定範圍左上角Y座標
bottomRightX:螢幕上指定範圍右下角X座標
bottomRightY:螢幕上指定範圍右下角Y座標
imageName:影象絕對路徑,必須是bmp型別
sim: 相似度,取值範圍為[0.0,1.0]。1表示影象完全匹配。該值越小,對相似度的要求也越低。如果為0,則該函式返回值為與搜尋圖最相似區域的中心座標。不填sim表示使用預設的相似度1.0. -
seekImage(imageInfo)
-
seekImage(imageName, sim)
imageInfo:ImageInfo物件,指令碼中用於儲存影象資訊的物件,其成員變數包括:
a) imagePath: 影象的絕對路徑
b) x: 螢幕搜尋範圍的左上角X座標
c) y: 螢幕搜尋範圍的左上角Y座標
d) width: 螢幕搜尋範圍的寬度
e) height: 螢幕搜尋範圍的高度
f) srcResolutionX: 要搜尋的影象從那款解析度的手機上獲得的,此引數為該解析度的寬度
g) srcResolutionY: 要搜尋的影象從那款解析度的手機上獲得的,此引數為該解析度的高度 -
seekImage(topLeftX, topLeftY, bottomRightX, bottomRightY, imageName, sim, beGray)
-
seekImage(imageName, sim, beGray)
topLeftX:螢幕上指定範圍左上角X座標
topLeftY:螢幕上指定範圍左上角Y座標
bottomRightX:螢幕上指定範圍右下角X座標
bottomRightY:螢幕上指定範圍右下角Y座標
imageName:影象絕對路徑,必須是bmp型別
sim: 相似度,取值範圍為[0.0,1.0]。1表示影象完全匹配。該值越小,對相似度的要求也越低。如果為0,則該函式返回值為與搜尋圖最相似區域的中心座標。不填sim表示使用預設的相似度1.0.
beGray:當此值為true時(預設為true),表示圖片要先進行灰度處理,然後再進行找圖。當此值為false時候,表示圖片不進行任何處理就進行找圖,當然灰度處理後找圖速度更快。
返回值:
物件:Coord 物件:可以通過獲取到的物件找到影象的中心點座標,比如coord.x;coord.y;
示例:
在全屏或指定區域範圍內尋找指定圖片的座標
指定圖片如下圖所示:
手機螢幕如下圖所示:
//seekImage(imageName) 示例,全屏查詢指定影象
var device = Device.getMain(); //獲取當前裝置物件
var ret = device.seekImage("E:\\sigmaTC\\產品20180611\\image\\image1080.bmp");
if (ret != null){
print("Center X: " + ret.x);
print("Center Y: " + ret.y);
} else {
print("沒有在手機螢幕找到該圖");
}
// seekImage(imageName, sim)示例,全屏查詢指定影象,指定相似度
var device = Device.getMain(); //獲取當前裝置物件
var ret = device.seekImage("E:\\sigmaTC\\產品20180611\\image\\image1080.bmp", 1.0);
if (ret != null){
print("Center X: " + ret.x);
print("Center Y: " + ret.y);
} else {
print("沒有在手機螢幕找到該圖");
}
//seekImage(topLeftX, topLeftY, bottomRightX, bottomRightY, imageName)示例,指定區域查詢指定影象
var device = Device.getMain(); //獲取當前裝置物件
var ret = device.seekImage(13, 422, 583, 986, "E:\\sigmaTC\\產品20180611\\image\\image1080.bmp");
if (ret != null){
print("Center X: " + ret.x);
print("Center Y: " + ret.y);
} else {
print("沒有在手機螢幕找到該圖");
}
//seekImage(topLeftX, topLeftY, bottomRightX, bottomRightY, imageName, sim)示例,指定區域查詢指定影象,指定相似度
var device = Device.getMain(); //獲取當前裝置物件
var ret = device.seekImage(13, 422, 583, 986, "E:\\sigmaTC\\產品20180611\\image\\image1080.bmp", 1.0);
if (ret != null){
print("Center X: " + ret.x);
print("Center Y: " + ret.y);
} else {
print("沒有在手機螢幕找到該圖");
}
//seekImage(imageInfo)示例,查詢指定ImageInfo物件影象
var device = Device.getMain(); //獲取當前裝置物件
// 建立ImageInfo物件
var imageInfo = newImageInfo();
imageInfo.imagePath="E:\\sigmaTC\\產品20180611\\image\\image1080.bmp";
imageInfo.x=3;
imageInfo.y=3;
imageInfo.width=device.width - 10;
imageInfo.height=device.height - 10;
imageInfo.srcResolutionX=720;
imageInfo.srcResolutionY=1280;
//指定區域查詢指定ImageInfo物件影象
var ret = device.seekImage(imageInfo);
if (ret != null){
print("Center X: " + ret.x);
print("Center Y: " + ret.y);
} else {
print("沒有在手機螢幕找到該圖");
}
//seekImage(imageName, sim)示例,查詢指定ImageInfo物件影象,指定相似度
var device = Device.getMain(); //獲取當前裝置物件
// 建立ImageInfo物件
var imageInfo = newImageInfo();
imageInfo.imagePath="E:\\sigmaTC\\產品20180611\\image\\image1080.bmp";
imageInfo.x=5;
imageInfo.y=5;
imageInfo.width=device.width - 5;
imageInfo.height=device.height - 10;
imageInfo.srcResolutionX=720;
imageInfo.srcResolutionY=1280;
//查詢指定ImageInfo物件影象,指定相似度
var ret = device.seekImage(imageInfo, 0.7);
if (ret != null){
print("Center X: " + ret.x);
print("Center Y: " + ret.y);
} else {
print("沒有在手機螢幕找到該圖");
}
//seekImage(topLeftX, topLeftY, bottomRightX, bottomRightY, imageName, sim, beGray)示例,指定區域查詢指定影象,指定相似度,指定是否進行影象灰度處理
var device = Device.getMain(); //獲取當前裝置物件
var ret = device.seekImage(13, 422, 583, 986, "E:\\sigmaTC\\產品20180611\\image\\image1080.bmp", 0.8, true);
if (ret != null){
print("Center X: " + ret.x);
print("Center Y: " + ret.y);
} else {
print("沒有在手機螢幕找到該圖");
}
//seekImage(imageName, sim, beGray)全屏查詢指定影象,指定相似度,指定是否進行影象灰度處理
var device = Device.getMain(); //獲取當前裝置物件
var ret = device.seekImage(13, 422, 583, 986, "E:\\sigmaTC\\產品20180611\\image\\image1.bmp", 0.8, false);
if (ret != null){
print("Center X: " + ret.x);
print("Center Y: " + ret.y);
} else {
print("沒有在手機螢幕找到該圖");
}
執行結果:
//在手機螢幕找到指定圖片,則返回:
Center X: 277
Center Y: 890
//在手機螢幕未找到指定圖片,則返回:
沒有在手機螢幕找到該圖