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.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

image

3、圖片質量不錯下面是0.25f和原圖的對比

image

image

上面是壓縮過后的,下面是原圖、看出來了嗎?

 

其他功能

最后附上其他功能使用的簡單例子

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM