java图片压缩(谷歌工具类)
引入jar依赖dependency>groupId>net.coobirdgroupId>artifactId>thumbnailatorartifactId>version>0.4.8version>dependency>直入主题-工具类package com.vue.img;import java.io.File;import java.io.IOExceptio
·
引入jar依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
直入主题-工具类
package com.vue.img;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
/**
*
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
谷歌图片压缩
*/
public class GoogleCompressImg {
private static final String basepath="D:\\picture\\pics";
public static void main(String[] args) {
File[] files=getFileList(basepath);
for (File file : files) {
System.out.println(file.getAbsolutePath());
googleCompress(file.getAbsolutePath(), "D:\\picture\\"+file.getName()+"_test"+file.getName().substring(file.getName().indexOf(".")));
}
}
public static void googleCompress(String olderImg,String newImg){
try {
/**
* scale图片长宽 outputQuality图片质量
*/
//Thumbnails.of(olderImg).scale(0.5f).outputQuality(0.5f).toFile(newImg);
/**
* 指定大小
*/
//Thumbnails.of(olderImg).size(300, 300).toFile(newImg);
/**
* keepAspectRatio(false)按比例
*/
//Thumbnails.of(olderImg).size(120, 120).keepAspectRatio(false).toFile(newImg);
/**
* rotate(角度),正数:顺时针 负数:逆时针
*/
// Thumbnails.of(olderImg).size(1280, 1024).rotate(90).toFile(newImg);
/**
* watermark(位置,水印图,透明度)
*/
Thumbnails.of(olderImg).size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File(olderImg)), 0.5f).outputQuality(0.8f).toFile(newImg);
/**
* 裁剪
*/
// Thumbnails.of(olderImg).sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false).toFile(newImg);
/**
* 转换图像格式
*/
// Thumbnails.of(olderImg).size(1280, 1024).outputFormat("png").toFile(newImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static File[] getFileList(String path){
File file=new File(path);
File[] filelist=null;
if(file.exists()){
filelist=file.listFiles();
}
return filelist;
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)