tif格式圖片轉換為gif、png、jpg格式(Java實戰)
tif的格式的圖片通常很大,且不能被瀏覽器直接預覽,一般處理方案都是服務端將其轉換為jpg、png等格式的圖片,再由前端進行展示。
網絡上也有很多轉換格式的樣例,但大都比較麻煩,本次實踐使用開源組件 thumbnailator 來實現圖片格式轉換,更為便捷。
引用依賴
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
實戰
tif圖片樣例:
存儲大小: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");
}
轉換后:
可以看到圖片肉眼看上去沒有什么變化。