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

3

10

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?

Greenbrier answered 9/8, 2021 at 18:59 Comment(3)
Why not use max available resolution? Not all devices will be able to handle your custom resolution, what happens to that? You should just use the maximum available for the device and scale/crop as needed to fit the resolution you need.Liver
Warning! you can currently set the resolution to high (720p), if you use flutter camera package. see github.com/flutter/flutter/issues/78247Caffeine
side note: when trying max resolution, beware of heating issuesBrunner
V
0

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));
Violone answered 19/7, 2022 at 15:22 Comment(1)
My question is in regard to CameraPreview, not loading filesGreenbrier
W
0

The only way you can do it is change cameraController value in runtime.

Let's start with constructing and initialization:

CameraController cameraController = CameraController(cameras[0], 
    ResolutionPreset.low, enableAudio: false); // the state field 

@override
void initState() {
   cameraController.initialize();
   // ...
}

After that you can change the dependency like that:

 _changeResolution() async {
    cameraController = CameraController( cameras[0], 
          ResolutionPreset.veryHigh, // new resolution
              enableAudio: false);
    await cameraController.initialize(); // initialize again
    setState(() {}); // changes UI
 }
Woolfell answered 31/7, 2023 at 12:26 Comment(1)
That didn't answer the question. I want a custom resolution not in ResolutionPresetGreenbrier
D
0

I think you must have already visited this Github issue, about Flutter's Camera package having custom resolution.

As the issue says, this option is yet not available in Camera package. But there is a CameraAwesome package which let's you do that.

Although, I suggest you don't. Even most inbuilt Camera apps in phones don't have such high resolution. Most have some specific resolution like 480p, 720p, 1080p, etc. I think this will be more than enough for most apps. Unless you are building your app for a very specific device with a high resolution camera specifically made for purpose which requires that high resolution.

Dikmen answered 21/6 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.