图片压缩,用这个就够了

现在的智能手机分辨率都很高,拍的高清照片动辄5M甚至7M。

上传到系统的图片太大了,导致页面加载缓慢。

为此,让组里一小伙做一个压缩工具。发版后,发现图片虽然是压缩了,不过有个别图片严重失真。

 

 然后,在网上查资料,发现有人分享google提供的开源工具Thumbnailator。

maven dependency引入方式:

<dependency>
   <groupId>net.coobird</groupId>
   <artifactId>thumbnailator</artifactId>
   <version>0.4.8</version>
</dependency>

代码简洁:

import net.coobird.thumbnailator.Thumbnails;
import org.junit.Test;

public class ImageUtilTest {

    @Test
    public void googleImgThumb() throws Exception{
        String path="D:\Users\liziqi\130******92-驾驶证照片-org.jpg";
        Thumbnails.of(path)
                .scale(0.5f)
                .outputQuality(1)
                .toFile("D:\Users\liziqi\130******92-驾驶证照片.jpg");
    }
}

通过比较,Google提供的这个工具果然完胜。

 参考:https://www.cnblogs.com/linkstar/p/7412012.html

原文地址:https://www.cnblogs.com/buguge/p/12748407.html