How to get the RGB of a pixel from an Image?

Hi, i’m trying to get the RGB of just one pixel from an image. Any idea to do it? My source using the method getRGB from Image Class don’t works, after using the method getRGB the array “argb” doesn’t contain what is supposed, it just contains one value: [I@1a4cfaaa

Image superficie, volcado;

int argb[];

superficie = Image.createImage("/pantalla11.png");

argb = new int[superficie.getWidth()/4*superficie.getHeight()];

superficie.getRGB(argb, 0,superficie.getWidth()/4, 0,0, superficie.getWidth()/4, superficie.getHeight());
volcado.createRGBImage(argb,superficie.getWidth()/4,superficie.getHeight(),true);

Passing an array into System.out.println(Object) will call the default toString of the int[] class.

That is what the output “[I@1a4cfaaa” is.

‘[’ signifies an array (1 dimension),
‘I’ indicates its an integer array,
‘@’ at…
‘1a4cfaaa’ the heap address the array is located at.

If you want to see the contents of the array, you will have to iterate over every element and generate some graphical representation of the data.

As to your actual problem :-

your int [] length should be image.getWidth()*image.getHeight(), as each pixel is packed into an integer.

All the other getWidth()/4’s should also lose the /4.

They code you posted doesn’t grab one pixel, it grabs one fourth of the width and all of the height (unless the image is 4 by 1 in which case that would evaluate to one pixel). And 1a4cfaaa is a perfectly valid value for an RGB pixel (mostly transparent blueish green with a hint of red). The way you are then trying to create an image from the pixels you grab is wrong though:


volcado.createRGBImage(argb,superficie.getWidth()/4,superficie.getHeight (),true);

will either through a NullPointerException if you didn’t initialize volcado before, or create an image and just “throw it away” since you’re not assigning the returned image to anything. It should probably be something like:


volcado = Image.createRGBImage(argb,superficie.getWidth()/4,superficie.getHeight (),true);

shmoove

Thanks Abuse and shmoove, very useful information.
The “/4” was because the image is too big and at the beginning i wanted to take all the pixels of the /4 part of the image, now i’ve changed my source.

Problem solved, thanks.