示例

## 获取网络图片并保存 ```js //这个是Auto.js图标的地址 var url = "https://www.autojs.org/assets/uploads/profile/3-profileavatar.png"; var logo = images.load(url); //保存到路径/sdcard/auto.js.png images.save(logo, "/sdcard/auto.js.png"); ``` ## 截图并保存 ```js if(!requestScreenCapture()){ toast("请求截图失败"); exit(); } var img = captureScreen(); images.saveImage(img, "/sdcard/1.png"); ``` ## 精确找色 ```js if(!requestScreenCapture()){ toast("请求截图失败"); stop(); } var img = captureScreen(); toastLog("开始找色"); //0x1d75b3为编辑器默认主题蓝色字体(if, var等关键字)的颜色 //找到颜色与0x1d75b3完全相等的颜色 var point = findColorEquals(img, 0x006699); if(point){ toastLog("x = " + point.x + ", y = " + point.y); }else{ toastLog("没有找到"); } ``` ## 模糊找色 ```js if(!requestScreenCapture()){ toast("请求截图失败"); exit(); } var img = captureScreen(); //0x9966ff为编辑器紫色字体的颜色 toastLog("开始找色"); var point = findColor(img, 0x9966ff); if(point){ toastLog("x = " + point.x + ", y = " + point.y); }else{ toastLog("没有找到"); } ``` ## 区域找色1 ```js if(!requestScreenCapture()){ toast("请求截图失败"); exit(); } var img = captureScreen(); toastLog("开始找色"); //指定在位置(100, 220)宽高为400*400的区域找色。 //#75438a是编辑器默认主题的棕红色字体(数字)颜色,位置大约在第5行的"2000",坐标大约为(283, 465) var point = findColorInRegion(img, "#75438a", 90, 220, 900, 1000); if(point){ toastLog("x = " + point.x + ", y = " + point.y); }else{ toastLog("没有找到"); } ``` ## 区域找色2 ```js if(!requestScreenCapture()){ toast("请求截图失败"); exit(); } var img = captureScreen(); //0xffffff为白色 toastLog("开始找色"); //指定在位置(90, 220)宽高为900*1000的区域找色。 //0xff00cc是编辑器的深粉红色字体(字符串)颜色 var point = findColor(img, "#ff00cc", { region: [90, 220, 900, 1000], threads: 8 }); if(point){ toastLog("x = " + point.x + ", y = " + point.y); }else{ toastLog("没有找到"); } ``` ## 实时显示触摸点颜色 ```js requestScreenCapture(); console.show(); events.observeTouch(); events.setTouchEventTimeout(30); events.on("touch", function(point){ var c = colors.toString(images.pixel(captureScreen(), point.x, point.y)); log("(" + point.x + ", " + point.y + "): " + c); }); ``` ## 图片处理 ```js "ui"; var url = "https://www.autojs.org/assets/uploads/files/1540386817060-918021-20160416200702191-185324559.jpg"; var logo = null; var currentImg = null; events.on("exit", function(){ if(logo != null){ logo.recycle(); } if(currentImg != null){ currentImg.recycle(); } }); ui.layout( <vertical> <img id="img" w="250" h="250" url="{{url}}" /> <scroll> <vertical> <button id="rotate" text="旋转" /> <button id="concat" text="拼接" /> <button id="grayscale" text="灰度化" /> <button id="binary" text="二值化" /> <button id="adaptiveBinary" text="自适应二值化" /> <button id="hsv" text="RGB转HSV" /> <button id="blur" text="模糊" /> <button id="medianBlur" text="中值滤波" /> <button id="gaussianBlur" text="高斯模糊" /> </vertical> </scroll> </vertical> ); //把一张图片设置到图片控件中 function setImage(img) { ui.run(() => { ui.img.setImageBitmap(img.bitmap); var oldImg = currentImg; //不能立即回收currentImg,因为此时img控件还在使用它,应该在下次消息循环再回收它 ui.post(()=>{ if(oldImg != null){ oldImg.recycle(); } }); currentImg = img; }); } //启动一个处理图片的线程 var imgProcess = threads.start(function () { setInterval(() => { }, 1000); }); //处理图片的函数,把任务交给图片处理线程处理 function processImg(process) { imgProcess.setTimeout(() => { if (logo == null) { logo = images.load(url); } //处理图片 var result = process(logo); //把处理后的图片设置到图片控件中 setImage(result); }, 0); } var degress = 0; ui.rotate.on("click", () => { processImg(img => { degress += 90; //旋转degress角度 return images.rotate(img, degress); }); }); ui.concat.on("click", () => { processImg(img => { if(currentImg == null){ toast("请先点击其他按钮,再点击本按钮"); return img.clone(); } //把currentImg拼接在img右边 return images.concat(img, currentImg, "right"); }); }); ui.grayscale.on("click", () => { processImg(img => { //灰度化 return images.grayscale(img); }); }); ui.binary.on("click", () => { processImg(img => { var g = images.grayscale(img); //二值化,取灰度为30到200之间的图片 var result = images.threshold(g, 100, 200); g.recycle(); return result; }); }); ui.adaptiveBinary.on("click", () => { processImg(img => { var g = images.grayscale(img); //自适应二值化,最大值为200,块大小为25 var result = images.adaptiveThreshold(g, 200, "MEAN_C", "BINARY", 25, 10); g.recycle(); return result; }); }); ui.hsv.on("click", () => { processImg(img => { //RGB转HSV return images.cvtColor(img, "BGR2HSV"); }); }); ui.blur.on("click", () => { processImg(img => { //模糊 return images.blur(img, [10, 10]); }); }); ui.medianBlur.on("click", () => { processImg(img => { //中值滤波 return images.medianBlur(img, 5); }); }); ui.gaussianBlur.on("click", () => { processImg(img => { //高斯模糊 return images.gaussianBlur(img, [5, 5]); }); }); ``` ## 颜色获取和检测 ```js if(!requestScreenCapture()){ toast("请求截图失败"); exit } sleep(2000); var x = 760; var y = 180; //获取在点(x, y)处的颜色 var c = images.pixel(captureScreen(), x, y); //显示该颜色 var msg = ""; msg += "在位置(" + x + ", " + y + ")处的颜色为" + colors.toString(c); msg += "\nR = " + colors.red(c) + ", G = " + colors.green(c) + ", B = " + colors.blue(c); //检测在点(x, y)处是否有颜色#73bdb6 (模糊比较) var isDetected = images.detectsColor(captureScreen(), "#73bdb6", x, y); msg += "\n该位置是否匹配到颜色#73bdb6: " + isDetected; alert(msg); ``` ## 找到QQ红点位置 ```js if(!requestScreenCapture()){ toast("请求截图失败"); exit(); } launchApp("QQ"); sleep(2000); var img = captureScreen(); toastLog("开始找色"); var point = findColor(img, "#f64d30"); if(point){ toastLog("x = " + point.x + ", y = " + point.y); }else{ toastLog("没有找到"); } ```