国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java處理Webp圖片格式轉換的示例代碼

2024-07-14 08:42:17
字體:
來源:轉載
供稿:網友

前言

Webp是Google推出的一種新型圖片格式,相比于 傳統的PNG/JPG圖片有著更小體積的優勢,在Web中有著廣泛的應用。由于Webp格式推出比較晚, Jdk 內置的圖片編解碼庫對此并不支持。

網上給出的Java環境解決方案往往需要手動在java.library.path中安裝對應的動態鏈接庫,windows是dll文件,linux是so文件。這對于開發部署非常不方便。

本文提供一種無需手動安裝動態鏈接庫,同時可以方便處理Webp的解決方案

WebP是谷歌的圖片格式,java 類庫imageio 是不支持此種格式的。目前除了在線轉換以及工具以外,第三方類庫轉webp格式

大致有:

  • linux:Google libwebp 既是類庫也可以在命令行調用
  • Python:Python Image Library(PIL)及其分支https://pypi.python.org/pypi/PIL 不太了解
  • Java:luciad/webp-imageio https://bitbucket.org/luciad/webp-imageio/src windows / linux親測可用

準備

先從github上面下載所需要的jar包

webp-imageio-core-0.1.0.jar

由于這個項目并未發布到maven中央倉庫,所以需要手動導入本地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));}

關于webp-imageio-core項目

這個項目是基于于qwong/j-webp項目,而 qwong/j-webp 是基于 webp project of Luciad 0.4.2項目。

webp project of Luciad這個項目提供了java上一個關于處理webp的可用實現,但是它需要開發者手動java.library.path中安裝對應的動態鏈接庫,非常不方便。qwong/j-webp項目作者為了解決這個問題,改進了對動態鏈接庫的讀取方式,把從java.library.path讀取改成了從項目resource文件中讀取(具體內容見com.luciad.imageio.webp.WebP.loadNativeLibrary方法)。

雖然qwong/j-webp項目解決了動態鏈接庫依賴問題,但是它的作者并未對這些代碼提供一個良好封裝,畢竟開發者不希望在自己項目里面直接引入第三方包的源碼,所以有了webp-imageio-core提供一個可用的jar包,只要導入項目即可使用。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 赣州市| 卫辉市| 乌鲁木齐县| 焉耆| 松原市| 蒲江县| 若尔盖县| 丽水市| 乌拉特后旗| 滦平县| 伊通| 镇江市| 临泉县| 仙桃市| 独山县| 体育| 荔浦县| 多伦县| 定日县| 大冶市| 扎囊县| 彰化市| 夏邑县| 汉中市| 剑川县| 南通市| 桐城市| 百色市| 汉源县| 九寨沟县| 太原市| 定州市| 类乌齐县| 扶绥县| 新安县| 鞍山市| 炉霍县| 富源县| 九江县| 合山市| 梧州市|