Share this page 

Get the color of a specific pixelTag(s): AWT


We assume that we have an Image called picture
    
pixels = new int[width*height];
PixelGrabber pg = 
   new PixelGrabber(picture, 0, 0, width, height, pixels, 0, width);
try { 
  pg.grabPixels(); 
}
catch (InterruptedException e)  { }
From here, individual pixel can be accessed via the pixels array.
int  c = pixels[index];  // or  pixels[x * width + y]
int  red = (c & 0x00ff0000) >> 16;
int  green = (c & 0x0000ff00) >> 8;
int  blue = c & 0x000000ff;
// and the Java Color is ...
Color color = new Color(red,green,blue);

Here another way to achieve this

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage

...

BufferedImage image = ImageIO.read(urlImage);
int c = image.getRGB(x,y);
int  red = (c & 0x00ff0000) >> 16;
int  green = (c & 0x0000ff00) >> 8;
int  blue = c & 0x000000ff;
// and the Java Color is ...
Color color = new Color(red,green,blue);