img-center-point-location.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * fun 标签:img-center-point-location
  3. * 图像匹配:识别模板图片在截图中的位置,返回中心点坐标
  4. */
  5. const path = require('path')
  6. const fs = require('fs')
  7. const os = require('os')
  8. const { spawnSync } = require('child_process')
  9. const configPath = process.env.STATIC_ROOT
  10. ? path.join(path.dirname(process.env.STATIC_ROOT), 'configs', 'config.js')
  11. : path.join(__dirname, '..', '..', '..', 'configs', 'config.js')
  12. const projectRoot = path.dirname(path.dirname(path.resolve(configPath)))
  13. const config = fs.existsSync(configPath) ? require(configPath) : {}
  14. const imageMatchScriptPath = path.join(projectRoot, 'python', 'scripts', 'image-match.py')
  15. const tagName = 'img-center-point-location'
  16. const schema = {
  17. description: '在屏幕截图中查找模板图片的位置并返回中心点坐标(可用于定位/点击)。',
  18. inputs: { template: '模板图片路径(相对于工作流目录)', variable: '输出变量名(保存中心点坐标)' },
  19. outputs: { variable: '中心点坐标(JSON 字符串格式,如:{"x":123,"y":456})' },
  20. }
  21. /** 解析 Python 可执行路径(与 config 中 pythonPath / pythonVenvPath 一致) */
  22. function getPythonPath() {
  23. const base = config.pythonPath?.path || config.pythonVenvPath || path.join(projectRoot, 'python', process.arch === 'arm64' ? 'arm64' : 'x64')
  24. const envPy = path.join(base, 'env', 'Scripts', 'python.exe')
  25. const scriptsPy = path.join(base, 'Scripts', 'python.exe')
  26. const pyEmbedded = path.join(base, 'py', 'python.exe')
  27. if (fs.existsSync(envPy)) return envPy
  28. if (fs.existsSync(scriptsPy)) return scriptsPy
  29. if (fs.existsSync(pyEmbedded)) return pyEmbedded
  30. return 'python'
  31. }
  32. /** 在设备截图中匹配模板,返回坐标与中心点 */
  33. function matchImageAndGetCoordinate(device, imagePath) {
  34. if (!imagePath || typeof imagePath !== 'string') return { success: false, error: '模板路径为空' }
  35. const templatePath = path.isAbsolute(imagePath) ? imagePath : path.resolve(projectRoot, imagePath)
  36. const ts = Date.now()
  37. const screenshotPath = path.join(os.tmpdir(), `ef-screenshot-${ts}.png`)
  38. const templateCopyPath = path.join(os.tmpdir(), `ef-template-${ts}.png`)
  39. fs.copyFileSync(templatePath, templateCopyPath)
  40. const pythonPath = getPythonPath()
  41. const adbPath = config.adbPath?.path
  42. ? (path.isAbsolute(config.adbPath.path) ? config.adbPath.path : path.resolve(projectRoot, config.adbPath.path))
  43. : path.join(projectRoot, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
  44. const r = spawnSync(pythonPath, [imageMatchScriptPath, '--adb', adbPath, '--device', device, '--screenshot', screenshotPath.replace(/\\/g, '/'), '--template', templateCopyPath.replace(/\\/g, '/'), '--method', 'feature'], {
  45. encoding: 'utf-8',
  46. timeout: 20000,
  47. env: { ...process.env, PYTHONIOENCODING: 'utf-8' },
  48. cwd: projectRoot
  49. })
  50. try { fs.unlinkSync(screenshotPath) } catch (_) {}
  51. try { fs.unlinkSync(templateCopyPath) } catch (_) {}
  52. if (r.status !== 0) return { success: false, error: (r.stderr || r.stdout || '').trim() || '图像匹配失败' }
  53. const out = JSON.parse(r.stdout.trim())
  54. if (!out.success) return { success: false, error: out.error || '未找到图片' }
  55. return {
  56. success: true,
  57. coordinate: { x: out.x, y: out.y, width: out.width, height: out.height },
  58. clickPosition: { x: out.center_x, y: out.center_y }
  59. }
  60. }
  61. async function executeImgCenterPointLocation({ device, template, folderPath }) {
  62. if (!device) return { success: false, error: '缺少设备 ID,无法自动获取截图' }
  63. if (!template || typeof template !== 'string') return { success: false, error: '缺少模板图片路径' }
  64. const baseDir = folderPath && typeof folderPath === 'string' ? folderPath : projectRoot
  65. const templatePath = template.startsWith('/') || template.includes(':') ? template : path.join(baseDir, 'resources', template)
  66. const result = matchImageAndGetCoordinate(device, templatePath)
  67. if (!result.success) return { success: false, error: result.error }
  68. const center = result.clickPosition || { x: result.coordinate.x + result.coordinate.width / 2, y: result.coordinate.y + result.coordinate.height / 2 }
  69. return { success: true, center, coordinate: result.coordinate }
  70. }
  71. module.exports = { tagName, schema, executeImgCenterPointLocation, matchImageAndGetCoordinate }