Showing posts with label pixel. Show all posts
Showing posts with label pixel. Show all posts

Thursday, January 29, 2009

Java: How to read pixel RGB values from images

BufferedImage img = null;

int w, h;

int pixels[];

try {

img = ImageIO.read(new 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) {

e.printStackTrace();

}


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