Why do WEBP Images cause problems for Java Developers?
ImageIO does not support WEBP images by default so existing Java Applications will not work with them. This is an increasing problem as WEBP images are quickly becoming a popular format. It is a well-supported format in browsers and offers significant advantages over established formats such as JPG.
// Current problem many developers face
//read
try {
ImageIO.read(imageFile);
} catch (IOException e) {
// This fails – ImageIO doesn’t support WEBP
}
//write
try {
ImageIO.write(bufferedImage, "webp", new File("output.webp"));
} catch (IOException e) {
// This fails – ImageIO doesn’t support WEBP
}
In this post, I will show you how to add WEBP support to existing Java Applications which use ImageIO (without having to make any code changes) and how to write out images as WEBP files in JDeli directly.
What options are there for supporting WEBP files in Java?
Java does not support WEBP images by default so you will need to use an external library or plugin. JDeli gives gives full WEBP support for reading and writing and can also be used an ImageIO plugin.
There is a free, open source plugin called webp-imageio which adds WEBP support to ImageIO.
We recommend JDeli because it is a pure, complete Java implementation with no known security issues. It can be used with existing ImageIO code and has an API to write new code so we will document that in this article. It can also read WebP files and supports several other Image formats.
How to upgrade ImageIO to support WEBP
It’s actually very simple and can be done without changing 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"));
// Write out BufferedImage as WEBP image file with ImageIO
ImageIO.write(bufferedImage, "webp", new File("output.webp"));
Steps to fix:
- Download the JDeli trial jar with our ImageIO plugin
- Follow the support documentation to install
How to write a WebP images directly with JDeli
- Add JDeli to your class or module path. (download link to the trial jar).
- Create a File (or OutputStream) object
- Pass image, WEBP type, and File (or OutputStream) object into write method
and the Java code to write WebP with JDeli…
File file = new File("/path/to/outputFile.webp");
JDeli.write(bufferedImage, "webp", file);
//In JDeli you can also use a typesafe version
JDeli.write(bufferedImage, OutputFormat.WEBP, file);
//or pass in a WebpEncoderOptions object for more control over output.
WebpEncoderOptions options = new WebpEncoderOptions();
JDeli.write(bufferedImage, options, file);
In below picture, my original file was a .jpg image, and I used JDeli to write it out as a .webp image.
More on WebP
What are WebP files used for?
The WebP file format was developed by Google to provide high-quality lossy and lossless compression for web images. In this article, I will walk you through how to write out images as WebP images in Java.
Which web browsers support WebP?
WebP is supported by the most popular web browsers including Google chrome, Safari, Microsoft edge, Mozilla firefox and many more.
Other useful WEBP links
- Related articles on our support site.
- How to read WebP files in Java.
- How to convert WEBP to JPG in bulk (Tutorial)
How can JDeli help?
JDeli is the best enterprise-level Java image library for image manipulation. You can use JDeli to read, write and convert your images as it has WebP support. Visit our documentation to learn more about JDeli’s WebP support.
Are you a Java Developer working with Image files?
// Read an image
BufferedImage bufferedImage = JDeli.read(avifImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(dicomImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(heicImageFile);
// Write an image
JDeli.write(bufferedImage, "heic", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpegImageFile);
// Write an image
JDeli.write(bufferedImage, "jpeg", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpeg2000ImageFile);
// Write an image
JDeli.write(bufferedImage, "jpx", outputStreamOrFile);
// Write an image
JDeli.write(bufferedImage, "pdf", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(pngImageFile);
// Write an image
JDeli.write(bufferedImage, "png", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(tiffImageFile);
// Write an image
JDeli.write(bufferedImage, "tiff", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(webpImageFile);
// Write an image
JDeli.write(bufferedImage, "webp", outputStreamOrFile);
What is JDeli?
JDeli is a commercial Java Image library that is used to read, write, convert, manipulate and process many different image formats.
Why use JDeli?
To handle many well known formats such as JPEG, PNG, TIFF as well as newer formats like AVIF, HEIC and JPEG XL in java with no calls to any external system or third party library.
What licenses are available?
We have 3 licenses available:
Server for on premises and cloud servers, Distribution for use in a named end user applications, and Custom for more demanding requirements.
How does JDeli compare?
We work hard to make sure JDeli performance is better than or similar to other java image libraries. Check out our benchmarks to see just how well JDeli performs.