image-region-location.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Func 标签:image-region-location
  3. *
  4. * 约定:src/pages/processing/func/ 目录下每个文件名就是一个"可用标签/能力"。
  5. * 本文件用于声明该标签存在(供文档/提示词/后续动态加载使用)。
  6. *
  7. * 语义:通过图像匹配找到区域截图在完整截图中的位置,返回区域的四个顶点坐标。
  8. * 当前项目里对应能力主要由 electronAPI.matchImageRegionLocation + main-js/func/image-center-location.js 实现承载。
  9. */
  10. export const tagName = 'image-region-location';
  11. export const schema = {
  12. description: '在完整截图中查找区域截图的位置,返回区域的四个顶点坐标(左上、右上、左下、右下)。',
  13. inputs: {
  14. screenshot: '完整截图路径(相对于工作流目录)',
  15. region: '区域截图路径(相对于工作流目录)',
  16. variable: '输出变量名(保存四个顶点坐标)',
  17. },
  18. outputs: {
  19. variable: '四个顶点坐标对象 {topLeft: {x, y}, topRight: {x, y}, bottomLeft: {x, y}, bottomRight: {x, y}}',
  20. },
  21. };
  22. /**
  23. * 执行 image-region-location 功能
  24. * 这个函数会被 ActionParser 调用
  25. *
  26. * @param {Object} params - 参数对象
  27. * @param {string} params.device - 设备 ID/IP:Port(可选,用于获取分辨率)
  28. * @param {string} params.screenshot - 完整截图路径
  29. * @param {string} params.region - 区域截图路径
  30. * @param {string} params.folderPath - 工作流文件夹路径
  31. * @returns {Promise<{success: boolean, corners?: Object, error?: string}>}
  32. */
  33. export async function executeImageRegionLocation({ device, screenshot, region, folderPath }) {
  34. try {
  35. if (!window.electronAPI || !window.electronAPI.matchImageRegionLocation) {
  36. return {
  37. success: false,
  38. error: 'matchImageRegionLocation API 不可用'
  39. };
  40. }
  41. // 如果 screenshot 为 null,使用特殊标记让主进程自动获取截图
  42. let screenshotPath = screenshot;
  43. if (screenshot === null) {
  44. screenshotPath = '__AUTO_SCREENSHOT__';
  45. } else if (screenshot) {
  46. // 构建完整路径(如果路径不是绝对路径,则相对于工作流目录的 resources 文件夹)
  47. // resources 作为根目录(使用已有的截图文件进行匹配,不主动截图)
  48. screenshotPath = screenshot.startsWith('/') || screenshot.includes(':')
  49. ? screenshot
  50. : `${folderPath}/resources/${screenshot}`;
  51. }
  52. const regionPath = region.startsWith('/') || region.includes(':')
  53. ? region
  54. : `${folderPath}/resources/${region}`;
  55. // 调用主进程的图像区域定位函数
  56. // 如果 screenshotPath 是 '__AUTO_SCREENSHOT__',主进程会自动获取截图
  57. const result = await window.electronAPI.matchImageRegionLocation(
  58. screenshotPath,
  59. regionPath,
  60. device // 可选,用于获取设备分辨率进行缩放或自动获取截图
  61. );
  62. if (!result.success) {
  63. return { success: false, error: result.error };
  64. }
  65. // 计算四个顶点坐标
  66. const { x, y, width, height } = result;
  67. const corners = {
  68. topLeft: { x, y },
  69. topRight: { x: x + width, y },
  70. bottomLeft: { x, y: y + height },
  71. bottomRight: { x: x + width, y: y + height }
  72. };
  73. return {
  74. success: true,
  75. corners: corners,
  76. bounds: { x, y, width, height } // 同时返回边界框信息
  77. };
  78. } catch (error) {
  79. return {
  80. success: false,
  81. error: error.message || '图像区域定位失败'
  82. };
  83. }
  84. }