piradoiv/xojo-blurhash

Encoding image

Closed this issue · 7 comments

LwRed commented

Hello, and thanks for sharing this.
Don't understand how work the encode image window. Can you help ?

Sure @LwRed, what are you trying to create?, do you have any project or something that I can take a look?

LwRed commented

Just want to try to encode an image but don’t understand how the encoder works. No drag, no load…

You're right, I've completely forgot to add some documentation into the README 😅

In order to encode an image, you'll need to create an instance of the encoder first, and then pass a Picture object to it:

Var encoder As New BlurHash.Encoder
Var hash As String = encoder.Encode(image) ' Image should be a Picture class

For displaying images from a blur hash, you can drop the BlurHashCanvas component directly into a Window, and set its "Hash" property to the hash you've generated in the previous example.

bhCanvas.Hash = hash

If you open the example project that comes with the repository, you can try to encode images using the menu "File" > "Encode Image" and drop a picture on the image well that you will see.

Check the example also to see how to use the BlurHashThread that comes with the library, so your app doesn't freezes while encoding pictures.

Hope this helps, let me know if you need to clarify anything! =)

LwRed commented

Hello, thank you for your reply.
In your example, just add File Type Group to match with :
Me.AcceptFileDrop("image/jpeg")
Me.AcceptFileDrop("image/png")
Or replace the 2 lines with this
Me.AcceptFileDrop("????")

LwRed commented

Sure @LwRed, what are you trying to create?, do you have any project or something that I can take a look?

I have 2 apps to convert images and i want to hash encode images in a custom list box to show miniatures...

I prefer to specify the types instead of using "????", or you will end up accepting the drop of non-image files. But if you want to go that way so you don't have to add a lot of mime types, adding a Try/Catch block can be perfectly enough.

Once you are receiving pictures into your DropObject event, you can encode the files you're receiving with this snippet of code:

Var image As Picture
If obj.PictureAvailable Then
  image = obj.Picture
Elseif obj.FolderItemAvailable Then
  image = Picture.Open(obj.FolderItem)
End If
If image = Nil Then Return

Var encoder As New BlurHash.Encoder
Var hash As String = encoder.Encode(image)
' This hash string can be used later with the
' decoder, to convert it into a picture

But please notice this is not meant to be used as a miniature, but to show "something similar" while the final image loads. You can test the resultant hash in the official page and read about more use cases: https://blurha.sh

LwRed commented

ok, thanks a lot