SOLVED
I have an array holding pixel values as Java int (32bit) thus in the form AARRGGBB I want to manipulate colors indipendently, so my source image will turn more green, or more red or more blue. How can I do this in Java? (this code works now)
/* Extract green from pixel (0-255)
* short is 16 bit, because all Java types开发者_C百科 are signed
* so a Java byte would be-128 +128 */
short green = (short) ((pixel >> 8) & 0xff);
// Add some amount
short newGreen = green + 50;
green = (newGreen > 255) ? (short)255 : newGreen;
// Now final step
int mask = 0xffff00ff;
pixels[index] = (pixel & mask) | ((green & 0xff) << 8);
SOLVED
public static void toGreenScale(BufferedImage bi){
for (int i = 0; i < bi.getWidth(); i++){
for (int j = 0; j < bi.getHeight(); j++)
{
int rgb = bi.getRGB(i, j);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 16) & 0xFF;
int blue = (rgb >> 16) & 0xFF;
int r= (red << 16) | (red << 8) | red;
int g = (green << 16) | (green << 8) | green;
int b = (blue << 16) | (blue << 8) | blue;
g=g+50;//add an if condition to handle if g>255
Color c=new Color(r,g,b);
bi.setRGB(i,j,c.getRGB(););}
}
}
精彩评论