【问题标题】:How to convert a ImageView to InputStream如何将 ImageView 转换为 InputStream
【发布时间】:2018-03-17 10:52:57
【问题描述】:

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

try {

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

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

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

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

谢谢

【问题讨论】:

    标签: java javafx inputstream fileinputstream


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,它成功了。但是有没有办法使用 JavaFX 代替 java.awt.image.BufferedImage (AWT)?
    • 我尝试用谷歌搜索,但有很多适用于 android 的解决方案,我对此一无所知(我不确定您是否为 android 编写此代码)。需要将Image转为字节数组,然后创建ByteArrayInputStream,可以尝试ImageView转字节数组搜索请求
    猜你喜欢
    • 2011-04-12
    • 2011-12-12
    • 2011-02-19
    • 2013-06-11
    • 2016-04-11
    • 2020-02-27
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多