简体   繁体   中英

How to convert a 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM