1

I originally wanted to know which image formats are supported by Swing, platform-independently. Swing is my preferred toolkit, and I am trying to research the decision of which image format to use in my hobby work. Once I know which formats are inherently supported, I will decide whether it is simpler to work with one of them or if it is simpler to find and work with appropriately-licensed third-party library. However, I've hit a snag and I want help to sort out where I went wrong.

I've done some digging through the javax.imageio API and I have unsuccessfully attempted to determine what formats are supported on my PC, by doing the following:

package iiortester;

import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageReaderSpi;

public class IIORTester {
    public static void main(String[] args) {
        IIORegistry iioRegistry = IIORegistry.getDefaultInstance();
        ImageReaderSpi imageReaderSpi =
                iioRegistry.getServiceProviderByClass(ImageReaderSpi.class);
        System.out.println(imageReaderSpi); // output is null
    }
}

The default IIORegistry instance did report the category ImageReaderSpi when I made a call to ServiceRegistry.getCategories(), but when I attempt to acquire the image reader service provider interface in the code above, I receive a null reference. This is likely a hint that I'm not going about this the right way, and my plan to query ImageReaderWriterSpi.getFormatNames() has been foiled. However, even if it was successful in reporting supported image formats, that would not tell me whether those formats are supported inherently in any JVM or whether they're supported because I am running a Windows 7 JVM.

Am I looking in the wrong place? Probably. Where should I have been looking? I don't know. Does the JVM even have built-in ImageIO service providers, or is this only for third-party library registration? Where in the Swing API can software look up supported image formats?

muffin
  • 668
  • 1
  • 6
  • 13

2 Answers2

2

GIF, JPEG and PNG should be supported in all JREs.

As a more specific answer to what any particular JRE supports, see the MediaTypes source code which provides lots of info.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I feel a little silly at how simple it was, now that I see what I missed: `ImageIO.getReaderMIMETypes()`. Thanks for the note about GIF/JPEG/PNG across platforms, too: do you know if this is stated officially somewhere? – muffin Jul 19 '14 at 19:20
  • *"do you know if this is stated officially somewhere?"* I'm sure I've seen it *vaguely* expressed in either the docs or tutorial. It read something like *"A JRE will support various types of images but **should include** JPG, GIF & PNG"*.. – Andrew Thompson Jul 19 '14 at 19:48
1

To see the supported image formats:

To see which files can be read: ImageIO.getReaderFormatNames();
To see which files can be written: ImageIO.getWriterFormatNames();

JOptionPane.showMessageDialog(null,"Java Image Formats:\r\nread:\t"+Arrays.toString(ImageIO.getReaderFormatNames())+"\r\nwrite:\t"+Arrays.toString(ImageIO.getWriterFormatNames()));

Image Types which are registered in Java will show up in the list.

Andie2302
  • 4,825
  • 4
  • 24
  • 43