Home Java Java-get-example Java program to get the color of pixel

Related Tutorials


 
 

Share on Google+Share on Google+

Java program to get the color of pixel

Advertisement
In this java example program we have to write code for getting the pixel color of an image.

Java program to get the color of pixel

     

In this java example program we have to write code for getting the pixel color of an image. To get the pixel color we need to first have an image and then we will be able to get the pixel color of any specific or particular pixel in the RGB format.

File file= new File("rockface.jpg");
BufferedImage image = ImageIO.read(file);

Above line of code creates an object of File with the image named "rockface.jpg" now we will read this file with the static method of ImageIO read(). Now we can get the pixel color with the getRGB() method with the image object.

Here is the example code of GetPixelColor.java as follows:

 

 

GetPixelColor.java

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

public class GetPixelColor
{
  public static void main(String args[]) throws IOException{
  File file= new File("rockface.jpg");
  BufferedImage image = ImageIO.read(file);
  // Getting pixel color by position x=100 and y=40 
  int clr=  image.getRGB(100,40)
  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);
  }
}

 

Output of the above Code:

C:\javaexamples>javac GetPixelColor.java

C:\javaexamples>java GetPixelColor
Red Color value = 33
Green Color value = 50
Blue Color value = 60

 

Download Source Code

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2008

Related Tutorials

Discuss: Java program to get the color of pixel   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Laszlo Orloci
April 17, 2011
Java applicaton program for color splitting

I am working on a problem that requires splitting the color of each pixel into red, green and blue over specified ranges of pixels in color images. I am looking for a complete Java code which would read jpg or bmp files, compute R, G, B values, and write the values on a text file. Can anybody help?
rolly
September 13, 2012
error

The method read(File) in the type ImageIO is not applicable for the arguments (String)
DMCA.com