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   instr  阅读(280)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示