图片压缩

maven所需依赖
<!-- 图片缩略图 -->
            <dependency>
                <groupId>net.coobird</groupId>
                <artifactId>thumbnailator</artifactId>
                <version>0.4.8</version>
            </dependency>
<!-- cmyk格式图片转换 -->    
        <dependency>        
            <groupId>com.twelvemonkeys.imageio</groupId>        
            <artifactId>imageio-jpeg</artifactId>        
            <version>3.3</version>    
        </dependency>    
        <dependency>        
            <groupId>com.twelvemonkeys.imageio</groupId>        
            <artifactId>imageio-tiff</artifactId>        
            <version>3.3</version>    
        </dependency>

Thumbnails.of("path/to/image")
    .size(10, 10)

.toFile("path/to/thumbnail");

List<String> filenames = new ArrayList<String>();
filenames.add("path/to/image1.jpg");
filenames.add("path/to/image2.jpg");

Thumbnails.of(filenames)
    .size(200, 200)
    .toFiles(Rename.PREFIX_DOT_THUMBNAIL);

Thumbnails.of("/path/to/image-400x300")
    .width(200)
    .toFile("/path/to/thumbnail-200x150")
Thumbnails.of("/path/to/image")
    .sourceRegion(Positions.CENTER, 400, 400)
    .size(200, 200)
    .toFile("/path/to/thumbnail")


https://github.com/coobird/thumbnailator/wiki/Changes


package cn.wujinkuaibao.common;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;


import javax.imageio.ImageIO;


import net.coobird.thumbnailator.Thumbnails;


public class ImageCompress {
/**
* ImageCompress.commpressPicForScale("F:\\111\\777.jpg","F:\\111\\7777.jpg", 500, 0.8); // 图片小于500kb

     * 根据指定大小和指定精度压缩图片
     * 
     * @param srcPath
     *            源图片地址
     * @param desPath
     *            目标图片地址
     * @param desFilesize
     *            指定图片大小,单位kb
     * @param accuracy
     *            精度,递归压缩的比率,建议小于0.9
     * @return
     */
    public static String commpressPicForScale(String srcPath, String desPath,
            long desFileSize, double accuracy) {
//        if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(srcPath)) {
//            return null;
//        }
    if("".equals(srcPath) || srcPath == null || "".equals(desPath) || desPath == null) {
    return null;
    }
        if (!new File(srcPath).exists()) {
            return null;
        }
        try {
            File srcFile = new File(srcPath);
            long srcFileSize = srcFile.length();
            System.out.println("源图片:" + srcPath + ",大小:" + srcFileSize / 1024
                    + "kb");
 
            // 1、先转换成jpg
            Thumbnails.of(srcPath).scale(1f).toFile(desPath);
            // 递归压缩,直到目标文件大小小于desFileSize
            commpressPicCycle(desPath, desFileSize, accuracy);
 
            File desFile = new File(desPath);
            System.out.println("目标图片:" + desPath + ",大小" + desFile.length()
                    / 1024 + "kb");
            System.out.println("图片压缩完成!");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return desPath;
    }
 
    private static void commpressPicCycle(String desPath, long desFileSize,
            double accuracy) throws IOException {
        File srcFileJPG = new File(desPath);
        long srcFileSizeJPG = srcFileJPG.length();
        // 2、判断大小,如果小于500kb,不压缩;如果大于等于500kb,压缩
        if (srcFileSizeJPG <= desFileSize * 1024) {
            return;
        }
        // 计算宽高
        BufferedImage bim = ImageIO.read(srcFileJPG);
        int srcWdith = bim.getWidth();
        int srcHeigth = bim.getHeight();
        int desWidth = new BigDecimal(srcWdith).multiply(
                new BigDecimal(accuracy)).intValue();
        int desHeight = new BigDecimal(srcHeigth).multiply(
                new BigDecimal(accuracy)).intValue();
 
        Thumbnails.of(desPath).size(desWidth, desHeight)
                .outputQuality(accuracy).toFile(desPath);
        commpressPicCycle(desPath, desFileSize, accuracy);
    }
}



package cn.wujinkuaibao.common;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;


import javax.imageio.ImageIO;


import net.coobird.thumbnailator.Thumbnails;


public class ImageCompress {
/**
* ImageCompress.commpressPicForScale("F:\\111\\777.jpg","F:\\111\\7777.jpg", 500, 0.8); // 图片小于500kb

     * 根据指定大小和指定精度压缩图片
     * 
     * @param srcPath
     *            源图片地址
     * @param desPath
     *            目标图片地址
     * @param desFilesize
     *            指定图片大小,单位kb
     * @param accuracy
     *            精度,递归压缩的比率,建议小于0.9
     * @return
     */
    public static String commpressPicForScale(String srcPath, String desPath,
            long desFileSize, double accuracy) {
//        if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(srcPath)) {
//            return null;
//        }
    if("".equals(srcPath) || srcPath == null || "".equals(desPath) || desPath == null) {
    return null;
    }
        if (!new File(srcPath).exists()) {
            return null;
        }
        try {
            File srcFile = new File(srcPath);
            long srcFileSize = srcFile.length();
            System.out.println("源图片:" + srcPath + ",大小:" + srcFileSize / 1024
                    + "kb");
 
            // 1、先转换成jpg
            Thumbnails.of(srcPath).scale(1f).toFile(desPath);
            // 递归压缩,直到目标文件大小小于desFileSize
            commpressPicCycle(desPath, desFileSize, accuracy);
 
            File desFile = new File(desPath);
            System.out.println("目标图片:" + desPath + ",大小" + desFile.length()
                    / 1024 + "kb");
            System.out.println("图片压缩完成!");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return desPath;
    }
 
    private static void commpressPicCycle(String desPath, long desFileSize,
            double accuracy) throws IOException {
        File srcFileJPG = new File(desPath);
        long srcFileSizeJPG = srcFileJPG.length();
        // 2、判断大小,如果小于500kb,不压缩;如果大于等于500kb,压缩
        if (srcFileSizeJPG <= desFileSize * 1024) {
            return;
        }
        // 计算宽高
        BufferedImage bim = ImageIO.read(srcFileJPG);
        int srcWdith = bim.getWidth();
        int srcHeigth = bim.getHeight();
        int desWidth = new BigDecimal(srcWdith).multiply(
                new BigDecimal(accuracy)).intValue();
        int desHeight = new BigDecimal(srcHeigth).multiply(
                new BigDecimal(accuracy)).intValue();
 
        Thumbnails.of(desPath).size(desWidth, desHeight)
                .outputQuality(accuracy).toFile(desPath);
        commpressPicCycle(desPath, desFileSize, accuracy);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值