java使用google開源工具實現圖片壓縮
前言
作為靠譜的java服務端程式設計師,圖片這個事情一直是個頭疼的事情。
現在很多網站上,都有上傳圖片這個功能,而圖片對於現在的很多手機來說,拍攝出來的都是高清圖片,解析度也是相當的高,當然佔用的儲存空間也就大了。問題也就產生了,你每個使用者都上傳個3M的圖片怎麼辦?
但是顯然現在硬碟的存放空間是不值錢的,1T、2T隨便來,存放是能用錢解決的問題。
但是網速太值錢了,使用者如果天天載入你的網頁載入個半天,就是因為圖片太大導致的那就不是錢能解決的問題了。
因為使用者的網路環境你是不可控制的。所以你只能考慮壓縮圖片的質量從而保證網站開啟的速度。
壓縮的要求
圖片壓縮,在我的想法裡面有下面幾個要求。
1、壓縮程度可控制,想壓縮成多小就多小。
2、壓縮之後圖片儘可能的不失真。
3、壓縮速度要快。
4、程式碼簡單,依賴較少。
實現
然後帶著這些要求去尋找,找到了Thumbnailator,一個google使用的開源的工具類。
這個工具類滿足了上面所說的所有的要求。
同時對於圖片的處理還有了別的方法,如旋轉,裁切,加水印等等。
在github上面的地址是:https://github.com/coobird/thumbnailator
maven的地址
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.14</version> </dependency>
使用起來特別的簡單:一行程式碼就搞定了
Thumbnails.of("原圖檔案的路徑")
.scale(1f)
.outputQuality(0.5f)
.toFile("壓縮後文件的路徑");
其中的scale是可以指定圖片的大小,值在0到1之間,1f就是原圖大小,0.5就是原圖的一半大小,這裡的大小是指圖片的長寬。
而outputQuality是圖片的質量,值也是在0到1,越接近於1質量越好,越接近於0質量越差。
對於壓縮圖片來說上面就已經足夠了。
PS:經過使用後的反饋,這個工具無法正確壓縮出png格式的圖片
因為png本身就是一種無損的圖片格式,而jpg是一種壓縮的圖片格式;
當前方法目的是為了在儘可能不丟失圖片質量的情況下進行的壓縮;
建議將圖片壓縮後的格式設定成jpg來解決;.outputFormat("jpg")
工具原始碼本身最後還是呼叫jdk中的ImageIO.createImageOutputStream(fos);來實現的;
優點
1、簡單容易使用。
2、壓縮圖片效果很好。如下:其中100是原圖,50就是0.5f
3、圖片質量不錯下面是0.25f和原圖的對比
上面是壓縮過後的,下面是原圖、看出來了嗎?
其他功能
最後附上其他功能使用的簡單例子
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
public class ThumbnailatorTest {
/**
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ThumbnailatorTest thumbnailatorTest = new ThumbnailatorTest();
thumbnailatorTest.test1();
thumbnailatorTest.test2();
thumbnailatorTest.test3();
thumbnailatorTest.test4();
thumbnailatorTest.test5();
thumbnailatorTest.test6();
thumbnailatorTest.test7();
thumbnailatorTest.test8();
thumbnailatorTest.test9();
}
/**
* 指定大小進行縮放
*
* @throws IOException
*/
private void test1() throws IOException {
/*
* size(width,height) 若圖片橫比200小,高比300小,不變
* 若圖片橫比200小,高比300大,高縮小到300,圖片比例不變 若圖片橫比200大,高比300小,橫縮小到200,圖片比例不變
* 若圖片橫比200大,高比300大,圖片按比例縮小,橫為200或高為300
*/
Thumbnails.of("images/test.jpg").size(200, 300).toFile("C:/image_200x300.jpg");
Thumbnails.of("images/test.jpg").size(2560, 2048).toFile("C:/image_2560x2048.jpg");
}
/**
* 按照比例進行縮放
*
* @throws IOException
*/
private void test2() throws IOException {
/**
* scale(比例)
*/
Thumbnails.of("images/test.jpg").scale(0.25f).toFile("C:/image_25%.jpg");
Thumbnails.of("images/test.jpg").scale(1.10f).toFile("C:/image_110%.jpg");
}
/**
* 不按照比例,指定大小進行縮放
*
* @throws IOException
*/
private void test3() throws IOException {
/**
* keepAspectRatio(false) 預設是按照比例縮放的
*/
Thumbnails.of("images/test.jpg").size(120, 120).keepAspectRatio(false).toFile("C:/image_120x120.jpg");
}
/**
* 旋轉
*
* @throws IOException
*/
private void test4() throws IOException {
/**
* rotate(角度),正數:順時針 負數:逆時針
*/
Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(90).toFile("C:/image+90.jpg");
Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(-90).toFile("C:/iamge-90.jpg");
}
/**
* 水印
*
* @throws IOException
*/
private void test5() throws IOException {
/**
* watermark(位置,水印圖,透明度)
*/
Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f).toFile("C:/image_watermark_bottom_right.jpg");
Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f).toFile("C:/image_watermark_center.jpg");
}
/**
* 裁剪
*
* @throws IOException
*/
private void test6() throws IOException {
/**
* 圖片中心400*400的區域
*/
Thumbnails.of("images/test.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false)
.toFile("C:/image_region_center.jpg");
/**
* 圖片右下400*400的區域
*/
Thumbnails.of("images/test.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false)
.toFile("C:/image_region_bootom_right.jpg");
/**
* 指定座標
*/
Thumbnails.of("images/test.jpg").sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false).toFile("C:/image_region_coord.jpg");
}
/**
* 轉化影象格式
*
* @throws IOException
*/
private void test7() throws IOException {
/**
* outputFormat(影象格式)
*/
Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("png").toFile("C:/image_1280x1024.png");
Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("gif").toFile("C:/image_1280x1024.gif");
}
/**
* 輸出到OutputStream
*
* @throws IOException
*/
private void test8() throws IOException {
/**
* toOutputStream(流物件)
*/
OutputStream os = new FileOutputStream("C:/image_1280x1024_OutputStream.png");
Thumbnails.of("images/test.jpg").size(1280, 1024).toOutputStream(os);
}
/**
* 輸出到BufferedImage
*
* @throws IOException
*/
private void test9() throws IOException {
/**
* asBufferedImage() 返回BufferedImage
*/
BufferedImage thumbnail = Thumbnails.of("images/test.jpg").size(1280, 1024).asBufferedImage();
ImageIO.write(thumbnail, "jpg", new File("C:/image_1280x1024_BufferedImage.jpg"));
}
}