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

作者:nintha 时间:2022-03-09 06:27:29 

前言

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

来源:https://segmentfault.com/a/1190000016324137

标签:Java,Webp,图片格式,转换
0
投稿

猜你喜欢

  • java 创建自定义数组

    2022-09-02 11:18:45
  • java序列化和java反序列化示例

    2023-11-24 04:01:03
  • Apache Commons fileUpload文件上传多个示例分享

    2021-11-13 07:23:51
  • IDEA下载并大学生edu邮箱认证免费使用教程(图文)

    2022-10-10 14:23:02
  • Java操作Mongodb数据库实现数据的增删查改功能示例

    2023-11-25 00:02:05
  • springboot嵌套子类使用方式—前端与后台开发的注意事项

    2023-09-16 12:37:22
  • java格式化数值成货币格式示例

    2023-01-31 06:27:45
  • SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法

    2023-05-16 12:53:02
  • JWT在OpenFeign调用中进行令牌中继详解

    2023-02-07 04:19:15
  • 快速排序算法在Java中的实现

    2022-05-25 01:06:15
  • springmvc 参数绑定总结

    2023-11-16 21:30:44
  • Java Swing实现扫雷源码

    2023-11-10 08:16:20
  • SpringBoot实现异步事件驱动的方法

    2023-11-01 07:48:54
  • linux下C语言中的mkdir函数与rmdir函数

    2023-07-07 14:16:16
  • java最新版本连接mysql失败的解决过程

    2022-05-21 17:29:58
  • Spring实战之SpEl语法实例详解

    2023-09-18 07:56:03
  • 基于C语言string函数的详解

    2023-06-28 05:33:25
  • java加载properties文件的六种方法总结

    2023-09-20 05:24:54
  • java实现ftp上传 如何创建文件夹

    2021-06-10 10:49:17
  • SpringBoot 使用Prometheus采集自定义指标数据的方案

    2023-04-25 02:50:39
  • asp之家 软件编程 m.aspxhome.com