Thursday, January 29, 2009

Java: How to read pixel RGB values from images

BufferedImage img = null;

int w, h;

int pixels[];

try {

img = File("images/secret.jpg"));

w = img.getWidth();

h = img.getHeight();

pixels = new int[w * h];

img.getRGB(0, 0, w, h, pixels, 0, w);

System.out.println(img.getWidth() + " X " + img.getHeight());

System.out.println("R:" + ((pixels[0] >> 16) & 0xff) + " G:" + ((pixels[0] >> 8) & 0xff) + " B:" + (pixels[0] & 0xff));

} catch (IOException e) {



//Based on the sample code, it is really easy to see how to read RGB values from any image files.


Anonymous said...

Thank you for posting your information! I think it will help me in my project!! Have a great day.

Sunil Kumar said...

Thanks you Junxian. This is a useful code for me.

Anonymous said...

Superb Code. It really helped me too much.


sakthi said...

can you pls help us out for grabbing pixels in image and use those pixels to cut the image diagonally

sakthi said...

can you pls tell how to convert pixel values to image