当前位置: 移动技术网 > IT编程>开发语言>Java > Java处理Webp图片格式转换的示例代码

Java处理Webp图片格式转换的示例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

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 windows / linux亲测可用

准备

先从github上面下载所需要的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>

例子

完整代码见 ,以下为部分摘录

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项目

这个项目是基于于项目,而 是基于 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包,只要导入项目即可使用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网