JAVA处理图片黑白
时间: 2024-10-03 19:04:59 浏览: 86
java实现图像处理小工具
在Java中处理图片转换成黑白(灰度图像),通常需要使用java.awt.image包下的BufferedImage类和javax.imageio.ImageIO类。以下是一个简单的步骤:
1. 加载原始彩色图片:
```java
BufferedImage originalImage = ImageIO.read(new File("path_to_your_image.jpg"));
```
2. 创建一个新的灰度 BufferedImage:
```java
BufferedImage grayscaleImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
```
3. 将原始颜色数据复制到灰度图像中:
```java
ColorConvertOp op = new ColorConvertOp();
grayscaleImage = op.filter(originalImage, grayscaleImage);
```
4. 可选地,保存处理后的灰度图像:
```java
FileOutputStream outputStream = new FileOutputStream("path_to_output_gray_image.png");
ImageIO.write(grayscaleImage, "png", outputStream);
outputStream.close();
```
阅读全文