How To Get Supported Image Formats Using Java

This example is for how to list read/write image formats in the Java applications. If you are working with the image processing applications, it is necessary to know the supported formats to inform the user. Lets look at the example on how to list read/write image formats. SupportedImageFormatsExample.java

package javabeat.net.core;

import java.util.HashSet;
import java.util.Set;

import javax.imageio.ImageIO;

/**
 * Supported Types Example
 *
 * @author Krishna
 *
 */
public class SupportedImageFormatsExample {
	public static void main(String[] args) {
		Set setOfSupportedTypes = new HashSet();

		//Get list of all registered readers
		String[] formatNamesArray = ImageIO.getReaderFormatNames();

		for (int i = 0; i < formatNamesArray.length; i++) {
			setOfSupportedTypes.add(formatNamesArray[i].toLowerCase());
		}

		System.out.println("Read Formats Supported : " + setOfSupportedTypes);
		setOfSupportedTypes.clear();

		//Get list of all registered writers
		formatNamesArray = ImageIO.getWriterFormatNames();

		for (int i = 0; i < formatNamesArray.length; i++) {
			setOfSupportedTypes.add(formatNamesArray[i].toLowerCase());
		}
		System.out.println("Write Formats Supported : " + setOfSupportedTypes);
		setOfSupportedTypes.clear();

		//Get list of all MIME types registered readers
		formatNamesArray = ImageIO.getReaderMIMETypes();

		for (int i = 0; i < formatNamesArray.length; i++) {
			setOfSupportedTypes.add(formatNamesArray[i].toLowerCase());
		}
		System.out.println("Supported read MIME types: " + setOfSupportedTypes);
		setOfSupportedTypes.clear();

		//Get list of all MIME types registered writers
		formatNamesArray = ImageIO.getWriterMIMETypes();

		for (int i = 0; i < formatNamesArray.length; i++) {
			setOfSupportedTypes.add(formatNamesArray[i].toLowerCase());
		}
		System.out.println("Supported write MIME types: " + setOfSupportedTypes);
	}
}

Output…

Read Formats Supported : [jpg, bmp, jpeg, wbmp, png, gif]
Write Formats Supported : [bmp, jpg, wbmp, jpeg, png, gif]
Supported read MIME types: [image/jpeg, image/png, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif]
Supported write MIME types: [image/png, image/jpeg, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif]

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*