getRGB方法返回单个int。我怎样才能单独获得红色,绿色和蓝色的颜色全部为0到255之间的值?Java BufferedImage单独获取红色,绿色和蓝色
46
A
回答
69
Java的Color类可以做转换:
Color c = new Color(image.getRGB());
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
7
你需要一些基本的二进制算术它分裂:
int blue = rgb & 0xFF;
int green = (rgb >> 8) & 0xFF;
int red = (rgb >> 16) & 0xFF;
(也可能是其他的全面,老实说,我不记得,文档也没有给我一个即时答案)
+0
我相信你是对的。 – HyperNeutrino 2015-07-05 21:19:30
105
一个像素由一个4字节(32位)整数表示,如下所示:
00000000 00000000 00000000 11111111
^ Alpha ^Red ^Green ^Blue
因此,要获得各个颜色成分,你只需要一个位二进制算术:
int rgb = getRGB(...);
int red = (rgb >> 16) & 0x000000FF;
int green = (rgb >>8) & 0x000000FF;
int blue = (rgb) & 0x000000FF;
这确实是什么java.awt.Color
类的方法做:
553 /**
554 * Returns the red component in the range 0-255 in the default sRGB
555 * space.
556 * @return the red component.
557 * @see #getRGB
558 */
559 public int getRed() {
560 return (getRGB() >> 16) & 0xFF;
561 }
562
563 /**
564 * Returns the green component in the range 0-255 in the default sRGB
565 * space.
566 * @return the green component.
567 * @see #getRGB
568 */
569 public int getGreen() {
570 return (getRGB() >> 8) & 0xFF;
571 }
572
573 /**
574 * Returns the blue component in the range 0-255 in the default sRGB
575 * space.
576 * @return the blue component.
577 * @see #getRGB
578 */
579 public int getBlue() {
580 return (getRGB() >> 0) & 0xFF;
581 }
6
对于简单颜色操作,你可以使用
bufImg.getRaster().getPixel(x,y,outputChannels)
outputChannels是一个用于存储获取像素的数组。它的长度取决于图像的实际通道数。例如,RGB图像有3个通道; RGBA图像有4个通道。
该方法有3种输出类型:int,float和double。 要获取颜色值范围从0到255,您的实际参数outputChannels应该是一个int []数组。
相关问题
- 1. 处理js红色()绿色()蓝色()
- 2. 使用Color.brighter方法打印红色,绿色和蓝色值
- 3. 从Android颜色对象获取红色,蓝色或绿色通道
- 4. RGBApixel'没有成员名为...红色蓝色和绿色
- 5. 删除图像的蓝色和绿色
- 6. opencv将黑色和白色转换为绿色和蓝色
- 7. 地块所有红色和绿色
- 8. .Net Reflector红色和绿色方块
- 9. 如何获得给定颜色代码(蓝色)的匹配颜色(红色和绿色)
- 10. 值红色/黄色/绿色(如何?)
- 11. 毕加索在顶角上显示蓝色的红色和绿色箭头
- 12. iOS:UIColor不尊重红色,绿色,阿尔法,蓝色。显示其他颜色?
- 13. BufferedImage:红色显示为灰色
- 14. jquery下拉列表都获得颜色绿色时selecet红色
- 15. 如何在画布中填充不同颜色的所有圈子:红色,黄色,蓝色和绿色?
- 16. Android中换入红色和蓝色
- 17. Esp8266蓝色和红色不断在
- 18. BufferedImage当我说红色时画白色
- 19. 什么是红色/绿色测试?
- 20. 将绿色转换为红色
- 21. 如何将JPG图像读入3个矩阵:红色,绿色,蓝色?
- 22. HTML颜色代码:红色到黄色到绿色
- 23. 蓝绿色转换策略
- 24. 指定的前景色无效:“default”。预计的(黑色,红色,绿色,黄色,蓝色,品红色,青色,白色)
- 25. 阿尔法混合红色,蓝色和绿色图像产生一个图像有色任何rgb值?
- 26. 用于C++的颜色地图库,可将给定值转换为红色,绿色和蓝色值
- 27. 当语法(iePURPLE或PURPAL)错误时,如何从csv文件分组颜色(蓝色,绿色,紫色,红色)?
- 28. 像油漆一样添加颜色(颜色)(蓝色+黄色=绿色等)
- 29. PHP和Django:Nginx,FastCGI和绿色独角兽?
- 30. 分别使用OpenCV for Android应用程序显示红色蓝色和绿色值
为图像的每个像素创建一个新的Color实例会产生大量垃圾,并且效率非常低。如果您打算处理图像的每个像素,使用'bufferedImage.getRGB(x,y)'获得每个像素的RGB颜色效率更高,并按照答案中的描述提取红色,绿色和蓝色分量由JoãoSilva提供。 – Marco13 2017-12-12 15:36:11