public static void toGrayImge(File img) {
try {
Image image = ImageIO.read(img);
int srcH = image.getHeight(null);
int srcW = image.getWidth(null);
BufferedImage bufferedImage = new BufferedImage(srcW, srcH, BufferedImage.TYPE_3BYTE_BGR);
bufferedImage.getGraphics().drawImage(image, 0, 0, srcW, srcH, null);
bufferedImage = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null).filter(bufferedImage, null);
String img_path = img.getAbsolutePath();
String img_type = img_path.substring(img_path.lastIndexOf(".") + 1);
String img_name = img.getName().replace("."+img_type,"");
img_path = img_path.replace(img_name, img_name+"_gray");
System.out.println(img_path);
ImageIO.write(bufferedImage, img_type, new File(img_path));
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException("图片转换出错!", e);
}
}
10-03
1084
09-07
1603
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交