JAVA 图片压缩

package image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
* 功能描述:图片处理器
* 作者:唐泽齐
*/
public class ImageUtils {

public static void main(String[] args) {
try {
//原图片地址
String filePath = "image/test.jpg";
//要压缩的效果
int newHeight = 600;
int newWidth = 400;
//压缩后图片存放位置
String newPath = "image/1.jpg";
BufferedImage read = ImageIO.read(new File(filePath));
int height = read.getHeight();
int width = read.getWidth();
System.out.println("读取图片大小 = " + width + "X" + height);
System.out.println("缩放图片大小 = " + newWidth + "X" + newHeight);
//获取缩放的图片
Image image = read.getScaledInstance(newWidth, newHeight, read.getType());
//将缩放图片存入缓存
BufferedImage bufferedImage = new BufferedImage(newWidth, newHeight, read.getType());
Graphics graphics = bufferedImage.getGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
//输出图片
ImageIO.write(bufferedImage, "jpg", new File(newPath));
} catch (IOException e) {
e.printStackTrace();
}
}

}

posted on 2023-02-03 16:29  instr  阅读(276)  评论(0编辑  收藏  举报

导航