Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching Camera Aspect Ratio in Flutter

How do we switch the camera preview aspect ratio in Flutter?

I would like to switch be able to switch from 1:1, 4:3, and 16:9 aspect ratio

I am using this Camera plugin but it doesn't seem to have a functionality to do so.

Any thoughts please?

like image 521
JayVDiyk Avatar asked Oct 23 '25 11:10

JayVDiyk


1 Answers

The camera output video stream is at a resolution, such as 1280x720 or 640x480, and the aspect is 16:9 or 4:3. When you want to use an aspect ratio is 1:1, firstly you need to choose a resolution. If you choose 640x480, you need to clip the video to 480x480 by yourself. The number of resolutions hardware camera support is very limited, and the aspect is also.

If you only need the target aspect ratio to render on the screen, you can try the code like this:

    return Center(child: ClipRRect(
      child: SizedOverflowBox(
        size: const Size(300, 300), // aspect is 1:1
        alignment: Alignment.center,
        child: CameraPreview(controller),
      ),
    ));
  }

If you need to take video from the camera, you need to use a library such as FFmpeg to modify the video file.

like image 60
wjploop Avatar answered Oct 25 '25 03:10

wjploop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!