Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedImage & createScreenCapture produces wrong colors

In my Java program I need to analyze a color of a pixel in given coordinates. Because of the fact that I need to do it often, first I capture a part of the screen, and then get a pixel color. I am doing this with:

BufferedImage bi = robot.createScreenCapture(new Rectangle(0,0,100,100));
...
pxcolor = GetPixelColor(bi,x,y);
...
ImageIO.write(bi, "bmp", new File("myScreenShot.bmp"));

The GetPixelColor function is quite obvious:

public Color GetPixelColor(BufferedImage b, int x, int y)
{
    int pc = b.getRGB(x, y);
    Color ccc = new Color(pc,true);
    int  red = (pc & 0x00ff0000) >> 16;   // for testing
    int  green = (pc & 0x0000ff00) >> 8;  // for testing
    int  blue = pc & 0x000000ff;          // for testing
    return ccc;     
}

For testing purposes I have created a pure pink picture (RGB(255,0,255)). The problem is that even if the pixel is pure pink, the function returns something like RGB(250,61,223) as well as testing variables red, green and blue there. Also, the saved file (the myScreenShot.bmp) looks quite different.

What am I doing wrong. Could it be somehow ColorModel related?

UPD: getting the DataBuffer from bi doesn't seems to produce right results - the first element of produced DataBuffer is equal to "-2105371". I don't know from where came minus sign, but if I transform it to HEX I will get something like "FFFFFFFFFFDFDFE5". The real pixel RGB is (E5,E5,EB), and the buffer is already corrupted, having instead RGB(DF,DF,E5). This drives me nuts already.

like image 742
Quest Avatar asked Nov 13 '22 20:11

Quest


1 Answers

It is most likely due to the color model.

According to this code it uses a DirectColorModel (see below) regardless of your color depth of your screen.

/*
 * Fix for 4285201
 * Create a DirectColorModel equivalent to the default RGB ColorModel,
 * except with no Alpha component.
 */
screenCapCM = new DirectColorModel(24,
                 /* red mask */    0x00FF0000,
                 /* green mask */  0x0000FF00,
                 /* blue mask */   0x000000FF);
like image 56
aioobe Avatar answered Dec 15 '22 21:12

aioobe