[java] Pixel grabber

Started by
7 comments, last by pamaro 22 years, 5 months ago
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
---------------------------------------------Looking for an artist and composer for a web and mobile game
Advertisement
http://www.gaffer.org/ check tinyptc there, it is what you are looking for.

it allows you to easily access the screenbuffer in an int-array.
Thanks, I''ll check it out.

---------------------------------------------Looking for an artist and composer for a web and mobile game
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
---------------------------------------------Looking for an artist and composer for a web and mobile game
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.
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
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!
---------------------------------------------Looking for an artist and composer for a web and mobile game
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
---------------------------------------------Looking for an artist and composer for a web and mobile game
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
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut

This topic is closed to new replies.

Advertisement