在Java中将图像转换为2色 [英] Convert an image to 2-colour in Java
本文介绍了在Java中将图像转换为2色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Java将图像转换为2色,黑色和白色。我使用以下代码转换为灰度:
I would like to convert an image to 2-color, black and white using Java. I'm using the following code to convert to grayscale:
ColorConvertOp op = new ColorConvertOp(
ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
BufferedImage grayImage = op.filter(image, null);
但我不知道如何修改它以转换为黑白。
But I'm not sure how to modify this to convert to just black and white.
推荐答案
基于另一个答案(产生灰度):
Based on another answer (that produced grayscale):
public static BufferedImage toBinaryImage(final BufferedImage image) {
final BufferedImage blackAndWhiteImage = new BufferedImage(
image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_BYTE_BINARY);
final Graphics2D g = (Graphics2D) blackAndWhiteImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return blackAndWhiteImage;
}
你不能用 ColorConvertOp $ c来做$ c>因为没有二进制颜色空间。
You cannot do it with ColorConvertOp
because there is not binary colorspace.
这篇关于在Java中将图像转换为2色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文