Java處理Webp圖片格式轉(zhuǎn)換的示例代碼
前言
Webp是Google推出的一種新型圖片格式,相比于 傳統(tǒng)的PNG/JPG圖片有著更小體積的優(yōu)勢,在Web中有著廣泛的應(yīng)用。由于Webp格式推出比較晚, Jdk 內(nèi)置的圖片編解碼庫對(duì)此并不支持。
網(wǎng)上給出的Java環(huán)境解決方案往往需要手動(dòng)在java.library.path中安裝對(duì)應(yīng)的動(dòng)態(tài)鏈接庫,windows是dll文件,linux是so文件。這對(duì)于開發(fā)部署非常不方便。
本文提供一種無需手動(dòng)安裝動(dòng)態(tài)鏈接庫,同時(shí)可以方便處理Webp的解決方案
WebP是谷歌的圖片格式,java 類庫imageio 是不支持此種格式的。目前除了在線轉(zhuǎn)換以及工具以外,第三方類庫轉(zhuǎn)webp格式
大致有:
- linux:Google libwebp 既是類庫也可以在命令行調(diào)用
- Python:Python Image Library(PIL)及其分支https://pypi.python.org/pypi/PIL 不太了解
- Java:luciad/webp-imageio https://bitbucket.org/luciad/webp-imageio/src windows / linux親測可用
準(zhǔn)備
先從github上面下載所需要的jar包
由于這個(gè)項(xiàng)目并未發(fā)布到maven中央倉庫,所以需要手動(dòng)導(dǎo)入本地jar包.
如果你用的是gradle,可以把jar包放入src/main/resource/libs
目錄,并在build.gradle
中加入依賴
dependencies { compile fileTree(dir:'src/main/resources/libs',include:['*.jar']) }
如果你用的是maven,可以把jar包放入${project.basedir}/libs目錄,并在pom.xml中加入依賴
<dependency> <groupId>com.github.nintha</groupId> <artifactId>webp-imageio-core</artifactId> <version>{versoin}</version> <scope>system</scope> <systemPath>${project.basedir}/libs/webp-imageio-core-{version}.jar</systemPath> </dependency>
例子
完整代碼見 https://github.com/nintha/webp-imageio-core,以下為部分摘錄
Webp編碼
public static void main(String args[]) throws IOException { String inputPngPath = "test_pic/test.png"; String inputJpgPath = "test_pic/test.jpg"; String outputWebpPath = "test_pic/test_.webp"; // Obtain an image to encode from somewhere BufferedImage image = ImageIO.read(new File(inputJpgPath)); // Obtain a WebP ImageWriter instance ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next(); // Configure encoding parameters WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale()); writeParam.setCompressionMode(WebPWriteParam.MODE_DEFAULT); // Configure the output on the ImageWriter writer.setOutput(new FileImageOutputStream(new File(outputWebpPath))); // Encode writer.write(null, new IIOImage(image, null, null), writeParam); }
Webp解碼
public static void main(String args[]) throws IOException { String inputWebpPath = "test_pic/test.webp"; String outputJpgPath = "test_pic/test_.jpg"; String outputJpegPath = "test_pic/test_.jpeg"; String outputPngPath = "test_pic/test_.png"; // Obtain a WebP ImageReader instance ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next(); // Configure decoding parameters WebPReadParam readParam = new WebPReadParam(); readParam.setBypassFiltering(true); // Configure the input on the ImageReader reader.setInput(new FileImageInputStream(new File(inputWebpPath))); // Decode the image BufferedImage image = reader.read(0, readParam); ImageIO.write(image, "png", new File(outputPngPath)); ImageIO.write(image, "jpg", new File(outputJpgPath)); ImageIO.write(image, "jpeg", new File(outputJpegPath)); }
關(guān)于webp-imageio-core項(xiàng)目
這個(gè)項(xiàng)目是基于于qwong/j-webp項(xiàng)目,而 qwong/j-webp 是基于 webp project of Luciad 0.4.2項(xiàng)目。
webp project of Luciad這個(gè)項(xiàng)目提供了java上一個(gè)關(guān)于處理webp的可用實(shí)現(xiàn),但是它需要開發(fā)者手動(dòng)java.library.path
中安裝對(duì)應(yīng)的動(dòng)態(tài)鏈接庫,非常不方便。qwong/j-webp項(xiàng)目作者為了解決這個(gè)問題,改進(jìn)了對(duì)動(dòng)態(tài)鏈接庫的讀取方式,把從java.library.path
讀取改成了從項(xiàng)目resource文件中讀取(具體內(nèi)容見com.luciad.imageio.webp.WebP.loadNativeLibrary
方法)。
雖然qwong/j-webp項(xiàng)目解決了動(dòng)態(tài)鏈接庫依賴問題,但是它的作者并未對(duì)這些代碼提供一個(gè)良好封裝,畢竟開發(fā)者不希望在自己項(xiàng)目里面直接引入第三方包的源碼,所以有了webp-imageio-core
提供一個(gè)可用的jar包,只要導(dǎo)入項(xiàng)目即可使用。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring cloud實(shí)現(xiàn)Eureka注冊(cè)中心的HA的方法
本篇文章主要介紹了spring cloud實(shí)現(xiàn)Eureka注冊(cè)中心的HA的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01SpringCloud OpenFeign 參數(shù)傳遞和響應(yīng)處理的詳細(xì)步驟
本文給大家講解SpringCloud OpenFeign 參數(shù)傳遞和響應(yīng)處理的詳細(xì)步驟,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02spring?Cloud微服務(wù)阿里開源TTL身份信息的線程間復(fù)用
這篇文章主要為大家介紹了spring?Cloud微服務(wù)中使用阿里開源TTL身份信息的線程間復(fù)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01基于ThreadLocal 的用法及內(nèi)存泄露(內(nèi)存溢出)
這篇文章主要介紹了基于ThreadLocal 的用法及內(nèi)存泄露(內(nèi)存溢出),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10