文件管理-java压缩文件(文件太大,浪费资源)

1 新建ImageCompressUtil.java工具类

package common.util;

import java.io.*;
import java.util.Date;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.*;

/**
 * 图片压缩处理
 */
public class ImageCompressUtil {
    private Image img;
    private int width;
    private int height;

    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception {
        System.out.println("开始:" + new Date().toLocaleString());
        ImageCompressUtil imgCom = new ImageCompressUtil("G://3.gif");
        File srcfile = new File("G://3.gif");
        String path = "G://3.gif";
        imgCom.resizeFix(getImgWidth(srcfile)[0], getImgWidth(srcfile)[1],path);
        System.out.println("结束:" + new Date().toLocaleString());
    }
    
    /**
     * 图片压缩工具类
     * @return
     */
    public static String ImgCompressUtil(String path){
        try {
            System.out.println("开始压缩");
            ImageCompressUtil imgCom = new ImageCompressUtil(path);
            File srcfile = new File(path);
            imgCom.resizeFix(getImgWidth(srcfile)[0], getImgWidth(srcfile)[1],path);
            System.out.println("结束压缩");
            return "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            return "ERROR";
        }
    }
    
    

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

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

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

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

    /**
     * 强制压缩/放大图片到固定的大小
     * 
     * @param w
     *            int 新宽度
     * @param h
     *            int 新高度
     *    path:图片路径
     */
    public void resize(int w, int h,String path) 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(path);
        FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
        // 可以正常实现bmp、png、gif转jpg
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(image); // JPEG编码
        out.close();
    }

    /**
     * 获取图片宽度
     * 
     * @param file
     *            图片文件
     * @return 宽度
     */
    public static int[] getImgWidth(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int result[] = { 0, 0 };
        try {
            is = new FileInputStream(file);
            src = javax.imageio.ImageIO.read(is);
            result[0] = src.getWidth(null); // 得到源图宽
            result[1] = src.getHeight(null); // 得到源图高
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

2 使用方式

String path = filePath+fileName;
ImageCompressUtil imgCom = new ImageCompressUtil(path);
imgCom.resizeFix(ImageCompressUtil.getImgWidth(myFile)[0], ImageCompressUtil.getImgWidth(myFile)[1],path);

案例图:

文件管理-java压缩文件(文件太大,浪费资源)_第1张图片

 

 

 

你可能感兴趣的:(java基础知识点,java,开发语言,p2p)