YYPOST群发软件 发表于 2026-2-6 09:14:58

AIWROK软件找字与OCR方法汇总示例




AIWROK软件找字与OCR方法汇总示例





// 找字与OCR方法汇总示例
// 兼容ES5和Rhino引擎
// 适用于安卓AIWROK环境
// 技术交流QQ群711841924群一,苹果内测群,528816639

/**
* 模块:基础找字方法
* 功能:提供多种文字查找方法
*/
var FindTextMethods = {
    /**
   * 方法1:基础找字方法
   * @param {string} text - 要查找的文字
   * @param {number} timeout - 超时时间(毫秒)
   * @returns {Object} 找到的文字位置信息
   */
    findTextBasic: function(text, timeout) {
      timeout = timeout || 5000;
      var start = new Date().getTime();
      var result;
      
      while (new Date().getTime() - start < timeout) {
            try {
                if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
                  result = auto.findText(text, 0.8);
                  if (result) {
                        return result;
                  }
                }
            } catch (e) {
                // 忽略错误,继续尝试
            }
            sleep.millisecond(100);
      }
      
      return null;
    },
   
    /**
   * 方法2:区域找字方法
   * @param {string} text - 要查找的文字
   * @param {number} x1 - 区域左上角X坐标
   * @param {number} y1 - 区域左上角Y坐标
   * @param {number} x2 - 区域右下角X坐标
   * @param {number} y2 - 区域右下角Y坐标
   * @returns {Object} 找到的文字位置信息
   */
    findTextInRegion: function(text, x1, y1, x2, y2) {
      try {
            if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
                var region = ;
                var result = auto.findText(text, 0.8, region);
                return result;
            }
      } catch (e) {
            // 忽略错误
      }
      return null;
    },
   
    /**
   * 方法3:模糊找字方法
   * @param {string} text - 要查找的文字
   * @param {number} similarity - 相似度(0-1)
   * @returns {Object} 找到的文字位置信息
   */
    findTextFuzzy: function(text, similarity) {
      similarity = similarity || 0.8;
      try {
            if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
                var result = auto.findText(text, similarity);
                return result;
            }
      } catch (e) {
            // 忽略错误
      }
      return null;
    },
   
    /**
   * 方法4:多次尝试找字方法
   * @param {string} text - 要查找的文字
   * @param {number} maxAttempts - 最大尝试次数
   * @param {number} interval - 尝试间隔(毫秒)
   * @returns {Object} 找到的文字位置信息
   */
    findTextWithRetry: function(text, maxAttempts, interval) {
      maxAttempts = maxAttempts || 5;
      interval = interval || 1000;
      
      for (var i = 0; i < maxAttempts; i++) {
            try {
                if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
                  var result = auto.findText(text, 0.8);
                  if (result) {
                        return result;
                  }
                }
            } catch (e) {
                // 忽略错误,继续尝试
            }
            sleep.millisecond(interval);
      }
      
      return null;
    },
   
    /**
   * 方法5:批量找字方法
   * @param {Array} texts - 要查找的文字数组
   * @returns {Object} 找到的第一个文字位置信息
   */
    findTextBatch: function(texts) {
      for (var i = 0; i < texts.length; i++) {
            try {
                if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
                  var result = auto.findText(texts, 0.8);
                  if (result) {
                        return {
                            text: texts,
                            position: result
                        };
                  }
                }
            } catch (e) {
                // 忽略错误,继续尝试下一个文字
            }
      }
      
      return null;
    },
   
    /**
   * 方法6:找字并点击方法
   * @param {string} text - 要查找并点击的文字
   * @param {number} timeout - 超时时间(毫秒)
   * @returns {boolean} 是否成功点击
   */
    findTextAndClick: function(text, timeout) {
      var result = this.findTextBasic(text, timeout);
      if (result) {
            var x = result.x + result.width / 2;
            var y = result.y + result.height / 2;
            // 添加随机偏移模拟真实点击
            var randomX = x + (Math.random() - 0.5) * 10;
            var randomY = y + (Math.random() - 0.5) * 10;
            try {
                if (typeof touch !== 'undefined' && typeof touch.tap === 'function') {
                  touch.tap(randomX, randomY);
                  return true;
                }
            } catch (e) {
                // 忽略错误
            }
      }
      return false;
    },
   
    /**
   * 方法7:等待文字出现方法
   * @param {string} text - 要等待的文字
   * @param {number} timeout - 超时时间(秒)
   * @returns {boolean} 是否在超时前找到文字
   */
    waitForText: function(text, timeout) {
      timeout = timeout || 30;
      var startTime = Date.now();
      var timeoutMs = timeout * 1000;
      
      while (Date.now() - startTime < timeoutMs) {
            try {
                if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
                  var result = auto.findText(text, 0.8);
                  if (result) {
                        return true;
                  }
                }
            } catch (e) {
                // 忽略错误,继续尝试
            }
            sleep.millisecond(1000);
      }
      
      return false;
    }
};

/**
* 模块:OCR方法
* 功能:提供多种OCR文字识别方法
*/
var OCRMethods = {
    /**
   * 方法1:基础OCR识别
   * @param {number} x1 - 区域左上角X坐标
   * @param {number} y1 - 区域左上角Y坐标
   * @param {number} x2 - 区域右下角X坐标
   * @param {number} y2 - 区域右下角Y坐标
   * @returns {string} 识别到的文字
   */
    ocrBasic: function(x1, y1, x2, y2) {
      try {
            if (typeof ocr === 'function') {
                var result = ocr(x1, y1, x2, y2);
                return result;
            }
      } catch (e) {
            // 忽略错误,返回空字符串
      }
      return "";
    },
   
    /**
   * 方法2:带语言设置的OCR识别
   * @param {number} x1 - 区域左上角X坐标
   * @param {number} y1 - 区域左上角Y坐标
   * @param {number} x2 - 区域右下角X坐标
   * @param {number} y2 - 区域右下角Y坐标
   * @param {string} lang - 语言设置
   * @returns {string} 识别到的文字
   */
    ocrWithLanguage: function(x1, y1, x2, y2, lang) {
      try {
            if (typeof ocr === 'function') {
                lang = lang || "chi_sim";
                var result = ocr(x1, y1, x2, y2, lang);
                return result;
            }
      } catch (e) {
            // 忽略错误,返回空字符串
      }
      return "";
    },
   
    /**
   * 方法3:全屏OCR识别
   * @returns {string} 识别到的文字
   */
    ocrFullScreen: function() {
      try {
            if (typeof ocr === 'function') {
                var screenWidth = 0;
                var screenHeight = 0;
                try {
                  if (typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function') {
                        screenWidth = screen.getScreenWidth();
                        screenHeight = screen.getScreenHeight();
                  }
                } catch (e) {
                  // 忽略错误,使用默认值
                }
                var result = ocr(0, 0, screenWidth, screenHeight);
                return result;
            }
      } catch (e) {
            // 忽略错误,返回空字符串
      }
      return "";
    },
   
    /**
   * 方法4:OCR识别并查找特定文字
   * @param {number} x1 - 区域左上角X坐标
   * @param {number} y1 - 区域左上角Y坐标
   * @param {number} x2 - 区域右下角X坐标
   * @param {number} y2 - 区域右下角Y坐标
   * @param {string} keyword - 要查找的关键字
   * @returns {boolean} 是否找到关键字
   */
    ocrAndFindKeyword: function(x1, y1, x2, y2, keyword) {
      try {
            var text = this.ocrBasic(x1, y1, x2, y2);
            return text.indexOf(keyword) !== -1;
      } catch (e) {
            // 忽略错误,返回false
      }
      return false;
    },
   
    /**
   * 方法5:OCR识别并提取数字
   * @param {number} x1 - 区域左上角X坐标
   * @param {number} y1 - 区域左上角Y坐标
   * @param {number} x2 - 区域右下角X坐标
   * @param {number} y2 - 区域右下角Y坐标
   * @returns {Array} 提取到的数字数组
   */
    ocrExtractNumbers: function(x1, y1, x2, y2) {
      try {
            var text = this.ocrBasic(x1, y1, x2, y2);
            var numbers = text.match(/\d+/g) || [];
            return numbers;
      } catch (e) {
            // 忽略错误,返回空数组
      }
      return [];
    },
   
    /**
   * 方法6:OCR识别并解析坐标
   * @param {number} x1 - 区域左上角X坐标
   * @param {number} y1 - 区域左上角Y坐标
   * @param {number} x2 - 区域右下角X坐标
   * @param {number} y2 - 区域右下角Y坐标
   * @returns {Array} 解析到的坐标数组
   */
    ocrExtractCoordinates: function(x1, y1, x2, y2) {
      try {
            var text = this.ocrBasic(x1, y1, x2, y2);
            var coords = [];
            var regex = /(\d+),(\d+)/g;
            var match;
            
            while ((match = regex.exec(text)) !== null) {
                coords.push({
                  x: parseInt(match, 10),
                  y: parseInt(match, 10)
                });
            }
            
            return coords;
      } catch (e) {
            // 忽略错误,返回空数组
      }
      return [];
    },
   
    /**
   * 方法7:OCR识别并提取邮箱
   * @param {number} x1 - 区域左上角X坐标
   * @param {number} y1 - 区域左上角Y坐标
   * @param {number} x2 - 区域右下角X坐标
   * @param {number} y2 - 区域右下角Y坐标
   * @returns {Array} 提取到的邮箱数组
   */
    ocrExtractEmails: function(x1, y1, x2, y2) {
      try {
            var text = this.ocrBasic(x1, y1, x2, y2);
            var emails = text.match(/+@+\.{2,}/g) || [];
            return emails;
      } catch (e) {
            // 忽略错误,返回空数组
      }
      return [];
    },
   
    /**
   * 方法8:OCR识别并提取URL
   * @param {number} x1 - 区域左上角X坐标
   * @param {number} y1 - 区域左上角Y坐标
   * @param {number} x2 - 区域右下角X坐标
   * @param {number} y2 - 区域右下角Y坐标
   * @returns {Array} 提取到的URL数组
   */
    ocrExtractURLs: function(x1, y1, x2, y2) {
      try {
            var text = this.ocrBasic(x1, y1, x2, y2);
            var urls = text.match(/https?:\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/g) || [];
            return urls;
      } catch (e) {
            // 忽略错误,返回空数组
      }
      return [];
    },
   
    /**
   * 方法9:基础文字查询方法 - 查询包含文字
   * @param {string} text - 要查询的文字
   * @returns {object|null} 识别到的目标对象或null
   */
    findIncludeText: function(text) {
      try {
            var result = new ocrResult().findIncludeText(text);
            return result;
      } catch (e) {
            // 忽略错误,返回null
      }
      return null;
    },
   
    /**
   * 方法10:基础文字查询方法 - 查询包含文字(多目标)
   * @param {string} text - 要查询的文字
   * @returns {Array} 识别到的目标对象数组
   */
    findIncludeTexts: function(text) {
      try {
            var result = new ocrResult().findIncludeTexts(text);
            return result;
      } catch (e) {
            // 忽略错误,返回空数组
      }
      return [];
    },
   
    /**
   * 方法11:基础文字查询方法 - 模糊查询(近似匹配)
   * @param {string} text - 要查询的文字
   * @returns {object|null} 识别到的模糊匹配目标或null
   */
    findSimText: function(text) {
      try {
            var img = screen.screenShotFull();
            var ocrres = img.MLKitOcr('zh');
            var result = ocrres.findSimText(text);
            img.recycle();
            return result;
      } catch (e) {
            // 忽略错误,返回null
      }
      return null;
    },
   
    /**
   * 方法12:基础文字查询方法 - 精确查询文字
   * @param {string} text - 要查询的文字
   * @returns {object|null} 识别到的精确目标或null
   */
    findText: function(text) {
      try {
            var img = screen.screenShotFull();
            var ocrres = img.MLKitOcr('zh');
            var result = ocrres.findText(text);
            img.recycle();
            return result;
      } catch (e) {
            // 忽略错误,返回null
      }
      return null;
    },
   
    /**
   * 方法13:基础文字查询方法 - 获取所有识别目标
   * @returns {Array} 所有识别目标的数组
   */
    getAllDetect: function() {
      try {
            var result = new ocrResult().getAllDetect();
            return result;
      } catch (e) {
            // 忽略错误,返回空数组
      }
      return [];
    },
   
    /**
   * 方法14:基础文字查询方法 - 获取所有识别文字
   * @returns {string} 所有识别文字的拼接结果
   */
    getAllString: function() {
      try {
            var result = new ocrResult().getAllString();
            return result;
      } catch (e) {
            // 忽略错误,返回空字符串
      }
      return "";
    },
   
    /**
   * 方法15:基础文字查询方法 - 获取JSON格式结果
   * @returns {string} OCR识别结果的JSON字符串
   */
    getJson: function() {
      try {
            var result = new ocrResult().getJson();
            return result;
      } catch (e) {
            // 忽略错误,返回空字符串
      }
      return "";
    },
   
    /**
   * 方法16:基础文字查询方法 - 获取JSON字符串
   * @returns {string} OCR识别结果的JSON字符串
   */
    getJsonString: function() {
      try {
            var result = new ocrResult().getJsonString();
            return result;
      } catch (e) {
            // 忽略错误,返回空字符串
      }
      return "";
    },
   
    /**
   * 方法17:核心识别方法 - MLKitOcr文字识别(Google MLKit引擎)
   * @param {string} lang - 识别语言,默认"zh"
   * @returns {object|null} OCR识别结果对象或null
   */
    MLKitOcr: function(lang) {
      try {
            lang = lang || "zh";
            var img = screen.screenShotFull();
            var result = img.MLKitOcr(lang);
            img.recycle();
            return result;
      } catch (e) {
            console.log("MLKitOcr error:", e.message);
            return null;
      }
    },
   
    /**
   * 方法18:核心识别方法 - MLKitOcrRect文字区域识别
   * @param {string} lang - 识别语言,默认"zh"
   * @param {Array} region - 识别区域的百分比坐标
   * @returns {object|null} OCR识别结果对象或null
   */
    MLKitOcrRect: function(lang, region) {
      try {
            lang = lang || "zh";
            region = region || ;
            var img = screen.screenShotFull();
            var result = img.MLKitOcrRect(lang, region);
            img.recycle();
            return result;
      } catch (e) {
            console.log("MLKitOcrRect error:", e.message);
            return null;
      }
    },
   
    /**
   * 方法19:核心识别方法 - aiworkOCR文字识别
   * @param {number} size - 识别尺寸,推荐值:640、320
   * @param {Array} region - 识别区域的百分比坐标
   * @returns {object|null} OCR识别结果对象或null
   */
    aiworkOCR: function(size, region) {
      try {
            size = size || 640;
            region = region || ;
            var img = screen.screenShotFull();
            var result = img.aiworkOCR(size, region);
            img.recycle();
            return result;
      } catch (e) {
            console.log("aiworkOCR error:", e.message);
            return null;
      }
    },
   
    /**
   * 方法20:核心识别方法 - paddleOCR文字识别
   * @param {number} size - 识别尺寸,推荐值:640、320
   * @param {Array} region - 识别区域的百分比坐标
   * @returns {object|null} OCR识别结果对象或null
   */
    paddleOCR: function(size, region) {
      try {
            size = size || 640;
            region = region || ;
            var img = screen.screenShotFull();
            var result = img.paddleOCR(size, region);
            img.recycle();
            return result;
      } catch (e) {
            console.log("paddleOCR error:", e.message);
            return null;
      }
    },
   
    /**
   * 方法21:OpenCV相关方法 - 通过训练字库识别
   * @param {object} matImg - 输入的图片对象(Mat类型)
   * @param {string} dictPath - 字库文件路径/名称
   * @param {number} threshold - 相似度阈值(0~1)
   * @param {number} expectedCount - 预期识别的文字数量
   * @param {Array} region - 识别区域的百分比坐标
   * @returns {object|null} OCR识别结果对象或null
   */
    opencvOCR: function(matImg, dictPath, threshold, expectedCount, region) {
      try {
            if (typeof opencv !== 'undefined' && typeof opencv.OCR === 'function') {
                dictPath = dictPath || "";
                threshold = threshold || 0;
                expectedCount = expectedCount || 0;
                region = region || [];
                var result = opencv.OCR(matImg, dictPath, threshold, expectedCount, region);
                return result;
            }
      } catch (e) {
            // 忽略错误,返回null
      }
      return null;
    },
   
    /**
   * 方法22:OpenCV相关方法 - 基于CV文件的OCR识别
   * @param {string} cvFileName - CV文件名
   * @returns {object|null} OCR识别结果对象或null
   */
    opencvOCREx: function(cvFileName) {
      try {
            if (typeof opencv !== 'undefined' && typeof opencv.OCREx === 'function') {
                cvFileName = cvFileName || "";
                var result = opencv.OCREx(cvFileName);
                return result;
            }
      } catch (e) {
            console.log("opencvOCREx error:", e.message);
            return null;
      }
      return null;
    }
};



/**
* 模块:OCR引擎管理
* 功能:管理OCR引擎的加载和卸载
*/
var OCREngineManager = {
    /**
   * 加载OCR引擎
   * @param {string} lang - 语言模型
   * @returns {boolean} 是否加载成功
   */
    loadOCREngine: function(lang) {
      lang = lang || "chi_sim";
      try {
            if (typeof loadOCR === 'function') {
                var result = loadOCR(lang);
                return result;
            } else {
                console.log("loadOCR function not available");
                return false;
            }
      } catch (e) {
            console.log("Error loading OCR engine:", e.message);
            return false;
      }
    },
   
    /**
   * 卸载OCR引擎
   * @returns {boolean} 是否卸载成功
   */
    unloadOCREngine: function() {
      try {
            if (typeof unloadOCR === 'function') {
                var result = unloadOCR();
                return result;
            } else {
                console.log("unloadOCR function not available");
                return false;
            }
      } catch (e) {
            console.log("Error unloading OCR engine:", e.message);
            return false;
      }
    },
   
    /**
   * 检查OCR引擎状态
   * @returns {boolean} OCR引擎是否已加载
   */
    isOCREngineLoaded: function() {
      try {
            if (typeof isOCRLoaded === 'function') {
                var result = isOCRLoaded();
                return result;
            } else {
                console.log("isOCRLoaded function not available");
                return false;
            }
      } catch (e) {
            console.log("Error checking OCR engine status:", e.message);
            return false;
      }
    }
};

/**
* 模块:工具函数
* 功能:提供各种工具函数
*/
var Utils = {
    /**
   * 延迟执行
   * @param {number} ms - 延迟毫秒数
   */
    sleep: function(ms) {
      java.lang.Thread.sleep(ms);
    },
   
    /**
   * 获取设备信息
   * @returns {Object} 设备信息
   */
    getDeviceInfo: function() {
      var screenWidth = 0;
      var screenHeight = 0;
      
      try {
            if (typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function') {
                screenWidth = screen.getScreenWidth();
                screenHeight = screen.getScreenHeight();
            }
      } catch (e) {
            // 忽略错误,使用默认值
      }
      
      return {
            width: screenWidth,
            height: screenHeight,
            model: "未知",
            brand: "未知",
            sdk: "未知",
            androidVersion: "未知"
      };
    },
   
    /**
   * 创建图像对象
   * @param {string} path - 图像路径
   * @returns {Object} 图像对象
   */
    createImage: function(path) {
      try {
            var img = new image();
            img.read(path);
            return img;
      } catch (e) {
            console.log("Error creating image object:", e.message);
            return null;
      }
    },
   
    /**
   * 打印日志
   * @param {string} message - 日志消息
   */
    log: function(message) {
      console.log(message);
    },
   
    /**
   * 随机数生成
   * @param {number} min - 最小值
   * @param {number} max - 最大值
   * @returns {number} 随机数
   */
    random: function(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }
};

/**
* 综合示例:使用找字和OCR方法
*/
function runComprehensiveDemo() {
    console.log("=== AIWROK 找字与OCR方法终极汇总示例 ===");
   
    // 1. 打印设备信息
    console.log("\n1. 设备信息:");
    var deviceInfo = Utils.getDeviceInfo();
    console.log("设备型号: " + deviceInfo.model);
    console.log("屏幕尺寸: " + deviceInfo.width + "x" + deviceInfo.height);
    console.log("Android版本: " + deviceInfo.androidVersion);
   
    // 2. 尝试使用基础找字方法
    console.log("\n2. 基础找字示例:");
    var textResult = FindTextMethods.findTextBasic("确定", 3000);
    if (textResult) {
      console.log("找到文字'确定',位置: x=" + textResult.x + ", y=" + textResult.y);
    } else {
      console.log("未找到文字'确定'");
    }
   
    // 3. 尝试使用OCR识别并点击
    console.log("\n3. OCR识别与点击示例:");
    var targetText = "雷电"; // 要查找并点击的文字
    var clicked = false;
   
    // 尝试使用内置的MLKitOcr进行识别
    console.log("\n1. 尝试使用MLKitOcr进行识别:");
    var mlkitResult = OCRMethods.MLKitOcr('zh');
    if (mlkitResult) {
      console.log("MLKitOcr识别成功");
      console.log("MLKitOcr识别结果:");
      console.log(mlkitResult);
      
      // 尝试查找并点击目标文字
      try {
            var target = mlkitResult.findIncludeText(targetText);
            if (target) {
                console.log("使用MLKitOcr找到文字'" + targetText + "',正在点击...");
                target.hidClick();
                clicked = true;
                console.log("点击成功!");
            } else {
                console.log("MLKitOcr未找到文字'" + targetText + "'");
            }
      } catch (e) {
            console.log("MLKitOcr点击失败:", e.message);
      }
    } else {
      console.log("MLKitOcr识别失败");
    }
   
    // 尝试使用MLKitOcrRect进行识别
    console.log("\n2. 尝试使用MLKitOcrRect进行识别:");
    var mlkitRectResult = OCRMethods.MLKitOcrRect('zh', );
    if (mlkitRectResult) {
      console.log("MLKitOcrRect识别成功");
      console.log("MLKitOcrRect识别结果:");
      console.log(mlkitRectResult);
      
      // 尝试查找并点击目标文字
      try {
            var target = mlkitRectResult.findIncludeText(targetText);
            if (target) {
                console.log("使用MLKitOcrRect找到文字'" + targetText + "',正在点击...");
                target.hidClick();
                clicked = true;
                console.log("点击成功!");
            } else {
                console.log("MLKitOcrRect未找到文字'" + targetText + "'");
            }
      } catch (e) {
            console.log("MLKitOcrRect点击失败:", e.message);
      }
    } else {
      console.log("MLKitOcrRect识别失败");
    }
   
    // 尝试使用aiworkOCR进行识别
    console.log("\n3. 尝试使用aiworkOCR进行识别:");
    var aiworkResult = OCRMethods.aiworkOCR(640, );
    if (aiworkResult) {
      console.log("aiworkOCR识别成功");
      console.log("aiworkOCR识别结果:");
      console.log(aiworkResult);
      
      // 尝试查找并点击目标文字
      try {
            var target = aiworkResult.findIncludeText(targetText);
            if (target) {
                console.log("使用aiworkOCR找到文字'" + targetText + "',正在点击...");
                target.hidClick();
                clicked = true;
                console.log("点击成功!");
            } else {
                console.log("aiworkOCR未找到文字'" + targetText + "'");
            }
      } catch (e) {
            console.log("aiworkOCR点击失败:", e.message);
      }
    } else {
      console.log("aiworkOCR识别失败");
    }
   
    // 尝试使用paddleOCR进行识别
    console.log("\n4. 尝试使用paddleOCR进行识别:");
    var paddleResult = OCRMethods.paddleOCR(640, );
    if (paddleResult) {
      console.log("paddleOCR识别成功");
      console.log("paddleOCR识别结果:");
      console.log(paddleResult);
      
      // 尝试查找并点击目标文字
      try {
            var target = paddleResult.findIncludeText(targetText);
            if (target) {
                console.log("使用paddleOCR找到文字'" + targetText + "',正在点击...");
                target.hidClick();
                clicked = true;
                console.log("点击成功!");
            } else {
                console.log("paddleOCR未找到文字'" + targetText + "'");
            }
      } catch (e) {
            console.log("paddleOCR点击失败:", e.message);
      }
    } else {
      console.log("paddleOCR识别失败");
    }
   
    // 尝试使用opencv.OCREx进行识别
    console.log("\n5. 尝试使用opencv.OCREx进行识别:");
    var opencvResult = OCRMethods.opencvOCREx('图色473670.cv');
    if (opencvResult) {
      console.log("opencv.OCREx识别成功");
      try {
            console.log("opencv.OCREx识别结果:");
            console.log(opencvResult.getAllString());
            
            // 尝试查找并点击目标文字
            var target = opencvResult.findIncludeText(targetText);
            if (target) {
                console.log("使用opencv.OCREx找到文字'" + targetText + "',正在点击...");
                target.hidClick();
                clicked = true;
                console.log("点击成功!");
            } else {
                console.log("opencv.OCREx未找到文字'" + targetText + "'");
            }
      } catch (e) {
            console.log("opencv.OCREx操作失败:", e.message);
      }
    } else {
      console.log("opencv.OCREx识别失败");
    }
   
    // 所有OCR方法都尝试完毕
    if (!clicked) {
      console.log("\n所有OCR方法都未找到文字'" + targetText + "'");
    }
   
    // 7. 批量找字示例
    console.log("\n4. 批量找字示例:");
    var texts = ["确定", "取消", "设置", "开始"];
    var batchResult = FindTextMethods.findTextBatch(texts);
    if (batchResult) {
      console.log("找到文字: " + batchResult.text + ",位置: x=" + batchResult.position.x + ", y=" + batchResult.position.y);
    } else {
      console.log("未找到任何文字");
    }
   
    console.log("\n=== 示例执行完成 ===");
}

/**
* 高级示例:自动化任务流程
*/
function runAutomationTask() {
    console.log("=== AIWROK 自动化任务流程示例 ===");
   
    console.log("✅ 开始执行自动化任务");
   
    // 1. 等待应用启动
    console.log("\n1. 等待应用启动...");
    // 这里可以使用基础找字方法等待应用启动
    var appStarted = false;
    var startTime = Date.now();
    var timeoutMs = 30 * 1000;
   
    while (Date.now() - startTime < timeoutMs) {
      var result = FindTextMethods.findTextBasic("首页", 1000);
      if (result) {
            appStarted = true;
            break;
      }
      sleep.millisecond(1000);
    }
   
    if (!appStarted) {
      console.log("❌ 应用启动超时");
      return;
    }
    console.log("✅ 应用启动成功");
   
    // 2. 点击登录按钮
    console.log("\n2. 点击登录按钮...");
    var loginResult = FindTextMethods.findTextBasic("登录", 3000);
    if (loginResult) {
      auto.clickPoint(loginResult.x, loginResult.y);
      sleep.millisecond(1000);
      console.log("✅ 登录按钮点击成功");
    } else {
      console.log("❌ 未找到登录按钮");
      return;
    }
   
    // 3. 等待登录页面
    console.log("\n3. 等待登录页面...");
    var loginPageLoaded = false;
    startTime = Date.now();
    timeoutMs = 10 * 1000;
   
    while (Date.now() - startTime < timeoutMs) {
      var result = FindTextMethods.findTextBasic("用户名", 1000);
      if (result) {
            loginPageLoaded = true;
            break;
      }
      sleep.millisecond(1000);
    }
   
    if (!loginPageLoaded) {
      console.log("❌ 登录页面加载超时");
      return;
    }
    console.log("✅ 登录页面加载成功");
   
    // 4. 执行登录操作(这里只是示例,实际需要输入用户名密码)
    console.log("\n4. 执行登录操作...");
    // 模拟输入用户名密码
    // input.setText("username", "test");
    // input.setText("password", "123456");
   
    // 5. 点击登录按钮
    var loginSubmitResult = FindTextMethods.findTextBasic("登录", 3000);
    if (loginSubmitResult) {
      auto.clickPoint(loginSubmitResult.x, loginSubmitResult.y);
      sleep.millisecond(1000);
      console.log("✅ 登录操作提交成功");
    } else {
      console.log("❌ 未找到登录提交按钮");
      return;
    }
   
    // 6. 等待登录成功
    console.log("\n5. 等待登录成功...");
    var loginSuccess = false;
    startTime = Date.now();
    timeoutMs = 15 * 1000;
   
    while (Date.now() - startTime < timeoutMs) {
      var result = FindTextMethods.findTextBasic("欢迎", 1000);
      if (result) {
            loginSuccess = true;
            break;
      }
      sleep.millisecond(1000);
    }
   
    if (!loginSuccess) {
      console.log("❌ 登录失败或超时");
      return;
    }
    console.log("✅ 登录成功");
   
    console.log("\n=== 自动化任务执行完成 ===");
}



// 运行示例(如果直接执行此文件)
if (typeof console !== 'undefined' && console.log) {
    // 延迟1秒后运行示例,以便有时间准备测试环境
    setTimeout(function() {
      runComprehensiveDemo();
      // 可以选择运行自动化任务示例
      // runAutomationTask();
    }, 1000);
}





页: [1]
查看完整版本: AIWROK软件找字与OCR方法汇总示例