Turn an array of pixels into an Image object with Java's ImageIO?

前端 未结 6 1165
粉色の甜心
粉色の甜心 2020-12-05 00:14

I\'m currently turning an array of pixel values (originally created with a java.awt.image.PixelGrabber object) into an Image object using the following code:



        
6条回答
  •  一生所求
    2020-12-05 00:53

    As this is one of the highest voted question tagged with ImageIO on SO, I think there's still room for a better solution, even if the question is old. :-)

    Have a look at the BufferedImageFactory.java class from my open source imageio project at GitHub.

    With it, you can simply write:

    BufferedImage image = new BufferedImageFactory(image).getBufferedImage();
    

    The other good thing is that this approach, as a worst case, has about the same performance (time) as the PixelGrabber-based examples already in this thread. For most of the common cases (typically JPEG), it's about twice as fast. In any case, it uses less memory.

    As a side bonus, the color model and pixel layout of the original image is kept, instead of translated to int ARGB with default color model. This might save additional memory.

    (PS: The factory also supports subsampling, region-of-interest and progress listeners if anyone's interested. :-)

提交回复
热议问题