Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

 

                         

» View all related tutorials
Related Tags: c class ant help property get tutorial name ria this for to e des pe in as sta m nt

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

it's good but it doesn't work properly it display this commentary: javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1263)
at GetPixelColor.main(GetPixelColor.java:10)
Exception in thread "main"
please help me where should i load the image

Posted by farfour on Tuesday, 04.7.09 @ 18:51pm | #86611

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.