Java – bufferedimage and createscreen capture produce wrong colors

In my java program, I need to analyze the color of pixels in a given coordinate Because I need to do this often, first I capture a part of the screen and then get the pixel color I do this:

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

The getpixelcolor function is 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 created a pure pin picture (RGB (255255)) The problem is that even if the pixel is pure pink, the function returns RGB (250,61223) and red, green and blue variables In addition, the saved file (myscreen shot. BMP) looks quite different

What on earth did I do wrong May it be related to colormodel in some way?

UPD: getting the databuffer from Bi does not seem to produce the correct result - the first element of the generated databuffer is equal to "- 2105371" I don't know where the minus sign comes from, but if I turn it into hex, I'll get something like "ffffffffdfdfe5" The real pixel RGB is (E5, E5, EB). The buffer has been damaged, but RGB (DF, DF, E5) It drives me crazy

Solution

This is probably due to the color model

According to this code, it uses directcolormodel (see below), regardless of your screen color depth

/*
 * 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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>