图片压缩工具类

package org.wxz.tools.oracle.io.file.image.util;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.wxz.tools.oracle.exception.print.util.ExceptionPrintUtil;
import org.wxz.tools.oracle.io.file.util.FileCoverUtil;
import org.wxz.tools.oracle.io.file.util.FileFormatUtil;

/**
* 图片压缩工具类
*
* @author XiongZhi.Wu 2017年12月10日
*/
public abstract class ImageZipUtil {

// 默认是否要覆盖
private static final boolean DEFAULT_COVER = false;

/**
 * 压缩
 * 
 * @author XiongZhi.Wu 2017年12月10日
 * @param source
 *            源图片路径
 * @param target
 *            新图片路径
 * @param width
 *            新图片宽度
 * @param heigth
 *            新图片高度
 * @return
 */
public static boolean zip(String source, String target, int width, int heigth) {
    return zip(source, target, width, heigth, DEFAULT_COVER);
}

/**
 * 压缩
 * 
 * @author XiongZhi.Wu 2017年12月10日
 * @param source
 *            源图片路径
 * @param target
 *            新图片路径
 * @param width
 *            新图片宽度
 * @param heigth
 *            新图片高度
 * @param cover
 *            是否要覆盖
 * @return
 */
public static boolean zip(String source, String target, int width, int heigth, boolean cover) {
    return zip(new File(source), new File(target), width, heigth, cover);
}

/**
 * 压缩
 * 
 * @author XiongZhi.Wu 2017年12月10日
 * @param source
 *            源图片文件
 * @param target
 *            新图片路径
 * @param width
 *            新图片宽度
 * @param heigth
 *            新图片高度
 * @return
 */
public static boolean zip(File source, String target, int width, int heigth) {
    return zip(source, target, width, heigth, DEFAULT_COVER);
}

/**
 * 压缩
 * 
 * @author XiongZhi.Wu 2017年12月10日
 * @param source
 *            源图片文件
 * @param target
 *            新图片路径
 * @param width
 *            新图片宽度
 * @param heigth
 *            新图片高度
 * @param cover
 *            是否要覆盖
 * @return
 */
public static boolean zip(File source, String target, int width, int heigth, boolean cover) {
    return zip(source, new File(target), width, heigth, cover);
}

/**
 * 压缩
 * 
 * @author XiongZhi.Wu 2017年12月10日
 * @param source
 *            源图片路径
 * @param target
 *            新图片文件
 * @param width
 *            新图片宽度
 * @param heigth
 *            新图片高度
 * @return
 */
public static boolean zip(String source, File target, int width, int heigth) {
    return zip(source, target, width, heigth, DEFAULT_COVER);
}

/**
 * 压缩
 * 
 * @author XiongZhi.Wu 2017年12月10日
 * @param source
 *            源图片路径
 * @param target
 *            新图片文件
 * @param width
 *            新图片宽度
 * @param heigth
 *            新图片高度
 * @param cover
 *            是否要覆盖
 * @return
 */
public static boolean zip(String source, File target, int width, int heigth, boolean cover) {
    return zip(new File(source), target, width, heigth, cover);
}

/**
 * 压缩
 * 
 * @author XiongZhi.Wu 2017年12月10日
 * @param source
 *            源图片文件
 * @param target
 *            新图片文件
 * @param width
 *            新图片宽度
 * @param heigth
 *            新图片高度
 * @return
 */
public static boolean zip(File source, File target, int width, int heigth) {
    return zip(source, target, width, heigth, DEFAULT_COVER);
}

/**
 * 压缩
 * 
 * @author XiongZhi.Wu 2017年12月10日
 * @param source
 *            源图片文件
 * @param target
 *            新图片文件
 * @param width
 *            新图片宽度
 * @param heigth
 *            新图片高度
 * @param cover
 *            是否要覆盖
 * @return
 */
public static boolean zip(File source, File target, int width, int heigth, boolean cover) {
    if (FileCoverUtil.needCover(target, cover)) {
        try {
            BufferedImage bufferedImage = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
            bufferedImage.getGraphics().drawImage(ImageIO.read(source), 0, 0, width, heigth, null);
            ImageIO.write(bufferedImage, FileFormatUtil.format(target), target);
        } catch (Exception e) {
            throw new RuntimeException(ExceptionPrintUtil.initExceptionPrintModel(e, "Image Zip Cause Exception."));
        }
    }
    return true;
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值