takePicture method

Future<void> takePicture()

The picture will only taken when CameraValue.isInitialized, and the camera is not taking pictures. 仅当初始化成功且相机未在拍照时拍照。

Implementation

Future<void> takePicture() async {
  if (!controller.value.isInitialized) {
    handleErrorWithHandler(
      StateError('Camera has not initialized.'),
      pickerConfig.onError,
    );
  }
  if (controller.value.isTakingPicture) {
    return;
  }
  try {
    final XFile file = await controller.takePicture();
    await controller.pausePreview();
    final bool? isCapturedFileHandled = pickerConfig.onXFileCaptured?.call(
      file,
      CameraPickerViewType.image,
    );
    if (isCapturedFileHandled ?? false) {
      return;
    }
    final AssetEntity? entity = await pushToViewer(
      file: file,
      viewType: CameraPickerViewType.image,
    );
    if (entity != null) {
      Navigator.of(context).pop(entity);
      return;
    }
    await controller.resumePreview();
  } catch (e) {
    realDebugPrint('Error when preview the captured file: $e');
    handleErrorWithHandler(e, pickerConfig.onError);
  } finally {
    safeSetState(() {});
  }
}