From 6e08c5cb5bebc667778a5786da064575b10bff4b Mon Sep 17 00:00:00 2001 From: e1399579 Date: Wed, 21 Aug 2024 08:52:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/OcrTool.js | 45 +++++++++++++------ ...54\344\270\234\347\255\276\345\210\260.js" | 6 +-- ...30\345\256\235\347\255\276\345\210\260.js" | 25 +---------- 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/lib/OcrTool.js b/lib/OcrTool.js index 3434597..f438d03 100644 --- a/lib/OcrTool.js +++ b/lib/OcrTool.js @@ -1,30 +1,49 @@ function OcrTool() { this.img = null; + this.offset = [0, 0]; - this.prepare = function() { + this.prepare = function(landscape) { // 请求截图并点击开始 threads.start(function () { let sel = packageName('com.android.systemui').text('立即开始'); let btn = sel.findOne(3000); if (btn) btn.click(); }); - if (!requestScreenCapture(false)) { + if (!requestScreenCapture(landscape)) { throw new Error("请求截图失败"); } sleep(500); } - this.captureOrClip = function(rect) { - toastLog("截图"); - let img = images.captureScreen(); - this.img = rect ? images.clip(img, rect[0], rect[1], rect[2], rect[3]) : img; - } + this.capture = function() { + log("截图"); + this.offset = [0, 0]; + this.img = images.captureScreen(); + return this.img; + }; + + this.clip = function(rect) { + log("裁剪"); + this.offset = [rect[0], rect[1]]; + return images.clip(this.img, rect[0], rect[1], rect[2], rect[3]); + }; + + this.captureAndClip = function(rect) { + this.capture(); + return this.clip(rect); + }; + + this.setOffset = function(x, y) { + this.offset = [x, y]; + }; + + this.getOffset = function() { + return this.offset; + }; - this.findText = function(keywords) { - toastLog("OCR开始"); - let list = ocr.detect(this.img); - toastLog("OCR结束"); - this.img.recycle(); + this.findText = function(img, keywords, recycle) { + let list = ocr.detect(img); + if (recycle) img.recycle(); let n = keywords.length; let result = []; let found = 0; @@ -34,7 +53,7 @@ function OcrTool() { if (obj.label.indexOf(keywords[i]) === -1) { result[i] = []; } else { - result[i] = [obj.bounds.left, obj.bounds.top]; + result[i] = [obj.bounds.left + this.offset[0], obj.bounds.top + this.offset[1]]; ++found; } } diff --git "a/\344\272\254\344\270\234\347\255\276\345\210\260.js" "b/\344\272\254\344\270\234\347\255\276\345\210\260.js" index 4b4fcee..2490e4b 100644 --- "a/\344\272\254\344\270\234\347\255\276\345\210\260.js" +++ "b/\344\272\254\344\270\234\347\255\276\345\210\260.js" @@ -25,12 +25,12 @@ sleep(10000); // 点击签到 let flag = false; -ocrTool.prepare(); +ocrTool.prepare(false); for (let i = 0;i < 3;i++) { flag = false; - ocrTool.captureOrClip([0, 0, 1080, 520]); + let tpl = ocrTool.captureAndClip([0, 0, 1080, 900]); let keywords = ["去登录", "明天", "赚更多京豆", "签到领豆"]; - let result = ocrTool.findText(keywords); + let result = ocrTool.findText(tpl, keywords, true); if (result[0].length > 0) { robot.click(result[0][0], result[0][1]); toastLog("点击登录"); diff --git "a/\346\224\257\344\273\230\345\256\235\347\255\276\345\210\260.js" "b/\346\224\257\344\273\230\345\256\235\347\255\276\345\210\260.js" index 15921cb..157f192 100644 --- "a/\346\224\257\344\273\230\345\256\235\347\255\276\345\210\260.js" +++ "b/\346\224\257\344\273\230\345\256\235\347\255\276\345\210\260.js" @@ -1,18 +1,16 @@ require("./解锁.js"); let Robot = require("./lib/Robot.js"); let WidgetAutomator = require("./lib/WidgetAutomator.js"); -//let OcrTool = require("./lib/OcrTool.js"); let robot = new Robot(); let widget = new WidgetAutomator(robot); -//let ocrTool = new OcrTool(); toastLog("支付宝签到"); // 启动APP widget.launchLikeName("支付宝", 5000); // 关闭更新弹窗 -if (id("update_cancel_tv").exists()) { - widget.clickCenterId("update_cancel_tv"); +if (id("btn_close").exists()) { + widget.clickCenterId("btn_close"); sleep(1000); } @@ -25,25 +23,6 @@ widget.clickCenterClass("android.view.ViewGroup"); sleep(8000); let keywords = ["每日签到", "全部领取"]; -// 使用OCR识别文字 -/*ocrTool.prepare(); -ocrTool.captureOrClip([0, 0, 1080, 1200]); -let result = ocrTool.findText(keywords); -if (result[1].length > 0) { - robot.click(result[1][0], result[1][1]); - toastLog("全部领取"); - sleep(1000); -} -if (result[0].length > 0) { - robot.click(result[0][0], result[0][1]); - toastLog("签到成功"); - sleep(3000); -} else { - toastLog("签到失败"); - log(selector().visibleToUser(true).find().toList()); - -} -*/ // 使用控件点击 if (text(keywords[1]).exists()) { widget.clickCenterText(keywords[1]);