Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get blurhash from image path #8

Closed
csbence10 opened this issue Aug 27, 2023 · 1 comment
Closed

Get blurhash from image path #8

csbence10 opened this issue Aug 27, 2023 · 1 comment

Comments

@csbence10
Copy link

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!

@drxddy
Copy link
Member

drxddy commented Aug 27, 2023

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

@drxddy drxddy closed this as completed Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants