image-center-location.js 2.5 KB

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