java压缩图片 生成缩略图

该博客介绍了一种使用Java进行图片压缩的方法,通过设置宽度、高度和质量参数,实现图片的等比例或固定尺寸压缩。代码中使用了`JPEGCodec`和`BufferedImage`等库,生成的压缩图片保存为新的文件,并保留原文件的扩展名。适用于需要处理大量图片的场景,例如网站缩略图生成。
摘要由CSDN通过智能技术生成

java压缩图片 生成缩略图

package com.test;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

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

public class PicCompression {

    public static void main(String[] args) {
        String path = "F:\\big.jpg";
        doCompress(path, 280, 280, 0.5f, "_small", true);
    }

    /**
     * 压缩图片方法
     * @param oldFile    将要压缩的图片
     * @param width      压缩宽
     * @param height     压缩高
     * @param quality    压缩清晰度 <b>建议为1.0</b>
     * @param smallIcon  压缩图片后,添加的扩展名(在图片后缀名前添加)
     * @param percentage 是否等比压缩 若true宽高比率将将自动调整
     * @return 如果处理正确返回压缩后的文件名 null则参数可能有误
     */
    public static String doCompress(String oldFile, int width, int height, float quality, String smallIcon, boolean percentage) {
        if (oldFile != null && width > 0 && height > 0) {
            Image srcFile = null;
            String newImage = null;
            FileOutputStream out = null;
            try {
                File file = new File(oldFile);
                // 文件不存在
                if (!file.exists()) {
                    return null;
                }
                /*读取图片信息*/
                srcFile = ImageIO.read(file);
                int new_w = width;
                int new_h = height;
                if (percentage) {
                    // 为等比缩放计算输出的图片宽度及高度
                    double rate1 = ((double) srcFile.getWidth(null)) / (double) width;
                    double rate2 = ((double) srcFile.getHeight(null)) / (double) height;
                    double rate = rate1 > rate2 ? rate1 : rate2;
                    new_w = (int) (((double) srcFile.getWidth(null)) / rate);
                    new_h = (int) (((double) srcFile.getHeight(null)) / rate);
                }
                /* 宽高设定*/
                BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
                tag.getGraphics().drawImage(srcFile, 0, 0, new_w, new_h, null);

                /*压缩后的文件名 */
                String filePrex = oldFile.substring(0, oldFile.lastIndexOf('.'));
                newImage = filePrex + smallIcon + oldFile.substring(filePrex.length());

                /*压缩之后临时存放位置*/
                out = new FileOutputStream(newImage);

                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);

                /* 压缩质量 */
                jep.setQuality(quality, true);
                encoder.encode(tag, jep);


            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                srcFile.flush();
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return newImage;
        } else {
            return null;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值