startImageStream method

Future<void> startImageStream(
  1. onLatestImageAvailable onAvailable
)

Start streaming images from platform camera.

Settings for capturing images on iOS and Android is set to always use the latest image available from the camera and will drop all other images.

When running continuously with CameraPreview widget, this function runs best with ResolutionPreset.low. Running on ResolutionPreset.high can have significant frame rate drops for CameraPreview on lower end devices.

Throws a CameraException if image streaming or video recording has already started.

The startImageStream method is only available on Android and iOS (other platforms won't be supported in current setup).

Implementation

// TODO(bmparr): Add settings for resolution and fps.
Future<void> startImageStream(onLatestImageAvailable onAvailable) async {
  assert(defaultTargetPlatform == TargetPlatform.android ||
      defaultTargetPlatform == TargetPlatform.iOS);
  _throwIfNotInitialized('startImageStream');
  if (value.isRecordingVideo) {
    throw CameraException(
      'A video recording is already started.',
      'startImageStream was called while a video is being recorded.',
    );
  }
  if (value.isStreamingImages) {
    throw CameraException(
      'A camera has started streaming images.',
      'startImageStream was called while a camera was streaming images.',
    );
  }

  try {
    _imageStreamSubscription = CameraPlatform.instance
        .onStreamedFrameAvailable(_cameraId)
        .listen((CameraImageData imageData) {
      onAvailable(CameraImage.fromPlatformInterface(imageData));
    });
    value = value.copyWith(isStreamingImages: true);
  } on PlatformException catch (e) {
    throw CameraException(e.code, e.message);
  }
}