View Javadoc

1   /*
2    * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: ImageIODetector.java,v 584e9b40f728 2009/04/09 07:41:44 maxberger $ */
18  
19  package net.sourceforge.jeuclid.converter;
20  
21  import java.util.Iterator;
22  import java.util.Set;
23  import java.util.TreeSet;
24  
25  import javax.imageio.ImageIO;
26  import javax.imageio.ImageWriter;
27  
28  /**
29   * Detects and registers the Converters from ImageIO.
30   * 
31   * @version $Revision: 584e9b40f728 $
32   */
33  public final class ImageIODetector implements ConverterDetector {
34  
35      /**
36       * Default constructor.
37       */
38      public ImageIODetector() {
39          // Empty on purpose
40      }
41  
42      /**
43       * Detects and registers all converters available through ImageIO.
44       * 
45       * @param registry
46       *            ConverterRegistry to use.
47       */
48      public void detectConversionPlugins(final ConverterRegistry registry) {
49  
50          final String[] mimeTypes = ImageIO.getWriterMIMETypes();
51  
52          final Set<String> noAlphaMimeTypes = new TreeSet<String>();
53          noAlphaMimeTypes.add("image/jpeg");
54          noAlphaMimeTypes.add("image/bmp");
55  
56          for (final String mimeType : mimeTypes) {
57              final Iterator<ImageWriter> iwit = ImageIO
58                      .getImageWritersByMIMEType(mimeType);
59              if (iwit != null) {
60                  while (iwit.hasNext()) {
61                      final ImageWriter iw = iwit.next();
62                      final String[] suffixes = iw.getOriginatingProvider()
63                              .getFileSuffixes();
64                      if (suffixes != null) {
65                          for (final String suffix : suffixes) {
66                              registry.registerMimeTypeAndSuffix(mimeType,
67                                      suffix, false);
68                          }
69                      }
70                      registry.registerConverter(mimeType,
71                              new ImageIOConverter(iw, noAlphaMimeTypes
72                                      .contains(mimeType)), false);
73                  }
74  
75              }
76  
77          }
78  
79      }
80  }