Java WebP Image Support – Read and Save WebP Images – Genuine Coder

Java WebP Image Support – Read and Save WebP Images

Java Webp Tutorial

WebP is a new generation image format developed by Google exclusively for the Web. It has both lossy and lossless compression support. When compared to the commonly used formats like JPEG, WebP has 25%-34% lesser file size for the same quality. WebP provides much better compression and reduced file sizes, which are crucial for the web since every byte of data saved matters.

In this tutorial, let’s see how we can process WebP images in the Java program. We will learn how to read and save/encode/write WebP images.

Add support for WebP into your Java program

As of writing this article in 2022, Java doesn’t natively support WebP images. However, there is a there is a library called webp-imageio that can support WebP image-io for Java. Let’s make use of this web-imageio library to process WebP images.

Add the dependency on to your project.

If you are using the Gradle, add the dependency as follows.

// https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio
implementation group: 'org.sejda.imageio', name: 'webp-imageio', version: '0.1.6'

If you are using the Maven, add the dependency as follows.

<!-- https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio -->
<dependency>
    <groupId>org.sejda.imageio</groupId>
    <artifactId>webp-imageio</artifactId>
    <version>0.1.6</version>
</dependency>

Read WebP image from Java

Now, let’s see how we can read WebP images to a BufferedImage object. You can read the WebP image just like reading any other JPEG or PNGs with ImageIO.read() function. Let’s understand it with a sample code snippet.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class JavaWebpSupportTest {

  public static void main(String[] args) throws IOException {
    JavaWebpSupportTest webPSupportTest = new JavaWebpSupportTest();
    webPSupportTest.readWebpImage();
  }

  private void readWebpImage() throws IOException {
    BufferedImage image = ImageIO.read(new File("/home/gc/my_image.webp"));
    System.out.printf("\nDimension of the image:%dx%d", image.getWidth(), image.getHeight());
  }
}

This will read the WebP image as BufferedImage and will print the dimension of the image. You can see a sample output below. You can then continue processing the BufferedImage just you do with any other image format.

Java program successfully read WebP image and displayed its dimension.

Save WebP image from Java

Now that we have learned how to read WebP images, let’s see how we can save a BufferedImage as a WebP image. For this, we will read a JPEG image and then save it as a WebP image. WebP supports for both lossless and lossy compressions. So, let’s explore how to do both cases with Java.

private void convertJpegToWebp() throws Exception {
  //Read JPG image
  BufferedImage image = ImageIO.read(new File("/home/afsal/sample.jpg"));
  // Encode it as webp using default settings and save it as webp file
  ImageIO.write(image, "webp", new File("/home/afsal/sample.webp"));
}

1. Save WebP with lossless encoding

In the above code snippet, we have used the default settings for writing the WebP image file. Now, let’s see how we can customize the encoding parameters to customize the image. First, let’s save the image in lossless format. We will set the compression type as LOSSLESS_COMPRESSION using the WebPWriteParam#setCompressionType option.

import com.luciad.imageio.webp.WebPWriteParam;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;

public class JavaWebpSupportTest {

  public static void main(String[] args) throws Exception {
    JavaWebpSupportTest webPSupportTest = new JavaWebpSupportTest();
    webPSupportTest.convertJpegToWebpLossless();
  }

  private void convertJpegToWebpLossless() throws Exception {
    BufferedImage image = ImageIO.read(new File("/home/gc/sample.jpg"));

    ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();

    WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
    //Notify encoder to consider WebPWriteParams
    writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    //Set lossless compression
    writeParam.setCompressionType(writeParam.getCompressionTypes()[WebPWriteParam.LOSSLESS_COMPRESSION]);

    // Save the image
    writer.setOutput(new FileImageOutputStream(new File("/home/gc/sample.webp")));
    writer.write(null, new IIOImage(image, null, null), writeParam);
  }
}

2. Save WebP with lossy encoding

Now, let’s see how we can configure the compression ratio and use lossy compression to encode an image as WebP. The following code will read a JPEG image and save it as WebP with 80% compression quality (0% has the highest compression and 100% has the highest quality).

private void convertJpegToWebpWithLossyCompression() throws Exception {
  BufferedImage image = ImageIO.read(new File("/home/gc/sample.jpg"));

  ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();

  WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
  //Notify encoder to consider WebPWriteParams
  writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  //Set lossy compression
  writeParam.setCompressionType(writeParam.getCompressionTypes()[WebPWriteParam.LOSSY_COMPRESSION]);
  //Set 80% quality. Allowed values are between 0 and 1
  writeParam.setCompressionQuality(0.8f);

  // Save the image
  writer.setOutput(new FileImageOutputStream(new File("/home/gc/sample.webp")));
  writer.write(null, new IIOImage(image, null, null), writeParam);
}

Conclusion

In this tutorial, we have learned how to process WebP images using Java. With the help of the webp-imageio library, we can read and write WebP images easily. Since the library works with ImageIO.read() and ImageIO.write(), the processing of images is a breeze. If you are using Linux and wants to enable WebP thumbnail support on your platform, learn how to do that from the following tutorial: How to add WebP thumbnail support on Ubuntu and Linux Mint.

Comments

50 responses to “Java WebP Image Support – Read and Save WebP Images”

  1. […] Источник […]

  2. … [Trackback]

    […] Find More on on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  3. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  4. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  5. … [Trackback]

    […] Info to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  6. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  7. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  8. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  9. … [Trackback]

    […] Find More on to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  10. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  11. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  12. … [Trackback]

    […] Information to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  13. … [Trackback]

    […] Information on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  14. … [Trackback]

    […] Information on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  15. … [Trackback]

    […] Here you will find 70829 additional Information to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  16. … [Trackback]

    […] Read More here on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  17. … [Trackback]

    […] Here you will find 53400 additional Information on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  18. … [Trackback]

    […] Read More on on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  19. … [Trackback]

    […] Here you can find 26101 more Info on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  20. … [Trackback]

    […] Read More here to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  21. … [Trackback]

    […] Here you can find 59007 additional Information on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  22. … [Trackback]

    […] Here you can find 38097 additional Info on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  23. … [Trackback]

    […] Here you can find 84864 more Information on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  24. … [Trackback]

    […] There you can find 17434 more Information on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  25. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  26. … [Trackback]

    […] Find More on on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  27. … [Trackback]

    […] Info on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  28. … [Trackback]

    […] Read More on on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  29. … [Trackback]

    […] There you can find 25648 additional Info to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  30. … [Trackback]

    […] Here you will find 2282 more Information on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  31. … [Trackback]

    […] There you can find 53348 additional Info to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  32. … [Trackback]

    […] Here you can find 89815 more Info on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  33. … [Trackback]

    […] Here you can find 26057 additional Information to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  34. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  35. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  36. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  37. … [Trackback]

    […] Here you will find 23705 more Information to that Topic: genuinecoder.com/java-webp-image-read-save-tutorial/ […]

  38. get androxal uk suppliers

    get androxal buy online no prescription

  39. online order enclomiphene cheap with fast shipping

    online order enclomiphene cost tablet

  40. cheap rifaximin generic alternative

    online order rifaximin generic does it works

  41. buying staxyn generic australia

    online order staxyn australia generic online

  42. compare avodart prices at major pharmacies

    ordering avodart cost at costco

  43. pharmacie canadienne des médicaments contre l’ed kamagra

    low cost kamagra de l’inde

  44. get flexeril cyclobenzaprine price for prescription

    purchase flexeril cyclobenzaprine buy japan

  45. buy cheap dutasteride cheap uk buy purchase

    how to buy dutasteride canada drugs

  46. discount gabapentin price generic

    purchase gabapentin generic from canada

  47. fildena cheap

    online order fildena australia discount

  48. cheap itraconazole price by pharmacy

    order itraconazole singapore where to buy

  49. purchase xifaxan purchase toronto

    buying xifaxan usa price

  50. kamagra z mexika bez lテゥkaナ冱kテゥho pナ册dpisu

    jak zテュskテ。m kamagra od svテゥho lテゥkaナ册

Leave a Reply Cancel reply

Exit mobile version