rename JS
This commit is contained in:
BIN
repo/js/OCR读取当前摩拉记录并发送通知/assets/CharacterMenu.png
Normal file
BIN
repo/js/OCR读取当前摩拉记录并发送通知/assets/CharacterMenu.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 847 B |
164
repo/js/OCR读取当前摩拉记录并发送通知/main.js
Normal file
164
repo/js/OCR读取当前摩拉记录并发送通知/main.js
Normal file
@@ -0,0 +1,164 @@
|
||||
// 定义替换映射表
|
||||
const replacementMap = {
|
||||
"监": "盐",
|
||||
"卵": "卯"
|
||||
};
|
||||
|
||||
// 定义所有图标的图像识别对象,每个图片都有自己的识别区域
|
||||
let CharacterMenuRo = RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/CharacterMenu.png"), 60, 991, 38, 38);
|
||||
|
||||
// 定义一个函数用于识别图像
|
||||
async function recognizeImage(recognitionObject, timeout = 5000) {
|
||||
let startTime = Date.now();
|
||||
while (Date.now() - startTime < timeout) {
|
||||
try {
|
||||
// 尝试识别图像
|
||||
let imageResult = captureGameRegion().find(recognitionObject);
|
||||
if (imageResult) {
|
||||
// log.info(`成功识别图像,坐标: x=${imageResult.x}, y=${imageResult.y}`);
|
||||
// log.info(`图像尺寸: width=${imageResult.width}, height=${imageResult.height}`);
|
||||
return { success: true, x: imageResult.x, y: imageResult.y };
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(`识别图像时发生异常: ${error.message}`);
|
||||
}
|
||||
await sleep(500); // 短暂延迟,避免过快循环
|
||||
}
|
||||
log.warn(`经过多次尝试,仍然无法识别图像`);
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
// 定义一个函数用于识别文字并点击
|
||||
async function recognizeTextAndClick(targetText, ocrRegion, timeout = 5000) {
|
||||
let startTime = Date.now();
|
||||
let retryCount = 0; // 重试计数
|
||||
while (Date.now() - startTime < timeout) {
|
||||
try {
|
||||
// 尝试 OCR 识别
|
||||
let resList = captureGameRegion().findMulti(RecognitionObject.ocr(ocrRegion.x, ocrRegion.y, ocrRegion.width, ocrRegion.height)); // 指定识别区域
|
||||
// 遍历识别结果,检查是否找到目标文本
|
||||
for (let res of resList) {
|
||||
// 后处理:根据替换映射表检查和替换错误识别的字符
|
||||
let correctedText = res.text;
|
||||
for (let [wrongChar, correctChar] of Object.entries(replacementMap)) {
|
||||
correctedText = correctedText.replace(new RegExp(wrongChar, 'g'), correctChar);
|
||||
}
|
||||
|
||||
if (correctedText.includes(targetText)) {
|
||||
// 如果找到目标文本,计算并点击文字的中心坐标
|
||||
let centerX = res.x + res.width / 2;
|
||||
let centerY = res.y + res.height / 2;
|
||||
await click(centerX, centerY);
|
||||
await sleep(500); // 确保点击后有足够的时间等待
|
||||
return { success: true, x: centerX, y: centerY };
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
retryCount++; // 增加重试计数
|
||||
log.warn(`页面标志识别失败,正在进行第 ${retryCount} 次重试...`);
|
||||
}
|
||||
await sleep(1000); // 短暂延迟,避免过快循环
|
||||
}
|
||||
log.warn(`经过多次尝试,仍然无法识别文字: ${targetText}`);
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
// 定义一个独立的函数用于在指定区域进行 OCR 识别并输出识别内容
|
||||
async function recognizeTextInRegion(ocrRegion, timeout = 5000) {
|
||||
let startTime = Date.now();
|
||||
let retryCount = 0; // 重试计数
|
||||
while (Date.now() - startTime < timeout) {
|
||||
try {
|
||||
// 在指定区域进行 OCR 识别
|
||||
let ocrResult = captureGameRegion().find(RecognitionObject.ocr(ocrRegion.x, ocrRegion.y, ocrRegion.width, ocrRegion.height));
|
||||
if (ocrResult) {
|
||||
// 后处理:根据替换映射表检查和替换错误识别的字符
|
||||
let correctedText = ocrResult.text;
|
||||
for (let [wrongChar, correctChar] of Object.entries(replacementMap)) {
|
||||
correctedText = correctedText.replace(new RegExp(wrongChar, 'g'), correctChar);
|
||||
}
|
||||
return correctedText; // 返回识别到的内容
|
||||
} else {
|
||||
log.warn(`OCR 识别区域未找到内容`);
|
||||
return null; // 如果 OCR 未识别到内容,返回 null
|
||||
}
|
||||
} catch (error) {
|
||||
retryCount++; // 增加重试计数
|
||||
log.warn(`OCR 摩拉数识别失败,正在进行第 ${retryCount} 次重试...`);
|
||||
}
|
||||
await sleep(500); // 短暂延迟,避免过快循环
|
||||
}
|
||||
log.warn(`经过多次尝试,仍然无法在指定区域识别到文字`);
|
||||
return null; // 如果未识别到文字,返回 null
|
||||
}
|
||||
|
||||
(async function () {
|
||||
setGameMetrics(1920, 1080, 1);
|
||||
await genshin.returnMainUi();
|
||||
const notify = settings.notify || false;
|
||||
|
||||
// 按下 C 键
|
||||
keyPress("C");
|
||||
await sleep(1500);
|
||||
|
||||
let recognized = false;
|
||||
|
||||
// 识别“角色菜单”图标或“天赋”文字
|
||||
let startTime = Date.now();
|
||||
while (Date.now() - startTime < 5000) {
|
||||
// 尝试识别“角色菜单”图标
|
||||
let characterMenuResult = await recognizeImage(CharacterMenuRo, 5000);
|
||||
if (characterMenuResult.success) {
|
||||
await click(177, 433);
|
||||
await sleep(500);
|
||||
recognized = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// 尝试识别“天赋”文字
|
||||
let targetText = "天赋";
|
||||
let ocrRegion = { x: 133, y: 395, width: 115, height: 70 }; // 设置对应的识别区域
|
||||
let talentResult = await recognizeTextAndClick(targetText, ocrRegion);
|
||||
if (talentResult.success) {
|
||||
log.info(`点击天赋文字,坐标: x=${talentResult.x}, y=${talentResult.y}`);
|
||||
recognized = true;
|
||||
break;
|
||||
}
|
||||
|
||||
await sleep(1000); // 短暂延迟,避免过快循环
|
||||
}
|
||||
|
||||
// 如果识别到了“角色菜单”或“天赋”,则识别“摩拉数值”
|
||||
if (recognized) {
|
||||
let ocrRegionMora = { x: 1620, y: 25, width: 152, height: 46 }; // 设置对应的识别区域
|
||||
let recognizedText = await recognizeTextInRegion(ocrRegionMora);
|
||||
if (recognizedText) {
|
||||
log.info(`成功识别到摩拉数值: ${recognizedText}`);
|
||||
if (notify) {
|
||||
notification.Send(`摩拉数值: ${recognizedText}`);
|
||||
}
|
||||
|
||||
// 获取当前时间
|
||||
const now = new Date();
|
||||
const formattedTime = now.toLocaleString(); // 使用本地时间格式化
|
||||
|
||||
|
||||
// 写入本地文件
|
||||
const filePath = "mora_log.txt";
|
||||
const logContent = `${formattedTime} - 摩拉数值: ${recognizedText}\n`;
|
||||
const result = file.WriteTextSync(filePath, logContent, true); // 追加模式
|
||||
if (result) {
|
||||
log.info("成功将摩拉数值写入日志文件");
|
||||
} else {
|
||||
log.error("写入日志文件失败");
|
||||
}
|
||||
} else {
|
||||
log.warn("未能识别到摩拉数值。");
|
||||
}
|
||||
} else {
|
||||
log.warn("未能识别到角色菜单或天赋,跳过摩拉数值识别。");
|
||||
}
|
||||
await sleep(500);
|
||||
await genshin.returnMainUi();
|
||||
})();
|
||||
|
||||
13
repo/js/OCR读取当前摩拉记录并发送通知/manifest.json
Normal file
13
repo/js/OCR读取当前摩拉记录并发送通知/manifest.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"manifest_version": 1,
|
||||
"name": "读取当前摩拉记录并发送通知",
|
||||
"version": "1.1",
|
||||
"description": "至少0.44.3版本。OCR识别摩拉数值输出到本地",
|
||||
"authors": [
|
||||
{
|
||||
"name": "吉吉喵"
|
||||
}
|
||||
],
|
||||
"settings_ui": "settings.json",
|
||||
"main": "main.js"
|
||||
}
|
||||
7
repo/js/OCR读取当前摩拉记录并发送通知/settings.json
Normal file
7
repo/js/OCR读取当前摩拉记录并发送通知/settings.json
Normal file
@@ -0,0 +1,7 @@
|
||||
[
|
||||
{
|
||||
"name": "notify",
|
||||
"type": "checkbox",
|
||||
"label": "通知"
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user