initialize method

Future<void> initialize()

Initializes the camera on the device.

Throws a CameraException if the initialization fails.

Implementation

Future<void> initialize() async {
  if (_isDisposed) {
    return Future<void>.value();
  }
  try {
    _creatingCompleter = Completer<void>();
    final Map<String, dynamic>? reply =
        await _channel.invokeMapMethod<String, dynamic>(
      'initialize',
      <String, dynamic>{
        'cameraName': description.name,
        'resolutionPreset': serializeResolutionPreset(resolutionPreset),
        'enableAudio': enableAudio,
        'iso': iso,
        'shutterSpeed': (shutterSpeed == 0) ? 0 : 1000000000 ~/ shutterSpeed,
        'whiteBalance': serializeWhiteBalancePreset(whiteBalance),
        'focusDistance': 1 / focusDistance,
      },
    );
    _textureId = reply!['textureId'];
    value = value.copyWith(
      isInitialized: true,
      previewSize: Size(
        reply['previewWidth'].toDouble(),
        reply['previewHeight'].toDouble(),
      ),
    );
  } on PlatformException catch (e) {
    throw CameraException(e.code, e.message!);
  }
  _eventSubscription =
      EventChannel('flutter.io/cameraPlugin/cameraEvents$_textureId')
          .receiveBroadcastStream()
          .listen(_listener);
  _creatingCompleter.complete();
  return _creatingCompleter.future;
}