如何在 Flutter MultiImagePicker 中指定照片的質量? (How can i specify the quality of the photo in Flutter MultiImagePicker?)


問題描述

如何在 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 EensaluGuillaume RouxAbdalla)

參考文件

  1. How can i specify the quality of the photo in Flutter MultiImagePicker? (CC BY‑SA 2.5/3.0/4.0)

#imagepicker #frontend #Flutter #image






相關問題

如何在 Flutter MultiImagePicker 中指定照片的質量? (How can i specify the quality of the photo in Flutter MultiImagePicker?)

在發布 apk(Flutter) 中面臨圖像選擇器的問題 (Facing trouble with image picker in release apk(Flutter))

如何在 SwiftUI 中對 Imagepicker 進行兩次單獨調用? (How do I make two separate calls to an Imagepicker in SwiftUI?)

錯誤:不支持的操作:使用 image_picker 時的 _Namespace (Error: Unsupported operation: _Namespace when using image_picker flutter web)

如何使用 Swift/SwiftUI 從 ImagePicker 獲取選定的圖像文件名 (How to get selected image file name from ImagePicker using Swift/SwiftUI)

如何在顫動中從 Firebase 存儲中獲取下載 URL (How to get Download URL from Firebase Storage in flutter)

嘗試打開相機時顫動 image_picker 提供程序錯誤 (Flutter image_picker provider error when trying to open camera)

每當活動被殺死或導航到新活動時,圖像就會消失 (Whenever the activity gets killed or navigated to new activity the image disappears)

Ionic 5 Image Picker 在 iOS 模擬器中崩潰 (Ionic 5 Image Picker crashes in iOS Simulator)

如何更新 csv 文件以記錄使用 toggleClass jQuery 函數實現的用戶選擇的圖像? (how do I update a csv file to record user selected images implemented with the toggleClass jQuery function?)







留言討論