How do I create a BufferedImage from array containing pixels?

前提是你 提交于 2019-12-01 00:44:09
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Then set the pixels again.

bufferedImage.setRGB(x, y, your_value);

PS: as stated in the comments, please use the answer from @TacticalCoder

I get the pixels from the BufferedImage using the method getRGB(). The pixels are stored in array called data[].

Note that this can possibly be terribly slow. If your BufferedImage supports it, you may want to instead access the underlying int[] and directly copy/read the pixels from there.

For example, to fastly copy your data[] into the underlying int[] of a new BufferedImage:

BufferedImage bi = new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB );
final int[] a = ( (DataBufferInt) res.getRaster().getDataBuffer() ).getData();
System.arraycopy(data, 0, a, 0, data.length);

Of course you want to make sure that your data[] contains pixels in the same representation as your BufferedImage (ARGB in this example).

You can set the RGB (int) values for the pixels in the new image using the setRGB methods.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!