Converting images to 2 colors in Java

I want to use java to convert the image to 2 colors, black and white I am converting to grayscale using the following code:

ColorConvertOp op = new ColorConvertOp(
             ColorSpace.getInstance(ColorSpace.CS_GRAY),null);
    BufferedImage grayImage = op.filter(image,null);

But I don't know how to change it to black and white

Solution

Based on another answer (generate 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,null);
    g.dispose();
    return blackAndWhiteImage;
}

You can't do it with colorconvertop because there is no binary color space

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>