The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.imageio  [6 examples]

e696. Listing the Image Formats That Can Be Read and Written

By default, the javax.imageio package can read GIF, PNG, and JPEG images and can write PNG and JPEG images. The complete list of available readable and writeable formats can be retrieved by calling ImageIO.getReaderFormatNames() and ImageIO.getWriterFormatNames().

Note: These methods may return the format name in both lowercase and uppercase. This example implements a unique() method that removes duplicates, regardless of case.

    // Get list of unique supported read formats
    String[] formatNames = ImageIO.getReaderFormatNames();
    formatNames = unique(formatNames);
    // e.g. png jpeg gif jpg
    
    // Get list of unique supported write formats
    formatNames = ImageIO.getWriterFormatNames();
    formatNames = unique(formatNames);
    // e.g. png jpeg jpg
    
    // Get list of unique MIME types that can be read
    formatNames = ImageIO.getReaderMIMETypes();
    formatNames = unique(formatNames);
    // e.g image/jpeg image/png image/x-png image/gif
    
    // Get list of unique MIME types that can be written
    formatNames = ImageIO.getWriterMIMETypes();
    formatNames = unique(formatNames);
    // e.g. image/jpeg image/png image/x-png
    
    // Converts all strings in 'strings' to lowercase
    // and returns an array containing the unique values.
    // All returned values are lowercase.
    public static String[] unique(String[] strings) {
        Set set = new HashSet();
        for (int i=0; i<strings.length; i++) {
            String name = strings[i].toLowerCase();
            set.add(name);
        }
        return (String[])set.toArray(new String[0]);
    }

 Related Examples
e694. Reading an Image from a File, InputStream, or URL
e695. Saving a Generated Graphic to a PNG or JPEG File
e697. Determining If an Image Format Can Be Read or Written
e698. Determining the Format of an Image in a File
e699. Compressing a JPEG File


© 2002 Addison-Wesley.