image-region-location.js 3.5 KB

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