img-bounding-box-location.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * fun 标签:img-bounding-box-location
  3. *
  4. * 在完整截图中查找区域截图的位置,返回区域包围框的四个顶点坐标。
  5. */
  6. function matchImageRegionLocation() {
  7. return { success: false, error: 'matchImageRegionLocation 需在主进程实现' }
  8. }
  9. const tagName = 'img-bounding-box-location'
  10. const schema = {
  11. description: '在完整截图中查找区域截图的位置,返回区域的四个顶点坐标(左上、右上、左下、右下)。',
  12. inputs: {
  13. screenshot: '完整截图路径(相对于工作流目录)',
  14. region: '区域截图路径(相对于工作流目录)',
  15. variable: '输出变量名(保存四个顶点坐标)',
  16. },
  17. outputs: {
  18. variable: '四个顶点坐标对象 {topLeft: {x, y}, topRight: {x, y}, bottomLeft: {x, y}, bottomRight: {x, y}}',
  19. },
  20. };
  21. /**
  22. * 执行 img-bounding-box-location 功能
  23. */
  24. async function executeImgBoundingBoxLocation({ device, screenshot, region, folderPath }) {
  25. let screenshotPath = screenshot;
  26. if (screenshot === null) {
  27. screenshotPath = '__AUTO_SCREENSHOT__';
  28. } else if (screenshot) {
  29. screenshotPath = screenshot.startsWith('/') || screenshot.includes(':')
  30. ? screenshot
  31. : `${folderPath}/resources/${screenshot}`;
  32. }
  33. const regionPath = region.startsWith('/') || region.includes(':')
  34. ? region
  35. : `${folderPath}/resources/${region}`;
  36. const result = matchImageRegionLocation(screenshotPath, regionPath, device);
  37. if (!result.success) {
  38. return { success: false, error: result.error };
  39. }
  40. const { x, y, width, height } = result;
  41. const corners = {
  42. topLeft: { x, y },
  43. topRight: { x: x + width, y },
  44. bottomLeft: { x, y: y + height },
  45. bottomRight: { x: x + width, y: y + height },
  46. };
  47. return {
  48. success: true,
  49. corners,
  50. bounds: { x, y, width, height },
  51. };
  52. }
  53. module.exports = { tagName, schema, executeImgBoundingBoxLocation };