問題描述
如何在 Flutter MultiImagePicker 中指定照片的質量? (How can i specify the quality of the photo in Flutter MultiImagePicker?)
我需要上傳一些照片並將這些照片發送到數據庫。由於照片質量很高,完成和上傳每張照片需要相當長的時間。我不需要非常高質量的照片,所以我需要壓縮照片。如果我使用 Flutter MultiImagePicker 類,最好的解決方案是什麼?
List<Asset> pickedImagesList = await MultiImagePicker.pickImages(maxImages: 25, enableCamera: false);
參考解法
方法 1:
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,
);
方法 2:
If you want to use the original width and height without any manipulate use
getByteData(quality: 80)
instead
getThumbByteData(quality: 80)
(by Sergei Eensalu、Guillaume Roux、Abdalla)