This week's book giveaway is in the Big Data forum. We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line! See this thread for details.

I try the following code snippet in order to find out how many colors in a BufferedImage, but it always returns 3. But i am sure that image has more than 3 color. Can you please tell me how can i find out how many color in an image?

The getNumComponents() method does not do what you think it does. It returns the number of component colors used for each pixel. Almost all computer and TV screens have three components (red, green, blue) that are "mixed" together to make a color. It does not return information like a histogram, which is what you seem to expect.

Use the getRGB method for every single pixel. Store those in a Map:

This code uses the advantages of both generics and autoboxing. Keep in mind that this could produce a really really big map - there are 256^3 different possibilities for RGB only, and that doesn't even take into account the alpha part.

Nice bit of code, but it's worth checking whether the getRGB method returns RGB or what Color#getRGB() returns, viz aRGB. Then rather than using RGB straight use . . .getRGB() & 0xffffff. That will reduce the combinations from 2^32 to 2^24. Still >16,000,000 however.

subject: How can i find out how many colors in a BufferedImage