private static ImageIcon makeGrayImageIcon1(Image img) { BufferedImage source = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = source.createGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); BufferedImage destination = colorConvert.filter(source, null); return new ImageIcon(destination); }
private static ImageIcon makeGrayImageIcon2(Image img) { int w = img.getWidth(null); int h = img.getHeight(null); BufferedImage destination = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); Graphics g = destination.createGraphics(); //// g.setColor(Color.WHITE); // https://community.oracle.com/thread/1373262 Color to Grayscale to Binary // g.fillRect(0, 0, w, h); // need to pre-fill(alpha?) g.drawImage(img, 0, 0, null); g.dispose(); return new ImageIcon(destination); }