setDisplayOrientation method

Future<void> setDisplayOrientation(
  1. int degrees
)

Set the clockwise rotation of preview display in degrees.

This affects the preview frames and the picture displayed after snapshot. This method is useful for portrait mode applications. Note that preview display of front-facing cameras is flipped horizontally before the rotation, that is, the image is reflected along the central vertical axis of the camera sensor. So the users can see themselves as looking into a mirror.

This does not affect the order of byte array passed in PreviewCallback, JPEG pictures, or recorded videos. This method is not allowed to be called during preview.

If you want to make the camera image show in the same orientation as the display, you can use the following code.

Future<void> setCameraDisplayOrientation(
  Camera camera,
  CameraInfo cameraInfo,
  Orientation orientation,
) {
  late final int angle;
  switch (orientation) {
    case Orientation.portrait:
      angle = 0;
      break;
    case Orientation.landscape:
      angle = 270;
      break;
  }

  late int displayOrientation;
  if (cameraInfo.facing == CameraInfo.cameraFacingFront) {
    displayOrientation = (cameraInfo.orientation + angle) % 360;
    displayOrientation = (360 - displayOrientation) % 360;
  } else {
    displayOrientation = (cameraInfo.orientation - angle + 360) % 360;
  }

  return camera.setDisplayOrientation(displayOrientation);
}

Note: Before API level 24, the default value for orientation is 0. Starting in API level 24, the default orientation will be such that applications in forced-landscape mode will have correct preview orientation, which may be either a default of 0 or 180. Applications that operate in portrait mode or allow for changing orientation must still call this method after each orientation change to ensure correct preview display in all cases.

degrees: the angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270.

Throws PlatformException if setting orientation fails; usually this would be because of a hardware or other low-level error, or because release has been called on this Camera instance.

Implementation

Future<void> setDisplayOrientation(int degrees) {
  return _channel.$setDisplayOrientation(this, degrees);
}