flutter - 如何在 Flutter 中 3 秒后自动拍照?
问题描述
我正在使用 Flutter Application 在平板电脑中开发打卡时钟,我需要此功能。
我知道如何让相机显示和用户制作截图,但我到处搜索如何在 3 秒后制作自动自拍截图,但没有找到任何东西。
有人有示例、教程或经验来制作这样的东西吗?
解决方案
您可以使用 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);
}
希望这可以帮助!
推荐阅读
- c# - NumberFormatInfo 设置
- python - 确定一个值是否超过另一个值的函数
- java - 我想在集合的所有文档中保存相同的字符串
- javascript - 通过 Cloud Functions 删除整个集合
- mysql - 默认情况下,所有 knex 查询都在事务中运行吗?
- c-preprocessor - 在预处理器 if 语句中定义 C 宏
- csv - plotly dash:如何获取 csv 的标题作为下拉选项
- jquery - 在 CoffeeScript 范围内更改变量值
- html - 使用 iFrame 无法显示嵌入的网页
- spotfire - Spotfire:如果子值之一为空,如何使给定 ID 的所有值都为空