hi people,

I want to convert the pixel to a set of rgb values, but I don't really have an idea. If i am not wrong, there are some java commands like readImage and readPixel and raster that are related to digital image.
Can someone shed a light on me?

Thanks,
tristan

Maybe use an imagefilter. If not that, then start shifting some bits.

Maybe use an imagefilter. If not that, then start shifting some bits.

hi pal,

i have checked out on some of the image filters available,most of them will filter out for the red,green or blue components but for my case, i need to let's say, for a green pixel, i want to translate the green pixel into a set of rgb values ie. (100,222,332), do you think i can get a program like this?

rgds,
tristan

Filter out the red, green, and blue and then put them together.

Oh, and you can't have 332 blue in a green pixel - not only would that be more blue than green, the numbers are only supposed to go up to 255 :)

Did any one find a solution ?

As the previous poster mentioned, the easiest way to do this is to get your image into a BufferedImage somehow or other, then call getRGB() on the pixel you want. If your image is in a file, then you can usually call ImageIO.read() to load that image into a BufferedImage. So for example, to get the red, green and blue components of pixel (4,4) of some image file:

BufferedImage img = ImageIO.read(new File("myfile.jpg"));
int pixelCol = img.getRGB(4, 4);
int a = (pixelCol >>> 24) & 0xff;
int r = (pixelCol >>> 16) & 0xff;
int g = (pixelCol >>> 8) & 0xff;
int b = pixelCol & 0xff;

The values will be between 0 and 255 obviously, with 'a' being the 'alpha' or transparency value (which may not be relevant if your image doesn't support transparency).

As the previous poster mentioned, the easiest way to do this is to get your image into a BufferedImage somehow or other, then call getRGB() on the pixel you want. If your image is in a file, then you can usually call ImageIO.read() to load that image into a BufferedImage. So for example, to get the red, green and blue components of pixel (4,4) of some image file:

BufferedImage img = ImageIO.read(new File("myfile.jpg"));
int pixelCol = img.getRGB(4, 4);
int a = (pixelCol >>> 24) & 0xff;
int r = (pixelCol >>> 16) & 0xff;
int g = (pixelCol >>> 8) & 0xff;
int b = pixelCol & 0xff;

The values will be between 0 and 255 obviously, with 'a' being the 'alpha' or transparency value (which may not be relevant if your image doesn't support transparency).

very true, but if you don't want to deal with bit shifts you can do something like this:

BufferedImage img = ImageIO.read(new File("myfile.jpg");
Color c = new Color(img.getRGB(4,4);
int r=c.getRed();
int g=c.getGreen();
int b=c.getBlue();
int a=c.getAlpha();

this will get the exact same results as the previous posts, but it avoids the bitshifts.

Well, if you really want to create an object for every single pixel you query, then I suppose so...

many people don't get bit shifts, and it does make it easier to manipulate the color as a whole if needed, as well as use it in a graphical application (using java.awt).

Yes, I agree with particularly the latter point -- if the thing you need to do is pick out a colour and then manipulate it, use it in various places, then Color is the right tool for the job. And yes, I do concede that many people don't get bit shifts. (I'd possibly argue, if you're going to do a lot of image manipulation, then unfortunately you do need to understand a little bit about bits and bytes sooner or later...)

Incidentally, if you do go for wrapping a Color around the int, be careful of the ALPHA channel: the example you give would actually ignore the alpha part of the int and assume that the pixel was opaque. So if the image does have an alpha channel, you'd need to do:

Color c = new Color(img.getRGB(x, y), [B]true[/B]);

oh, yea, i forgot about that parameter, oops

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.