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
624 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
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

770 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