图片压缩指定大小

我们经常会遇到一些关于图片大小的问题,比如我最大只能容忍500KB,你给我传一个600KB的,或者更大的,这个时候就很不舒服了,而且为了服务器存储压力,也建议一般限制上传文件的大小。下面是之前遇到需要压缩文件大小时写的工具类(借鉴了网上的代码,仅此做个分享和后续查阅),代码中有两处被注释的代码,往下的第三行代码是用来替代前两行代码的,为什么要做这个替代呢?因为com.sun.image.codec.jpeg这个包sun公司开发的封闭类,在jdk1.8中被oracle去除掉了,所以当在jdk1.8环境下编译是会通过不了的,为了各个版本兼容考虑,所以这里将这两行代码用第三行替换掉了。

package com.huifu.test.others;

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

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

public class ImageUtils {

    /**
     *
     * @param srcBytes 源文件二进制byte
     * @param maxSize 最大大小 KB
     * @return
     * @throws Exception
     */
    public static byte[]  CompressPic(byte[] srcBytes,int maxSize) throws Exception {
        double cutPercent=0.1;
        ByteArrayOutputStream deskImage = null;
        BufferedImage bufferedImage= ImageIO.read(new ByteArrayInputStream(srcBytes));
        int width=bufferedImage.getWidth(null);
        int height=bufferedImage.getHeight(null);
        long fileLength=srcBytes.length;
        while((fileLength/1024)>=maxSize) {
            width=(int)(width*(1-cutPercent));
            height=(int)(height*(1-cutPercent));
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
            tag.getGraphics().drawImage(bufferedImage, 0, 0, width, height, null); // 绘制缩小后的图
            deskImage = new ByteArrayOutputStream(); // 输出到文件流
//            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(deskImage);
//            encoder.encode(tag); // 近JPEG编码
            ImageIO.write(tag,"jpg",deskImage);
            deskImage.close();

            BufferedImage bufferedImage1=ImageIO.read(new ByteArrayInputStream(deskImage.toByteArray()));
            width=bufferedImage1.getWidth(null);
            height=bufferedImage1.getHeight(null);
            fileLength=deskImage.toByteArray().length;
        }
        if(deskImage!=null){
            return deskImage.toByteArray();
        }
        return null;
    }


    public static String CompressPic(String srcPath,String targetPath,int maxSize) throws Exception {
        double cutPercent=0.1;
        File file=new File(srcPath);
        BufferedImage bufferedImage= ImageIO.read(new FileInputStream(file));
        int width=bufferedImage.getWidth(null);
        int height=bufferedImage.getHeight(null);
        long fileLength=file.length();
        while((fileLength/1024)>=300) {
            width=(int)(width*(1-cutPercent));
            height=(int)(height*(1-cutPercent));
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
            tag.getGraphics().drawImage(bufferedImage, 0, 0, width, height, null); // 绘制缩小后的图
            FileOutputStream deskImage = new FileOutputStream(targetPath); // 输出到文件流
//            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(deskImage);
//            encoder.encode(tag); // 近JPEG编码
            ImageIO.write(tag,"jpg",deskImage);
            deskImage.close();

            File file1=new File(targetPath);
            BufferedImage bufferedImage1=ImageIO.read(new FileInputStream(file1));
            width=bufferedImage1.getWidth(null);
            height=bufferedImage1.getHeight(null);
            fileLength=file1.length();
        }

        return targetPath;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_30095631/article/details/106014136