Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter the best way to compress image

Tags:

I'm try to compress image before upload to the storage using flutter image_picker package. I found three ways to do it, but I'm not sure which way is the best.

  1. using image_picker package's imageQuality params

image_picker

  1. using flutter_image_compress package

flutter_image_compress

  1. using flutter_native_image package

flutter_native_image

is there any difference between these options?

Appreciate any helps and explanation.

like image 908
user12208004 Avatar asked Nov 11 '19 11:11

user12208004


People also ask

How do I compress images in flutter?

Use image_picker package: You can use the built-in imageQuality property of ImagePicker to compress the image. This property takes a value between 0 and 100 and represents a percentage of the quality of the original image. First, add image_picker as a dependency in your pubspec. yaml file.

How do I compress a PNG file in flutter?

dart'; //if the origin image is PNG, it doesn't work var compressImageData = await FlutterImageCompress. compressWithFile( //returns Future<List<int>> image_path, minWidth: 50, minHeight: 50, quality: 100, format: CompressFormat. png //e.g. compress to PNG );

How do I compress a PDF in flutter?

You can use any file compression method, not just those specific to PDF. You could use the archive package, which has multiple compression options. Show activity on this post.


1 Answers

Here's a breakdown of the potential solutions. The TLDR is that the flutter_image_compress package shrinks files much more than the ImagePicker class can itself.

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. The benefit to this approach is that it's built into the image_picker package and is therefore incredibly easy to use.

  File _image;    Future getImage() async {     var image = await ImagePicker.pickImage(         source: ImageSource.gallery,                   imageQuality: 25,     );      setState(() {       _image = image;     });   } 

I did a little test to see how much various values effect the size of the image:

100% Image Quality - 4.58MB enter image description here

50% Image Quality - 3.58MB enter image description here

25% Image Quality - 2.61MB enter image description here

1% Image Quality - 2.12MB enter image description here

0% Image Quality - 3.9MB enter image description here

In summary, you can reduce the file size of an image by more than half without much of a visible difference. Trying to force the image quality to 0% doesn't actually shrink the file size.

flutter_image_compress
The flutter_image_compress package is fairly simple to use and it appears to be much better at actually reducing the file size.

Future<File> compressFile(File file) async {   final filePath = file.absolute.path;    // Create output file path   // eg:- "Volume/VM/abcd_out.jpeg"   final lastIndex = filePath.lastIndexOf(new RegExp(r'.jp'));   final splitted = filePath.substring(0, (lastIndex));   final outPath = "${splitted}_out${filePath.substring(lastIndex)}";   var result = await FlutterImageCompress.compressAndGetFile(     file.absolute.path, outPath,     quality: 5,   );    print(file.lengthSync());   print(result.lengthSync());    return result;  } 

I'll omit the images here because the all look the same:
50% Image Quality - 590KB
25% Image Quality - 276KB
5% Image Quality - 211KB

flutter_native_image The flutter_native_image package is comparable to the flutter_image_compress package as far as file sizes go.

Future<File> compressFile(File file) async{     File compressedFile = await FlutterNativeImage.compressImage(file.path,         quality: 5,);     return compressedFile;   } 

50% Image Quality - 1.02MB
25% Image Quality - 309KB
5% Image Quality - 204KB

I guess one of the benefits to this method is that you don't need to provide it with an output pathway for the compressed file.

like image 53
Joe Muller Avatar answered Sep 24 '22 13:09

Joe Muller