4

I need to extract the width and height of webp image in classic java

I searched for libraries and found webp-imageio but it can't extract the image size

For others formats like jpg/png/gif I'm using ImageIO that extracts the size from headers only (but it can't handle webp unfortunately)

How can I do the same with webp ?

Regards

Bouki
  • 1,349
  • 2
  • 14
  • 25

3 Answers3

2

This project webp-imageio-core may help you. It integrates webp converter native system libs(dll/so/dylib).

Download it and import to you project. Example code:

 public static void main(String args[]) throws IOException {
        String inputWebpPath = "test_pic/test.webp";
        String outputJpgPath = "test_pic/test_.jpg";
        String outputJpegPath = "test_pic/test_.jpeg";
        String outputPngPath = "test_pic/test_.png";

        // 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(inputWebpPath)));

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

        ImageIO.write(image, "png", new File(outputPngPath));
        ImageIO.write(image, "jpg", new File(outputJpgPath));
        ImageIO.write(image, "jpeg", new File(outputJpegPath));

    }

Then you can using ImageIO that extracts the size from headers.

Lin CS
  • 324
  • 1
  • 3
  • 10
2

From my answer here:

The Webp Container Sepcs defines that the Webp Extended File Format images which are currently in use has headers in the starting few bits corresponding to the type, file size, is there any alpha, is there any animation, height and width etc.

Though the docs seems to be outdated (it shows that the value of height and width corresponds to index 20 to 25 but rather I found it to be on 24 to 29 indexes).

public class JavaRes {
    public static java.awt.Dimension extract(InputStream is) throws IOException {
        byte[] data = is.readNBytes(30);
        if (new String(Arrays.copyOfRange(data, 0, 4)).equals("RIFF") && data[15] == 'X') {
            int width = 1 + get24bit(data, 24);
            int height = 1 + get24bit(data, 27);

            if ((long) width * height <= 4294967296L) return new Dimension(width, height);
        }
        return null;
    }

    private static int get24bit(byte[] data, int index) {
        return data[index] & 0xFF | (data[index + 1] & 0xFF) << 8 | (data[index + 2] & 0xFF) << 16;
    }
}

See also: Parsing webp file header in Kotlin to get its height and width, but getting unexpected results

Animesh Sahu
  • 7,445
  • 2
  • 21
  • 49
  • Can you help me out with reading the container. All works fine just ALPHA channel is missing and need to read and write that chunk. but its bit confusing. If you say I will raise a quetsion – CrackerKSR Jul 02 '22 at 20:03
0

Apache Tika uses this metadata extractor library to read metadata for webp, so maybe it also suits your needs.

wero
  • 32,544
  • 3
  • 59
  • 84
  • including tika would triple my dependencies only for 1 little feature – Bouki Feb 25 '16 at 20:08
  • 1
    I intended to recommend the compact *metadata extractor* library not Tika (which is a monster). Also you should amend your question that you are only talking about an unimportant *little feature* - this will people allow to save time. – wero Feb 25 '16 at 20:45
  • I tried https://github.com/drewnoakes/metadata-extractor it's not working for webp just found "Metadata (0 directories)" for my test image https://lh3.googleusercontent.com/xpkyqgacjrWTa_IzKVCRPskmB0DH89tHb6Pl55vw9CYd3rx3edBkuqNiKZ8-ALuT_6oi=h900-rw – Bouki Feb 25 '16 at 21:00