initializeCamera method

  1. @override
Future<void> initializeCamera(
  1. int cameraId, {
  2. ImageFormatGroup imageFormatGroup = ImageFormatGroup.unknown,
})

Initializes the camera on the device.

imageFormatGroup is used to specify the image formatting used. On Android this defaults to ImageFormat.YUV_420_888 and applies only to the imageStream. On iOS this defaults to kCVPixelFormatType_32BGRA. On Web this parameter is currently not supported.

Implementation

@override
Future<void> initializeCamera(
  int cameraId, {
  ImageFormatGroup imageFormatGroup = ImageFormatGroup.unknown,
}) {
  _channels.putIfAbsent(cameraId, () {
    final MethodChannel channel =
        MethodChannel('plugins.flutter.io/camera_avfoundation/camera$cameraId');
    channel.setMethodCallHandler((MethodCall call) => handleCameraMethodCall(call, cameraId));
    return channel;
  });

  final Completer<void> completer = Completer<void>();

  onCameraInitialized(cameraId).first.then((CameraInitializedEvent value) {
    completer.complete();
  });

  _channel.invokeMapMethod<String, dynamic>(
    'initialize',
    <String, dynamic>{
      'cameraId': cameraId,
      'imageFormatGroup': imageFormatGroup.name(),
    },
  ).catchError(
    // TODO(srawlins): This should return a value of the future's type. This
    // will fail upcoming analysis checks with
    // https://github.com/flutter/flutter/issues/105750.
    // ignore: body_might_complete_normally_catch_error
    (Object error, StackTrace stackTrace) {
      if (error is! PlatformException) {
        // ignore: only_throw_errors
        throw error;
      }
      completer.completeError(
        CameraException(error.code, error.message),
        stackTrace,
      );
    },
  );

  return completer.future;
}