压缩图片
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public static void compressImage(File srcFile, File outFile, double compressRatio,String imgeType) throws IOException {
BufferedImage srcImage = ImageIO.read(srcFile);
int width = (int) (srcImage.getWidth() * compressRatio);
int height = (int) (srcImage.getHeight() * compressRatio);
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImage, 0, 0, width, height, null);
g.dispose();
ImageIO.write(resizedImage, imgeType, outFile);
}
public static void main(String[] args) throws IOException {
File srcFile = new File("D:/project/..");
File outFile = new File("D:/project/..");
compressImage(srcFile, outFile, 0.5,"原图片的格式或者写死,按自己需求,写死支持的格式多");
}