Auto.js实现页面找图标点击


前言

随着技术的不断发展,广大软件都添加了防止自动化的功能,导致常规通过控件ID、坐标、属性点击方法无法使用,这时就需要找图的方法替代。


一、准备工作

首先需要三张图片,一张手机软件的页面图,两张从页面PS下来的小图标,注意需要寻找的小图标一定要是从页面图上面扣下来的,否则肯定找不到图标,文章最后附上运行效果视频,及本文使用的素材图片,直接添加测试。

二、代码使用

1.代码片段

代码如下(示例):

auto.waitFor();
console.show();
console.setPosition(100, 1200);
//打开目标app
app_init(0);
//寻找返回图标 图标也可以放在sd卡下如"/sdcard/ceshi.jpg"
finding("./ceshi.jpg","./img_back.png",0.75,1,1);
//寻找加号图标
finding("./ceshi.jpg","./img_add.png",0.75,1,1);

 /**
     * 启动应用 这步是为了真实点击到页面图标 可以省略看log找没找到
     */
    function app_init(num) {
        var packageName = getPackageName("找图测试");
        if(null!=packageName){
         home();
         sleep(1000);
         app.launchPackage(packageName);
         echo_log("打开找图测试");
         echo_log("手机分辨率--"+device.width+"/"+device.height);
        }else{
         echo_log("未找到找图测试,任务结束");
         exit();
        }
    }
     /**
      * 寻找图标
      *parent 大图
      *find 要寻找的图标
      *sbl 图片相似度。取值范围为0~1的浮点数。默认值为0.9。
      *a|b int类型 解决点击在图标左上角点击不到 加几个像素
      */
function finding(parent,find,sbl,a,b){
    let imgPath = parent;
    imgPath = files.path(imgPath);
    let img = images.read(imgPath);
    let grayImg = images.grayscale(img);
    let threshold = 188;
    let thresholdImg = images.threshold(grayImg, threshold, 255);
    images.save(thresholdImg, "/sdcard/二值化.png");
    img.recycle();
    grayImg.recycle();
    thresholdImg.recycle();
    let big= images.read("/sdcard/二值化.png");
    if(big==null)
      echo_log("大图二值化失败")
     let small1= images.read(find);
     if(small1==null)
       echo_log("获取小图标失败")
       //1440 2960为截大图的手机分辨率
    let sx = device.width/1440;
    let sy = device.height/2960;
    let small= images.scale(small1,sx,sy);

    if(small==null)
       echo_log("图标缩放失败");
       else
       echo_log("图标缩放比例"+sx+"--"+sy);

    let result = images.matchTemplate(big,small,{
        max:5,
        region:[0,0, device.width,device.height],
        threshold:sbl,
    });
   if(result != null){
   if(0===result.matches.length){
     echo_log("未找到"+result.matches.length)
   }else{
     echo_log("找到"+result.matches.length)
      for(let i =0; i < result.matches.length; i++){
               let pp = result.matches[i].point
               echo_log("找到--"+find+"/坐标/"+pp.x+"/"+pp.y)
               click(pp.x + a, pp.y + b);
               echo_log("点击"+find)
               sleep(2000);
               break;
      }
      //将找到图片的位置滑下来 找到图标后可以去sd卡看一下 也可以删掉
      var points=result.points;
      var canvas = new Canvas(big);
      var paint = new Paint();
      paint.setColor(colors.parseColor("#ffff00"));
      points.forEach(point => {
          canvas.drawRect(point.x, point.y, point.x + small.width, point.y + small.height, paint);
      });
      var image = canvas.toImage();
      images.save(image, "/sdcard/找图.png");
      big.recycle();
      small1.recycle();
      small.recycle();
   }
   }else{
        echo_log("未找到"+find);
         big.recycle();
         small1.recycle();
         small.recycle();
   }
   }

        /**
         * 在控制台统一输出内容
        */
        function echo_log(msg) {
            console.log(msg);
        }




2.注意事项

注意:如果找不到图标可以修改图片相似度、检查图标切的准确度、截图手机与运行手机的分辨率问题

3.素材

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

总结

`
以上就是今天要讲的内容,也是查阅了很多大佬文章摸索出来的,希望对大家有所帮助。

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
spop.js是一款纯js toast消息提示通知插件。通过spop.js插件,你可以快速的在网页上制作出漂亮的toast消息提示效果。该js toast消息提示插件的特点还有: 内置4种主题样式:default, success, warning, error。 toast可以显示在屏幕的6个位置:top-left, top-center, top-right, bottom-left, bottom-center, bottom-right。 可以对消息提示进行编组。 提供打开和关闭消息提示时的回调函数。 支持HTML内容。 使用方法 在页面中引入spop.css和spop.js文件。 <link rel="stylesheet" type="text/css" href="./css/spop.css"> [removed][removed] 初始化toast 该js toast消息提示插件最基本的使用方法如下: spop('Default SmallPop'); spop('<h4 class="spop-title">Success</h4>I´m a success SmallPop', 'success'); spop('<strong>Warning pop</strong>', 'warning'); spop('<strong>Error Here!</strong>', 'error'); 分组 可以对消息进行分组,分组后的消息每次只显示一条。 spop({ template: 'All fields are required!', group: 'submit-satus', style: 'error' }); spop({ template: 'Your information has been submitted', group: 'submit-satus', style: 'success' autoclose: 2000 }); 回调函数 在toast消息提示打开和关闭时都可以使用回调函数。 spop({ template: 'Please, close me.', onOpen: function () { document.body.style.background = "#fff"; }, onClose: function() { document.body.style.background = ""; spop({ template: 'Thank you!', style: 'success', autoclose: 2000 }); } }); 配置参数 spop.js消息提示插件的可用配置参数如下: spop({ template : null,// string required. Without it nothing happens! style : 'info',// success, warning or error autoclose : false,// miliseconds position : 'top-right',// top-left top-center bottom-left bottom-center bottom-right icon : true,// or false group : false,// string, add a id reference onOpen : funtion() { }, onClose : funtion() { } }); template :消息提示的模板。可以是一个字符串,或这是HTML代码。 style :toast消息提示的主题样式,可以是info,success, warning 或 error。 autoclose :是否自动关闭。 position :toast消息提示的位置。可以是:top-right,top-left,top-center,bottom-left,bottom-center或bottom-right。 icon :是否显示图标。 group :是否对消息进行分组。 onOpen :toast消息提示打开时的回调函数。 onClose :toast消息提示关闭时的回调函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木易明~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值