Create a buffered image from rgb pixel values

Posted by Jeff Storey on Stack Overflow See other posts from Stack Overflow or by Jeff Storey
Published on 2011-01-06T17:36:15Z Indexed on 2011/01/06 17:53 UTC
Read the original article Hit count: 261

Filed under:
|
|

I have an integer array of RGB pixels that looks something like:

pixels[0] = <rgb-value of pixel(0,0)>
pixels[1] = <rgb-value of pixel(1,0)>
pixels[2] = <rgb-value of pixel(2,0)>
pixels[3] = <rgb-value of pixel(0,1)>
...etc...

And I'm trying to create a BufferedImage from it. I tried the following:

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
img.getRaster().setPixels(0, 0, width, height, pixels);

But the resulting image has problems with the color bands. The image is unclear and there are diagonal and horizontal lines through it.

What is the proper way to initialize the image with the rgb values?

thanks, Jeff

© Stack Overflow or respective owner

Related posts about java

Related posts about bufferedimage