java图片批量压缩工具类(传入文件源地址和文件新地址,不改变文件目录结构)

 

import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.file.Files;
import java.util.List;

import javax.imageio.ImageIO;

public  class  ImgCompressUtil {
    static BufferedImage img = null;
    //static List<String> list = null;
    private static final Logger logger = LoggerFactory.getLogger(ImgCompressUtil.class);

    /**
     * 构造函数
     */
    public   ImgCompressUtil(String fileName) throws IOException {

        File file = new File(fileName);// 读入文件
        img = ImageIO.read(file);      // 构造Image对象
    }


    /**
     * 图片转换
     * @param newUrl
     * @param sourceUrl
     * @throws IOException
     */
    public static void ImageCompression(String newUrl, String  sourceUrl) throws IOException {
        
        System.out.println("开始压缩");
        operationImg(newUrl,sourceUrl);
        System.out.println("压缩完成");
    }

    /**
     *图片操作
     * @param newUrl
     * @param sourceUrl
     * @throws IOException
     */
    public static void operationImg(String newUrl , String sourceUrl) throws IOException   {
        File folder = new File(sourceUrl);
        String url = null;
        File[] files = folder.listFiles();
        for (File file : files) {
            try {
            if (file.isFile()) {
                 url = sourceUrl + '\\' + file.getName();
                String newsUrl = newUrl ;
                String allNewsUrl = newsUrl + '\\' + file.getName();
//                System.out.println("源地址" + url);
//                System.out.println("新地址" + newsUrl);
                   // if (list.contains(file.getName())){
                        ImgInformation information = ImgCompressUtil.getImgInformation(url);

                        File fileMkdir = new File(newsUrl);
                        fileMkdir.mkdirs();
                        if (Float.parseFloat(information.getImgSize()) > 3000){
                            ImgCompressUtil imgCom = new ImgCompressUtil(url);
                            imgCom.resize(information.getImgWidth(), information.getImgHeight(),allNewsUrl);
                        }else {
                            File fileurl = new File(url);
                            File allNewsUrlFile = new File(allNewsUrl);
                            Files.copy(fileurl.toPath(), allNewsUrlFile.toPath());
                        }
                   // }




            }else{
                String imgUrl = sourceUrl + '\\' + file.getName();
                String newsUrl = newUrl + '\\' + file.getName();
                operationImg(newsUrl,imgUrl);
            }
            } catch (Exception e) {
//                System.err.println("问题图片:" + url + "报错信息:" + e);
                logger.info("问题图片:" + url + "报错信息:" + e);

            }
            continue;
        }
    }

    /**
     * 获取图片信息
     * @param url
     * @return
     * @throws IOException
     */
   public static ImgInformation getImgInformation(String url) throws IOException {
        File picture = new File(url);
       BufferedImage sourceImg = ImageIO.read(picture);


       ImgInformation information =new ImgInformation();
       //        源图大小
       information.setImgSize(String.format("%.1f",picture.length()/1024.0));
       //        源图宽度
       information.setImgWidth(sourceImg.getWidth());
       // 源图高度
       information.setImgHeight(sourceImg.getHeight());
       return information;
        }


    /**
     * 强制压缩/放大图片到固定的大小
     *
     * @param w int 新宽度
     * @param h int 新高度

     * allNewsUrl 目标路径 + 图片名
     */
    public static void resize(int w, int h, String allNewsUrl) throws IOException {
        // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
        File destFile = new File(allNewsUrl);
//        destFile.mkdirs();
        FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
        // 可以正常实现bmp、png、gif转jpg
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(image); // JPEG编码
        out.close();
    }

}
class ImgInformation{
    private String imgSize;
    private int imgWidth;
    private int imgHeight;

    public String getImgSize() {
        return imgSize;
    }

    public void setImgSize(String imgSize) {
        this.imgSize = imgSize;
    }

    public int getImgWidth() {
        return imgWidth;
    }

    public void setImgWidth(int imgWidth) {
        this.imgWidth = imgWidth;
    }

    public int getImgHeight() {
        return imgHeight;
    }

    public void setImgHeight(int imgHeight) {
        this.imgHeight = imgHeight;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值