JavaFx图像到字节[]数组(闭合)
问题描述:
我需要一种快速方法将JavaFX图像转换为字节数组。JavaFx图像到字节[]数组(闭合)
“BufferedImage bImage = SwingFXUtils.fromFXImage(i,null);”是慢下来的。
我认为最好不要将图像先转换为awt.BufferedImage。
所以我至今是:
PixelReader pr = img.getPixelReader();
WritablePixelFormat<ByteBuffer> pixelformat = WritablePixelFormat.getByteBgraInstance();
int w = (int) img.getWidth();
int h = (int) img.getHeight();
int offset = 0;
int scanlineStride = w * 4;
byte[] buffer = new byte[w * h * 4];
pr.getPixels(0, 0, w, h, pixelformat, buffer, offset, scanlineStride);
但作为例外,这是行不通的。
似乎像字节[]是空的呢?
您能否解释行为与您的期望有何不同? –
我想现在我的问题在哪里。我试图在网页上显示图像。但即时通讯读取blob做错了。我发布的代码,是吗? – Marcel
我的解决方案在一些调用后也会导致OutOfMemory。 – Marcel