Home > OS >  Image Compression in flutter of a Image File
Image Compression in flutter of a Image File

Time:03-07

Future<void> saveLocalStorage(File image) async {

// get file format of the image
String fileFormat = image.path.split('.').last; // jpg

// get path for applications directory
final directory = await getApplicationDocumentsDirectory();

// January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch.
int time = DateTime.now().millisecondsSinceEpoch;

// saving image into applications document directory
image.copy('${directory.path}/$time.$fileFormat');
}

This is the code that I am currently using to save an image File. But before saving, I wish to compress the Image. I found a solution using the image package but its documentation itself says that it isn't fast enough so I don't prefer using it. Another solution was using the image_picker whose example code is:

ImagePicker imagePicker = ImagePicker();
PickedFile compressedImage = await imagePicker.getImage(
  source: ImageSource.camera,
  imageQuality: 85,
);

I tried using the same example but the source attribute doesn't File as its value so I am unable to do the compression. Please suggest some solution to get this done.

CodePudding user response:

Use this library : Flutter Image Compress

Compresses image as native plugin (Obj-C/Kotlin)

or unknown reasons, image compression in Dart language is not efficient, even in release version.

CodePudding user response:

Thanks, @Narendra_Nath, ur answer was quite useful.
Later, I came across this solution, Flutter the best way to compress image it explained 3 packages, one of which was the flutter_image_compress package, it was suitable for my case but I preferred using the flutter_native_image package as it had easy to understand syntax.
The flutter_image_compress package has a required attribute i.e. output path which was to be determined forehand, it felt quite overwhelming for me so, I used the flutter_native_image package.
Check the solution mentioned earlier in this answer to know more about those 3 packages.

  • Related