| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /**
- * fun 标签:img-cropping
- * 根据区域坐标裁剪截图指定区域并保存
- */
- const path = require('path')
- const fs = require('fs')
- const { spawnSync } = require('child_process')
- const { captureScreenshot } = require('../../adb/adb-screencap.js')
- const tagName = 'img-cropping'
- const projectRoot = path.resolve(__dirname, '..', '..', '..')
- const config = require(path.join(projectRoot, 'configs', 'config.js'))
- const imgCropScriptPath = path.join(projectRoot, 'python', 'scripts', 'img-crop.py')
- /** 解析 area 为 { x, y, width, height } */
- function parseAreaToRect(area) {
- const obj = typeof area === 'string' ? JSON.parse(area) : area
- if (obj.topLeft && obj.bottomRight) {
- return {
- x: parseInt(obj.topLeft.x, 10),
- y: parseInt(obj.topLeft.y, 10),
- width: parseInt(obj.bottomRight.x - obj.topLeft.x, 10),
- height: parseInt(obj.bottomRight.y - obj.topLeft.y, 10),
- }
- }
- if (obj.topLeft && obj.topRight && obj.bottomLeft && obj.bottomRight) {
- return {
- x: parseInt(obj.topLeft.x, 10),
- y: parseInt(obj.topLeft.y, 10),
- width: parseInt(obj.bottomRight.x - obj.topLeft.x, 10),
- height: parseInt(obj.bottomRight.y - obj.topLeft.y, 10),
- }
- }
- if (obj.x != null && obj.y != null && obj.width != null && obj.height != null) {
- return {
- x: parseInt(obj.x, 10),
- y: parseInt(obj.y, 10),
- width: parseInt(obj.width, 10),
- height: parseInt(obj.height, 10),
- }
- }
- return null
- }
- /** 构建截图路径 */
- function buildScreenshotPath(folderPath) {
- return path.join(folderPath, 'history', 'ScreenShot.png')
- }
- /** 构建保存路径 */
- function buildSavePath(savePath, folderPath) {
- return savePath.includes(':') ? savePath : path.join(folderPath, savePath)
- }
- /** 调用 Python 裁剪图片并保存 */
- function cropAndSaveImage(inputPath, outputPath, x, y, width, height) {
- const envVenv = path.join(config.pythonEnvPath, 'env', 'Scripts', 'python.exe')
- const venvScripts = path.join(config.pythonEnvPath, 'Scripts', 'python.exe')
- const pyEmbedded = path.join(config.pythonEnvPath, 'py', 'python.exe')
- const pythonExe = fs.existsSync(envVenv) ? envVenv : (fs.existsSync(venvScripts) ? venvScripts : pyEmbedded)
- const r = spawnSync(pythonExe, [imgCropScriptPath, inputPath, outputPath, String(x), String(y), String(width), String(height)], {
- encoding: 'utf-8',
- timeout: 10000,
- cwd: projectRoot
- })
- if (r.status !== 0) {
- return { success: false, error: (r.stderr || r.stdout || '').trim() || '裁剪失败' }
- }
- return { success: true }
- }
- /** 执行 img-cropping */
- async function executeImgCropping({ area, savePath, folderPath, device }) {
- const rect = parseAreaToRect(area)
- if (!rect || rect.width <= 0 || rect.height <= 0) {
- return { success: false, error: '区域坐标格式不正确' }
- }
- const screenshotPath = buildScreenshotPath(folderPath)
- const outputPath = buildSavePath(savePath, folderPath)
- if (device) {
- const cap = captureScreenshot(device, screenshotPath)
- if (!cap.success) return { success: false, error: cap.error }
- }
- const crop = cropAndSaveImage(screenshotPath, outputPath, rect.x, rect.y, rect.width, rect.height)
- if (!crop.success) return { success: false, error: crop.error }
- return { success: true }
- }
- module.exports = { tagName, executeImgCropping, cropAndSaveImage }
|