当前位置:网站首页>Auto.js找图找色常用功能

Auto.js找图找色常用功能

2022-08-10 20:26:00 aiguangyuan

1. 颜色的表示方法

AA 是Alpha通道(透明度)的值;

RR 是R通道(红色)的值;

GG 是G通道(绿色)的值;

BB 是B通道(蓝色)的值;

1.1. 颜色表示方式;

"#AARRGGBB",需要表示透明度的时候用这种方式;

1.2. 颜色表示方式;

"#RRGGBB",不需要表示透明度的时候用这种方式;

2. 颜色的转换方法

2.1. 返回颜色值的字符串;

// 返回颜色整数值所对应的字符串
// colors.toString(colorNum);

console.log(colors.toString(-16777166));
// #ff000032

2.2. 返回颜色的整数值;

// 返回字符串所对应的颜色整数值
// colors.parseColor(colorStr);

console.log(colors.parseColor("#000032");
// -16777166

2.3. 返回这些颜色通道构成的整数颜色值;

// 返回透明度与三种颜色构成的颜色整数值
// colors.argb(alphaNum, redNum, greenNum, blueNum);

console.log(colors.argb(255,255,180,0));
// -19456

console.log(colors.argb(100,255,180,0));
// 1694479360

2.4. 返回这些颜色通道构成的整数颜色值,Alpha通道将是255(不透明);

// 返回三种颜色构成的整数值
// colors.rgb(redNum, greenNum, blueNum);

console.log(colors.rgb(255,180,0));
// -19456

console.log(colors.toString(-19456));
// #ffffb400

2.5. 返回颜色color的Alpha通道的值,范围0~255;

// 返回透明通道的值
// colors.alpha(Num | Str);

console.log(colors.alpha("#ff0032");
// 255(默认值)

console.log(colors.alpha("#faff0032");
// 250

2.6. 返回颜色color的R通道的值,范围0~255;

// 返回红色通道的值
// colors.red(Num | Str);

console.log(colors.red("#CC00FF"));
// 204

2.7. 返回颜色color的G通道的值,范围0~255;

// 返回绿色通道的值
// colors.green(Num | Str);

console.log(colors.green("#CC00FF"));
// 0

2.8. 返回颜色color的B通道的值,范围0~255;

// 返回蓝色通道的值
// colors.blue(Num | Str);

console.log(colors.blue("#CC00FF"));
// 255

3. 比较图片的颜色

3.1. 比较两个颜色是否相似;

// 返回两个颜色是否相似(颜色值1,颜色值2,[临界值(默认为4),算法(默认diff)])
// colors.isSimilar(num|str, num|str,[thresholdNum,algorithm]);

console.log(colors.isSimilar('#000000','#000001'));
// true 

console.log(colors.isSimilar('#000000','#000001',0));
// false

3.2. 比较两个颜色是否相等;

// 返回两个颜色是否相等,该函数会忽略Alpha通道的值进行比较
// colors.equals(num|str, num|str);

console.log(colors.equals('#000000','#000000'));
// true

4. Auto.js内置的颜色

1. colors.BLACK

黑色,颜色值 #FF000000

2. colors.DKGRAY

深灰色,颜色值 #FF444444

3. colors.GRAY

灰色,颜色值 #FF888888

4. colors.LTGRAY

亮灰色,颜色值 #FFCCCCCC

5. colors.WHITE

白色,颜色值 #FFFFFFFF

6. colors.RED

红色,颜色值 #FFFF0000

7. colors.GREEN

绿色,颜色值 #FF00FF00

8. colors.BLUE

蓝色,颜色值 #FF0000FF

9. colors.YELLOW

黄色,颜色值 #FFFFFF00

10. colors.CYAN

青色,颜色值 #FF00FFFF

11. colors.MAGENTA

品红色,颜色值 #FFFF00FF

12. colors.TRANSPARENT

透明,颜色值 #00000000

5. 图片防止内存泄漏

图片对象创建后尽量在不使用时进行回收,图片对象通过recycle函数来回收,同时避免循环创建大量图片。

// 读取图片
var img = images.read("./1.png");

// 对图片进行需要操作

// 回收图片
img.recycle();

截屏的图片不需要回收。

var img = caputerScreen();

// 不需要回收操作

6. 图片操作常用方法

6.1. 读取本地图片;

// 读取本地图片,返回image对象或null
// images.read(path);   

var img = images.read('a.jpg');

img.recycle();

6.2. 加载远程图片;

// 加载远程图片,返回image对象或null
// images.load(url);    

var img = images.load('https://hyb1996.github.io/AutoJs-Docs/images/logo.png');

img.recycle();

6.3. 复制图片;

// 复制图片,返回image对象或null
// images.copy(obj);

var img1 = images.read('a.jpg');

var img2 = images.copy(img1);

6.4. 保存图片;

// 保存图片对象(图片对象,保存路径,[格式,质量])
// images.save(obj,path,[format,quality]);

var img = images.load("https://hyb1996.github.io/AutoJs-Docs/images/logo.png");

images.save(img,"/sdcard/a.jpg","jpg",50);
// 如果文件不存在会被创建,如果存在会被覆盖

6.5. 另存图片;

// 将图片对象另存为一个指定的地址
// obj.saveTo(path);

var img1 = images.read('a.jpg');
var img2 = images.copy(img1);

img2.saveTo('/sdcard/copy.png')

img1.recycle();
img2.recycle();

6.6. 获取图片尺寸;

// 返回以像素为单位图片宽度
// img.getWidth()  

// 返回以像素为单位的图片高度
// img.getHeight()  

var img = images.read('a.png');

if(img) {
    var width = img.getWidth();
    var height = img.getHeight();
    console.log(width,height);
    // 300,400
};

7. 图片的编码转换

// 返回img对象
// images.fromBase64(base64);

// 返回base64数据
// images.toBase64(img,[format = "png", quality = 100]); 

// 返回img对象
// images.fromBytes(bytes); 

// 返回图片字节数据
// images.toBytes(img,[format = "png", quality = 100]); 

var result = http.get("https://hyb1996.github.io/AutoJs-Docs/images/logo.png");

var obj = result.body.bytes();

var img = images.fromBytes(obj);

img.saveTo("/sdcard/bbb.png");

8. 获取屏幕小图

// 从大图片的指定位置处剪切指定尺寸的小图片,返回剪切的小图片
// images.clip(img, x, y, w, h);

// 封装获取屏幕小图的函数
function clipImg(x1,y1,x2,y2,path){
    
    var screen = images.captureScreen();

    var img = images.clip(screen,x1,y1,x2-x1,y2-y1);

    img.saveTo(path);
    
    img.recycle();
};

9. 图片处理的函数

// 调整图片大小,返回调整后的图片
// images.resize(img,size,[interpolation]);

// 放缩图片尺寸,返回放缩后的图片
// images.scale(img,fx,fy,[interpolation]);

// 将图片逆时针旋转degress度,返回旋转后的图片
// images.rotate(img, degress,[x, y]);  

// 连接两张图片,并返回连接后的图像,如果两张图片大小不一致,小的那张将适当居中
// images.concat(img1,img2,[direction]);

// 灰度化图片,并返回灰度化后的图片
// images.grayscale(img);  

// 将图片阈值化,并返回处理后的图像
// image.threshold(img, threshold, maxVal,[type]);

// 对图片进行自适应阈值化处理,并返回处理后的图像
// images.adaptiveThreshold(img, maxValue, adaptiveMethod, thresholdType, blockSize, C); 

// 对图像进行颜色空间转换,并返回转换后的图像
// images.cvtColor(img, code,[dstCn]);   

// 将图片二值化,在lowerBound到upperBound范围以外的颜色都变成0,在范围以内的颜色都变成255
// images.inRange(img,lowerBound,upperBound);

// 将图片二值化,在color-interval ~ color+interval范围以外的颜色都变成0,在范围以内的颜色都变成255
// images.interval(img, color, interval);

// 对图像进行模糊(平滑处理),返回处理后的图像
// images.blur(img, size,[anchor, type]);

// 对图像进行中值滤波,返回处理后的图像
// images.medianBlur(img,size);

// 对图像进行高斯模糊,返回处理后的图像
// images.gaussianBlur(img,size,[sigmaX,sigmaY,type]);  

// 把OpenCV的Mat对象转换为Image对象
// images.matToImage(mat);  

10. 申请截屏权限

// 申请截图权限
// images.requestScreenCapture([landscape]);

// 主线程申请权限时,新开一个线程点击授权

// 自动点击获取授权
threads.start(function(){
    // 在新线程执行的代码
    while(true){
        if(text('立即开始').findOnce()) {
            text('立即开始').findOnce().click()
            break;
        }else{
            sleep(3000)
        }
    }
});

// 请求截屏
if(!requestScreenCapture()){
    toast("请求截屏失败");
    exit();
};

11. 使用截屏功能

在使用截屏功能时,首先要申请截屏权限。

11.1. 截屏有返回对象;

// 截屏有返回对象

// var img = images.captureScreen();
// 等价于
var img = captureScreen();
// 可对返回对象执行相关操作
img.saveTo('/sdcard/a.png');

11.2. 截屏无返回对象;

// 截屏无返回对象

// images.captureScreen("/sdcard/b.png");
// 等价于
captureScreen("/sdcard/b.png");

12. 获取图片中某个点的颜色

// 读取图片
var img = images.read('/sdcard/a.png');

// 获取图片上某个点的颜色值
// var dot = images.pixel(img, 500, 155);
// 或者
var dot = img.pixel(500, 155);
// 将点的颜色值转换为字符串形式
var color = colors.toString(dot);

13. 在图片中寻找颜色

13.1. 在整个图片区域寻找颜色;

// 在图片中查找指定的颜色
// images.findColor(image,color,options);
// 等价于
// findColor(image,color,options);    


if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
};

// 循环找色,找到红色(#ff0000)时停止并报告坐标
while(true){
    var img = captureScreen();
    // 在图片中查找指定的颜色,可指定临界值
    var point = findColor(img, "#ff0000",{
        threshold: 0
    });
    if(point){
        toast("找到了,坐标为(" + point.x + ", " + point.y + ")");
        break;
    }else{
        toast("没找到");
    };
    sleep(1500);
}

13.2. 在指定区域中寻找颜色;

// 在图片中查找指定的颜色
// images.findColor(image,color,options);
// 等价于
// findColor(image,color,options); 


if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
};

// 循环找色,找到红色(#ff0000)时停止并报告坐标
while(true){
    var img = captureScreen();
    var point = findColor(img, "#ff0000", {
        // 在指定的区域找到指定颜色,数组支持2个值和4个值
        region: [100, 100, 500, 800],
        // 临界值
        threshold: 4
    });
    if(point){
       toast("找到了,坐标为(" + point.x + ", " + point.y + ")");
       break;
    }else{
       toast("没找到");
    };
    sleep(1500);
}

13.3. 在指定区域查找颜色另一种方法;

// 在指定的区域找到指定颜色
// images.findColorInRegion(img, color, x, y,[width, height, threshold]);

if(!requestScreenCapture()){
    toast("请求截屏失败");
    exit();
};

while(true){
    var img = captureScreen();
    // 在指定的区域找到指定颜色,x坐标与y坐标必须指定,长宽尺寸和临界值可不指定
    var point = findColorInRegion(img, "#ff0000",100,100,500,800,4);
    if(point){
       toast("找到了,坐标为(" + point.x + ", " + point.y + ")");
       break;
    }else{
       toast("没找到");
    };
    sleep(1500);
}

13.4. 在图片中寻找完全相等的颜色点;

// 在图片中寻找完全相等的颜色点
// findColorEquals(img, color,[x,y,width,height])

if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
};

while(true){
    var img = captureScreen();
    // 查找相等的颜色,可指定坐标与尺寸,不支持临界值
    var point = findColorEquals(img,"#f74c31");
    if(point){
        toast("找到了,坐标为(" + point.x + ", " + point.y + ")");
        break;
    }else{
       toast("没找到");
    };
    sleep(1500)
};

13.5. 多点找色,在一个图片对象中查找一个符合指定颜色,并且满足多点参考条件的坐标;

// images.findMultiColors(img, firstColor, colors[, options]);

if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
};

var img = images.captureScreen();

var position = images.findMultiColors(img, "#123456", 
    // 该点必须符合以下这些匹配条件
    [
        [10, 20, "#ffffff"], 
        [30, 40, "#000000"]
    ],
    {
        threshold:0 
    },
    region:[,]
);

console.log(position);

13.6. 判断图片中某个位置是否是特定的颜色,常用于点赞操作;


// 在图片中查找指定的位置是否是指定的颜色
// images.detectsColor(image, color, x, y,[threshold = 16, algorithm = "diff"])

if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
};
// 等待排行榜出现
text("排行榜").waitFor();
sleep(1000);

// 找到点赞控件
var like = id("com.tencent.mm:id/bo_").findOne();
sleep(1000);

// 获取该控件中心坐标
var x = like.bounds().centerX();
var y = like.bounds().centerY();

// 屏幕截图
var img = captureScreen();
// 判断在该坐标的颜色是否为指定颜色
if(images.detectsColor(img, "#dedede", x, y)){
    toast("没有点过赞");
    like.parent().parent().click();
}else{
    toast("已经点赞了");
};

14. 在图片中找图片

14.1. 全屏找图;

// images.findImage(img, template,[options])
// 找到时返回位置坐标(Point),找不到时返回null

if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
};
// 要查找的区域
var img = captureScreen();
// 要查找的小图
var template= images.read("/sdcard/small.png");
var result = findImage(img,template);

if(result){
    toast("找到啦");
    click(result.x,result.y);
}else{
    toast("没找到");
};

14.2. 指定区域找图;

// images.findImage(img, template,[options])
// 找到时返回位置坐标(Point),找不到时返回null

if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
};

var icon = images.read("/sdcard/icon.png");
// 返回桌面
home();
// 截图找图
var result = findImage(captureScreen(),icon,{
    // 加上查找起始点,支持两个值或四个值,用来规定区域
    region: [0, 50],
    threshold: 0.8
});

if(result){
    toast("在桌面找到了微信图标");
}else{
    toast("桌面没有找到微信图标");
}

14.3. 区域找图另一种方式;

// 在大图中指定的区域查找小图
// images.findImageInRegion(img,template,x,y,[width,height,threshold])

if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
};

// 要查找的大图
var img = captureScreen();
// 要查找的小图
var template= images.read("/sdcard/small.png");


// var result = images.findImageInRegion(img,template,20,30,200,300,0);
// 等价于
var result = findImageInRegion(img,template,20,30,200,300,0);

if(result){
    toast("找到啦");
}else{
    toast("没找到");
};

15. 批量找图及其相关操作

15.1. 批量找图;

// 根据指定的数量批量找图
// images.matchTemplate(img, template,[options])

if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
}

var img = captureScreen();

var template= images.read("/sdcard/small.png");
// 在屏幕中查找10个小图
var result = images.matchTemplate(img,template,{
    // 指定查找的数量
    max: 10
});

console.log(result);

15.2. 批量找图忽略相似度输出坐标不一样的图;

if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
};


var img = captureScreen();
var template = images.read("/sdcard/small.png");
var result = images.matchTemplate(img,template,{
    max: 100
});
var oldObj = null;


// 忽略相似度,输出找到的坐标不一样的对象
result.matches.forEach(obj => {
    if(oldObj != null){
        if(obj.point.x != oldObj.point.x || obj.point.y != oldObj.point.y){
            console.log("point = " + obj.point);
        };
    }else{
        console.log("point = " + obj.point);
    };
    oldObj = obj
});

15.3. 批量找图结果中相关的操作方法;

在大图片中搜索小图片,并返回搜索结果MatchingResult。该函数可以用于找图时找出多个位置,可以通过max参数控制最大的结果数量,也可以对匹配结果进行排序、求最值等操作。

1. points

匹配位置的数组。

2. first()

 第一个匹配结果,如果没有任何匹配则返回null。

3. last()

最后一个匹配结果,如果没有任何匹配则返回null。

4. leftmost()

位于大图片最左边的匹配结果,如果没有任何匹配则返回null。

5. topmost()

位于大图片最上边的匹配结果,如果没有任何匹配则返回null。

6. rightmost()

位于大图片最右边的匹配结果,如果没有任何匹配则返回null。

7. bottommost()

位于大图片最下边的匹配结果,如果没有任何匹配则返回null。

8. best()

相似度最高的匹配结果,如果没有任何匹配则返回null。

9. worst()

相似度最低的匹配结果,如果没有任何匹配则返回null。

10. sortBy()

比较函数,或者是一个字符串表示排序方向,方向包括left(左) ,top(上) ,right(右) ,bottom(下)。

例如:"left"表示将匹配结果按匹配位置从左往右排序,"top"表示将匹配结果按匹配位置从上往下排序,"left-top"表示将匹配结果按匹配位置从左往右、从上往下排序。

var result = images.matchTemplate(img, template, {
    max: 100
});
console.log(result.sortBy("top-right"));
原网站

版权声明
本文为[aiguangyuan]所创,转载请带上原文链接,感谢
https://aiguangyuan.blog.csdn.net/article/details/126203005