I'm scanning through the pixels in a BufferedImage
to see if some of the pixels are a certain color. I've tried this by doing the following:
for(int x = 0; x < 16; x++) {
for(int y = 0; y < 16; y++) {
if(image.getRGB(x, y) == new Color(209, 167, 86).getRGB()) System.out.println("Same Color Detected!");
}
}
But image.getRGB()
returns a different value to Color.getRGB()
. How can I compare them?
These are some examples of the values (first digit is from image, second the colour I'm comparing):
0 : -8060928
-16777216 : -8060928
-3037354 : -8060928
-3037354 : -8060928
-16777216 : -8060928
Here's how I'm getting the image:
playerOrig = ImageIO.read(getClass().getResourceAsStream("/Player/player.gif"));
I'm using Eclipse with Java 1.6
I printed out the ColorModel
of the image and got this:
IndexColorModel: #pixelBits = 4 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@45d6a56e transparency = 2 transIndex = 11 has alpha = true isAlphaPre = false
I then printed out the ColorSpace
of the Color
object and got this:
java.awt.color.ICC_ColorSpace@45d6a56e
Here's the image:
Works for me ...
NOTE: well I tested the code on my local machine and it works for me just fine as you have it with the default
ColorModel
. I am using OSX andJava(TM) SE Runtime Environment (build 1.7.0_25-b15)
Good Luck!Here is a link to my entire Mavenized project on GitHub.com
Here is the output I get for the
16 X 16
file you posted:Based on the sample image you've supplied, you're only inspecting a 16x16 grid of the image, when the image is 320x320 pixels in size...
When I update your code to include the full width and height of the image, it works just fine...
Updated
Based on the new, smaller, example the code still works...
To prove it, I wrote a simple color replacement algorithim...