Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
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
?
644 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 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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

577 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