首页 > 解决方案 > 如何在 Flutter 中 3 秒后自动拍照?

问题描述

我正在使用 Flutter Application 在平板电脑中开发打卡时钟,我需要此功能。

我知道如何让相机显示和用户制作截图,但我到处搜索如何在 3 秒后制作自动自拍截图,但没有找到任何东西。

有人有示例、教程或经验来制作这样的东西吗?

标签: flutterdart

解决方案


您可以使用 Timer 类在一定时间后执行一些操作,如下所示

Timer(Duration(milliseconds: 3000), () {
    //after 3 seconds this will be called, 
    //once this is called take picture or whatever function you need to do
    takeScreenshot();
  });

如果您愿意,请使用下面的代码截取此答案中提到的屏幕截图 Take Screenshot Stackoverflow answer

takeScreenShot() async{
   RenderRepaintBoundary boundary = 
      previewContainer.currentContext.findRenderObject();
   ui.Image image = await boundary.toImage();
   final directory = (await getApplicationDocumentsDirectory()).path;
   ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
   Uint8List pngBytes = byteData.buffer.asUint8List();
   print(pngBytes);
   File imgFile =new File('$directory/screenshot.png');
   imgFile.writeAsBytes(pngBytes);
  }

希望这可以帮助!


推荐阅读