start method

Future<Stream<T?>> start({
  1. int maxWidth = 0,
})

Start streaming images. If maxWidth is specified, the image will be scaled down while maintaining the aspect ratio.

Implementation

Future<Stream<T?>> start({int maxWidth = 0}) async {
  bool flg = false;
  await stop();
  _stream = StreamController<T?>();
  const angles = {
    DeviceOrientation.portraitUp: 0,
    DeviceOrientation.landscapeLeft: 90,
    DeviceOrientation.portraitDown: 180,
    DeviceOrientation.landscapeRight: 270,
  };
  await camera?.startImageStream((CameraImage image) {
    if (flg || _worker == null) return;
    flg = true;
    final plane = image.planes.first;
    final int orientation;
    if (Platform.isIOS) {
      orientation = 0;
    } else {
      final angle = angles[camera!.value.deviceOrientation] ?? 0;
      orientation =
          (360 - camera!.description.sensorOrientation + angle) % 360;
    }
    _worker!.exec<T?>({
      'method': workerMethod,
      'bytesPerRow': plane.bytesPerRow,
      'height': image.height,
      'width': image.width,
      'buff': plane.bytes,
      'orientation': orientation,
      'maxWidth': maxWidth,
    }).then((res) {
      final st = _stream;
      if (st != null && !st.isClosed) st.sink.add(res);
      flg = false;
    });
  });
  return _stream!.stream;
}