imageio
List read/write supported image formats
This is an example on how to list read/write image formats in a Java Desktop applications. You can find this very useful when you want to create “Help” tooltip list with all the supported image files to inform the user. You can also use it for input validation.
Basically, in order to list read/write image formats, you should:
- Create a bew
HashSet<String>
. - Use
ImageIO.getReaderFormatNames
to read all the format names you can read. - Use
ImageIO.getWriterFormatNames()
to get all the image formats you can write. - Use
ImageIO.getReaderMIMETypes()
to get list of all MIME types understood by the current set of registered readers. - Use
ImageIO.getWriterMIMETypes()
to get list of all MIME types understood by the current set of registered writers.
Let’s see the code:
package com.javacodegeeks.snippets.desktop; import java.io.IOException; import java.util.HashSet; import java.util.Set; import javax.imageio.ImageIO; public class ListReadWriteSupportedImageFormats { public static void main(String[] args) throws IOException { Set<String> set = new HashSet<String>(); // Get list of all informal format names understood by the current set of registered readers String[] formatNames = ImageIO.getReaderFormatNames(); for (int i = 0; i < formatNames.length; i++) { set.add(formatNames[i].toLowerCase()); } System.out.println("Supported read formats: " + set); set.clear(); // Get list of all informal format names understood by the current set of registered writers formatNames = ImageIO.getWriterFormatNames(); for (int i = 0; i < formatNames.length; i++) { set.add(formatNames[i].toLowerCase()); } System.out.println("Supported write formats: " + set); set.clear(); // Get list of all MIME types understood by the current set of registered readers formatNames = ImageIO.getReaderMIMETypes(); for (int i = 0; i < formatNames.length; i++) { set.add(formatNames[i].toLowerCase()); } System.out.println("Supported read MIME types: " + set); set.clear(); // Get list of all MIME types understood by the current set of registered writers formatNames = ImageIO.getWriterMIMETypes(); for (int i = 0; i < formatNames.length; i++) { set.add(formatNames[i].toLowerCase()); } System.out.println("Supported write MIME types: " + set); } }
Output:
Supported read formats: [jpg, bmp, jpeg, wbmp, png, gif] Supported write formats: [jpg, bmp, jpeg, wbmp, png, gif] Supported read MIME types: [image/jpeg, image/png, image/x-png, image/vnd.wap.wbmp, image/gif, image/bmp] Supported write MIME types: [image/jpeg, image/png, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif]
This was an example on how to list read/write supported image formats.