TwelveMonkeys 提供了很多图片格式的支持,其中也包括了webp,但是其仅支持webp格式的读取,不支持webp格式的写出,这样的话如果想把图片转换成webp格式的图片就没办法实现了;下面我们使用 webp-imageio-core 对ImageIO图片格式进行扩展,使其能够支持webp图片格式的读写;
首先我们需要引入jar包:
下载地址: Release v0.1.3 Release · nintha/webp-imageio-core · GitHubJava Image I/O reader and writer for the Google WebP image format without system native libs - Release v0.1.3 Release · nintha/webp-imageio-corehttps://github.com/nintha/webp-imageio-core/releases/tag/v0.1.3
下载好jar包之后,我们把jar包放在项目的lib目录下,使用maven进行引入:
<dependency><groupId>com.github.nintha</groupId><artifactId>webp-imageio-core</artifactId><version>0.1.3</version><scope>system</scope><systemPath>${project.basedir}/lib/webp-imageio-core-0.1.3.jar</systemPath>
</dependency>
引入之后,我们可以使用代码查看一下ImageIO支持的读写图片的格式:
List<String> readerFormatNames = Arrays.asList(ImageIO.getReaderFormatNames());
System.out.println("readers: " + readerFormatNames);
List<String> writerFormatNames = Arrays.asList(ImageIO.getWriterFormatNames());
System.out.println("writers: " + writerFormatNames);
运行的结果如下:
readers: [JPG, jpg, tiff, bmp, BMP, pcx, PCX, gif, GIF, WBMP, png, PNG, raw, RAW, JPEG, webp, pnm, PNM, tif, TIF, TIFF, WebP, wbmp, jpeg]
writers: [JPG, jpg, tiff, bmp, BMP, pcx, PCX, gif, GIF, WBMP, png, PNG, raw, RAW, JPEG, webp, pnm, PNM, tif, TIF, TIFF, WebP, jpeg, wbmp]
从运行的结果来看,读写图片已经支持了webp格式,这样就可以进行webp格式与其他的图片格式进行相互转换了。