Skip to content

图片 Images

截图、拍照、模板找图,以及裁剪、缩放。识字见 文字识别 OCR,取色找色见 取色。权限与适用场景见 概述

capture

返回

如果在截图时,被运行时间悬浮窗遮挡,可以使用System.setTimeWindowShow(false)隐藏时间窗口

屏幕截图(需屏幕截图 / MediaProjection 权限)

javascript
try{
    let imageFile = Images.capture();//获取图片地址  后续可以通过此地址进行图片处理
}catch(e){
    console.log("截图异常:" + e.message);
}

takePhoto / takePhoto(path)

path {string} 可选。省略时写入应用 cache

返回 {string} JPEG 照片绝对路径

使用后置摄像头静默拍照。需要 CAMERA 权限不需要 MediaProjection(与 capture 无关)。

javascript
try{
    let photo = Images.takePhoto();
    console.log("照片:" + photo);

    // 或指定路径(绝对路径 / project:// / 相对项目根)
    let photo2 = Images.takePhoto(Files.getCachePath() + "/shot.jpg");
}catch(e){
    console.log("拍照异常:" + e.message);
}

getMat(imageFile)

imageFile {string} 图片地址

返回 {Mat} 矩阵对象

获取图片的矩阵对象

javascript
try{
    let imageFile = Images.capture();//获取图片地址  后续可以通过此地址进行图片处理
    let mat = Images.getMat(imageFile);
}catch(e){
    console.log("异常:" + e.message);
}

findOne(source, template, threshold)

source {Mat|string} 大图 Mat,或图片路径

template {Mat|string} 模板 Mat,或模板路径

threshold {number} 匹配阈值,常用 0.8;路径重载可省略(默认 0.8

返回 {Point|null} 模板左上角;未找到为 null

从 source 中查找 template。也支持直接传文件路径:

javascript
// 路径重载(推荐)
let point = Images.findOne(Images.capture(), "/xxx/template.png", 0.8);

// Mat 重载
let mat = Images.getMat(Images.capture());
let templateMat = Images.getMat("/xxx/template.png");
let point2 = Images.findOne(mat, templateMat, 0.8);
if (point2 != null) {
    console.log("位置:" + point2.x + "," + point2.y);
}

findOneMatch(sourceFile, templateFile, threshold)

返回

findOne 类似,但返回中心点与分数:x y centerX centerY score width height。点击请用中心点。

javascript
let m = Images.findOneMatch(Images.capture(), "/xxx/template.png", 0.8);
if (m) {
    console.log(m.centerX, m.centerY, m.score);
}

findOneInRegion(sourceFile, templateFile, threshold, left, top, width, height)

返回

仅在指定矩形区域内找图。

javascript
let m = Images.findOneInRegion(
    Images.capture(),
    "/xxx/template.png",
    0.8,
    0, 0, 1080, 800
);
if (m) {
    console.log(m.centerX, m.centerY);
}

find(source, template, threshold)

source / template

返回 {Point[]} 所有匹配左上角(含 NMS 去重)

javascript
let points = Images.find(Images.capture(), "/xxx/template.png", 0.8);
for (let i = 0; i < points.length; i++) {
    console.log(points[i].x, points[i].y);
}

findMatches(sourceFile, templateFile, threshold)

返回

多目标找图,每项含中心点与分数。

javascript
let matches = Images.findMatches(Images.capture(), "/xxx/icon.png", 0.8);
for (let i = 0; i < matches.length; i++) {
    console.log(matches[i].centerX, matches[i].centerY, matches[i].score);
}

crop(imageFile, left: number, top: number, width: number, height: number)

imageFile {string} 图片地址

left {number} 左边距

top {number} 上边距

width {number} 采集图片宽度

height {number} 裁剪图片高度

返回 {string} 裁剪后的图片地址

裁剪图片

javascript
try{
    let imageFile = Images.capture();//获取图片地址  后续可以通过此地址进行图片处理
    let scopImages = Images.crop(imageFile, 0, 0, 100, 100);
    console.log(scopImages);//裁剪后的图片地址
}catch(e){
    console.log("异常:" + e.message);
}

scale(imageFile, multiple)

imageFile {string} 图片地址

multiple {number} 缩放倍数(支持小数,如 0.5

返回 {string} 缩放后的图片地址

缩放图片

javascript
try{
    let imageFile = Images.capture();
    let scopImages = Images.scale(imageFile, 0.5);
    console.log(scopImages);
}catch(e){
    console.log("异常:" + e.message);
}

相关页