问题
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.
Code for BufferedImage
(faster):
BufferedImage bi = createCompatibleImage( width, height );
WritableRaster raster = bi.getRaster();
DataBufferInt dataBuffer = (DataBufferInt) raster.getDataBuffer();
System.arraycopy( pixels, 0, dataBuffer.getData(), 0, pixels.length );
Code for WritableImage
(slower):
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 );
Maybe there's a way to write to WritableImage
's backing array too?
回答1:
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()
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.
来源:https://stackoverflow.com/questions/33911010/javafx-fastest-way-to-write-pixels-to-pixelwriter