我尝试用给定的颜色创建彩色灰色图像,但总是失败。我知道我必须使用 BufferedImage 并将源图像的所有像素加载到数组中,但我不知道如何使用颜色值,因此在将源颜色和给定颜色相乘后总是有一些奇怪的颜色(据记录我'我们有代码来获取颜色的整数)。
感谢您的帮助。
最佳答案
您是否尝试将图像灰色 channel (白-黑)更改为(some_color-black)?
public void recolor(BufferedImage source, BufferedImage destination, int color) {
WritableRaster destinationRaster = destination.getRaster();
Raster sourceRaster = source.getRaster();
//red, green, blue
int channels = new int[]{color & 0xFF0000, color & 0xFF00, color & 0xFF};
for (int channel=0; channel<3; channel++){
for (int y=0; y<sourceRaster.getHeight(); y++) {
for (int x=0; x<rangeSlicer.getWidth(); x++) {
pixel = sourceRaster.getSample(x, y, channel);
pixel = Math.round((double)pixel/255.0 * channels[channel]);
destinationRaster.setSample(x, y, channel, pixel);
}
}
}
}
不确定这是否可以编译,但想法就是这样。更有效的程序检查RescaleOp .
关于Java - 图像着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7947769/