Skip to content

A simple native image compression library for Flutter that supports Windows and Linux as well

License

Notifications You must be signed in to change notification settings

heiha100/simple_native_image_compress

 
 

Repository files navigation

simple_native_image_compress

A simple native image compression library for Flutter written in rust using flutter_rust_bridge, image, cargokit and kamadak-exif

Why?

  1. For some reason, image compression in Dart is slow. Even with isolate.
  2. There is no native libraries that supports WINDOWS & LINUX when it comes to image compression.

What does it do?

  • If path for an image file is given, it will resize and return Jpeg/WebP image as Uint8List.

Prerequisite

  1. Rust
  2. Android NDK for Android

Supported Output Formats

  • Jpeg
  • WebP

Sampling Filter Types

  • 5 types of sampling filters are available and can be selected

    1. Nearest, // Nearest Neighbor
    2. Triangle, // Linear Filter (DEFAULT)
    3. CatmullRom, // Cubic Filter
    4. Gaussian, // Gaussian Filter
    5. Lanczos3, // Lanczos with window 3
  • when samplingFilter is null, FilterType.Triangle is set to be default.

  • you can read more about sampling filters here image crate doc

Example

Call Library as a Singleton

final compress = SimpleNativeImageCompress();

"contain" will make the image fit into the given max width/height.

try{
  final bytes = await compress.contain(
    filePath: yourFilePath,
    compressFormat: CompressFormat.Jpeg,
    quality: 90,
    maxWidth: 512,
    maxHeight: 512,
    samplingFilter: FilterType.Lanczos3
  );
} catch (e) {
  print(e);
}

"fitWidth" will make the image fit into the given max width.

try{
  final bytes = await compress.fitWidth(
    filePath: yourFilePath,
    compressFormat: CompressFormat.WebP,
    maxWidth: 512,
    samplingFilter: FilterType.Lanczos3
  );
} catch (e) {
  print(e);
}

"fitHeight" will make the image fit into the given max height.

try{
  final bytes = await compress.fitHeight(
    filePath: yourFilePath,
    compressFormat: CompressFormat.WebP,
    maxHeight: 512,
    samplingFilter: FilterType.Lanczos3
  );
} catch (e) {
  print(e);
}

Default values

  • Default value for width and/or height is 1024 px
  • Default value for Jpeg quality is 80 (For webP Quality does nothing)

About

A simple native image compression library for Flutter that supports Windows and Linux as well

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 53.1%
  • CMake 13.2%
  • C++ 12.2%
  • Rust 9.2%
  • C 3.7%
  • Ruby 3.4%
  • Other 5.2%