Search code examples
imageflutterfrontendimagepicker

How can i specify the quality of the photo in Flutter MultiImagePicker?


I need to upload an amount of photos and send these photos to the database. Due to the high quality of the photo, it takes a fair amount of time to complete and upload each photo. I don't need a very high quality photo so I need to compress the photo. What the best solution to do that if i use the class Flutter MultiImagePicker ?

List<Asset> pickedImagesList = await MultiImagePicker.pickImages(maxImages: 25, enableCamera: false);

Solution

  • Your package already propose a few options to compress an Asset object.

    List<Asset> pickedImagesList = await MultiImagePicker.pickImages(maxImages: 25, enableCamera: false);
    for (Asset asset in pickedImagesList) {
       ByteData assetData = await asset.getThumbByteData(
          width: // desired width,
          height: // desired height,
          quality: //desired quality,
       );
       // Send assetData to your database
    }
    

    EDIT

    I think this could work to keep your aspect ratio:

    double getAspectRatio(double originalSize, double desiredSize) => desiredSize / originalSize;
    
    final aspectRatio = getAspectRatio(asset.originalWidth, imageDesiredWidth);
    ByteData assetData = await asset.getThumbByteData(
       width: (asset.originalWidth * aspectRatio).round(),
       height: (asset.originalHeight * aspectRatio).round(),
       quality: //desired quality,
    );