JAVA 图片处理 缩放,分辨率 添加水印,旋转
使用谷歌开源工具 thumbnailator
github地址:https://github.com/coobird/thumbnailator
maven地址:https://mvnrepository.com/artifact/net.coobird/thumbnailator
快速上手
导入依赖
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
测试
package com.example.javae2;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class TestMain {
public static void main(String[] args) throws Exception {
TestMain.test1();
}
/**
* 根据已有照片创建一个缩略图
* @throws IOException
*/
public static void test0() throws IOException {
Thumbnails.of(new File("C:\\temp\\javae2\\11.jpg"))
.size(460, 460)
.outputQuality(1)
.toFile(new File("C:\\temp\\12.jpg"));
}
/**
* 创建一个图片旋转并且添加水印
* @throws IOException
*/
public static void test1() throws IOException {
Thumbnails.of(new File("C:\\temp\\javae2\\11.jpg"))
.size(860, 860)
.rotate(90)
.watermark(Positions.CENTER_RIGHT, ImageIO.read(new File("C:\\temp\\javae2\\water.png")), 0.5f)
.outputQuality(0.8)
.toFile(new File("C:\\temp\\javae2\\water-print.jpg"));
}
}
效果
原图
缩放后的图
添加水印照片