From f1a019ea9801050040c65c02eef24b13f8ed9180 Mon Sep 17 00:00:00 2001 From: yan Date: Sun, 8 Feb 2026 14:36:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(config):=20=E6=B7=BB=E5=8A=A0OCR=E8=AF=86?= =?UTF-8?q?=E5=88=ABUID=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在配置文件中导入ocrUid工具函数 - 修复checkKey函数中参数赋值的格式问题 - 在initConfig函数中集成OCR识别获取用户UID - 将识别到的UID存储到配置对象中 - 在主程序中使用配置中的UID替代未定义变量 - 新增tool.js工具模块实现区域OCR识别功能 - 新增uid.js模块专门处理UID识别逻辑 - 实现了屏幕指定区域的文字识别功能 --- repo/js/AutoPlanDomain/config/config.js | 6 ++++- repo/js/AutoPlanDomain/main.js | 2 +- repo/js/AutoPlanDomain/utils/tool.js | 33 +++++++++++++++++++++++++ repo/js/AutoPlanDomain/utils/uid.js | 31 +++++++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 repo/js/AutoPlanDomain/utils/tool.js create mode 100644 repo/js/AutoPlanDomain/utils/uid.js diff --git a/repo/js/AutoPlanDomain/config/config.js b/repo/js/AutoPlanDomain/config/config.js index 417982391..a468d5f53 100644 --- a/repo/js/AutoPlanDomain/config/config.js +++ b/repo/js/AutoPlanDomain/config/config.js @@ -1,3 +1,5 @@ +import {ocrUid} from "../utils/uid"; + const config = { //setting设置放在这个json domain: { @@ -116,10 +118,11 @@ async function getValueByMultiCheckboxName(name) { log.debug("values={key}", JSON.stringify(values)) return values } + /** * 检查密钥是否正确 */ -async function checkKey(key="") { +async function checkKey(key = "") { if (config.info.key !== key.trim()) { throw new Error("密钥错误"); } @@ -410,6 +413,7 @@ async function initConfig() { if (config.domainList.length <= 0) { throw new Error("配置文件缺失或读取异常!") } + config.user.uid = await ocrUid() } export { diff --git a/repo/js/AutoPlanDomain/main.js b/repo/js/AutoPlanDomain/main.js index 19e035e9b..e3acb516e 100644 --- a/repo/js/AutoPlanDomain/main.js +++ b/repo/js/AutoPlanDomain/main.js @@ -46,7 +46,7 @@ function initDomainOrderList(domainConfig) { DomainRoundNum: undefined,//副本轮数 } // 秘境信息对象 }*/ - const uid = undefined + const uid = config.user.uid if (uid) { const configAutoFightOrderMap = JSON.parse(file.readTextSync(config.path.domainConfig)) || new Map() const uidConfigList = configAutoFightOrderMap.get(uid) || []; diff --git a/repo/js/AutoPlanDomain/utils/tool.js b/repo/js/AutoPlanDomain/utils/tool.js new file mode 100644 index 000000000..bfb09fc11 --- /dev/null +++ b/repo/js/AutoPlanDomain/utils/tool.js @@ -0,0 +1,33 @@ +/** + * 对指定区域进行OCR文字识别 + * @param {number} x - 区域左上角x坐标,默认为0 + * @param {number} y - 区域左上角y坐标,默认为0 + * @param {number} w - 区域宽度,默认为1920 + * @param {number} h - 区域高度,默认为1080 + * @returns {Promise} 返回识别到的文本内容,如果识别失败则返回null + */ +async function ocrRegion(x = 0, + y = 0, + w = 1920, + h = 1080) { + // 创建OCR识别对象,使用指定的坐标和尺寸 + let recognitionObjectOcr = RecognitionObject.Ocr(x, y, w,h); + // 捕获游戏区域图像 + let region3 = captureGameRegion() + try { + // 在捕获的区域中查找OCR识别对象 + let res = region3.find(recognitionObjectOcr); + // 返回识别到的文本内容,如果不存在则返回undefined + return res?.text + } catch (e) { + // 捕获并记录错误信息 + log.error("识别异常:{1}", e.message) + return null + } finally { + // 确保释放区域资源 + region3.Dispose() + } +} +export { + ocrRegion +} \ No newline at end of file diff --git a/repo/js/AutoPlanDomain/utils/uid.js b/repo/js/AutoPlanDomain/utils/uid.js new file mode 100644 index 000000000..53efdac2f --- /dev/null +++ b/repo/js/AutoPlanDomain/utils/uid.js @@ -0,0 +1,31 @@ +import {ocrRegion} from './tool.js' + +async function saveOnlyNumber(str) { + str = str ? str : ''; + // 使用正则表达式匹配字符串中的所有数字 + // \d+ 匹配一个或多个数字 + // .join('') 将匹配到的数字数组连接成一个字符串 + // parseInt 将连接后的字符串转换为整数 + return parseInt(str.match(/\d+/g).join('')); +} + +/** + * OCR识别UID的异步函数 + * 该函数用于通过OCR技术识别屏幕上特定位置的UID文本 + * @returns {Promise} - 异步函数,没有明确的返回值 + */ +async function ocrUid() { + // 定义OCR识别的坐标和尺寸参数 + let uid_json = { + x: 1683, // OCR识别区域的左上角x坐标 + y: 1051, // OCR识别区域的左上角y坐标 + width: 234, // OCR识别区域的宽度 + height: 28, // OCR识别区域的高度 + } + let text = await ocrRegion(uid_json.x, uid_json.y, uid_json.width, uid_json.height); + return await saveOnlyNumber(text); +} + +export { + ocrUid, +} \ No newline at end of file