| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- /**
- * Func 标签:image-center-location
- *
- * 图像匹配功能:识别模板图片是否在截图中的位置,返回中心点坐标
- * 注意:实际的 OpenCV 处理需要在 Node.js 主进程中实现
- */
- export const tagName = 'image-center-location';
- export const schema = {
- description: '在屏幕截图中查找模板图片的位置并返回中心点坐标(可用于定位/点击)。',
- inputs: {
- template: '模板图片路径(相对于工作流目录)',
- variable: '输出变量名(保存中心点坐标)',
- },
- outputs: {
- variable: '中心点坐标(JSON 字符串格式,如:{"x":123,"y":456})',
- },
- };
- /**
- * 执行 image-center-location 功能
- * 这个函数会被 ActionParser 调用
- *
- * @param {Object} params - 参数对象
- * @param {string} params.device - 设备 ID/IP:Port(必需,用于获取截图)
- * @param {string} params.template - 模板图片路径
- * @param {string} params.folderPath - 工作流文件夹路径
- * @returns {Promise<{success: boolean, center?: Object, error?: string}>}
- */
- export async function executeImageCenterLocation({ device, template, folderPath }) {
- try {
- if (!window.electronAPI || !window.electronAPI.matchImageAndGetCoordinate) {
- return {
- success: false,
- error: 'matchImageAndGetCoordinate API 不可用'
- };
- }
- if (!device) {
- return {
- success: false,
- error: '缺少设备 ID,无法自动获取截图'
- };
- }
- // 构建模板图片完整路径(如果路径不是绝对路径,则相对于工作流目录的 resources 文件夹)
- // resources 作为根目录
- const templatePath = template.startsWith('/') || template.includes(':')
- ? template
- : `${folderPath}/resources/${template}`;
- // 调用主进程的图像匹配函数(会自动获取设备截图)
- const result = await window.electronAPI.matchImageAndGetCoordinate(
- device,
- templatePath
- );
- if (!result.success) {
- return { success: false, error: result.error };
- }
- // 返回中心点坐标
- const center = result.clickPosition || { x: result.coordinate.x + result.coordinate.width / 2, y: result.coordinate.y + result.coordinate.height / 2 };
- return {
- success: true,
- center: center,
- coordinate: result.coordinate // 同时返回完整坐标信息
- };
- } catch (error) {
- return {
- success: false,
- error: error.message || '图像中心点定位失败'
- };
- }
- }
|