?
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
Medium Priority
?
637 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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

764 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