WebP4j is a Java library based on JNI (Java Native Interface) that supports WebP image encoding and decoding in Java projects. This project utilizes Google's libwebp library (version 1.6.0) and exposes its functionality to Java applications.
- Supports WebP encoding of RGB and RGBA images with both lossy and lossless compression.
- Supports decoding WebP images to RGB and RGBA formats.
- Provides efficient image compression and decompression using libwebp.
- Compatible with multiple platforms (supports x86 and ARM).
- Developed and tested with JDK 17.
- Published WebP4j to Maven Central Repository.
This library is developed and tested with Java 17+. While Java 8 should be supported, it has not been thoroughly tested. For best compatibility, we recommend using Java 17 or later.
My current development and compilation environment is based on JDK 17. If you’re using a version of JDK below 17, please perform your own testing to ensure compatibility. If you encounter any issues, feel free to provide feedback, and I will do my best to assist.
WebP4j currently supports the following platforms:
- Windows(x86-64)
- Linux(x86-64)
- Linux(arm64)
- MacOS(arm64)
Unfortunately, I do not have access to a macOS (x86-64) device, so I am unable to compile the native library for this platform. If you have access to a macOS (x86-64) device, I would greatly appreciate it if you could compile the native library for me and share it with the project. Thank you in advance for your contribution!
To use WebP4j in your project, add the following dependency to your pom.xml file:
<dependency>
<groupId>dev.matrixlab</groupId>
<artifactId>webp4j</artifactId>
<version>1.2.0</version>
</dependency>public native boolean getInfo(byte[] data, int[] dimensions);
public native int getFeatures(byte[] data, int dataSize, WebPBitstreamFeatures features);
public native byte[] encodeRGB(byte[] image, int width, int height, int stride, float quality);
public native byte[] encodeRGBA(byte[] image, int width, int height, int stride, float quality);
public native byte[] encodeLosslessRGB(byte[] image, int width, int height, int stride);
public native byte[] encodeLosslessRGBA(byte[] image, int width, int height, int stride);
public native boolean decodeRGBInto(byte[] data, byte[] outputBuffer, int outputStride);
public native boolean decodeRGBAInto(byte[] data, byte[] outputBuffer, int outputStride);public static byte[] encodeImage(BufferedImage bufferedImage, float quality) throws IOException;
public static byte[] encodeImage(BufferedImage bufferedImage, float quality, boolean lossless) throws IOException;
public static byte[] encodeLosslessImage(BufferedImage bufferedImage) throws IOException;
public static BufferedImage decodeImage(byte[] webPData) throws IOException;You can use the encodeImage() and decodeImage() methods of the WebPCodec class to convert image formats such as JPG/PNG to WEBP format. The library supports both lossy and lossless compression modes.
- Lossless compression: Recommended for PNG and other lossless image formats to preserve image quality without any data loss.
- Lossy compression: Recommended for JPG and other lossy image formats. Using lossless compression on already-compressed JPG images is not recommended as it may result in larger file sizes without quality benefits.
// Lossy compression example (recommended for JPG sources)
public void encodeToWebP() throws IOException {
// Read the source image from disk
BufferedImage bufferedImage = ImageIO.read(new File("input.jpg"));
// Set the compression quality (0.0f = worst, 100.0f = best)
float quality = 75.0f;
// Encode the BufferedImage into WebP byte array
byte[] encodedWebP = WebPCodec.encodeImage(bufferedImage, quality);
// Write the encoded bytes to the output file
try (FileOutputStream fos = new FileOutputStream("output.webp")) {
fos.write(encodedWebP);
}
}
// Lossless compression example (recommended for PNG sources)
public void encodeToLosslessWebP() throws IOException {
// Read the source image from disk
BufferedImage bufferedImage = ImageIO.read(new File("input.png"));
// Encode the BufferedImage into lossless WebP byte array
byte[] encodedWebP = WebPCodec.encodeLosslessImage(bufferedImage);
// Write the encoded bytes to the output file
try (FileOutputStream fos = new FileOutputStream("output_lossless.webp")) {
fos.write(encodedWebP);
}
}
// Decoding example
public void decodeFromWebP() throws IOException {
// Read all bytes from the WebP file into memory
byte[] webPData = Files.readAllBytes(Paths.get("input.webp"));
// Decode the WebP byte array into a BufferedImage
BufferedImage image = WebPCodec.decodeImage(webPData);
// Write the decoded image as a JPEG file
ImageIO.write(image, "jpg", new File("decoded_output.jpg"));
}Currently, WebP4j has encapsulated native methods. We will continue to update, develop more efficient APIs, and continuously improve documentation.
This project is licensed under the MIT License.