在位图中分别处理颜色

时间:2011-06-01 12:59:14

标签: java colors bitmap bit-manipulation

解决

我有一个数组,其像素值为Java int(32位),因此格式为AARRGGBB 我想独立操作颜色,所以我的源图像会变得更绿,或更红或更蓝。我怎么能用Java做到这一点? (此代码现在有效)

/* Extract green from pixel (0-255)
 * short is 16 bit, because all Java types 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);

解决

1 个答案:

答案 0 :(得分:-1)

           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(););}
           }

}