How to get byte array form javafx.scene.image.Image object?

This article will show you how to get byte array from Image object in JavaFX. I was creating a JavaFX application when I faced this problem. I created a Customer model which contain id, firstName, lastName, contact, address and picture:a JavaFX image object.

In order to make my code cleaner I created a class that handled all the customer related database operations. One method in the class is insert method which takes customer object and insert into the database.

So, my problem was that I now have only customer object to work with. Therefore I needed a way to get a byte array from an image object.

So I searched all over the internet. Heck, even asked a question in StackOverflow – here’s the link. But could not find a solution.

Following solution was created by combining bits and pieces that I found on the Internet and some help form the folks at the Stack Overflow.

Image image = customer.getImage();

BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null);
ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();

try {
    ImageIO.write(bufferedImage, "jpeg", byteArrayInputStream);
} catch (IOException e) {

byte[] buffer = byteArrayInputStream.toByteArray();

Leave a Reply

Your email address will not be published. Required fields are marked *