JavaFX:将像素写入PixelWriter的最快方法 [英] JavaFX: Fastest way to write pixels to PixelWriter
问题描述
我正在寻找在 javafx.scene.image.Image
上写像素的最快方法。写入 BufferedImage
的后备阵列要快得多。至少在测试图像上我只花了~20ms BufferedImage
, WritableImage
另一方面需要~100ms 。我已经尝试 SwingFXUtils
但没有运气。
I'm looking for the fastest way to write pixels on javafx.scene.image.Image
. Writing to BufferedImage
's backing array is much faster. At least on the test image I made it took only ~20ms for BufferedImage
, WritableImage
on the other hand took ~100ms. I already tried SwingFXUtils
but no luck.
BufferedImage的代码
(更快):
BufferedImage bi = createCompatibleImage( width, height );
WritableRaster raster = bi.getRaster();
DataBufferInt dataBuffer = (DataBufferInt) raster.getDataBuffer();
System.arraycopy( pixels, 0, dataBuffer.getData(), 0, pixels.length );
代码> WritableImage
(较慢):
WritableImage wi = new WritableImage( width, height );
PixelWriter pw = wi.getPixelWriter();
WritablePixelFormat<IntBuffer> pf = WritablePixelFormat.getIntArgbInstance();
pw.setPixels( 0, 0, width, height, pf, pixels, 0, width );
也许有办法写入 WritableImage
也支持数组吗?
Maybe there's a way to write to WritableImage
's backing array too?
推荐答案
对于像素编写器的性能,选择正确的像素格式绝对至关重要。你可以检查原生像素格式是什么
For the performance of the pixel writer it is absolutely crucial that you pick the right pixel format. You can check what the native pixel format is via
pw.getPixelFormat().getType()
在我的Mac上,这是PixelFormat.Type.BYTE_BGRA_PRE。如果原始数据符合此像素格式,则传输到图像的速度应该非常快。否则,必须转换像素数据,这需要一些时间。
On my Mac this is PixelFormat.Type.BYTE_BGRA_PRE. If your raw data conforms to this pixel format, then the transfer to the image should be pretty fast. Otherwise the pixel data has to be converted and that takes some time.
这篇关于JavaFX:将像素写入PixelWriter的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!