缓冲图像createScreenCapture产生错误的颜色 [英] BufferedImage & createScreenCapture produces wrong colors

查看:61
本文介绍了缓冲图像createScreenCapture产生错误的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"));

GetPixelColor函数非常明显:

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

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

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.

我在做什么错。

UPD:从bi中获取DataBuffer似乎不会产生正确的结果-产生的DataBuffer的
第一个元素是相等的改为 -2105371。我不知道减号是从哪里来的,但是如果将其转换为十六进制,我会得到类似 FFFFFFFFFFDFDDFE5的信息。实际像素RGB为(E5,E5,EB),并且缓冲区已损坏,取而代之的是RGB(DF,DF,E5)。

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.

推荐答案

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

It is most likely due to the color model.

根据此代码,它使用 DirectColorModel (见下文),而不管屏幕的颜色深度如何。

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

这篇关于缓冲图像createScreenCapture产生错误的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆