Skip to content

图片模块(image)

image 模块约定

找色 / 找图:区域四元组在前,如 findPic(0, 0, 1080, 1920, 'btn.bmp', 0.9)。模板放在 template/ 或脚本同目录,按文件名读取。找图为逐像素比较(sim 控制颜色容差)。单点接口返回首个命中或 null*Ex 返回数组。颜色推荐 #RRGGBB-#tol。多点找色 points[{ dx, dy, color }, ...]。无默认值,参数均必填。

最短示例:

javascript
reg('YOUR_KEY');
let p = image.findPic(0, 0, 1080, 1920, 'btn.bmp', 0.9);
if (p) auto.click(p.centerX, p.centerY);
const c = image.findColor(0, 0, 1080, 1920, '#FF0000-#101010', 0.9, 0);
if (c) auto.click(c.x, c.y);

image.capture - 截图

typescript
function image.capture(): Promise<imageId | null>;
function image.capture(x1, y1, x2, y2): Promise<imageId | null>;

参数说明:

参数名类型是否必填默认值描述
(无参)---全屏截图
x1,y1,x2,y2number区域时必填-矩形区域(物理像素)

返回值:

类型描述
`Promise<imageIdnull>`

示例:

javascript
let id = image.capture();
const part = image.capture(0, 0, 200, 200);

image.findColor - 找色(首个命中)

typescript
function image.findColor(x1, y1, x2, y2, color, sim, dir): Promise<{x,y}|null>;

参数说明:

参数名类型是否必填默认值描述
x1, y1, x2, y2number-查找区域(物理像素)
colorstring-#FFFFFF-#202020;多色用 `
simnumber-相似度 0~1
dirnumber-查找方向:0 左→右/上→下;1 左→右/下→上;2 右→左/上→下;3 右→左/下→上

返回字段:

字段类型描述
x, ynumber命中点物理像素;未找到返回 null

示例:

javascript
let p = image.findColor(0, 0, 1080, 1920, '#00FF00-#101010', 0.92, 0);
if (p) auto.click(p.x, p.y);

image.findColorEx - 找色(全部命中)

typescript
function image.findColorEx(x1, y1, x2, y2, color, sim, dir, limit): Promise<{x,y}[]>;

参数说明:

参数名类型是否必填默认值描述
x1, y1, x2, y2number-查找区域(物理像素)
colorstring-#FFFFFF-#202020;多色用 `
simnumber-相似度 0~1
dirnumber-查找方向:0 左→右/上→下;1 左→右/下→上;2 右→左/上→下;3 右→左/下→上
limitnumber-最多命中数

返回值:

类型描述
Promise<{x,y}[]>全部命中点;无则为空数组

示例:

javascript
let hits = image.findColorEx(0, 0, 500, 800, '#FFFFFF-#20', 0.9, 0, 10);
logger.debug('命中数', hits.length);

image.findMultiColor - 多点找色(首个命中)

typescript
function image.findMultiColor(x1, y1, x2, y2, firstColor, points, sim, dir): Promise<{x,y}|null>;

参数说明:

参数名类型是否必填默认值描述
x1, y1, x2, y2number-查找区域(物理像素)
firstColorstring-首色
pointsArray<{dx,dy,color}>-相对偏移点列表
simnumber-相似度
dirnumber-查找方向:0 左→右/上→下;1 左→右/下→上;2 右→左/上→下;3 右→左/下→上

示例:

javascript
let p = image.findMultiColor(0, 0, 1080, 1920, '#FF0000-#10', [
 { dx: 10, dy: 0, color: '#00FF00-#10' },
 { dx: 0, dy: 10, color: '#0000FF-#10' },
], 0.9, 0);

image.findMultiColorEx - 多点找色(全部命中)

typescript
function image.findMultiColorEx(x1, y1, x2, y2, firstColor, points, sim, dir, limit): Promise<{x,y}[]>;

参数说明:

参数名类型是否必填默认值描述
x1, y1, x2, y2number-查找区域(物理像素)
firstColorstring-首色
pointsArray<{dx,dy,color}>-相对偏移点列表
simnumber-相似度
dirnumber-查找方向:0 左→右/上→下;1 左→右/下→上;2 右→左/上→下;3 右→左/下→上
limitnumber-最多命中数

示例:

javascript
let hits = image.findMultiColorEx(0, 0, 1080, 1920, '#FF0000-#10', [
 { dx: 10, dy: 0, color: '#00FF00-#10' },
], 0.9, 0, 10);

image.findPic - 找图(首个命中)

typescript
function image.findPic(x1, y1, x2, y2, pic, sim): Promise<FindHit|null>;

参数说明:

参数名类型是否必填默认值描述
x1, y1, x2, y2number-查找区域(物理像素)
picstring-模板路径或内存 imageId(推荐 BMP)
simnumber-相似度;逐像素比较,1=精确,0.9≈每通道容差 25

返回字段(FindHit):

字段类型描述
x, ynumber左上角
ex, eynumber右下角
width, heightnumber宽高
centerX, centerYnumber中心(常用于点击)
confidencenumber匹配度

示例:

javascript
let p = image.findPic(0, 0, 1080, 1920, 'btn.bmp', 0.9);
if (p) auto.click(p.centerX, p.centerY);

image.findPicEx - 找图(全部命中)

typescript
function image.findPicEx(x1, y1, x2, y2, pic, sim, limit): Promise<FindHit[]>;

参数说明:

参数名类型是否必填默认值描述
x1, y1, x2, y2number-查找区域(物理像素)
picstring-模板路径或内存 imageId(推荐 BMP)
simnumber-相似度;逐像素比较,1=精确,0.9≈每通道容差 25
limitnumber-最多命中数

返回值:

类型描述
Promise<FindHit[]>全部命中;无则为空数组(字段同 findPic)

示例:

javascript
let all = image.findPicEx(0, 0, 500, 800, 'icon.bmp', 0.85, 10);
logger.debug('命中数', all.length);

image.cmpColor - 比色

typescript
function image.cmpColor(x, y, color, sim): Promise<boolean>;
function image.cmpColor(points, sim): Promise<boolean>;

参数说明:

参数名类型是否必填默认值描述
x,y,color-单点形式-单点比色
pointsArray<{x,y,color}>多点形式-全部匹配才为 true

示例:

javascript
let ok = image.cmpColor(100, 200, '#FF0000-#10', 0.9);

image.readImage / saveTo / release / getSize / pixel / argb

typescript
readImage(path) → imageId
saveTo(imageId, filePath) → boolean
release(imageId) / releaseAll()
getSize(imageId) → {width,height}
pixel(imageId, x, y) → number
argb(color) → string

示例:

javascript
let id = image.readImage('tpl.png');
const sz = image.getSize(id);
image.release(id);