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.getReaderFormatNamesto 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 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.
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.



