(轉)Java使用google開源工具Thumbnailator實現圖片壓縮

【轉載原因:Thumbnailator處理壓縮圖片,非常好用】

【轉載原文:https://blog.csdn.net/zmx729618/article/details/78729049

 

前言

作爲靠譜的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質量越差。

對於壓縮圖片來說上面就已經足夠了。

 

優點

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或者網絡上的其他資源。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章