View Javadoc

1   package com.melloware.jukes.file.image;
2   
3   import java.awt.Graphics2D;
4   import java.awt.Image;
5   import java.awt.MediaTracker;
6   import java.awt.image.BufferedImage;
7   import java.io.File;
8   import java.io.IOException;
9   import java.net.MalformedURLException;
10  import java.net.URL;
11  import java.util.Locale;
12  
13  import javax.imageio.ImageIO;
14  import javax.swing.ImageIcon;
15  
16  import org.apache.commons.lang.StringUtils;
17  import org.apache.commons.lang.SystemUtils;
18  
19  import com.melloware.jukes.file.FileUtil;
20  import com.melloware.jukes.file.filter.ImageFilter;
21  
22  /**
23   * Class to represent and storage area for all icon references.  This will
24   * reduce overhead and memory usage throughout the application.  Also contains
25   * some static helper methods for loading and scaling images.
26   * <p>
27   * <p>Copyright: Copyright (c) 2006</p>
28   * <p>Company: Melloware, Inc.</p>
29   * @author Emil A. Lefkof III
30   * @version 4.0
31   */
32  public final class ImageFactory {
33  
34      public static final ImageIcon ICO_TRAYICON;
35      public static final ImageIcon ICO_TRAYPLAY;
36      public static final ImageIcon ICO_TRAYPAUSE;
37      public static final ImageIcon ICO_TRAYSTOP;
38      public static final ImageIcon IMAGE_NOCOVER;
39  
40      static {
41         if (SystemUtils.IS_OS_WINDOWS) {
42            ICO_TRAYICON = getIcon("images/trayicon.png");
43            ICO_TRAYPLAY = getIcon("images/trayplay.png");
44            ICO_TRAYPAUSE = getIcon("images/traypause.png");
45            ICO_TRAYSTOP = getIcon("images/traystop.png");
46         } else {
47            ICO_TRAYICON = getIcon("images/tray_icon_24x24.png");
48            ICO_TRAYPLAY = getIcon("images/tray_play_24x24.png");
49            ICO_TRAYPAUSE = getIcon("images/tray_pause_24x24.png");
50            ICO_TRAYSTOP = getIcon("images/tray_stop_24x24.png");
51         }
52          
53          IMAGE_NOCOVER = getIcon("images/image_nocover.jpg");
54      }
55  
56      /**
57       * Default constructor. Private so no instantiation.
58       */
59      private ImageFactory() {
60          super();
61      }
62  
63      /**
64       * Gets the disc and returns it the correct size.
65       * <p>
66       * @param aFileName the file location of the disc
67       * @return the ImageIcon
68       */
69      public static ImageIcon getDiscImage(final String aFileName) {
70          return getScaledImage(aFileName, 300, 300);
71      }
72  
73      /**
74       * Method to create a new ImageIcon and return the reference
75       * <p>
76       * @param aFileName filename of icon to load
77       * @return ImageIcon reference of fileName
78       */
79      public static ImageIcon getIconFromFile(final String aFileName) {
80          return new ImageIcon(aFileName);
81      }
82  
83      /**
84       * Gets an Image from the URL specified.  Safe so we return NULL if
85       * anything goes wrong and the image can not be loaded.
86       * <p>
87       * @param aUrl the URL to look for the image
88       * @return the Image if found, NULL if nothing found
89       */
90      public static Image getImageFromUrl(final String aUrl) {
91          Image image = null;
92          if (StringUtils.isBlank(aUrl)) {
93              throw new IllegalArgumentException("aUrl must be specified");
94          }
95  
96          try {
97              final URL url = new URL(aUrl);
98              image = ImageIO.read(url);
99          } catch (MalformedURLException ex) {
100             image = null;
101         } catch (IOException ex) {
102             image = null;
103         }
104         return image;
105     }
106 
107     /**
108      * Gets an ImageIcon and scales it to the size requested.
109      * <p>
110      * @param aFileName the filename to get
111      * @param height the desired height of the image
112      * @param width the desired width of the image
113      * @return the ImageIcon
114      */
115     public static ImageIcon getScaledImage(final String aFileName, final int height, final int width) {
116         ImageIcon icon = null;
117         if (StringUtils.isBlank(aFileName)) {
118             icon = IMAGE_NOCOVER;
119         } else {
120             // get the image
121             icon = new ImageIcon(aFileName);
122         }
123 
124         // if no image was found return the default
125         if (MediaTracker.ERRORED == icon.getImageLoadStatus()) {
126             icon = IMAGE_NOCOVER;
127         }
128 
129         // resizing Image Icon
130         final Image image = icon.getImage().getScaledInstance(height, width, Image.SCALE_DEFAULT);
131 
132         return new ImageIcon(image);
133     }
134 
135     /**
136      * Saves an image file to disk in the location specified.
137      * <p>
138      * @param image the image to save
139      * @param filename the file to save it as
140      * @throws IOException if any error occurs writing the file.
141      */
142     public static File saveImage(final Image image, final String filename)
143                           throws IOException {
144         final int w = image.getHeight(null);
145         final int h = image.getHeight(null);
146         final BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
147         final Graphics2D g2 = bi.createGraphics();
148         g2.drawImage(image, 0, 0, null);
149         g2.dispose();
150         final File file = new File(filename);
151         ImageIO.write(bi, "jpg", file);
152         return file;
153     }
154 
155     /**
156      * Saves this image file based on a format from prefs.  The format is in
157      * aFormat and can have values  %a for artist, and %d for disc.
158      * Replaces any invalid characters
159      * (\\, /, :, , *, ?, ", <, >, or |) with underscores _ to prevent any
160      * errors on file systems.
161      *
162      * Examples:
163      * %a - %d = Black Crowes - Amorica.jpg
164      * <p>
165      * @param image the image to save
166      * @param aFormat the string format like %a - %d = Black Crowes - Amorica.jpg
167      * @param aDirectory the directory to save the file in.
168      * @param aArtist the name of the artist
169      * @param aDisc the name of the disc
170      * @return true if renamed, false if failure
171      * @throws IOException if any error occurs writing the file out
172      */
173     public static String saveImageWithFileFormat(final Image image,
174                                                  final String aFormat,
175                                                  final String aDirectory,
176                                                  final String aArtist,
177                                                  final String aDisc)
178                                           throws IOException {
179         String result = null;
180         String newFileName = aFormat;
181         newFileName = StringUtils.replace(newFileName, "%a", aArtist);
182         newFileName = StringUtils.replace(newFileName, "%d", aDisc);
183         newFileName = StringUtils.replace(newFileName, "%A", aArtist.toUpperCase(Locale.US));
184         newFileName = StringUtils.replace(newFileName, "%D", aDisc.toUpperCase(Locale.US));
185         newFileName = FileUtil.correctFileName(newFileName);
186         newFileName = aDirectory + SystemUtils.FILE_SEPARATOR + newFileName + "." + ImageFilter.JPG;
187 
188         result = newFileName;
189 
190         saveImage(image, newFileName);
191 
192         return result;
193     }
194 
195     /**
196      * Method to create a new ImageIcon and return the reference
197      * <p>
198      * @param aFileName filename of icon to load
199      * @return ImageIcon reference of fileName
200      */
201     private static ImageIcon getIcon(final String aFileName) {
202         return new ImageIcon(ClassLoader.getSystemResource(aFileName));
203     }
204 }    // end class ImageFactory