上传图片时对图片压缩

//newFilePath 表示图片存放的路径     w表示图片的原宽   h表示图片的原高
//inputStream   表示文件流
//压缩图片
public static boolean uploadJPGfile(InputStream inputStream,String newFilePath ,int w, int h) throws Exception {
   
int targetW = 700;
int targetH = 600;

double sx = (double)targetW/w;
double sy = (double)targetH/h;

if(sx < sy){
sx = sy;
targetW = (int)(sx * w);
}else{
sy = sx;
targetH = (int)(sy * h);
}
BufferedImage tag = new BufferedImage(targetW, targetH,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(ImageIO.read(inputStream), 0, 0, targetW, targetH, null);
FileOutputStream out = new FileOutputStream(newFilePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
return true;
}

你可能感兴趣的:(压缩)