Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image size taken from Flutter Image_Picker plugin is way too big

I want to use auto-focus on the camera, which is available on the image_picker plugin. However, when I call:

var bytes = new File(imagePath);
var enc = await bytes.readAsBytes();
print(enc.length);

I got: 5121126

which takes at least 10 seconds when I want to encode into json to send to an API server:

var body = json.encode({
      'image' : enc
})

In contrast, with the camera plugin, my byte array is only 420685, which is 10 times smaller, but it doesn't have the auto-focus feature.

Can I get some advice on how to reduce the size of the byte array from image_picker? Thank you.

like image 909
Dai Dao Avatar asked May 21 '18 17:05

Dai Dao


People also ask

How do I reduce the size of the image in flutter?

Image Picker As others have suggested, you can use the built in imaqeQuality property from ImagePicker to compress the image. This property takes a value between 0 and 100 and represents a percentage of the original image quality.

How do you change the width and height of an image in flutter?

To set specific width for Image widget in Flutter Application, set width property of Image object with required double value.


2 Answers

The camera plugin has 3 default resolutions, and you are probably selecting or defaulting to a lower resolution (than the hardware's full resolution).

The image_picker plugin doesn't have these presets, but does have some optional arguments on the pickImage method (maxWidth and maxHeight). Experiment setting one or both of these to VGA type resolutions (640, 480, even 800, 600) to see if this reduces the size of the captured image.

There's also a package called image which would allow you to post-process the image. The sample on the main page does a resize, maintaining aspect ratio.

like image 172
Richard Heap Avatar answered Oct 29 '22 10:10

Richard Heap


Adding small values for the maxHeight and maxWidth parameter in the ImagePicker plugin compresses the size. Here is an example:

var fileFromCamera = await ImagePicker.pickImage(source: ImageSource.camera, maxHeight: 480, maxWidth: 640);

like image 32
Idee Avatar answered Oct 29 '22 09:10

Idee