Skip to content

Java ImageIO WebP support (includes ARM chips support)

License

Notifications You must be signed in to change notification settings

usefulness/webp-imageio

 
 

Repository files navigation

webp-imageio

Build Maven Central Static Badge

Description

Java Image I/O reader and writer for the Google WebP image format.

Highlights:

  • macos-aarch64 architecture support (ARM chipsets from Apple, M1, M2) ✅
  • Sharp YUV option support ✅
  • Written in Kotlin, compatible with Java

Supported platforms

See the full list of supported platforms

Usage

  1. Add dependency com.github.usefulness:webp-imageio to your application
dependencies {
    runtimeOnly("com.github.usefulness:webp-imageio:x.y.z")
}
  1. The WebP reader and writer can be used like any other Image I/O reader and writer.

Decoding

WebP images can be decoded using default settings as follows.

Kotlin
val image = ImageIO.read(File("input.webp"))
Java
BufferedImage image = ImageIO.read(new File("input.webp"));

To customize the WebP decoder settings you need to create instances of ImageReader and WebPReadParam.

Kotlin
// Obtain a WebP ImageReader instance
val reader = ImageIO.getImageReadersByMIMEType("image/webp").next()

// Configure decoding parameters
val readParam = WebPReadParam().apply {
    bypassFiltering = true
}

// Configure the input on the ImageReader
reader.setInput(FileImageInputStream(File("input.webp")))

// Decode the image
val image = reader.read(0, readParam)
Java
// Obtain a WebP ImageReader instance
ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();

// Configure decoding parameters
WebPReadParam readParam = new WebPReadParam();
readParam.setBypassFiltering(true);

// Configure the input on the ImageReader
reader.setInput(new FileImageInputStream(new File("input.webp")));

// Decode the image
BufferedImage image = reader.read(0, readParam);

Encoding

Encoding is done in a similar way to decoding.

You can either use the Image I/O convenience methods to encode using default settings.

Kotlin
// Obtain an image to encode from somewhere
val image = ImageIO.read(File("input.png"))

// Encode it as webp using default settings
ImageIO.write(image, "webp", File("output.webp"))
Java
// Obtain an image to encode from somewhere
BufferedImage image = ImageIO.read(new File("input.png"));

// Encode it as webp using default settings
ImageIO.write(image, "webp", new File("output.webp"));

Or you can create an instance of ImageWriter and WebPWriteParam to use custom settings.

Kotlin
// Obtain an image to encode from somewhere
val image = ImageIO.read(File("input.png"))

// Obtain a WebP ImageWriter instance
val writer = ImageIO.getImageWritersByMIMEType("image/webp").next()

// Configure encoding parameters
val writeParam = (writer.defaultWriteParam as WebPWriteParam).apply {
    compressionType = CompressionType.Lossy
    alphaCompressionAlgorithm = 1
    useSharpYUV = true
}

// Configure the output on the ImageWriter
writer.output = FileImageOutputStream(File("output.webp"))

// Encode
writer.write(null, IIOImage(image, null, null), writeParam)
Java
// Obtain an image to encode from somewhere
BufferedImage image = ImageIO.read(new File("input.png"));

// Obtain a WebP ImageWriter instance
ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();

// Configure encoding parameters
WebPWriteParam writeParam = ((WebPWriteParam) writer.getDefaultWriteParam());
writeParam.setCompressionType(CompressionType.Lossy);
writeParam.setAlphaCompressionAlgorithm(3);
writeParam.setUseSharpYUV(true);

// Configure the output on the ImageWriter
writer.setOutput(new FileImageOutputStream(new File("output.webp")));

// Encode
writer.write(null, new IIOImage(image, null, null), writeParam);

R8 / ProGuard

If you are using R8 the shrinking and obfuscation rules are included automatically.

ProGuard users must manually add the options from webp-imageio.pro

License

webp-imageio is distributed under the Apache Software License version 2.0.
libwebp binaries are distributed under the Following License

Packages

 
 
 

Languages

  • Kotlin 30.4%
  • C++ 29.6%
  • Shell 25.1%
  • C 7.7%
  • Java 6.4%
  • CMake 0.8%