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"));
}
}
效果
原圖
縮放後的圖
添加水印照片