//压缩文件
File readImage = new File(Path);
//压缩后文件
File outImage = new File(Path);
BufferedImage outs=null;
//读入图像字节
BufferedImage ins= ImageIO.read(readImage);
//取得长宽
int width= ins.getWidth();
int height= ins.getHeight();
//按比例缩放
if(width-height>0){
height=height*110/width;
width=110;
}else{
width=width*110/height;
height=110;
}
//新图片字节实例
outs= new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
//新图片的Graphics实例,用来处理图像
Graphics g = outs.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0, width, height);
g.drawImage(ins,0,0,width,height,null);
//把新图片字节写入图片文件
ImageIO.write(outs, "jpeg", outImage);
完成。
java 图片缩放
最新推荐文章于 2024-10-06 20:16:20 发布