java压缩图片

java压缩图片

 

复制代码
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
 
public class ImageResizer {
 
    public static void resizeImage(String inputImagePath, String outputImagePath, int targetWidth, int targetHeight) throws IOException {
        File inputFile = new File(inputImagePath);
        BufferedImage inputImage = ImageIO.read(inputFile);
 
        BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = outputImage.createGraphics();
 
        // 缩放图片
        Image img = inputImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
        g2d.drawImage(img, 0, 0, null);
        g2d.dispose();
 
        // 输出图片
        ImageIO.write(outputImage, "jpg", new File(outputImagePath));
    }
 
    public static void main(String[] args) {
        try {
            resizeImage("input.jpg", "output.jpg", 100, 100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
复制代码

 

 

#################

posted @   西北逍遥  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2023-04-03 QLabel 显示opencv Mat
2022-04-03 WebClient异步请求
2021-04-03 PCL point cloud
2020-04-03 IfcRelConnects
2020-04-03 IfcRelationship
2019-04-03 ubuntu18(笔记本) faster-rcnn实例程序运行
点击右上角即可分享
微信分享提示