Uma*_*hir 5 javafx image bytearray
我们可以使用创建FX Image对象
byte [] bytes = ------; //valid image in bytes
javafx.scene.image.Image image = new Image(new ByteArrayInputStream(bytes));
Run Code Online (Sandbox Code Playgroud)
这可以设置为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
);
Run Code Online (Sandbox Code Playgroud)
但是通过写byte []缓冲区生成的文件不是有效图像.
有什么见解吗?
编辑:如何从javafx imageView获取byte []的解决方案不能应用于我的问题.正如我已经清楚地提到过,我不想使用SwingFXUtils将其转换为BufferedImage.此外,我想将其转换为字节数组,以便可以将其写入图像文件.
这是后来的,但如果有人想要找出来,试试这个:
// Load the Image into a Java FX Image Object //
Image img = new Image(new FileInputStream("SomeImageFile.png") );
// Cache Width and Height to 'int's (because getWidth/getHeight return Double) and getPixels needs 'int's //
int w = (int)img.getWidth();
int h = (int)img.getHeight();
// Create a new Byte Buffer, but we'll use BGRA (1 byte for each channel) //
byte[] buf = new byte[w * h * 4];
/* Since you can get the output in whatever format with a WritablePixelFormat,
we'll use an already created one for ease-of-use. */
img.getPixelReader().getPixels(0, 0, w, h, PixelFormat.getByteBgraInstance(), buf, 0, w * 4);
/* Second last parameter is byte offset you want to start in your buffer,
and the last parameter is stride (in bytes) per line for your buffer. */
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2802 次 |
| 最近记录: |