如何将ImageView转换为InputStream

时间:2018-03-17 10:52:58

标签: java javafx inputstream fileinputstream

我有ImageView包含图片,我需要的是制作ImageView的getImage()并将其转换为InputStream。这是我的代码:

try {

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

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

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

有没有办法获取ImageView图片并将其转换为InputStream

由于

1 个答案:

答案 0 :(得分:3)

如果从图像中获取字节并创建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();
}