captureScreen static method

Future<String?> captureScreen()

Implementation

static Future<String?> captureScreen() async {
  await requestScreenRecordingPermission();
  String directory;

  if (TencentCloudChatPlatformAdapter().isWindows) {
    final String documentsDirectoryPath = "${Platform.environment['USERPROFILE']}";
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String pkgName = packageInfo.packageName;
    directory = Pertypath().join(documentsDirectoryPath, "Documents", ".TencentCloudChat", pkgName, "screenshots");
  } else {
    final dic = await getApplicationSupportDirectory();
    directory = dic.path;
  }
  final uuid = DateTime.now().microsecondsSinceEpoch;
  final fileName = 'screenshot_$uuid.png';
  final filePath = '$directory/$fileName';
  if (Platform.isMacOS) {
    // 在macOS平台上使用screencapture工具
    final result = await Process.run(
      'screencapture',
      ['-i', '-s', '-o', filePath],
    );
    if (result.exitCode == 0) {
      return filePath;
    } else {
      return null;
    }
  } else if (Platform.isWindows) {
    // 在Windows平台上使用snippingtool工具
    final result = await Process.run(
      'snippingtool',
      ['/clip', filePath],
    );
    if (result.exitCode == 0) {
      return filePath;
    } else {
      return null;
    }
  } else {
    // 不支持的平台
    return null;
  }
}