缓冲图像和createScreenCapture 产生错误的颜色

发布于 2024-11-17 19:46:56 字数 1228 浏览 4 评论 0原文

在我的 Java 程序中,我需要分析给定坐标中像素的颜色。由于我需要经常这样做,因此我首先捕获屏幕的一部分,然后获取像素颜色。我正在这样做:

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

GetPixelColor 函数非常明显:

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;     
}

出于测试目的,我创建了一个 纯粉色图片 (RGB(255,0,255))。问题是 即使像素是纯粉色,该函数也会返回类似 RGB(250,61,223) 的内容,并在那里测试变量红色、绿色和蓝色。此外,保存的文件(myScreenShot.bmp)看起来完全不同

我做错了什么。它可能与 ColorModel 有关吗?

UPD:从 bi 获取 DataBuffer 似乎不会产生正确的结果 - 生成的 DataBuffer 的第一个元素等于“-2105371”。我不知道减号从哪里来,但如果我将其转换为十六进制,我会得到类似“FFFFFFFFFFDFFDFE5”的内容。真实像素 RGB 是 (E5,E5,EB),缓冲区已损坏,取而代之的是 RGB(DF,DF,E5)。这已经让我抓狂了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2024-11-24 19:46:56

这很可能是由于颜色模型造成的。

根据这个code 它使用 DirectColorModel (见下文),无论屏幕的颜色深度如何。

/*
 * 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文