How do I set the resolution in Flutter's Camera package

Viewed 2263

I'm working on a flutter app that is using the Camera package's camera preview.

I'm initializing the camera controller like so:

final controller = CameraController(
  _cameras[0],
  ResolutionPreset.ultraHigh,
  enableAudio: false,
  imageFormatGroup: ImageFormatGroup.bgra8888,
);

The ResolutionPreset enum has these options:

enum ResolutionPreset {
  /// 352x288 on iOS, 240p (320x240) on Android
  low,

  /// 480p (640x480 on iOS, 720x480 on Android)
  medium,

  /// 720p (1280x720)
  high,

  /// 1080p (1920x1080)
  veryHigh,

  /// 2160p (3840x2160)
  ultraHigh,

  /// The highest resolution available.
  max,
}

The resolution needs to be 4032x2034. How do I use a custom resolution?

1 Answers

ResolutionPreset enum doesn't provide the option to set a custom resolution. As a workaround, you can use ResolutionPreset.max then resize the image. Using the image package, you can resize the image to the desired width and height and write it on storage.

// Read a jpeg image from file path
Image image = decodeImage(File('image path').readAsBytesSync());

// Resize the image
Image resizedImage = copyResize(image, width: 4032, height: 2034);

// Save the image as jpg
File('out/image-resized.jpg')
    ..writeAsBytesSync(encodeJpg(resizedImage, quality: 100));
Related