Java Graphics How to - Get color of each pixel of an image using BufferedImages








Question

We would like to know how to get color of each pixel of an image using BufferedImages.

Answer

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/* w  ww. j  a v a 2  s. c  o m*/
import javax.imageio.ImageIO;

public class Main {
  public static void main(String args[]) throws IOException {
    File file = new File("your_file.jpg");
    BufferedImage image = ImageIO.read(file);
    
    int x = 10;
    int y = 10;
    
    int clr = image.getRGB(x, y);
    int red = (clr & 0x00ff0000) >> 16;
    int green = (clr & 0x0000ff00) >> 8;
    int blue = clr & 0x000000ff;
    System.out.println("Red Color value = " + red);
    System.out.println("Green Color value = " + green);
    System.out.println("Blue Color value = " + blue);
  }
}