Why do WEBP Images cause issues for Java Developers?
ImageIO does not read WEBP file types by default so existing Java Applications will not work with them. If you have an existing Java Application using ImageIO you will find it cannot process these images.
In this post, I will cover how to upgrade ImageIO to support WEBP files so existing Java Applications which use ImageIO will work (often without having to make any code changes) and how to read WEBP files in JDeli directly.
What options are there for reading WEBP files in Java?
Java does not read WEBP images by default so you will need to use an external library or plugin for ImageIO. There are a range of Open Source and Commercial options available, including Java wrappers on non-Java solutions.
If you are looking for a free solution, there is an WEBP ImageIO plugin on GitHub (which includes Java wrappers). In this article we will be using our JDeli pure Java library to read WEBP files.
How to upgrade ImageIO to read WEBP files
It’s actually very simple and can be done without rewriting your existing code!
For example, the code below does not work with ImageIO for WEBP images
// Read WEBP image into Java with ImageIO
BufferedImage bufferedImage = ImageIO.read(new File("ImageFile.webp"));
Steps to fix with JDeli:
- Download the JDeli trial jar with our ImageIO plugin
- Follow the support documentation to install
How to read a WebP image in Java directly with JDeli
- Add JDeli to your class or module path. (download the trial jar).
- Create a File handle, InputStream pointing to the raw WebP image. You can also use a byte[] containing the image data.
- Read the WebP image into a BufferedImage
and the Java code to read WebP with JDeli…
File file = new File(“/path/to/file.webp”);
BufferedImage image = JDeli.read(file);
Not sure if it’s working? Try print it out! If you see information about the image, it’s working!
Other useful WEBP links
- Related articles on our support site.
- How to write WEBP files in Java.
- How to convert WEBP to JPG in bulk (Tutorial)