We can create FX Image object by using
byte [] bytes = ------; //valid image in bytes
javafx.scene.image.Image image = new Image(new ByteArrayInputStream(bytes));
And this can be set to an ImageView.
I need the opposite without converting it first to the BufferedImage (SwingFXUtils.fromFXImage(image,null)).
So that I can directly write the bytes to a file.
I've tried the following:
PixelReader pixelReader = image.getPixelReader();
int width = (int)image.getWidth();
int height = (int)image.getHeight();
byte[] buffer = new byte[width * height * 4];
pixelReader.getPixels(
0,
0,
width,
height,
PixelFormat.getByteBgraInstance(),
buffer,
0,
width * 4
);
But the file generated by writing the byte [] buffer is not a valid image.
Any insights on this?
EDIT: The solutions given at How to get byte[] from javafx imageView cannot be applied to my question. As I've already mentioned clearly that I do not want to use converting it to BufferedImage using SwingFXUtils. Moreover, I want to convert it to the byte array so that it can be written to an image file.