java – BufferedImage和createScreenCapture产生错误的颜色
发布时间:2020-12-15 02:26:29 所属栏目:Java 来源:网络整理
导读:在我的 Java程序中,我需要分析给定坐标中像素的颜色.由于我需要经常这样做,首先我捕获屏幕的一部分,然后获得像素颜色.我这样做: BufferedImage bi = robot.createScreenCapture(new Rectangle(0,100,100));...pxcolor = GetPixelColor(bi,x,y);...ImageIO.w
在我的
Java程序中,我需要分析给定坐标中像素的颜色.由于我需要经常这样做,首先我捕获屏幕的一部分,然后获得像素颜色.我这样做:
BufferedImage bi = robot.createScreenCapture(new Rectangle(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; } 出于测试目的,我创建了一个pure pink picture(RGB(255,255)).问题是 我究竟做错了什么.可能是某种方式与ColorModel有关吗? UPD:从bi获取DataBuffer似乎不会产生正确的结果 – 解决方法
这很可能是由于颜色模型.
根据this 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); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |