[英]How to take screenshot of entire screen outside of your FLUTTER DESKTOP app(mostly for Windows OS)?
我是 flutter 的新手。 現在,我可以使用 Screenshot package 為我的整個桌面應用程序屏幕截屏並將該圖像存儲到本地存儲。
但我的要求是捕獲 window 的整個屏幕的屏幕截圖,就像如果在 1 個屏幕中打開 2 個應用程序(1 個 Flutter + 1 個任何其他應用程序,例如瀏覽器),那么我們不僅可以截取整個屏幕的屏幕截圖 Z5ACEBC4BACB70DDABAB076074B0AC76
請幫助我如何在 Windows OS 桌面應用程序中截取整個窗口的屏幕截圖? 如果不能直接從 Flutter 實現,那么如何通過使用 Flutter 實現一些本機代碼來實現這一點?
檢查這是否完全按預期工作
bool _isAccessAllowed = false;
CapturedData? _lastCapturedData;
@override
void initState() {
super.initState();
_init();
}
void _init() async {
_isAccessAllowed = await ScreenCapturer.instance.isAccessAllowed();
}
void _handleClickCapture(CaptureMode mode) async {
Directory directory = await getApplicationDocumentsDirectory();
String imageName =
'Screenshot-${DateTime.now().millisecondsSinceEpoch}.png';
String imagePath =
'${directory.path}/screen_capturer_example/Screenshots/$imageName';
_lastCapturedData = await ScreenCapturer.instance.capture(
mode: mode,
imagePath: imagePath,
silent: true,
);
if (_lastCapturedData != null) {
// ignore: avoid_print
// print(_lastCapturedData!.toJson());
} else {
// ignore: avoid_print
print('User canceled capture');
}
setState(() {});
}
Widget _buildBody(BuildContext context) {
return PreferenceList(
children: <Widget>[
if (Platform.isMacOS)
PreferenceListSection(
children: [
PreferenceListItem(
title: const Text('isAccessAllowed'),
accessoryView: Text('$_isAccessAllowed'),
onTap: () async {
bool allowed =
await ScreenCapturer.instance.isAccessAllowed();
BotToast.showText(text: 'allowed: $allowed');
setState(() {
_isAccessAllowed = allowed;
});
},
),
PreferenceListItem(
title: const Text('requestAccess'),
onTap: () async {
await ScreenCapturer.instance.requestAccess();
},
),
],
),
PreferenceListSection(
title: const Text('METHODS'),
children: [
PreferenceListItem(
title: const Text('capture'),
accessoryView: Row(children: [
CupertinoButton(
child: const Text('region'),
onPressed: () {
_handleClickCapture(CaptureMode.region);
},
),
CupertinoButton(
child: const Text('screen'),
onPressed: () {
_handleClickCapture(CaptureMode.screen);
},
),
CupertinoButton(
child: const Text('window'),
onPressed: () {
_handleClickCapture(CaptureMode.window);
},
),
]),
),
],
),
if (_lastCapturedData != null && _lastCapturedData?.imagePath != null)
Container(
margin: const EdgeInsets.only(top: 20),
width: 400,
height: 400,
child: Image.file(
File(_lastCapturedData!.imagePath!),
),
),
],
);
}
您可以嘗試使用這個 package: screen_capturer 。 它適用於 Windows、Linux 和 MacOS。
來自文檔:使用示例:
import 'package:screen_capturer/screen_capturer.dart';
CapturedData? capturedData = await screenCapturer.capture(
mode: CaptureMode.screen, // screen, window
imagePath: '<path>',
);
CaptureMode.screen 是捕獲整個屏幕。
您提到的屏幕截圖 package 僅用於為您的應用程序的小部件而不是整個屏幕截取屏幕截圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.