【发布时间】:2018-06-06 03:25:32
【问题描述】:
我正在尝试编写一个程序,该程序将 bufferedImage 作为输入并将所有接近黑色的颜色(R
public static BufferedImage BlackAndWhite(BufferedImage image) {
ColorModel model = new BlackWhiteColorModel(DataBuffer.TYPE_INT);
WritableRaster raster = image.getRaster();
BufferedImage newImage = new BufferedImage(model, raster, false, null);
return newImage;
}
BlackWhiteColorModel 被定义为
public class BlackWhiteColorModel extends ColorModel {
public BlackWhiteColorModel(int bits) {
super(bits);
}
@Override
public int getRed(int pixel) {
int[] rgb = getRgb(pixel);
if (rgb[0] < 32 && rgb[1] < 32 && rgb[2] < 32) {
return 0;
} else {
return 255;
}
}
@Override
public int getGreen(int pixel) {
int[] rgb = getRgb(pixel);
if (rgb[0] < 32 && rgb[1] < 32 && rgb[2] < 32) {
return 0;
} else {
return 255;
}
}
@Override
public int getBlue(int pixel) {
int[] rgb = getRgb(pixel);
if (rgb[0] < 32 && rgb[1] < 32 && rgb[2] < 32) {
return 0;
} else {
return 255;
}
}
@Override
public int getAlpha(int pixel) {
return pixel;
}
private int[] getRgb(int pixel) {
int r = (pixel) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = (pixel >> 16) & 0xFF;
int a = (pixel >> 24) & 0xFF;
return new int[]{r, g, b, a};
}
}
但是,我最终得到了 isCompatibleRasterException。谁能给我一些建议?
【问题讨论】:
-
您需要黑白图像还是灰度图像?
标签: java image-processing colors