多功能图片压缩工具类

在网上看了一些图片压缩的方法,在这里我利用重载,编写了多种功能的图片压缩工具类

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

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

/**
 * < p>< strong>图片压缩工具类< /strong>< /p>
 * < p>功能支持:< br>
 * 1)将源路径的图片/源图片文件,按照指定的图片尺寸、图片质量(默认图片质量为1)压缩,存放到目标路径下< br>
 * 2)将源路径的图片/源图片文件,按照指定的图片比例、图片质量(默认图片质量为1)压缩,存放到目标路径下< br>
 * 3)将源路径的图片/源图片文件,按照指定的最大长度进行、图片质量(默认图片质量为1)压缩,存放到目标路径下< br>
 * 
 * < p>该工具类还包含其他方法:< br>
 * (一)读取图片文件< br>
 * (二)输出图片文件(根据指定的图片质量输出)< br>
 * (三)压缩图片(其中压缩图片包含三种重构:1)按照指定的尺寸 2)按照指定的比例 3)按照指定的最大长度进行)< br>
 * < /p>
 * @version 1.0.0
 */
public class ImageCompressionUtil {
    /**
     * 将源路径的图片,按照指定的图片尺寸、图片质量压缩,存放到目标路径下
     * @param sourcePath 源图片路径
     * @param targetPath 目标路径
     * @param new_width 压缩后的长度
     * @param new_height 压缩后的高度
     * @param percentage 图片质量百分比
     * @version 1.0.0
     * @throws ImageFormatException 
     * @throws IOException 
     */
    public void compression(String sourcePath, String targetPath, 
            int new_width, int new_height, float percentage) throws ImageFormatException, IOException {
        //得到图片
        BufferedImage sourceImage = inputImage(sourcePath);
        //压缩图片
        BufferedImage targetImage = getCompressionedImage(sourceImage, new_width, new_height);
        //输出图片
        outputImage(targetPath,targetImage,percentage);
    }

    /**
     * 将源路径的图片,按照指定的图片尺寸压缩,存放到目标路径下(默认图片质量为1)
     * @param sourcePath 源图片路径
     * @param targetPath 目标路径
     * @param new_width 压缩后的长度
     * @param new_height 压缩后的高度
     * @version 1.0.0
     * @throws ImageFormatException 
     * @throws IOException 
     */
    public void compression(String sourcePath, String targetPath, 
            int new_width, int new_height) throws ImageFormatException, IOException {
        compression(sourcePath, targetPath, new_width, new_height, 1F);
    }

    /**
     * 将源路径的图片,按照指定的图片比例、图片质量压缩,存放到目标路径下
     * @param sourcePath 源图片路径
     * @param targetPath 目标路径
     * @param ratio 压缩百分比
     * @param percentage 图片质量百分比
     * @version 1.0.0
     * @throws ImageFormatException 
     * @throws IOException 
     */
    public void compression(String sourcePath, String targetPath, 
            float ratio, float percentage) throws ImageFormatException, IOException {
        BufferedImage sourceImage = inputImage(sourcePath);
        BufferedImage targetImage = getCompressionedImage(sourceImage, ratio);
        outputImage(targetPath,targetImage,percentage);
    }

    /**
     * 将源路径的图片,按照指定的图片比例压缩,存放到目标路径下(图片质量默认为1)
     * @param sourcePath 源图片路径
     * @param targetPath 目标路径
     * @param ratio 压缩百分比
     * @version 1.0.0
     * @throws ImageFormatException 
     * @throws IOException 
     */
    public void compression(String sourcePath, String targetPath, 
            float ratio) throws ImageFormatException, IOException {
        compression(sourcePath, targetPath, ratio, 1F);
    }

    /**
     * 将源图片文件,按照指定的图片尺寸、图片质量压缩,存放到目标路径下
     * @param sourceImage 源图片文件
     * @param targetPath 目标路径
     * @param new_width 压缩后的长度
     * @param new_height 压缩后的高度
     * @param percentage 图片质量百分比
     * @version 1.0.0
     * @throws IOException 
     * @throws ImageFormatException 
     */
    public void compression(BufferedImage sourceImage, String targetPath, 
            int new_width, int new_height, float percentage) throws ImageFormatException, IOException {
        BufferedImage targetImage = getCompressionedImage(sourceImage, new_width, new_height);
        outputImage(targetPath,targetImage,percentage);
    }

    /**
     * 将源图片文件,按照指定的图片尺寸压缩,存放到目标路径下(图片质量默认为1)
     * @param sourceImage 源图片文件
     * @param targetPath 目标路径
     * @param new_width 压缩后的长度
     * @param new_height 压缩后的高度
     * @version 1.0.0
     * @throws IOException 
     * @throws ImageFormatException 
     */
    public void compression(BufferedImage sourceImage, String targetPath, 
            int new_width, int new_height) throws ImageFormatException, IOException {
        compression(sourceImage, targetPath, new_width, new_height, 1F);
    }

    /**
     * 将源图片文件,按照指定的图片比例、图片质量压缩,存放到目标路径下
     * @param sourceImage 源图片文件
     * @param targetPath 目标路径
     * @param ratio 压缩百分比
     * @param percentage 图片质量百分比
     * @version 1.0.0
     * @throws IOException 
     * @throws ImageFormatException 
     */
    public void compression(BufferedImage sourceImage, String targetPath, 
            float ratio, float percentage) throws ImageFormatException, IOException {
        BufferedImage targetImage = getCompressionedImage(sourceImage, ratio);
        outputImage(targetPath,targetImage,percentage);
    }

    /**
     * 将源图片文件,按照指定的图片比例压缩,存放到目标路径下(图片质量默认为1)
     * @param sourceImage 源图片文件
     * @param targetPath 目标路径
     * @param ratio 压缩百分比
     * @version 1.0.0
     * @throws IOException 
     * @throws ImageFormatException 
     */
    public void compression(BufferedImage sourceImage, String targetPath, 
            float ratio) throws ImageFormatException, IOException {
        compression(sourceImage, targetPath, ratio, 1F);
    }

    /**
     * 将源路径的图片,按照指定的最大长度进行、图片质量压缩,存放到目标路径下
     * @param sourcePath 源图片路径
     * @param targetPath 目标路径
     * @param maxLength 压缩后的最大长度(长/高)
     * @param percentage 图片质量百分比
     * @version 1.0.0
     * @throws ImageFormatException 
     * @throws IOException 
     */
    public void compression(String sourcePath, String targetPath, 
            int maxLength, float percentage) throws ImageFormatException, IOException {
        BufferedImage sourceImage = inputImage(sourcePath);
        BufferedImage targetImage = getCompressionedImage(sourceImage, maxLength);
        outputImage(targetPath,targetImage,percentage);
    }

    /**
     * 将源路径的图片,按照指定的最大长度进行压缩,存放到目标路径下(图片质量默认为1)
     * @param sourcePath 源图片路径
     * @param targetPath 目标路径
     * @param maxLength 压缩后的最大长度(长/高)
     * @version 1.0.0
     * @throws ImageFormatException 
     * @throws IOException 
     */
    public void compression(String sourcePath, String targetPath, 
            int maxLength) throws ImageFormatException, IOException {
        compression(sourcePath, targetPath, maxLength, 1F);
    }

    /**
     * 将源图片文件,按照指定的最大长度进行、图片质量压缩,存放到目标路径下
     * @param sourceImage 源图片文件
     * @param targetPath 目标路径
     * @param maxLength 压缩后的最大长度(长/高)
     * @param percentage 图片质量百分比
     * @version 1.0.0
     * @throws IOException 
     * @throws ImageFormatException 
     */
    public void compression(BufferedImage sourceImage, String targetPath, 
            int maxLength, float percentage) throws ImageFormatException, IOException {
        BufferedImage targetImage = getCompressionedImage(sourceImage, maxLength);
        outputImage(targetPath,targetImage,percentage);
    }

    /**
     * 将源图片文件,按照指定的最大长度进行压缩,存放到目标路径下(图片质量默认为1)
     * @param sourceImage 源图片文件
     * @param targetPath 目标路径
     * @param maxLength 压缩后的最大长度(长/高)
     * @version 1.0.0
     * @throws IOException 
     * @throws ImageFormatException 
     */
    public void compression(BufferedImage sourceImage, String targetPath, 
            int maxLength) throws ImageFormatException, IOException {
        compression(sourceImage, targetPath, maxLength, 1F);
    }

    /**
     * 读取源图片
     * @param sourcePath 源图片路径
     * @version 1.0.0
     * @throws IOException 
     */
    public BufferedImage inputImage(String sourcePath) throws IOException {
        BufferedImage sourceImage = null;
        //读取文件
        File file = new File(sourcePath);
        //获取文件流
        FileInputStream fis;
        fis = new FileInputStream(file);
        //字节
        byte[] b = new byte[5];
        fis.read(b);
        sourceImage = javax.imageio.ImageIO.read(file);
        fis.close();
        return sourceImage;
    }

    /**
     * 将图片文件输出到目标路径,并设置压缩质量
     * @param targetPath 目标路径
     * @param targetImage 指定图片文件
     * @param percentage 图片质量百分比
     * @version 1.0.0
     * @throws IOException 
     * @throws ImageFormatException 
     */
    public void outputImage(String targetPath,BufferedImage targetImage,
            float percentage) throws ImageFormatException, IOException{
        File file  = new File(targetPath);
        //判断输出的文件夹路径是否存在,不存在则创建
        if(!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        //输出到文件流
        FileOutputStream fos = new FileOutputStream(targetPath);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
        JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(targetImage);
        //压缩质量
        jep.setQuality(percentage, true);
        encoder.encode(targetImage, jep);
        fos.close();
    }

    /**
     * 将源图片进行压缩,得到压缩后的图片
     * @param sourceImage 源图片文件
     * @param new_width 压缩后的长度
     * @param new_height 压缩后的高度
     * @version 1.0.0
     */
    public BufferedImage getCompressionedImage(BufferedImage sourceImage,int new_width, int new_height){
        System.out.println("[" + new Date().toString() + "]-正在压缩图片...");
        //得到原图片的长和高
        int old_width = sourceImage.getWidth();
        int old_height = sourceImage.getHeight();
        //根据原图大小,创建一个空白画布
        BufferedImage blankCanvas = new BufferedImage(old_width,old_height,
                BufferedImage.TYPE_INT_RGB);
        //在新的画布上从生成原图的缩略图
        Graphics2D g2D = blankCanvas.createGraphics();
        g2D.setColor(Color.white);
        //填充,参数x/y分别表示起始坐标(左上角坐标),w/h分别表示长和高(单位是像素)
        g2D.fillRect(0, 0, old_width, old_height);
        g2D.drawImage(sourceImage, 0, 0, old_width, old_height, Color.white, null);
        g2D.dispose();
        BufferedImage targetImage = new BufferedImage(new_width, new_height,
                BufferedImage.TYPE_INT_RGB);
        //缩放图片,getScaledInstance方法实现缩放
        targetImage.getGraphics().drawImage(blankCanvas.getScaledInstance(
                new_width, new_height, Image.SCALE_SMOOTH), 0, 0, null);
        System.out.println("[" + new Date().toString() + "]-压缩完成");
        return targetImage;
    }

    /**
     * 根据比例对图片进行压缩
     * @param sourceImage 源图片文件
     * @param ratio 百分比
     * @version 1.0.0
     */
    public BufferedImage getCompressionedImage(BufferedImage sourceImage,float ratio){
        int old_w = sourceImage.getWidth();  
        int old_h = sourceImage.getHeight();  
        return getCompressionedImage(sourceImage, (int) Math.round(old_w * ratio), (int) Math.round(old_h * ratio));
    }

    /**
     * 根据最大长度或宽对图片进行压缩
     * @param sourceImage 源图片文件
     * @param maxLength 长或者宽的最大值 
     * @version 1.0.0
     */
    public BufferedImage getCompressionedImage(BufferedImage sourceImage,int maxLength){
        int old_width = sourceImage.getWidth();  
        int old_height = sourceImage.getHeight();
        int new_width = 0;  
        int new_height = 0;  
        // 根据图片尺寸压缩比得到新图的尺寸  
        if (old_width > old_height) {  
            // 图片要缩放的比例  
            new_width = maxLength;  
            new_height = (int) Math.round(old_height * ((float) maxLength / old_width));  
        } else {  
            new_width = (int) Math.round(old_width * ((float) maxLength / old_height));  
            new_height = maxLength;  
        }  
        return getCompressionedImage(sourceImage, new_width, new_height);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值