native_image_compress 0.2.2
native_image_compress: ^0.2.2 copied to clipboard
Compress images to JPEG, PNG, WebP, HEIC, or AVIF using the platform's native encoders.
native_image_compress #
Compress images to JPEG, PNG, WebP, HEIC, or AVIF using the platform's native encoders.
Platform support #
| Android | iOS | |
|---|---|---|
| Support | SDK 28+ | ❌ |
| JPEG | ✅ | ❌ |
| PNG | ✅ | ❌ |
| WebP | ✅ | ❌ |
| HEIC | ✅ | ❌ |
| AVIF | ⚠️ | ❌ |
Installation #
flutter pub add native_image_compress
Android #
This plugin requires minSdk 28. If your app's minSdkVersion is lower, raise it in android/app/build.gradle.kts:
android {
defaultConfig {
minSdk = maxOf(flutter.minSdkVersion, 28)
}
}
AVIF support #
AVIF encoding requires the device to have an AV1 hardware or software encoder registered
with MediaCodec. This is not guaranteed on all SDK 28+ devices (even recent ones) — if
none is available, compressToAvif throws a PlatformException with code
avifNotSupported instead of attempting the encode.
Usage #
import 'package:native_image_compress/image_compress.dart';
final jpeg = await compressToJpeg(originalBytes, quality: 80);
final png = await compressToPng(originalBytes);
final webp = await compressToWebp(originalBytes, quality: 75);
final heic = await compressToHeic(originalBytes, quality: 70);
final avif = await compressToAvif(originalBytes, quality: 55);
Crop and resize #
Every compressTo* function accepts optional cropOptions and resizeOptions:
final result = await compressToJpeg(
originalBytes,
quality: 80,
cropOptions: CropOptions(x: 0, y: 0, width: 800, height: 600),
resizeOptions: ResizeOptions(maxWidth: 1080, maxHeight: 1080),
);
cropOptionscuts out the given rectangle (in original-image pixel coordinates) before encoding.resizeOptionsscales the (cropped) image down to fit withinmaxWidthxmaxHeight, preserving aspect ratio. Images already smaller than the target are left untouched.
Errors #
Invalid cropOptions, resizeOptions, or quality values throw an ArgumentError
immediately, before any native work happens. Failures on the platform side surface as a
PlatformException:
| Code | Cause |
|---|---|
decodeFailed |
Input bytes aren't a decodable image. |
invalidCrop |
cropOptions doesn't fit within the actual image dimensions. |
avifNotSupported |
No AV1 encoder available on this device (AVIF only). |