'Image compression takes too long on Flutter Web
I tried to compress web image using the below code (with image_compression_flutter package), however it takes about 1 minute to compress a 6 mb image.
Future<Uint8List?> compressImage(Uint8List imgBytes,
{required String path, int quality = 70}) async {
final input = ImageFile(
rawBytes: imgBytes,
filePath: path,
);
Configuration config = Configuration(
outputType: ImageOutputType.jpg,
// can only be true for Android and iOS while using ImageOutputType.jpg or ImageOutputType.pngÏ
useJpgPngNativeCompressor: false,
// set quality between 0-100
quality: quality,
);
final param = ImageFileConfiguration(input: input, config: config);
final output = await compressor.compress(param);
return output.rawBytes;
}
Is it because of web limitations it takes such a long time to compress? Is there any workaround regarding this issue
Solution 1:[1]
Eventually, I found a solution for compressing web image. Image picker now has XFile type which can be directly compressed in web. It took about 2 seconds to compress a 12mb image to 50kb
final XFile? pickedImageFile = await ImagePicker().pickImage(
source: isCamera ? ImageSource.camera : ImageSource.gallery,
imageQuality: imageQuality,
maxWidth: maxWidth,
preferredCameraDevice: CameraDevice.rear);
While uploading to a server there is no need to convert XFile to any type. Just need to read bytes from pickedImageFile
await pickedImageFile.readAsBytes();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Behzod Faiziev |
