imageio

List read/write supported image formats

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: April 27th, 2013
0 260 1 minute read

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 bewHashSet<String>.
  • UseImageIO.getReaderFormatNames to read all the format names you can read.
  • UseImageIO.getWriterFormatNames() to get all the image formats you can write.
  • UseImageIO.getReaderMIMETypes() to get list of all MIME types understood by the current set of registered readers.
  • UseImageIO.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 readersString[] 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 writersformatNames = 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 readersformatNames = 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 writersformatNames = 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.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: April 27th, 2013
0 260 1 minute read
Photo of 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 atJava Code Geeks.

Related Articles

Compress a JPEG file

November 11th, 2012

Determine format of an image

November 11th, 2012

Read image from file

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.