Skip to main content

Convert colored image to back/white image

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
nihasmata
Offline
Joined: 2013-09-11

I am working on converting colored image to black and white image.I am using BufferedImage for this with the type of TYPE_BYTE_BINARY.But output image is not converted correctly.For example,if image contains blue letters on black background,result image for this part is totaly black. Can anybody help me ? My code is below.

//Invert the colormodel
byte[] map = new byte[] { (byte) (255), (byte) (0) };
IndexColorModel colorModel = new IndexColorModel(1, 2, map,
map, map);

BufferedImage bufferedImage = new BufferedImage(
img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_BYTE_BINARY, colorModel);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(img, 0, 0, null);
g2.dispose();