startCamera method

  1. @override
Future<int?> startCamera({
  1. double quality = 1.0,
  2. String facing = 'back',
  3. String aspectRatio = '3:4',
})
override

Starts the camera with the specified configuration.

Implementation

@override
Future<int?> startCamera({
  double quality = 1.0,
  String facing = 'back',
  String aspectRatio = '3:4',
}) async {
  _quality = quality;
  _aspectRatio = aspectRatio;
  _facing = facing == 'front' ? 'user' : 'environment';

  await _stopCurrentStream();

  // Build MediaStreamConstraints
  final videoConstraints = {
    'facingMode': _facing,
    'aspectRatio': _parseAspectRatioDouble(aspectRatio),
  }.jsify();

  final constraints = web.MediaStreamConstraints(
    video: videoConstraints as JSAny,
    audio: false.toJS,
  );

  try {
    _mediaStream = await web.window.navigator.mediaDevices
        .getUserMedia(constraints)
        .toDart;
  } catch (e) {
    debugPrint('flutter_crop_camera_web: getUserMedia error: $e');
    return null;
  }

  // Build a video element and attach the stream
  final video = web.HTMLVideoElement();
  video.autoplay = true;
  video.muted = true;
  video.srcObject = _mediaStream;
  video.style.width = '100%';
  video.style.height = '100%';
  video.style.objectFit = 'cover';
  _videoElement = video;

  // Register the platform view so CameraPreview can embed it
  const viewType = 'flutter_crop_camera_web_view';
  ui_web.platformViewRegistry.registerViewFactory(viewType, (int id) => video);

  // Texture id is unused on web; return 0 as sentinel
  return 0;
}