/**
* image 原图流
* outputFilePath 转换后目标图存储路径
*/
public static void binaryImage(BufferedImage image,String outputFilePath) throws Exception{
//如传入原图路径 将以下两行代码打开即可,并将参数设置位inputFilePath
// File file = new File(inputFilePath);
// BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
for(int i= 0 ; i < width ; i++){
for(int j = 0 ; j < height; j++){
int rgb = image.getRGB(i, j);
grayImage.setRGB(i, j, rgb);
}
}
File newFile = new File(outputFilePath);
//输出图片格式可随便定义 如: jpg\png\bmp等
ImageIO.write(grayImage, "bmp", newFile);
}
java实现普通图片转黑白图片
最新推荐文章于 2023-08-09 09:10:09 发布