| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /**
- * Func 标签:image-region-location
- *
- * 约定:src/pages/processing/func/ 目录下每个文件名就是一个"可用标签/能力"。
- * 本文件用于声明该标签存在(供文档/提示词/后续动态加载使用)。
- *
- * 语义:通过图像匹配找到区域截图在完整截图中的位置,返回区域的四个顶点坐标。
- * 当前项目里对应能力主要由 electronAPI.matchImageRegionLocation + main-js/func/image-center-location.js 实现承载。
- */
- const electronAPI = require('../node-api.js')
- const tagName = 'image-region-location'
- const schema = {
- description: '在完整截图中查找区域截图的位置,返回区域的四个顶点坐标(左上、右上、左下、右下)。',
- inputs: {
- screenshot: '完整截图路径(相对于工作流目录)',
- region: '区域截图路径(相对于工作流目录)',
- variable: '输出变量名(保存四个顶点坐标)',
- },
- outputs: {
- variable: '四个顶点坐标对象 {topLeft: {x, y}, topRight: {x, y}, bottomLeft: {x, y}, bottomRight: {x, y}}',
- },
- };
- /**
- * 执行 image-region-location 功能
- * 这个函数会被 ActionParser 调用
- *
- * @param {Object} params - 参数对象
- * @param {string} params.device - 设备 ID/IP:Port(可选,用于获取分辨率)
- * @param {string} params.screenshot - 完整截图路径
- * @param {string} params.region - 区域截图路径
- * @param {string} params.folderPath - 工作流文件夹路径
- * @returns {Promise<{success: boolean, corners?: Object, error?: string}>}
- */
- async function executeImageRegionLocation({ device, screenshot, region, folderPath }) {
- try {
- if (!electronAPI.matchImageRegionLocation) {
- return {
- success: false,
- error: 'matchImageRegionLocation API 不可用'
- };
- }
- // 如果 screenshot 为 null,使用特殊标记让主进程自动获取截图
- let screenshotPath = screenshot;
- if (screenshot === null) {
- screenshotPath = '__AUTO_SCREENSHOT__';
- } else if (screenshot) {
- // 构建完整路径(如果路径不是绝对路径,则相对于工作流目录的 resources 文件夹)
- // resources 作为根目录(使用已有的截图文件进行匹配,不主动截图)
- screenshotPath = screenshot.startsWith('/') || screenshot.includes(':')
- ? screenshot
- : `${folderPath}/resources/${screenshot}`;
-
- }
-
- const regionPath = region.startsWith('/') || region.includes(':')
- ? region
- : `${folderPath}/resources/${region}`;
- // 调用主进程的图像区域定位函数
- // 如果 screenshotPath 是 '__AUTO_SCREENSHOT__',主进程会自动获取截图
- const result = await electronAPI.matchImageRegionLocation(
- screenshotPath,
- regionPath,
- device // 可选,用于获取设备分辨率进行缩放或自动获取截图
- );
- if (!result.success) {
- return { success: false, error: result.error };
- }
- // 计算四个顶点坐标
- const { x, y, width, height } = result;
- const corners = {
- topLeft: { x, y },
- topRight: { x: x + width, y },
- bottomLeft: { x, y: y + height },
- bottomRight: { x: x + width, y: y + height }
- };
- return {
- success: true,
- corners: corners,
- bounds: { x, y, width, height } // 同时返回边界框信息
- };
- } catch (error) {
- return {
- success: false,
- error: error.message || '图像区域定位失败'
- };
- }
- }
- module.exports = { tagName, schema, executeImageRegionLocation }
|