screenshot method

  1. @override
Future<ScreenshotResult> screenshot(
  1. String path
)
override

Capture a PNG to path for device-mode perception; also the coordinate reference (ratio = pixel / size).

Implementation

@override
Future<ScreenshotResult> screenshot(String path) async {
  // `exec-out screencap -p` streams raw PNG bytes to stdout.
  final ProcessResult res;
  try {
    res = await runBounded(
      adbPath,
      ['-s', deviceSerial, 'exec-out', 'screencap', '-p'],
      timeout: const Duration(seconds: 15),
      phase: 'adb screencap',
      stdoutEncoding: null, // keep stdout as raw bytes
    );
  } on Object catch (e) {
    return ScreenshotResult(error: 'adb screencap failed: $e');
  }
  if (res.exitCode != 0) {
    return ScreenshotResult(
      error: (res.stderr as String?)?.trim().isNotEmpty == true
          ? (res.stderr as String).trim()
          : 'adb screencap exited ${res.exitCode}',
    );
  }
  try {
    File(path).writeAsBytesSync(res.stdout as List<int>);
  } on Object catch (e) {
    return ScreenshotResult(error: 'could not write screenshot: $e');
  }
  final size = pngSize(path);
  return ScreenshotResult(path: path, width: size?.$1, height: size?.$2);
}