Java处理Webp图片格式转换

前言

Webp是Google推出的一种新型图片格式,相比于 传统的PNG/JPG图片有着更小体积的优点,在Web中有着普遍的应用。因为Webp格式推出比较晚, Jdk 内置的图片编解码库对此并不支持。java

网上给出的Java环境解决方案每每须要手动在java.library.path中安装对应的动态连接库,windows是dll文件,linux是so文件。这对于开发部署很是不方便。linux

本文提供一种无需手动安装动态连接库,同时能够方便处理Webp的解决方案git

准备

先从github上面下载所须要的jar包github

webp-imageio-core-0.1.0.jarweb

因为这个项目并未发布到maven中央仓库,因此须要手动导入本地jar包.windows

若是你用的是gradle,能够把jar包放入src/main/resource/libs目录,并在build.gradle中加入依赖maven

dependencies {
    compile fileTree(dir:'src/main/resources/libs',include:['*.jar'])
}

若是你用的是maven,能够把jar包放入${project.basedir}/libs目录,并在pom.xml中加入依赖gradle

<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/web...,如下为部分摘录ui

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包,只要导入项目便可使用。

相关文章
相关标签/搜索