前言
作為靠譜的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.8</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"));
}
}
其他的具體方法細節可以自己去檢視官方的API或者網絡上的其他資源。
參考部落格:
http://blog.csdn.net/wangpeng047/article/details/17610451
http://blog.csdn.net/qiaqia609/article/details/53171149
http://www.cnblogs.com/miskis/p/5500822.html
轉載請注明出處:http://www.cnblogs.com/linkstar/p/7412012.html
作者:LinkinStar