第一步:修改pom.xml文件,添加依賴
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.11</version>
</dependency>
第二步:工具類展示(廢話不多,直接乾貨)
此工具類只做簡單示範,具體使用請結合業務實現
public class ImgYsUtil {
//測試原圖
private static File fromPic = new File("F:\\869074038846502_20200610140742_0A_0_0_res.jpg");
//水印
private static File waterPic = new File("F:\\20150327232807070.jpg");
/**
* 壓縮圖片
* @throws Exception
*/
public static void reduceImage() throws Exception{
File toPic = new File("F:\\20150327232556186_test.jpg");
Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);
}
/**
* 修改圖片大小:遵循原圖高寬比例
* @throws Exception
*/
public static void modifyImageSize(int width,int height) throws Exception{
File toPic = new File("F:\\20150327232556186_"+ width + "x" +height + ".jpg");
Thumbnails.of(fromPic).size(width,height).toFile(toPic);
}
/**
* 修改圖片大小:不遵循原圖高寬比例
* @throws Exception
*/
public static void modifyImageSizeKeepAspect(int width,int height) throws Exception{
File toPic = new File("F:\\20150327232556186_keep_"+ width + "x" +height + ".jpg");
Thumbnails.of(fromPic).size(width,height).keepAspectRatio(false).toFile(toPic);
}
/**
* 按照指定比例進行縮小和放大
* @throws Exception
*/
public static void modifyImageSizeByScale(Double scale) throws Exception{
File toPic = new File("F:\\20150327232556186_scale"+ scale + ".jpg");
Thumbnails.of(fromPic).scale(scale).toFile(toPic);
}
/**
* 旋轉圖片
* @param direction 角度 正數正向轉,負數逆向轉
* @throws Exception
*/
public static void modifyImageDirection(int width,int height,double direction) throws Exception{
File toPic = new File("F:\\20150327232556186_direction"+ direction + ".jpg");
Thumbnails.of(fromPic).size(width,height).rotate(direction).toFile(toPic);
}
/**
* 圖片加水印
* @throws Exception
*/
public static void modifyImageAddSignet(int width,int height) throws Exception{
File toPic = new File("F:\\20150327232556186_Signet.jpg");
Thumbnails.of(fromPic)
.size(width,height)
.watermark(Positions.TOP_LEFT, ImageIO.read(waterPic),0.5f)
.outputQuality(1f)
.toFile(toPic);
}
/**
* 圖片裁剪
* @throws Exception
*/
public static void catImage(int width,int height) throws Exception{
File toPic = new File("F:\\20150327232556186_cat.jpg");
Thumbnails.of(fromPic)
.sourceRegion(Positions.CENTER,width,height)
.size(width,height)
.outputQuality(1f)
.toFile(toPic);
/** 也可以自定義定位0,0表示左上角開始
Thumbnails.of(fromPic)
.sourceRegion(0,0,width,height)
.size(width,height)
.outputQuality(1f)
.toFile(toPic);
*/
}
/**
* 轉換圖片格式
* @throws Exception
*/
public static void modifyImageFormat() throws Exception{
File toPic = new File("F:\\20150327232556186_Format.png");
Thumbnails.of(fromPic)
.scale(1f)
.outputFormat("png")
.outputQuality(1f)
.toFile(toPic);
}
/**
* 輸出成文件流OutputStream
* @throws Exception
*/
public static void modifyImageStream() throws Exception{
File toPic = new File("F:\\20150327232556186_stream.png");
OutputStream os=new FileOutputStream(toPic);
Thumbnails.of(fromPic).scale(1f).toOutputStream(os);
}
public static void main(String[] args) throws Exception {
reduceImage(); //壓縮圖片
modifyImageSize(200,200); //修改圖片大小:遵循原圖高寬比例
modifyImageSizeByScale(0.5); //按比例放大縮小
modifyImageSizeByScale(2.0); //按比例放大縮小
modifyImageSizeKeepAspect(200,200); //修改圖片大小:不遵循原圖高寬比例
modifyImageDirection(200,200,90); //旋轉圖片
modifyImageAddSignet(1024,200); //圖片加水印
catImage(200,200); //圖片裁剪
modifyImageFormat(); //轉換圖片格式
modifyImageStream();
}
}
效果