Solved

Java - Want to know how to grab one single pixel from an image and display its color value.

Posted on 2008-10-30
4
634 Views
Last Modified: 2013-11-23
I want to know how to grab one single pixel from an image and display its color value.

I currently have another application that returns a color value for a particular pixel on the screen, and i want to get a java application to 'speak the same language' as that application.

example: Pixel at 1200, 15 is color 191.  

I know that's a reddish color, but is that the RGB value? and also how do i go about obtaining that in Java.
0
Comment
Question by:zTechSuperPhys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
mbodewes earned 100 total points
ID: 22843936
Do you want the pixel color values of a single pixel from an image or from the screen? The first can be done in Java, the other one requires access to some OS library. Normally the color is divided in three separate parts using the RGB color space (there are others): red, green and blue. Normally they are displayed using hexadecimals (0..9ABCDEF) where A is valued 10 and F valued 16. So a color in the normal 16M color space #000000 is black, #FFFFFF is white and #FF0000 is bright red.

Use the BufferedImage.getRGB(x, y) method for images to get the RGB pixel values. You've now got an int which holds the RGB values 00RRGGBB in hex. You can get the values by using the following snippet (out of the top of my head):


int rgb = bufferedImage.getRGB(1200, 15);
int r = (rgb & 00FF0000) >> 16;
int g = (rgb & 0000FF00) >> 8;
int b = (rgb & 000000FF); // >> 0 is redundant

Open in new window

0
 
LVL 9

Expert Comment

by:mbodewes
ID: 22843948
Oh, and you can use the ImageIO class to quickly load an image from file.
0
 

Author Closing Comment

by:zTechSuperPhys
ID: 31511695
I have an additional question, but won't hold the points as randsom for it...you mentioned that getting a pixel from the screen is not possible w/o some OS library.  

Would taking a screenshot with robot and working off of that do as a workaround?
0
 
LVL 9

Expert Comment

by:mbodewes
ID: 22848822
"Would taking a screenshot with robot and working off of that do as a workaround?"

Sure you can, that seems to be a very valid solution. If the robot works through a .dll you could use the Java Native Interface (or a derivative like JNA) to get to the graphics. Otherwise you can just store the file at a temporary location and load it using Java.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question