关于Java:将像素值转换为RGB格式

convert pixel values into RGB format

I a分别具有像素的红色,绿色和蓝色值。如何将它们转换为RBG格式以创建新图像?我基本上需要一个相反的过程:

1
2
3
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;


1
int rgb = ((r << 16) | ((g << 8) | b);

假设它是RGB888。确保r,g and b都在0-255范围内


改用java.awt.Color类是一个好习惯。
它将使事情变得更加简单。您的情况是:

1
2
Color myColor = new Color(red, green, blue); //Construct the color
myColor.getRGB(); //Get the RGB of the constructed color

或相反:

1
2
3
4
Color myColor = new Color(rgb); //Construct the color with the RGB value
myColor.getRed(); //Get the separate components of the constructed color
myColor.getGreen();
myColor.getBlue();

有关更多信息,请查阅Javadocs。