Java Question Write JavaFX image to array of bytes (and vice versa)

moster67

Expert
Licensed User
Longtime User
Anyone who knows how to write a JavaFX image to an array of bytes (and vice versa)?
If possible, I don't want to load from or write to disk. I want to use the Image in memory (like Bitmap in B4A).

There are examples online but Image in JavaFX is not the same as in normal Java so it has to be treated differently (I think).

Perhaps BA already has a method for doing this?

Any help would be appreciated. Thanks.
 

moster67

Expert
Licensed User
Longtime User
Hmm...it should be a simple task but ....

I tried this:
B4X:
private byte[] convertToBytes(Object object) throws IOException {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutput out = new ObjectOutputStream(bos)) {
            out.writeObject(object);
            return bos.toByteArray();
        }
    }

but it throws en exception error: java.io.NotSerializableException: javafx.scene.image.Image

Apparently javafx.scene.image.Image is not Serializable...
 

JordiCP

Expert
Licensed User
Longtime User
Just guessing...according to THIS , shouldn't it be?
B4X:
PixelReader pxr = myImage.getPixelReader();
pxr.getPixels(...)
 

moster67

Expert
Licensed User
Longtime User
Thanks. I tried your idea by implementing a class similar to the one found in this post on SO. It permitted me to serialize the image but apparently some information was being lost (related to the type of the image) because then TensorFlow gave an error since it was not recognized as a valid image-format (jpg, png, gif).

However I managed to resolve it by using code posted by @OliverA here on this forum (of all places :) ) in this post:
https://www.b4x.com/android/forum/t...ay-with-resize-quality-options.91746/#content
Just what I needed. My thanks to @OliverA for this useful code!

I had already tried similar code using SwingFXUtils.fromFXImage and BufferedImage but did not get it to work.
 

moster67

Expert
Licensed User
Longtime User
Use BitmapCreator.
Not sure what you mean? That is a library made for B4X, right?
I needed java code to use within one of my java classes in a wrapper. Don't understand how I could use it within my wrapper or can I call its method, using composition, in my java-code?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I needed java code to use within one of my java classes in a wrapper. Don't understand how I could use it within my wrapper or can I call its method, using composition, in my java-code?
I don't have the complete context so my answer may not be relevant. However as a general rule the library code shouldn't do things that the developer can easily do in the B4X code. It is very simple to convert an image to an array of bytes and vice versa with BitmapCreator.
 

moster67

Expert
Licensed User
Longtime User
I agree with you. I guess I could change the exposed method to take a byte array instead of an Image-object.
I will review the code. Thanks for the idea.
 
Top