| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import { ipcMain } from 'electron';
- import { exec } from 'child_process';
- import { promisify } from 'util';
- import { getCachedAdbPath } from '../config.js';
- const execAsync = promisify(exec);
- // 抓取设备截屏(返回 base64 PNG/JPEG)
- export async function captureScreenshot(ipPort, options = {}) {
- if (!ipPort) {
- return { success: false, error: '缺少设备 ID' };
- }
- try {
- const adbPath = getCachedAdbPath();
-
- // 从选项或默认值获取参数
- const format = options.format || 'png'; // 'png' 或 'jpeg'
- const quality = options.quality || 80; // JPEG 质量 1-100
- const scale = options.scale || 1.0; // 缩放比例 0.1-1.0
-
- // 构建 screencap 命令
- let command = `${adbPath} -s ${ipPort} exec-out screencap`;
-
- // 根据格式选择参数
- if (format === 'jpeg') {
- // JPEG 格式(更小,延迟更低)
- command += ` -j ${quality}`;
- } else {
- // PNG 格式(默认)
- command += ' -p';
- }
-
- // 如果缩放比例不是 1.0,需要通过 shell 命令处理
- // 注意:screencap 本身不支持缩放,需要通过其他方式实现
- // 这里先实现基本功能,缩放可以在后续优化
-
- const { stdout } = await execAsync(command, {
- encoding: 'buffer',
- maxBuffer: 25 * 1024 * 1024,
- });
-
- return { success: true, data: stdout.toString('base64') };
- } catch (error) {
- console.error('截屏失败:', error);
- return { success: false, error: error.message };
- }
- }
- // 注册 IPC 处理器
- export function registerIpcHandlers() {
- // IPC 处理程序:抓取设备截屏(返回 base64 PNG/JPEG)
- ipcMain.handle('capture-screenshot', async (event, ipPort, options = {}) => {
- return await captureScreenshot(ipPort, options);
- });
- }
|