blurhash_ffi

A Blurhash encoder and decoder FFI implementation for flutter, Supports Android, iOS, Linux, macOS and Windows.

Getting Started

This project is a starting point for a Flutter FFI plugin, a specialized package that includes native code directly invoked with Dart FFI.

Usage

To use this plugin, add blurhash_ffi as a dependency in your pubspec.yaml file

Example

/// Encoding Image to BlurHash String
/// 
/// Steps: 
///     1. get an Instance of [BlurHashImageInfo], there are a couple of 
///     ways to get that which inclues information necessary for the
///     second step of encoding.
///     2. pass the Instance of [BlurHashImageInfo] to the [encodeBlurHash] 


/// from Asset Images
final BlurHashImageInfo info = await getBlurHashImageInfoFromAsset(assetName);

/// for Images from any ImageProvider
final BlurHashImageInfo info = await getImageInfoFromImageProvider(ImageProvider imageProvider);


final Future<String> blurhash = encodeBlurHash(info);


/// Decoding BlurHash String to Image
///
/// [blurHashDecodeImage] 
/// Parameters:
///  *`hash`      - String  - blurhash String value,
///  *`width`     - int     - width of the output Image (it may stretch when the aspect ratio deviates from the original image)
///  * `height`  - int     - height of the output Image(similarly to width output image may stretch or distort when the aspect ratio deviates from the original image)

Future<ui.Image> blurHashDecodeImage(
    String hash, int width, int height, int punch); 

check the example for more details

Project structure

This template uses the following structure:

  • src: Contains the native source code, and a CmakeFile.txt file for building that source code into a dynamic library.

  • lib: Contains the Dart code that defines the API of the plugin, and which calls into the native code using dart:ffi.

  • platform folders (android, ios, windows, etc.): Contains the build files for building and bundling the native code library with the platform application.

Building and bundling native code

The pubspec.yaml specifies FFI plugins as follows:

  plugin:
    platforms:
      some_platform:
        ffiPlugin: true

This configuration invokes the native build for the various target platforms and bundles the binaries in Flutter applications using these FFI plugins.

This can be combined with dartPluginClass, such as when FFI is used for the implementation of one platform in a federated plugin:

  plugin:
    implements: some_other_plugin
    platforms:
      some_platform:
        dartPluginClass: SomeClass
        ffiPlugin: true

A plugin can have both FFI and method channels:

  plugin:
    platforms:
      some_platform:
        pluginClass: SomeName
        ffiPlugin: true

The native build systems that are invoked by FFI (and method channel) plugins are:

  • For Android: Gradle, which invokes the Android NDK for native builds.
    • See the documentation in android/build.gradle.
  • For iOS and MacOS: Xcode, via CocoaPods.
    • See the documentation in ios/blurhash_ffi.podspec.
    • See the documentation in macos/blurhash_ffi.podspec.
  • For Linux and Windows: CMake.
    • See the documentation in linux/CMakeLists.txt.
    • See the documentation in windows/CMakeLists.txt.

Binding to native code

To use the native code, bindings in Dart are needed. To avoid writing these by hand, they are generated from the header file (src/blurhash_ffi.h) by package:ffigen. Regenerate the bindings by running flutter pub run ffigen --config ffigen.yaml.

Flutter help

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.