1. 方法1
BufferedImage grayImage = new BufferedImage(width, height, colorImage.TYPE_BYTE_GRAY);
Graphics g = grayImage.getGraphics();
g.drawImage(colorImage, 0, 0, null);
g.dispose();
2. 方法2(不推荐)
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
BufferedImage grayImage = op.filter(colorImage, null);
注意这种方法得到的灰度图grayImage与彩色图colorImage的通道数一样。
出处: http://stackoverflow.com/questions/9131678/convert-a-rgb-image-to-grayscale-image-reducing-the-memory-in-java
时间: 2024-10-30 07:53:51