2011-04-18 77 views
3

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

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

回答

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

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

0

改为使用java.awt.Color类是一种很好的做法。
它会使事物更简单更容易。在你的情况将是:

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

或者逆:

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