tif格式圖片轉換爲gif、png、jpg格式(Java實戰)

tif格式圖片轉換爲gif、png、jpg格式(Java實戰)

tif的格式的圖片通常很大,且不能被瀏覽器直接預覽,一般處理方案都是服務端將其轉換爲jpg、png等格式的圖片,再由前端進行展示。

網絡上也有很多轉換格式的樣例,但大都比較麻煩,本次實踐使用開源組件 thumbnailator 來實現圖片格式轉換,更爲便捷。

引用依賴

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>

實戰

tif圖片樣例:
image

存儲大小:18.4MB

實戰代碼:

public static void main(String[] args) throws IOException {

    /*------------ 轉換爲jpg -------------*/
    Thumbnails.of(new File("/Users/axin/IdeaProjects/axin-framework/world/src/main/java/com/axin/world/picTest/tifdemo2.tiff"))
            .size(1440, 2560)
            .outputFormat("jpg")
            .toFile("image-conver.jpg");

    /*------------ 轉換爲gif -------------*/
    Thumbnails.of(new File("/Users/axin/IdeaProjects/axin-framework/world/src/main/java/com/axin/world/picTest/tifdemo2.tiff"))
            .size(1440, 2560)
            .outputFormat("gif")
            .toFile("image-conver2.gif");

    /*------------ 轉換爲png -------------*/
    Thumbnails.of(new File("/Users/axin/IdeaProjects/axin-framework/world/src/main/java/com/axin/world/picTest/tifdemo2.tiff"))
            .size(1440, 2560)
            .outputFormat("png")
            .toFile("image-conver.png");
}

轉換後:
image

image

可以看到圖片肉眼看上去沒有什麼變化。

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