BufferedImage bufferImage;
File outputFile = null;
try{
Image image = ImageIO.red(new File("c:\\pic\\aa.jpeg"));
int width = image.getWidth(null);
int height = image.getHeight(null);
bufferImage= new BufferedImage (width,height,BufferedImage.TYPE_INT_RGB);
bufferImage.getGraphics().drawImage(image,0,0,width,height,image.getGraphics().getColor(),null);//设置底色
outputFile = new File("c:\\pic\\bb.jpeg");
if(!outputFile.exists())
outputFile.mkdirs();
ImageIO.write(bufferImage,"jpeg",outputFile);
}catch(IOException e){
e.printStackTrace();
}
File outputFile = null;
try{
Image image = ImageIO.red(new File("c:\\pic\\aa.jpeg"));
int width = image.getWidth(null);
int height = image.getHeight(null);
bufferImage= new BufferedImage (width,height,BufferedImage.TYPE_INT_RGB);
bufferImage.getGraphics().drawImage(image,0,0,width,height,image.getGraphics().getColor(),null);//设置底色
outputFile = new File("c:\\pic\\bb.jpeg");
if(!outputFile.exists())
outputFile.mkdirs();
ImageIO.write(bufferImage,"jpeg",outputFile);
}catch(IOException e){
e.printStackTrace();
}
本文介绍如何使用Java编程语言,通过读取本地图片文件,调整尺寸,并使用ImageIO库将处理后的图片写入到指定的目标文件路径。演示了从原始图片中获取宽度和高度,创建新的BufferedImage对象,绘制图片并设置背景颜色,最后输出转换后的图片到目标文件。
1511

被折叠的 条评论
为什么被折叠?



