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
617 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
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
mbodewes earned 100 total points
Comment Utility
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
Comment Utility
Oh, and you can use the ImageIO class to quickly load an image from file.
0
 

Author Closing Comment

by:zTechSuperPhys
Comment Utility
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
Comment Utility
"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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Increment alphanumeric sequence 6 78
countHi2 challenge 7 42
mapShare challenge 13 65
bitbucket vs gitbucket 3 28
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now