image-center-location.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Func 标签:image-center-location
  3. *
  4. * 图像匹配功能:识别模板图片是否在截图中的位置,返回中心点坐标
  5. * 注意:实际的 OpenCV 处理需要在 Node.js 主进程中实现
  6. */
  7. export const tagName = 'image-center-location';
  8. export const schema = {
  9. description: '在屏幕截图中查找模板图片的位置并返回中心点坐标(可用于定位/点击)。',
  10. inputs: {
  11. template: '模板图片路径(相对于工作流目录)',
  12. variable: '输出变量名(保存中心点坐标)',
  13. },
  14. outputs: {
  15. variable: '中心点坐标(JSON 字符串格式,如:{"x":123,"y":456})',
  16. },
  17. };
  18. /**
  19. * 执行 image-center-location 功能
  20. * 这个函数会被 ActionParser 调用
  21. *
  22. * @param {Object} params - 参数对象
  23. * @param {string} params.device - 设备 ID/IP:Port(必需,用于获取截图)
  24. * @param {string} params.template - 模板图片路径
  25. * @param {string} params.folderPath - 工作流文件夹路径
  26. * @returns {Promise<{success: boolean, center?: Object, error?: string}>}
  27. */
  28. export async function executeImageCenterLocation({ device, template, folderPath }) {
  29. try {
  30. if (!window.electronAPI || !window.electronAPI.matchImageAndGetCoordinate) {
  31. return {
  32. success: false,
  33. error: 'matchImageAndGetCoordinate API 不可用'
  34. };
  35. }
  36. if (!device) {
  37. return {
  38. success: false,
  39. error: '缺少设备 ID,无法自动获取截图'
  40. };
  41. }
  42. // 构建模板图片完整路径(如果路径不是绝对路径,则相对于工作流目录的 resources 文件夹)
  43. // resources 作为根目录
  44. const templatePath = template.startsWith('/') || template.includes(':')
  45. ? template
  46. : `${folderPath}/resources/${template}`;
  47. // 调用主进程的图像匹配函数(会自动获取设备截图)
  48. const result = await window.electronAPI.matchImageAndGetCoordinate(
  49. device,
  50. templatePath
  51. );
  52. if (!result.success) {
  53. return { success: false, error: result.error };
  54. }
  55. // 返回中心点坐标
  56. const center = result.clickPosition || { x: result.coordinate.x + result.coordinate.width / 2, y: result.coordinate.y + result.coordinate.height / 2 };
  57. return {
  58. success: true,
  59. center: center,
  60. coordinate: result.coordinate // 同时返回完整坐标信息
  61. };
  62. } catch (error) {
  63. return {
  64. success: false,
  65. error: error.message || '图像中心点定位失败'
  66. };
  67. }
  68. }