javajavafxinputstreamfileinputstream

How to convert a JavaFX ImageView to InputStream


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


Solution

  • 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();
    }