initializeCamera method

Future<void> initializeCamera({
  1. required CameraLensDirection cameraLensDirection,
})

Implementation

Future<void> initializeCamera({required CameraLensDirection cameraLensDirection}) async {
  if (_cameraAvailable.isEmpty) {
    await _initCameraAvailable();
  }

  if (_tempOnCameraInitialized != null) {
    _onCameraInitialized = _tempOnCameraInitialized;
    _tempOnCameraInitialized = null;
  }

  currentCameraLensDirection = cameraLensDirection;
  CameraDescription? selectedCameraDescription = await _getCameraBasedOnFacingType(cameraLensDirection);

  if (selectedCameraDescription == null) {
    log("no camera with $cameraLensDirection available");
    return;
  }
  log("initialized camera with $cameraLensDirection");

  cameraController = CameraController(selectedCameraDescription, ResolutionPreset.high);
  cameraController!.initialize().then((_) {
    _onCameraInitialized?.call(cameraController!);
  }).catchError((e) {
    if (e is CameraException) {
      switch (e.code) {
        case 'CameraAccessDenied':
          _onCameraInitializedFailure?.call(
            FeatureCameraException(
              code: EnumFeatureCameraException.permission.name,
              message: 'give app access to camera permission',
            ),
          );
          break;
        default:
          _onCameraInitializedFailure?.call(
            FeatureCameraException(
              code: EnumFeatureCameraException.other.name,
              message: 'code: ${e.code}, message: ${e.description}',
            ),
          );
          break;
      }
    } else {
      _onCameraInitializedFailure?.call(FeatureCameraException(code: 'OTHER', message: 'other exception: $e'));
    }
  });
}