folksable / blurhash_ffi

a port of blurhash to flutter written in c via dart:ffi
https://pub.dev/packages/blurhash_ffi
BSD 3-Clause "New" or "Revised" License
41 stars 6 forks source link

Get blurhash from image path #8

Closed csbence10 closed 1 year ago

csbence10 commented 1 year ago

Hello,

I wanted to ask if it's feasible to obtain the blurhash string from an image path. In our app, the client calculates the hash before uploading a new profile picture. Currently, we're utilizing the blurhash_dart package for both encoding and decoding. I'd be open to switching to this package if it offers better performance.

In any case, great work!

dhikshithrm commented 1 year ago

Hello @csbence10,

Yes, it is possible to obtain blurhash string from various sources including asset images, all you need to encode is an instance of an ImageProvider (no image package is required), as ImageProvider is one of the core classes of flutter, you can obtain one using any of AssetBundleImageProvider, FileImage, MemoryImage, NetworkImage, ResizeImage, ScrollAwareImageProvider.

In your case, I'm assuming you have a File object of an image, in that case, FileImage as the ImageProvder should work fine, or if you are working with a Local Image, you can use an AssetImage which implements ImageProvider.

here is how you can encode with any ImageProvider instance:

import 'package:blurhash_ffi/blurhash_ffi.dart';

/// Encoding a blurhash from an image provider
///
/// You can use any ImageProvider you want, including NetworkImage, FileImage, MemoryImage, AssetImage, etc.
final imageProvider = NetworkImage('https://picsum.photos/512');
final imageProvider2 = AssetImage('assets/image.jpg');

/// Signature
/// static Future<String> encode(
///   ImageProvider imageProvider, {
///   int componentX = 4,
///   int componentY = 3,
/// })
/// may throw `BlurhashFFIException` if encoding fails.
final String blurHash = await BlurhashFFI.encode(imageProvider);

I also used blurhash_dart previously and found that it was freezing the UI, and using it in a separate isolate also didn't help with performance, that's why I wrote this package, It solved the problem in my app (fast and not freezing UI thread), it is visibly more performant compared to blurhash_dart, but i did not write a performance test #7 to compare both.

Cheers, Have a Good Day