How do I create a BufferedImage from array containing pixels?
Solution 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
Solution 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).
Saurabh
Updated on June 28, 2022Comments
-
Saurabh almost 2 years
I get the pixels from
BufferedImage
using the methodgetRGB()
. The pixels are stored in array calleddata[]
. After some manipulation on data array, I need to create aBufferedImage
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.