native_image_compress 0.1.0
native_image_compress: ^0.1.0 copied to clipboard
Compress images to JPEG, PNG, WebP, HEIC, or AVIF using the platform's native encoders. Android only for now.
native_image_compress #
Compress images to JPEG, PNG, WebP, HEIC, or AVIF using the platform's native encoders — no Dart-side image decoding, no bundled codec binaries.
Platform support #
| Android | iOS |
|---|---|
| ✅ (minSdk 28) | ❌ not yet |
AVIF and HEIC encoding depend on the device's hardware/software codecs via
androidx.heifwriter.
Availability can vary by device; if the OS can't produce the requested format,
the returned future fails with a PlatformException.
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)
}
}
Usage #
import 'package:native_image_compress/image_compress.dart';
final png = await compressToPng(originalBytes);
final jpeg = await compressToJpeg(originalBytes, quality: 80);
final webp = await compressToWebp(originalBytes, quality: 75);
final heic = await compressToHeic(originalBytes, quality: 70);
final avif = await compressToAvif(originalBytes, quality: 55);
Each function takes the raw bytes of an already-decoded image (e.g. from
image_picker or File.readAsBytes()) and returns the re-encoded bytes.
quality accepts values from 0 to 100; passing anything outside that range
throws an ArgumentError. PNG is lossless and has no quality parameter.
Error handling #
try {
final compressed = await compressToJpeg(originalBytes);
} on PlatformException catch (e) {
// e.code: DECODE_FAILED, COMPRESSION_FAILED, NULL_RESULT
}
See example/ for a full sample app that picks an image and compresses it to
each supported format.
License #
MIT, see LICENSE.