Home » Desktop Java » imageio » List read/write supported image formats

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

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.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of