Auto.js images.matchTemplate() 函数的特点
官方文档:https://hyb1996.github.io/AutoJs-Docs/#/images?id=imagesmatchtemplateimg-template-options
images.matchTemplate(img, template, options)
[v4.1.0新增]
img
{Image} 大图片template
{Image} 小图片(模板)options
{Object} 找图选项:threshold
{number} 图片相似度。取值范围为0~1的浮点数。默认值为0.9。region
{Array} 找图区域。参见findColor函数关于region的说明。max
{number} 找图结果最大数量,默认为5level
{number} 一般而言不必修改此参数。不加此参数时该参数会根据图片大小自动调整。找图算法是采用图像金字塔进行的, level参数表示金字塔的层次, level越大可能带来越高的找图效率,但也可能造成找图失败(图片因过度缩小而无法分辨)或返回错误位置。因此,除非您清楚该参数的意义并需要进行性能调优,否则不需要用到该参数。
- 返回 {MatchingResult}
在大图片中搜索小图片,并返回搜索结果MatchingResult。该函数可以用于找图时找出多个位置,可以通过max参数控制最大的结果数量。也可以对匹配结果进行排序、求最值等操作。
找图失败的原因很可能是:从本地读取的图片文件路径不对,图片对象为undefined 或 null。
images.matchTemplate() 如果找图失败,会直接抛出空指针异常:
01:08:28.081/E: Wrapped java.lang.NullPointerException: template = null (file:///android_asset/modules/__images__.js#430)
Wrapped java.lang.NullPointerException: template = null
at file:///android_asset/modules/__images__.js:430:0
at /storage/emulated/0/脚本/有用的脚本/收取能量测试.js:6:0
结果是脚本直接在此处抛异常,并停止。
结论:
images.matchTemplate() 如果找图失败,并在images.matchTemplate()方法出停止运行脚本,找图失败多是因为图片对象为undefined 或 null。
如果找图成功,返回 {MatchingResult},{MatchingResult}可以是空集。当返回空集时,代表找不到图片,找图的算法是正常运行结束的,不是抛出异常结束脚本。