【发布时间】:2016-10-31 22:45:26
【问题描述】:
我们可以通过使用来创建 FX Image 对象
byte [] bytes = ------; //valid image in bytes
javafx.scene.image.Image image = new Image(new ByteArrayInputStream(bytes));
这可以设置为 ImageView。
我需要相反的无需先将其转换为BufferedImage(SwingFXUtils.fromFXImage(image,null ))。
这样我就可以直接将字节写入文件。
我尝试了以下方法:
PixelReader pixelReader = image.getPixelReader();
int width = (int)image.getWidth();
int height = (int)image.getHeight();
byte[] buffer = new byte[width * height * 4];
pixelReader.getPixels(
0,
0,
width,
height,
PixelFormat.getByteBgraInstance(),
buffer,
0,
width * 4
);
但是写入byte []缓冲区生成的文件不是有效图片。
对此有何见解?
编辑:How to get byte[] from javafx imageView 给出的解决方案不适用于我的问题。正如我已经明确提到的,我不想使用 SwingFXUtils 将其转换为 BufferedImage。 此外,我想将其转换为字节数组,以便将其写入图像文件。
【问题讨论】: