批量图片压缩,测试代码

该代码看着不错,收藏了

package com.dayu.test;

import com.sun.image.codec.jpeg.JPEGCodec;
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.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class ImgCompress {
    private Image img;
    private int width;
    private int height;
    //图片路径
    public static final String READ_FILE_PATH = "C:\Users\Administrator\Desktop\sssssssss\";
    //输出图片的路径
    public static final String WRITE_FILE_PATH = "C:\Users\Administrator\Desktop\sssssssss\output";

    /**
     * 构造函数,读取文件
     */
    public ImgCompress(String fileName) throws IOException {
        File file = new File(fileName);// 读入文件
        img = ImageIO.read(file);      // 构造Image对象
        width = img.getWidth(null);    // 得到源图宽
        height = img.getHeight(null);  // 得到源图长
    }

    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception {
        System.out.println("开始:" + new Date().toLocaleString());
        // 文件名人为重命名,我这里10个图片,一次命名为1-10.jpg        
        for (int i = 1; i <= 10; i++) {
            String fileName=i+".jpg";
        ImgCompress imgCom = new ImgCompress(READ_FILE_PATH +fileName);
        imgCom.resizeFix(1500, 1500,fileName);
        }
        System.out.println("结束:" + new Date().toLocaleString());
    }

    /**
     * 按照宽度还是高度进行压缩
     *  @param w int 最大宽度
     * @param h int 最大高度
     * @param fileName
     */
    public void resizeFix(int w, int h, String fileName) throws IOException {
        if (width / height > w / h) {
            resizeByWidth(w,fileName);
        } else {
            resizeByHeight(h,fileName);
        }
    }

    /**
     * 以宽度为基准,等比例放缩图片
     *
     * @param w int 新宽度
     * @param fileName
     */
    public void resizeByWidth(int w, String fileName) throws IOException {
        int h = (int) (height * w / width);
        resize(w, h,fileName);
    }

    /**
     * 以高度为基准,等比例缩放图片
     *
     * @param h int 新高度
     * @param fileName
     */
    public void resizeByHeight(int h, String fileName) throws IOException {
        int w = (int) (width * h / height);
        resize(w, h,fileName);
    }

    /**
     * 强制压缩/放大图片到固定的大小
     *
     * @param w int 新宽度
     * @param h int 新高度
     */
    public void resize(int w, int h,String fileName) throws IOException {
        // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
        File file=new File(WRITE_FILE_PATH);
        if(!file.exists()){
            file.mkdirs();
        }

        String newFilePath = WRITE_FILE_PATH+File.separator+fileName;
        File destFile = new File(newFilePath);
        FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
        // 可以正常实现bmp、png、gif转jpg
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(image); // JPEG编码
        out.close();
    }
}
原文地址:https://www.cnblogs.com/dayu007/p/8966013.html