How do I create a BufferedImage from array containing pixels?

泄露秘密 提交于 2019-11-30 19:47:41

问题


I get the pixels from BufferedImage using the method getRGB(). The pixels are stored in array called data[]. After some manipulation on data array, I need to create a BufferedImage again so that I can pass it to a module which will display the modified image, from this data array, but I am stuck with it.


回答1:


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




回答2:


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).




回答3:


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



来源:https://stackoverflow.com/questions/9396159/how-do-i-create-a-bufferedimage-from-array-containing-pixels

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