图片通道混合算法只在图像色彩模式为RGB、CMYK时才起作用,其算法为对某一像素的R、G、B分别按一定的比例进行操作,得到
最新的像素,本算法计算公式如下:
R_new = 0.75 * (0.5 * G_old + 0.5 * B_old) + 0.25 * R_old
G_new = 0.25 * (0.5 * B_old + 0.5 * R_old) + 0.75 * G_old
B_new = 0.25 * (0.5 * R_old + 0.5 * G_old) + 0.75 * B_old
原图如下:
效果图如下:
核心代码如下:
/**
* 对图片增加滤镜
*
* @param image 图片
* @return 增加滤镜之后的图片
*/
@Override
public BufferedImage pictureAddFilter(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
int minWidth = image.getMinX();
int minHeight = image.getMinY();
for (int i = minWidth; i < width; i++) {
for (int y = minHeight; y < height; y++) {
int rgb = image.getRGB(i, y);
Color color = new Color(rgb);
color = filterColor(color);
image.setRGB(i,y,color.getRGB());
}
}
return image;
}
/**
* 对像素color 进行处理
*
* @param color 原color
* @return 新color
*/
@Override
protected Color filterColor(Color color) {
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
log.info("random filter");
Random random = new Random();
return new Color(
(int) (0.75 * (0.5 * color.getGreen() + 0.5 * color.getBlue()) + 0.25 * color.getRed()),
(int) (0.25 * (0.5 * color.getBlue() + 0.5 * color.getRed()) + 0.75 * color.getGreen()),
(int) (0.25 * (0.5 * color.getRed() + 0.5 * color.getGreen()) + 0.75 * color.getBlue())
);
}