Sign in to follow this  

[java] int array of RGB values saving to an image?!

Recommended Posts

SirGorthon    180
Howdy ya'll! I've got an array of int RGB values that I'm trying to save to an image! The resulting image's size is right, but it's just BLACK! The array with the pixel data isn't a bunch of 0's, so I think the problem is in the saving part! Here's the code:
			BufferedImage buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
			buffer.setRGB(0, 0, w, h, pix, 0, w);
			File f = new File(src1+".png");
				ImageIO.write(buffer, "png", f);
			catch(Exception e)

(pix is my int array, w the width, h the height) Any help would be greatly appreciated! *edit: I'm not very good at Java, so please be gentle when you point out my problems :( Thanks! SirGorthon

Share this post

Link to post
Share on other sites
tebriel    904
Wild guess, but what does the data in your array look like? Are you storing each RGB (0-255) value in their own integer field, or have you combined them? (as in storing three 0-255 values per (assuming)4-byte int) I don't think I've used ImageIO before so I'm not sure which would be correct. It would be wasteful to use an entire int to store one 0-255 value, so I imagine they'd have chosen to use a char[] if they wanted one 0-255 value per array element?

I guess basically... are you sure the BufferedImage is okay?

Also, have you tried using this yet?

void setRGB(int x, int y, int rgb)
Sets a pixel in this BufferedImage to the specified RGB value.

Can you set a few pixels to some color and write the file then? If you need to use something like 00FFFFFF (hex) as your pixel value for white (and looking at that method I imagine you do), then you probably need to combine your RGB values like I tried to explain above.

Standard disclaimer: "Than again I'm posting this after having stayed up all night, so I could be way off."

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this