如何在 Flutter MultiImagePicker 中指定照片的质量?

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

我需要上传一些照片并将这些照片发送到数据库。由于照片质量高,完成和上传每张照片需要相当长的时间。我不需要非常高质量的照片,所以我需要压缩照片。如果我使用 class Flutter MultiImagePicker,最好的解决方案是什么?

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

您的包已经提出了几个压缩 Asset 对象的选项。

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
}

编辑

我认为这可以保持您的纵横比:

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,
);

如果想不做任何操作直接使用原来的宽高 使用

getByteData(quality: 80)

改为

getThumbByteData(quality: 80)