Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 颤振摄影机控制-如何在指定的持续时间内拍照_Android_Mobile_Flutter - Fatal编程技术网

Android 颤振摄影机控制-如何在指定的持续时间内拍照

Android 颤振摄影机控制-如何在指定的持续时间内拍照,android,mobile,flutter,Android,Mobile,Flutter,我刚开始触摸一款手机应用程序的flifter。我在我的应用程序中有一个页面,可以通过点击按钮或5秒的持续时间拍摄一张手机摄像头的照片。通过单击按钮拍照很容易实现,但它的持续时间不是5秒 我添加了一个计时器,如果页面打开并且没有按钮点击,它会在5秒钟后调用拍照方法 请对我的代码的任何想法发表评论,或者对我如何实现我的功能提出建议?我对Flitter和移动应用程序是全新的。非常感谢 import 'dart:async'; import 'dart:io'; import 'package:cam

我刚开始触摸一款手机应用程序的flifter。我在我的应用程序中有一个页面,可以通过点击按钮或5秒的持续时间拍摄一张手机摄像头的照片。通过单击按钮拍照很容易实现,但它的持续时间不是5秒

我添加了一个计时器,如果页面打开并且没有按钮点击,它会在5秒钟后调用拍照方法

请对我的代码的任何想法发表评论,或者对我如何实现我的功能提出建议?我对Flitter和移动应用程序是全新的。非常感谢

import 'dart:async';
import 'dart:io';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart' show join;
import 'package:path_provider/path_provider.dart';

Future<void> main() async {
  // Obtain a list of the available cameras on the device.
  final cameras = await availableCameras();

  // Get a specific camera from the list of available cameras.
  final firstCamera = cameras.first;

  runApp(
    MaterialApp(
      theme: ThemeData.dark(),
      home: TakePictureScreen(
        // Pass the appropriate camera to the TakePictureScreen widget.
        camera: firstCamera,
      ),
    ),
  );
}

// A screen that allows users to take a picture using a given camera.
class TakePictureScreen extends StatefulWidget {
  final CameraDescription camera;

  const TakePictureScreen({
    Key key,
    @required this.camera,
  }) : super(key: key);

  @override
  TakePictureScreenState createState() => TakePictureScreenState();
}

class TakePictureScreenState extends State<TakePictureScreen> {
  CameraController _controller;
  Future<void> _initializeControllerFuture;

  @override
  void initState() {
    super.initState();
    // To display the current output from the Camera,
    // create a CameraController.
    _controller = CameraController(
      // Get a specific camera from the list of available cameras.
      widget.camera,
      // Define the resolution to use.
      ResolutionPreset.medium,
    );

    // Next, initialize the controller. This returns a Future.
    _initializeControllerFuture = _controller.initialize();
  }

  @override
  void dispose() {
    // Dispose of the controller when the widget is disposed.
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Take a picture')),
      // Wait until the controller is initialized before displaying the
      // camera preview. Use a FutureBuilder to display a loading spinner
      // until the controller has finished initializing.
      body: FutureBuilder<void>(
        future: _initializeControllerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            // If the Future is complete, display the preview.
            **Timer const Duration(seconds: 5), (){
                takePic();
            };**
            return CameraPreview(_controller);
          } else {
            // Otherwise, display a loading indicator.
            return Center(child: CircularProgressIndicator());
          }
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.camera_alt),
        // Provide an onPressed callback.
        onPressed: () async {
          takePic();
        },
      ),
    );
  }
}

void takePic() async {
// Take the Picture in a try / catch block. If anything goes wrong,
          // catch the error.
          try {
            // Ensure that the camera is initialized.
            await _initializeControllerFuture;

            // Construct the path where the image should be saved using the 
            // pattern package.
            final path = join(
              // Store the picture in the temp directory.
              // Find the temp directory using the `path_provider` plugin.
              (await getTemporaryDirectory()).path,
              '${DateTime.now()}.png',
            );

            // Attempt to take a picture and log where it's been saved.
            await _controller.takePicture(path);

            // If the picture was taken, display it on a new screen.
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => DisplayPictureScreen(imagePath: path),
              ),
            );
          } catch (e) {
            // If an error occurs, log the error to the console.
            print(e);
          }
}

// A widget that displays the picture taken by the user.
class DisplayPictureScreen extends StatelessWidget {
  final String imagePath;

  const DisplayPictureScreen({Key key, this.imagePath}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Display the Picture')),
      // The image is stored as a file on the device. Use the `Image.file`
      // constructor with the given path to display the image.
      body: Image.file(File(imagePath)),
    );
  }
}
导入'dart:async';
导入“dart:io”;
导入“包:摄像机/摄像机.dart”;
进口“包装:颤振/材料.省道”;
导入“package:path/path.dart”显示连接;
导入“package:path_provider/path_provider.dart”;
Future main()异步{
//获取设备上可用摄像头的列表。
最终摄像机=等待可用摄像机();
//从可用摄影机列表中获取特定摄影机。
final firstCamera=cameras.first;
runApp(
材料聚丙烯(
主题:ThemeData.dark(),
主页:TakePictureScreen(
//将适当的相机传递到TakePictureScreen小部件。
摄像机:第一台摄像机,
),
),
);
}
//允许用户使用给定相机拍照的屏幕。
类TakePictureScreen扩展StatefulWidget{
最终摄像机描述摄像机;
康斯特塔克图片屏幕({
关键点,
@需要这个摄像头,
}):super(key:key);
@凌驾
TakePictureScreensState createState()=>TakePictureScreenState();
}
类takePictureScreentState扩展了状态{
摄像机控制器(CameraController)控制器;;
未来(初始化控制未来);
@凌驾
void initState(){
super.initState();
//要显示摄像机的电流输出,
//创建一个CameraController。
_控制器=摄像机控制器(
//从可用摄影机列表中获取特定摄影机。
照相机,
//定义要使用的分辨率。
ResolutionPreset.medium,
);
//接下来,初始化控制器。这将返回未来。
_initializeControllerFuture=_controller.initialize();
}
@凌驾
无效处置(){
//处置小部件时处置控制器。
_controller.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“拍照”),
//等待控制器初始化后再显示
//摄影机预览。使用FutureBuilder显示加载微调器
//直到控制器完成初始化。
正文:未来建设者(
未来:_initializeControllerFuture,
生成器:(上下文,快照){
if(snapshot.connectionState==connectionState.done){
//如果未来已完成,则显示预览。
**计时器常数持续时间(秒:5),(){
takePic();
};**
返回CameraPreview(_控制器);
}否则{
//否则,显示加载指示器。
返回中心(子项:CircularProgressIndicator());
}
},
),
浮动操作按钮:浮动操作按钮(
子:图标(Icons.camera\u alt),
//提供一个onPressed回调。
onPressed:()异步{
takePic();
},
),
);
}
}
void takePic()异步{
//在try/catch块中拍摄照片。如果出现任何问题,
//抓住错误。
试一试{
//确保摄像头已初始化。
等待(u initializeControllerFuture);;
//使用
//图案包。
最终路径=连接(
//将图片存储在临时目录中。
//使用'path_provider'插件查找临时目录。
(等待getTemporaryDirectory())。路径,
“${DateTime.now()}.png”,
);
//尝试拍摄照片并记录保存的位置。
等待控制器。拍照(路径);
//如果照片是拍摄的,请将其显示在新屏幕上。
导航器。推(
上下文
材料路线(
生成器:(上下文)=>DisplayPictureScreen(imagePath:path),
),
);
}捕获(e){
//如果发生错误,请将错误记录到控制台。
印刷品(e);
}
}
//显示用户拍摄的照片的小部件。
类DisplayPictureScreen扩展了无状态小部件{
最终字符串图像路径;
constDisplayPictureScreen({Key-Key,this.imagePath}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“显示图片”),
//图像作为文件存储在设备上。请使用'image.file'`
//使用给定路径来显示图像的构造函数。
body:Image.file(file(imagePath)),
);
}
}