Archived

This topic is now archived and is closed to further replies.

pamaro

[java] Pixel grabber

Recommended Posts

pamaro    122
Hi I''m trying to do a pixel grabber... the objective is to get the color of a certain point on the screen... The function should be something like int get_pixel(int x, int y, Graphics g) I tried checking to see if it existed something similar under AWT, but failed to find it... I have no idea how to program one either (I did a couple in my Pascal days, but I can''t figure out how to do it in Java). Any suggestions...? Thanks in advance Pedro Amaro

Share this post


Link to post
Share on other sites
pamaro    122
I''ve checked it out but couldn''t see how to work with it... at least failed to find the documentation on the site... nevertheless, taking a peek at the imports that it did, I found out a PixelGrabber class...
I tried doing something like

PixelGrabber grabber = new PixelGrabber(*)

* - it contained six arguments. I used
(image, 0,0,width, height, pixels, 0, width)
being "image" from the Image type and pixels an int[width]

then did something like grabber.grabPixels();

But, when I tried to check out what the pixels array had, it was filled with zeros (which shouldn''t happen according to the image I was using). Am I doing something wrong? How can it be corrected?

Pedro Amaro

Share this post


Link to post
Share on other sites
AdmiralBinary    100
Are u using Windows API and C++? If you are, then you can use GetDC and GetPixel to obtain the results u want.

---------------

I finally got it all together...
...and then forgot where I put it.

Share this post


Link to post
Share on other sites
Wrathnut    466
Try this, it shold work for everyone cause it is from the JDK 1.1:

    
int w = myImage.getWidth(this);
int h = myImage.getHeight(this);

int buffer[] = new int [w * h];
PixelGrabber grabber = new PixelGrabber(myImage, 0, 0, w, h, buffer, 0, w);

grabber.grabPixels();



constructors for a PixelGrabber are:
        
public PixelGrabber(Image img, int x, int y, int w, int h, int pix[], int off, int scansize);

public PixelGrabber(ImageProducer ip, int x, int y, int w, int h, int pix[], int off, int scansize);


Arguments specify the image source, the shape and size of the region, a destination array, an offset to the position in the array to recieve the first pixel, and the scan size of eachg row(typically the same as the w argument).
- that came from the book, "Java in Plain English second ed."

Edited by - wrathnut on November 13, 2001 6:20:28 PM

Share this post


Link to post
Share on other sites
pamaro    122
I''ll try to check it out. I wasn''t sure about what those parameters meant, so I tried to "guess" them... I''ll check out what you gave me and see how it works. Thanks for the help!

Share this post


Link to post
Share on other sites
pamaro    122
Sorry to bother again, but I have another doubt...

What''s the number saved in the int[]? I assume that it''s a representation of the each pixel''s RGB code... I get a negative 8 digit int... is there any way to make it a long? I assume it''s negative due to something similar to an overflow effect... Or am I totally wrong and it means something else?

Pedro

Share this post


Link to post
Share on other sites
WayfarerX    130
0xAARRGGBB

The first byte is the alpha value and is set to FF (and since the first bit is the sign bit, this is why you get a negative number). The next byte is red, then green, then blue. To get the individual RGB components, mask out and shift the bits:

    
int rgb = pixels[index];
int red = (rgb & 0x00FF0000) >> 16;
int green = (rgb & 0x0000FF00) >> 8;
int blue = rgb & 0x000000FF;


"So crucify the ego, before it's far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that's imagined and all conceivable."
- Tool



Edited by - wayfarerx on November 14, 2001 6:21:13 PM

Share this post


Link to post
Share on other sites