1. 程式人生 > >google使用的開源的工具類Thumbnailator圖像處理

google使用的開源的工具類Thumbnailator圖像處理

順時針 dex com 圖片 mave image alt tid write

maven依賴 <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency> 使用接口例子 https://github.com/coobird/thumbnailator/wiki/Examples 1、指定大小進行縮放

Thumbnails.of("images/a380_1280x1024.jpg").size(200, 300)

.toFile("c:/a380_200x300.jpg");

2、按照比例進行縮放

Thumbnails.of("images/a380_1280x1024.jpg").scale(0.25f).toFile("c:/a380_25%.jpg");

3、不按照比例,指定大小進行縮放

//keepAspectRatio(false) 默認是按照比例縮放的

Thumbnails.of("images/a380_1280x1024.jpg").size(200,200).keepAspectRatio(false).toFile("c:/a380_200x200.jpg");

4、旋轉

//rotate(角度),正數:順時針負數:逆時針 Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280,1024).rotate(90).toFile("c:/a380_rotate+90.jpg"); 5、水印

Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(newFile("images/watermark.png")),0.5f).outputQuality(0.8f).toFile("c:/a380_watermark_bottom_right.jpg");

6、裁剪

//圖片中心400*400的區域

Thumbnails.of("images/a380_1280x1024.jpg")

.sourceRegion(Positions.CENTER,400,400).size(200,200).keepAspectRatio(false).toFile("c:/a380_region_center.jpg");

7、轉化圖像格式

Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).outputFormat("png").toFile("c:/a380_1280x1024.png");

8、輸出到OutputStream //toOutputStream(流對象)

OutputStream os = new FileOutputStream("c:/a380_1280x1024_OutputStream.png");

Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).toOutputStream(os);

9、輸出到BufferedImage

//asBufferedImage()返回BufferedImage

BufferedImage thumbnail=Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).asBufferedImage();

ImageIO.write(thumbnail,"jpg",newFile("c:/a380_1280x1024_BufferedImage.jpg"));

google使用的開源的工具類Thumbnailator圖像處理