2010年4月16日星期五

利用 Java 改變圖像的色相 (Use Java the edit the Hue value of an image)

import java.awt.image.BufferedImage;
import java.awt.Color;

public static BufferedImage hue(BufferedImage source, float value){
    BufferedImage out = new BufferedImage(source.getWidth(), source.getHeight(), source.getType());
    for (int i = 0; i < source.getWidth(); i++){
        for (int j = 0; j < source.getHeight(); j++){
            Color color = new Color(source.getRGB(i, j));
            float[] hsb = new float[3];
            Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
            hsb[0] += value;
            color = Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
            out.setRGB(i, j, color.getRGB());
        }
    }
    return out;
}

修改前
before
apple.jpg

以 setHue(source, 1F / 3F) 修改後
edit with setHue(source, 1F / 3F)
apple3.jpg

以 setHue(source, -1F / 3F) 修改後
edit with setHue(source, -1F / 3F)
apple2.jpg

沒有留言 :

發佈留言