文件/图片压缩工具

使用Thumbnails压缩工具进行压缩

导包

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>

工具类

package cn.oxe.utils;

import net.coobird.thumbnailator.Thumbnails;
import org.springframework.util.ObjectUtils;

import java.io.*;


/*
 *@author cirry
 *@Date: 2021/12/10$ 9:56$
 */
public class FileUtil {

    public static void main(String[] args) throws Exception {
        //输入文件
        File inFile = new File("C:/Users/86156/Desktop/11.jpg");

        byte[] bytes = zipFile(FileUtil.fileToByte(inFile), 300 * 1024, 0.9f);
        System.out.println(bytes.length);
        
        //输出文件
        File outFile = new File("C:/Users/86156/Desktop/300.jpg");
        FileUtil.byteToFile(bytes, outFile);
    }
    
    /**
     * 压缩文件 压到指定大小
     *
     * @param bytes     文件byte
     * @param maxSize   压缩到此值以内
     * @param ziPratio  压缩比率 取值0-1 越大越接近原图
     * @return
     */
    public static byte[] zipFile(byte[] bytes, long maxSize, float ziPratio){
        System.out.println("开始压缩图片, 原始图片大小: " + bytes.length);
        if (bytes.length < maxSize){
            return bytes;
        }
        ByteArrayInputStream ins = new ByteArrayInputStream(bytes);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            Thumbnails.of(ins)
                    .scale(ziPratio) //图片大小(长宽)压缩 从0按照
                    .outputQuality(ziPratio) //图片质量压缩比例 从0-1,越接近1质量越好
                    .toOutputStream(out);       //输出流输出
//                    .toOutputStream(new FileOutputStream("C:/Users/86156/Desktop/11_10%.jpg"))    //文件输出
        } catch (IOException e) {
            e.printStackTrace();
        }

        //递归压缩 压倒指定大小为止
        return zipFile(out.toByteArray(), maxSize, ziPratio);
    }


    /**
     * 文件转byte[]
     *
     * @param file
     * @return
     */
    public static byte[] fileToByte(File file){
        if (ObjectUtils.isEmpty(file)){
            return null;
        }
        byte[] fb = null;
        FileInputStream fs = null;
        try {
            fs = new FileInputStream(file);
            fb = new byte[(int)file.length()];
            fs.read(fb);
            return fb;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (null != fs){
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fb;
    }

    /**
     * byte数组转文件
     *
     * @param bytes
     * @param file
     */
    public static void byteToFile(byte[] bytes, File file) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try {
            //输出流
            fos = new FileOutputStream(file);

            //缓冲流
            bos = new BufferedOutputStream(fos);

            //将字节数组写出
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值