有没有办法在Flutter中打开onPressed()事件?

gajydyqb  于 2023-05-23  发布在  Flutter
关注(0)|答案(4)|浏览(203)

我正在尝试使用相机服务使用Flutter相机包是否有任何方法可以切换到前到后或后到前相机打开按钮点击

xpcnnkqh

xpcnnkqh1#

获取可用的摄像机并在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');
  }

}

mznpcxlj

mznpcxlj2#

我有两个建议
1.请使用软件包camera_camera https://github.com/gabulsavul/camera_camera
它提供了一个很好的例子和很多功能。
您可以直接使用或修改此软件包。
此软件包的屏幕

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,
        ),
      ),
    );
  }
}

u0sqgete

u0sqgete3#

你可以这样做:

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();
         
    }
eimct9ow

eimct9ow4#

这是我的解决方案:

List<CameraDescription> cameras = [];
  late CameraController _controller;
  Future<void>? _initializeControllerFuture;

...

  void _toggleCameraLens()
  {
    if (cameras.length <= 2) {
      return;
    }

    final lensDirection = _controller.description.lensDirection;
    if (lensDirection == CameraLensDirection.front)
    {
      setState(() {
        _controller = CameraController(
          cameras.first,
          ResolutionPreset.medium,
        );
      });
    }
    else
    {
      setState(() {
        _controller = CameraController(
          cameras.last,
          ResolutionPreset.medium,
        );
      });
    }

    _initializeControllerFuture = _controller.initialize();
  }

相关问题