当前位置:网站首页>Common functions of Auto.js to find pictures and colors

Common functions of Auto.js to find pictures and colors

2022-08-10 20:58:00 aiguangyuan

1. 颜色的表示方法

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

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

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

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

1.1. 颜色表示方式;

"#AARRGGBB",In this way when need said transparency;

1.2. 颜色表示方式;

"#RRGGBB",Don't need to said transparency in this way;

2. Color conversion method

2.1. The color value string;

// Returns the string of color by integer value
// colors.toString(colorNum);

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

2.2. Returns the color integer value;

// Returns a string that corresponds to the color of the integer value
// colors.parseColor(colorStr);

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

2.3. Returns the color channel color value of integer;

// Returns the transparency and the color of 3 kinds of color composition integer value
// 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. Returns the color channel color value of integer,AlphaThe channel will be255(不透明);

// Returns the 3 kinds of color composition integer value
// 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;

// Returns the value of transparent channel
// 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. Compare the color of the picture

3.1. Compare the two color is similar;

// Similar whether returns two color(颜色值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. 比较两个颜色是否相等;

// Returns two colors are equal,This function will ignoreAlpha通道的值进行比较
// 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. To prevent a memory leak images

Image objects are created when not in use as far as possible to recycle,Image objects throughrecycle函数来回收,同时避免循环创建大量图片.

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

// Need to manipulate images

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

Screenshots of the images don't need to recycle.

var img = caputerScreen();

// Don't need to recovery operation

6. Image manipulation methods

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. 保存图片;

// Save the object images(图片对象,保存路径,[格式,质量])
// 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);
// 如果文件不存在会被创建,If there will be covering

6.5. 另存图片;

// Save the image object to a specified address
// 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. 获取图片尺寸;

// Return in pixels width images
// img.getWidth()  

// Return in pixels of the image height
// 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. The picture of the code conversion

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

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

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

// Returns the byte data images
// 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. Take screen picture

// Cut from the location on the big picture of the specified size small picture,Returns the shear small picture
// images.clip(img, x, y, w, h);

// Encapsulation for screen insets function
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. The image processing function

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

// Zoom in image size,After return to zoom in pictures
// images.scale(img,fx,fy,[interpolation]);

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

// To connect two pictures,And return after the connection of the image,If two images size inconsistent,The one small are centered properly
// images.concat(img1,img2,[direction]);

// 灰度化图片,And return after the grayscale images
// images.grayscale(img);  

// The image threshold value,And the processed image is returned
// image.threshold(img, threshold, maxVal,[type]);

// Adaptive threshold binarization processing images,And the processed image is returned
// images.adaptiveThreshold(img, maxValue, adaptiveMethod, thresholdType, blockSize, C); 

// 对图像进行颜色空间转换,And returns the converted image
// 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]);

// The main thread right time,Open a new thread click authorization

// Automatically click access authorization
threads.start(function(){
    // 在新线程执行的代码
    while(true){
        if(text('立即开始').findOnce()) {
            text('立即开始').findOnce().click()
            break;
        }else{
            sleep(3000)
        }
    }
});

// Request screenshots
if(!requestScreenCapture()){
    toast("Request screenshots failed");
    exit();
};

11. 使用截屏功能

When using screenshot function,First to apply for screenshots permission.

11.1. Screenshots have return objects;

// Screenshots have return objects

// var img = images.captureScreen();
// 等价于
var img = captureScreen();
// Related operations can be performed to return objects
img.saveTo('/sdcard/a.png');

11.2. Screenshots no return objects;

// Screenshots no return objects

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

12. Access to a certain point in the picture color

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

// Access to a certain point on the picture color value
// var dot = images.pixel(img, 500, 155);
// 或者
var dot = img.pixel(500, 155);
// Will point the color value converted to a string of
var color = colors.toString(dot);

13. Looking for color in the picture

13.1. In the whole area for color images;

// To look for in the image specified color
// images.findColor(image,color,options);
// 等价于
// findColor(image,color,options);    


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

// 循环找色,找到红色(#ff0000)When to stop and report the coordinates
while(true){
    var img = captureScreen();
    // To look for in the image specified color,Can specify the critical value
    var point = findColor(img, "#ff0000",{
        threshold: 0
    });
    if(point){
        toast("找到了,坐标为(" + point.x + ", " + point.y + ")");
        break;
    }else{
        toast("没找到");
    };
    sleep(1500);
}

13.2. In the designated area for color;

// To look for in the image specified color
// images.findColor(image,color,options);
// 等价于
// findColor(image,color,options); 


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

// 循环找色,找到红色(#ff0000)When to stop and report the coordinates
while(true){
    var img = captureScreen();
    var point = findColor(img, "#ff0000", {
        // Specified in the designated area to find color,数组支持2个值和4个值
        region: [100, 100, 500, 800],
        // 临界值
        threshold: 4
    });
    if(point){
       toast("找到了,坐标为(" + point.x + ", " + point.y + ")");
       break;
    }else{
       toast("没找到");
    };
    sleep(1500);
}

13.3. In the designated area to find color another way;

// Specified in the designated area to find color
// images.findColorInRegion(img, color, x, y,[width, height, threshold]);

if(!requestScreenCapture()){
    toast("Request screenshots failed");
    exit();
};

while(true){
    var img = captureScreen();
    // Specified in the designated area to find color,x坐标与yCoordinates must be specified,Width size and critical value is not specified
    var point = findColorInRegion(img, "#ff0000",100,100,500,800,4);
    if(point){
       toast("找到了,坐标为(" + point.x + ", " + point.y + ")");
       break;
    }else{
       toast("没找到");
    };
    sleep(1500);
}

13.4. Seek exactly equal in the picture the color of the point;

// Seek exactly equal in the picture the color of the point
// findColorEquals(img, color,[x,y,width,height])

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

while(true){
    var img = captureScreen();
    // Find the same color,But with the specified coordinates to the size,Does not support threshold
    var point = findColorEquals(img,"#f74c31");
    if(point){
        toast("找到了,坐标为(" + point.x + ", " + point.y + ")");
        break;
    }else{
       toast("没找到");
    };
    sleep(1500)
};

13.5. 多点找色,In an image object lookup a conform to the specified color,And the coordinates of the multi-point reference condition;

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

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

var img = images.captureScreen();

var position = images.findMultiColors(img, "#123456", 
    // This point must meet the following these matching conditions
    [
        [10, 20, "#ffffff"], 
        [30, 40, "#000000"]
    ],
    {
        threshold:0 
    },
    region:[,]
);

console.log(position);

13.6. Determine a location in the picture is the color of the specific,Often for thumb up operation;


// To find the location specified in the picture is the color of the specified
// images.detectsColor(image, color, x, y,[threshold = 16, algorithm = "diff"])

if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
};
// Waiting list appears
text("排行榜").waitFor();
sleep(1000);

// Find a thumb up control
var like = id("com.tencent.mm:id/bo_").findOne();
sleep(1000);

// Access to the control center coordinates
var x = like.bounds().centerX();
var y = like.bounds().centerY();

// 屏幕截图
var img = captureScreen();
// Whether the color at the judgment in the coordinates to the specified color
if(images.detectsColor(img, "#dedede", x, y)){
    toast("没有点过赞");
    like.parent().parent().click();
}else{
    toast("已经点赞了");
};

14. Find images in pictures

14.1. 全屏找图;

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

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

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

14.2. The designated area looking for a figure;

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

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

var icon = images.read("/sdcard/icon.png");
// 返回桌面
home();
// Screenshots find figure
var result = findImage(captureScreen(),icon,{
    // And find the starting point,Support two or four values,Used to designated areas
    region: [0, 50],
    threshold: 0.8
});

if(result){
    toast("Found WeChat on the desktop icon");
}else{
    toast("The desktop is not found WeChat icon");
}

14.3. Area to find figure another way;

// In a larger designated area to find insets
// images.findImageInRegion(img,template,x,y,[width,height,threshold])

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

// To find a larger version of
var img = captureScreen();
// To find the picture
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. Batch find figure and its related operations

15.1. Batch to find figure;

// According to the specified number of batch find figure
// images.matchTemplate(img, template,[options])

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

var img = captureScreen();

var template= images.read("/sdcard/small.png");
// In the screen to find10个小图
var result = images.matchTemplate(img,template,{
    // Specifies the amount of search
    max: 10
});

console.log(result);

15.2. Batch find figure ignore similarity output coordinates are not the same figure;

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;


// Ignore the similarity,Is not the same as the output to find the coordinates of the object
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. Batch to find figure results in the relevant operating method;

In the big picture search little pictures,并返回搜索结果MatchingResult.This function can be used to find the exact location of the multiple while looking for a figure,可以通过maxParameters control the maximum number of results,Also can sort the result of the match、Get the most value, etc.

1. points

Match the location of the array.

2. first()

 The first matching results,If no match is returnednull.

3. last()

The final match result,If no match is returnednull.

4. leftmost()

Located at the most on the left side of the big picture matching results,If no match is returnednull.

5. topmost()

In the big picture of the top matches,If no match is returnednull.

6. rightmost()

In the big picture at the far right matching results,If no match is returnednull.

7. bottommost()

In the big picture of the bottom match results,If no match is returnednull.

8. best()

The highest similarity matching results,If no match is returnednull.

9. worst()

The lowest similarity matching results,If no match is returnednull.

10. sortBy()

比较函数,Or a string representation sort direction,方向包括left(左) ,top(上) ,right(右) ,bottom(下).

例如:"left"Said it will match the results according to the matching position from left to right order,"top"Said it would match result according to the matching position from the top down,"left-top"Said it will match the results according to the matching position from left to right、From above sorting.

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

版权声明
本文为[aiguangyuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102024550108.html