有什么办法可以在Flutter中打开onPressed()事件上的相机吗?

时间:2019-07-26 18:50:44

标签: flutter dart

我正在尝试使用Flutter相机包使用相机服务,是否有任何方法可以切换到前置或后置摄像头或单击按钮,返回到前置摄像头单击

3 个答案:

答案 0 :(得分:5)

获取可用的摄像头,并在initState()中使用其中一个初始化摄像头控制器。另外,将可用的摄像机存储到类变量中。

 CameraController _controller;
 List<CameraDescription> _availableCameras;

 @override
 void initState() {
   super.initState();
   _getAvailableCameras();
 }

 // get available cameras
 Future<void> _getAvailableCameras() async{
   WidgetsFlutterBinding.ensureInitialized();
   _availableCameras = await availableCameras();
   _initCamera(_availableCameras.first);
 }

 // init camera
 Future<void> _initCamera(CameraDescription description) async{
  _controller = CameraController(description, ResolutionPreset.max, enableAudio: true);

  try{
    await _controller.initialize();
    // to notify the widgets that camera has been initialized and now camera preview can be done
    setState((){}); 
  }
  catch(e){
    print(e);
  }  
}

只需在单击按钮时使用新的摄像机描述来重新初始化摄像机控制器。

void _toggleCameraLens() {
 // get current lens direction (front / rear)
 final lensDirection =  _controller.description.lensDirection;
 CameraDescription newDescription;
 if(lensDirection == CameraLensDirection.front){
    newDescription = _availableCameras.firstWhere((description) => description.lensDirection == CameraLensDirection.back);
 }
 else{
    newDescription = _availableCameras.firstWhere((description) => description.lensDirection == CameraLensDirection.front);
 }

  if(newDescription != null){
    _initCamera(newDescription);
  }
  else{
    print('Asked camera not available');
  }

}

答案 1 :(得分:0)

我有2条建议
1.请使用camera_camera https://github.com/gabulsavul/camera_camera软件包
它提供了一个很好的例子,并且已经提供了很多功能。
您可以直接使用此软件包或对其进行修改。
此包装的屏幕
enter image description here

  1. 在官方示例代码中
    https://github.com/flutter/plugins/blob/master/packages/camera/example/lib/main.dart
    通过此功能切换相机
    您可以直接使用此示例

    void onNewCameraSelected(CameraDescription cameraDescription) async {
    if (controller != null) {
      await controller.dispose();
    }
    controller = CameraController(
      cameraDescription,
      ResolutionPreset.high,
      enableAudio: enableAudio,
    );
    
    // If the controller is updated then update the UI.
    controller.addListener(() {
      if (mounted) setState(() {});
      if (controller.value.hasError) {
        showInSnackBar('Camera error ${controller.value.errorDescription}');
      }
    });
    
    try {
      await controller.initialize();
    } on CameraException catch (e) {
      _showCameraException(e);
    }
    
    if (mounted) {
      setState(() {});
    }
    }
    

    显示一行切换按钮以选择相机

      Widget _cameraTogglesRowWidget() {
    final List<Widget> toggles = <Widget>[];
    
    if (cameras.isEmpty) {
      return const Text('No camera found');
    } else {
      for (CameraDescription cameraDescription in cameras) {
        toggles.add(
          SizedBox(
            width: 90.0,
            child: RadioListTile<CameraDescription>(
              title: Icon(getCameraLensIcon(cameraDescription.lensDirection)),
              groupValue: controller?.description,
              value: cameraDescription,
              onChanged: controller != null && controller.value.isRecordingVideo
                  ? null
                  : onNewCameraSelected,
            ),
          ),
        );
      }
    }
    

    enter image description here

答案 2 :(得分:0)

你可以这样做:

onTap: () {
      this.setState(() {
        /// Change the current selected camera State.
        this.selectedCamera =this.selectedCamera == 0 ? 1 : 0;
      });

      this._cameraController = CameraController(
          this._camerasAvailable[this.selectedCamera],
          ResolutionPreset.max,
      );
      /// Reinit camera.
      this.cameraInitialize = this._cameraController.initialize();
         
    }