代码:
private BufferedImage performBinary(BufferedImage original) {
int step = 127;
int min = 0;
int max = 1;
Raster originalRaster = original.getData();
BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_BYTE_BINARY);
WritableRaster resultRaster = result.getRaster();
for (int x = 0; x < originalRaster.getWidth(); x++) {
for (int y = 0; y < originalRaster.getHeight(); y++) {
int pix = originalRaster.getPixel(x, y, (int[]) null)[0];
if (pix < step) {
resultRaster.setPixel(x, y, new int[] { min });
} else {
resultRaster.setPixel(x, y, new int[] { max });
}
}
}
return result;
}
...
ImageIO.write(result, "jpg", new File(pathToFile));
...
问题:我将此结果保存到图像文件中,图像是我想要的黑白图像,但它具有24位深度。我想要黑白1位深度。 我在BufferedImage,ColorModel和ColorSpace上引用了Javadoc,但是不明白如何在输出上获得1位。帮帮我