#!/usr/bin/env node /** * ADB 截图:将设备屏幕捕获到本地文件 * 用法: node adb-screencap.js * 示例: node adb-screencap.js 192.168.2.5:5555 C:\temp\screen.png */ const { spawnSync } = require('child_process') const path = require('path') const fs = require('fs') const config = require(path.join(__dirname, '..', '..', 'configs', 'config.js')) const projectRoot = path.resolve(__dirname, '..', '..') const adbPath = config.adbPath?.path ? path.resolve(projectRoot, config.adbPath.path) : path.join(projectRoot, 'lib', 'scrcpy-adb', 'adb.exe') const deviceId = process.argv[2] const outputPath = process.argv[3] if (!deviceId || !outputPath) { process.stderr.write('Usage: node adb-screencap.js \n') process.exit(1) } const deviceFlag = deviceId && deviceId.includes(':') ? ['-s', deviceId] : [] const args = [...deviceFlag, 'exec-out', 'screencap', '-p'] const result = spawnSync(adbPath, args, { encoding: null, timeout: 10000, maxBuffer: 16 * 1024 * 1024 }) if (result.error) { process.stderr.write(`screencap error: ${result.error.message}\n`) process.exit(1) } if (result.status !== 0) { process.stderr.write((result.stderr || result.stdout || Buffer.from('')).toString('utf8')) process.exit(1) } let data = result.stdout if (!data || data.length === 0) { process.stderr.write('screencap returned empty data\n') process.exit(1) } // 注意:不要对 PNG 做 \r\n 替换,会破坏 IDAT 压缩块导致 OpenCV/PIL 无法解析 const dir = path.dirname(outputPath) if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }) } fs.writeFileSync(outputPath, data) process.stdout.write(outputPath) process.exit(0)