lockCaptureOrientation method

  1. @override
Future<void> lockCaptureOrientation(
  1. int cameraId,
  2. DeviceOrientation orientation
)

Locks the capture orientation.

Implementation

@override
Future<void> lockCaptureOrientation(
  int cameraId,
  DeviceOrientation orientation,
) async {
  try {
    final web.ScreenOrientation screenOrientation = window.screen.orientation;
    final web.Element? documentElement = window.document.documentElement;

    if (documentElement != null) {
      final String orientationType =
          _cameraService.mapDeviceOrientationToOrientationType(orientation);

      // Full-screen mode may be required to modify the device orientation.
      // See: https://w3c.github.io/screen-orientation/#interaction-with-fullscreen-api
      // Recent versions of Dart changed requestFullscreen to return a Future instead of void.
      // This wrapper allows use of both the old and new APIs.
      dynamic fullScreen() => documentElement.requestFullScreenTweak();
      await fullScreen();
      await screenOrientation.lock(orientationType).toDart;
    } else {
      throw PlatformException(
        code: CameraErrorCode.orientationNotSupported.toString(),
        message: 'Orientation is not supported in the current browser.',
      );
    }
  } on web.DOMException catch (e) {
    throw PlatformException(code: e.name, message: e.message);
  }
}