1

I have an ImageView that contains an image, what I need is to make a getImage() of the ImageView and convert it to an InputStream. This is the code that I have:

try {

    File fileImage = new File(ivImage.getImage());

    InputStream isImage = (InputStream) new FileInputStream(fileImage);

} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Is there a way to get the ImageView image and convert it to InputStream ?

Thanks

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Splanas00
  • 35
  • 1
  • 8

1 Answers1

4

What if you get bytes from an image and create ByteArrayInputStream?

ImageView view = new ImageView();
BufferedImage bImage = SwingFXUtils.fromFXImage(view.getImage(), null);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
    ImageIO.write(bImage, "png", outputStream);
    byte[] res  = outputStream.toByteArray();
    InputStream inputStream = new ByteArrayInputStream(res);
} catch (IOException e) {
    e.printStackTrace();
}
  • Thanks a lot, it worked. But there is a way to use JavaFX instead of java.awt.image.BufferedImage (AWT)? – Splanas00 Mar 17 '18 at 11:28
  • I tried to google it but there are lots of solutions for android and I know nothing about it (I am not sure if you write this code for android or not). You need to convert Image to byte array and then create ByteArrayInputStream, so you can try _ImageView to byte array_ search request – Liudmila Kornilova Mar 17 '18 at 11:51