处理图片 Thumbnails
1、pom
<!-- 图片处理 -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
2、修改图片尺寸
// 图片原始路径
private String oldsrc = "D:/HandleTools/picture/test.png";
// 处理之后的图片路径
private String changeSizePicSrc = "D:/HandleTools/picture/changesize.png";
/**
* 修改图片大小
* @throws IOException
*/
@Test
public void changesize() throws IOException {
Thumbnails.of(oldsrc).size(1000,500).toFile(changeSizePicSrc);
}
3、图片等比缩放
/**
* 图片等比缩放
* @throws IOException
* 0.5:等比缩放50%
*/
@Test
public void caling() throws IOException{
Thumbnails.of(oldsrc).scale(0.5).toFile(changePicSrc);
}
4、图片旋转
/**
* 图片旋转
* @throws IOException
* 1:原图
* 180:旋转180
*/
@Test
public void rotating() throws IOException{
Thumbnails.of(oldsrc).scale(1).rotate(180).toFile(changePicSrc);
}
5、图片裁剪
/**
* 图片旋转 居中裁剪 600*600
* @throws IOException
* Positions.CENTER:位置 枚举
* 600:
* 600:
*/
@Test
public void tailoring() throws IOException{
Thumbnails.of(oldsrc).scale(1).sourceRegion(Positions.CENTER,600,600).toFile(changePicSrc);
}
6、添加水印
// 水印图片地址
private String watermark = "D:/HandleTools/picture/watermark.jpg";
/**
* 添加水印
* @throws IOException
* Positions.BOTTOM_RIGHT: 谁赢位置
* ImageIO.read(new File(watermark)):水印图片
* 0.5f:透明度
*/
@Test
public void watermark() throws IOException {
Thumbnails.of(oldsrc).scale(1).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(watermark)),0.5f).toFile(changePicSrc);
}