In this blog post, I will show you how to read WebP files in Java. We also have a related article covering how to write WebP files in Java.
ImageIO does not support WebP file types by default so you will need to use an ImageIO plugin or an external library. I will demonstrate using an Open source ImageIO plugin and using the JDeli Image Library.
How to read a WebP image in Java with ImageIO
- Download an ImageIO plugin (try this one or this one) and add it to classpath.
- Create a File handle, InputStream, or URL pointing to the raw WebP image.
- ImageIO will now be able to read a WebP file into a BufferedImage. This syntax is like so:
BufferedImage image = ImageIO.read(webpfFileOrInputStreamOrURL)
How to read a WebP image in Java 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
BufferedImage image = JDeli.read(webpFile);
Start reading and writing images with one line of code
BufferedImage image = JDeli.read(streamOrFile);
JDeli.write(myBufferedImage, OutputFormat.HEIC, outputStreamOrFile)