javajavax.imageio

ImageIO.getImageWritersByFormatName, how to know if I have the registered ImageWriters


For the Class ImageIO, method getImageWritersByFormatName api

https://docs.oracle.com/javase/8/docs/api/javax/imageio/ImageIO.html

it said

Returns an Iterator containing all currently registered ImageWriters that claim to be able to encode the named format.

But how to know what registered ImageWriters my server have? Especially I want to know if my server has WMF image writer registered.

And if no, how to register a WMF image writer?


Solution

  • But how to know what registered ImageWriters my server have? Especially I want to know if my server has WMF image writer registered.

    The answer is easy, you do exactly what the documentation suggests:

    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("WMF");
    
    // Just print the installed writers
    if (writers.hasNext()) {
        System.out.println("Installed writers for WMF format:");
        while (writers.hasNext()) {
            ImageWriter writer = writers.next();
            System.out.println("   " + writer);
        }
    }
    else {
        System.err.println("No writes for WMF format installed");
    }
    

    When you run this code on the server, it will print the installed WMF writers. Most likely though, you will find that your server does not have any WMF writers installed.


    Assuming that by "WMF" you mean Windows Metafile format, I doubt that you find any ImageIO writer plugin. WMF is mostly used to store "vector" graphics in the form of drawing commands, but this is not possible in the ImageIO API which is designed for raster graphics. It could be possible to create a writer implementation that stored a simple WMF containing a bitmap if you really need to, but in these cases using a simple raster format, like BMP or PNG, would make more sense.

    Normally, ImageIO plugins (like ImageWriters and ImageReaders) are installed via the JAR Service Provider (SPI) mechanism. It's also possible to register a plugin at runtime, using code something like:

    IIORegistry registry = IIORegistry.getDefaultInstance();
    registry.registerServiceProvider(new WMFWriterSpi());
    

    (where WMFWriterSpi is the service provider, ImageWriterSpi, of an ImageWriter that supports WMF format).