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.awt.image.ImageObserver;
8 import java.io.File;
9 import java.io.IOException;
10 import java.net.MalformedURLException;
11 import java.net.URL;
12 import java.util.Locale;
13
14 import javax.imageio.ImageIO;
15 import javax.swing.ImageIcon;
16
17 import org.apache.commons.lang.StringUtils;
18 import org.apache.commons.lang.SystemUtils;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import com.jgoodies.uif.util.ResourceUtils;
23 import com.melloware.jukes.file.FileUtil;
24 import com.melloware.jukes.file.filter.ImageFilter;
25 import com.melloware.jukes.gui.tool.Settings;
26
27
28
29
30
31
32
33
34
35
36
37
38 public final class ImageFactory {
39
40 private static final Log LOG = LogFactory.getLog(ImageFactory.class);
41
42 public static final ImageIcon ICO_TRAYICON;
43 public static final ImageIcon ICO_TRAYPLAY;
44 public static final ImageIcon ICO_TRAYPAUSE;
45 public static final ImageIcon ICO_TRAYSTOP;
46 public static final ImageIcon IMAGE_NOCOVER;
47
48 public static Settings settings = null;
49
50
51
52 public static void setSettings(Settings aSettings) {
53 settings = aSettings;
54 }
55
56 static {
57 if (SystemUtils.IS_OS_WINDOWS) {
58 ICO_TRAYICON = getIcon("images/trayicon.png");
59 ICO_TRAYPLAY = getIcon("images/trayplay.png");
60 ICO_TRAYPAUSE = getIcon("images/traypause.png");
61 ICO_TRAYSTOP = getIcon("images/traystop.png");
62 } else {
63 ICO_TRAYICON = getIcon("images/tray_icon_24x24.png");
64 ICO_TRAYPLAY = getIcon("images/tray_play_24x24.png");
65 ICO_TRAYPAUSE = getIcon("images/tray_pause_24x24.png");
66 ICO_TRAYSTOP = getIcon("images/tray_stop_24x24.png");
67 }
68
69 IMAGE_NOCOVER = getIcon("images/image_nocover.jpg");
70 }
71
72
73
74
75 private ImageFactory() {
76 super();
77 }
78
79
80
81
82
83
84
85 public static ImageIcon getDiscImage(final String aFileName) {
86
87
88 return getScaledImage(aFileName, settings.getCoverSizeLarge(), settings.getCoverSizeLarge());
89 }
90
91
92
93
94
95
96
97 public static ImageIcon getIconFromFile(final String aFileName) {
98 return new ImageIcon(aFileName);
99 }
100
101
102
103
104
105
106
107
108 public static Image getImageFromUrl(final String aUrl) {
109 Image image = null;
110 if (StringUtils.isBlank(aUrl)) {
111 throw new IllegalArgumentException("aUrl must be specified");
112 }
113
114 try {
115 final URL url = new URL(aUrl);
116 image = ImageIO.read(url);
117 } catch (MalformedURLException ex) {
118 image = null;
119 } catch (IOException ex) {
120 image = null;
121 }
122 return image;
123 }
124
125
126
127
128
129
130
131
132
133 public static ImageIcon getScaledImage(final String aFileName, final int height, final int width) {
134 ImageIcon icon = null;
135 final String errorString = ResourceUtils.getString("messages.ErrorReadingImageFile") + ": " + aFileName;
136 if (StringUtils.isBlank(aFileName)) {
137 icon = IMAGE_NOCOVER;
138 } else {
139
140
141 try {
142 icon = new ImageIcon(aFileName);
143 }
144 catch (Exception ex) {
145 LOG.error(errorString, ex);
146 }
147 }
148
149
150 if (MediaTracker.ERRORED == icon.getImageLoadStatus()) {
151 icon = IMAGE_NOCOVER;
152 }
153
154
155
156 final Image image = icon.getImage().getScaledInstance(height, width, Image.SCALE_SMOOTH);
157
158 return new ImageIcon(image);
159 }
160
161
162
163
164
165
166
167
168 public static File saveImage(final Image image, final String filename)
169 throws IOException {
170 final int w = image.getHeight(null);
171 final int h = image.getHeight(null);
172 final BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
173 final Graphics2D g2 = bi.createGraphics();
174 g2.drawImage(image, 0, 0, null);
175 g2.dispose();
176 final File file = new File(filename);
177 ImageIO.write(bi, "jpg", file);
178 return file;
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 public static String saveImageWithFileFormat(final Image image,
200 final String aFormat,
201 final String aDirectory,
202 final String aArtist,
203 final String aDisc,
204 final String aYear)
205 throws IOException {
206 String result = null;
207 String newFileName = aFormat;
208 newFileName = StringUtils.replace(newFileName, "%y", aYear);
209 newFileName = StringUtils.replace(newFileName, "%a", aArtist);
210 newFileName = StringUtils.replace(newFileName, "%d", aDisc);
211 newFileName = StringUtils.replace(newFileName, "%A", aArtist.toUpperCase(Locale.US));
212 newFileName = StringUtils.replace(newFileName, "%D", aDisc.toUpperCase(Locale.US));
213 newFileName = FileUtil.correctFileName(newFileName);
214 newFileName = aDirectory + SystemUtils.FILE_SEPARATOR + newFileName + "." + ImageFilter.JPG;
215
216 result = newFileName;
217
218 saveImage(image, newFileName);
219
220 return result;
221 }
222
223
224 public static String saveImageToUserDefinedDirectory(final File imageFile,
225 final String aArtist,
226 final String aDisc,
227 final String aYear)
228 throws IOException {
229 String result = null;
230 String newFileName = settings.getFileFormatImage();
231 String aDirectory = settings.getImagesLocation().toString();
232 int size = settings.getCoverSizeLarge();
233
234 if (settings.isCopyImagesToDirectory()) {
235 newFileName = StringUtils.replace(newFileName, "%y", aYear);
236 newFileName = StringUtils.replace(newFileName, "%a", aArtist);
237 newFileName = StringUtils.replace(newFileName, "%d", aDisc);
238 newFileName = StringUtils.replace(newFileName, "%A", aArtist.toUpperCase(Locale.US));
239 newFileName = StringUtils.replace(newFileName, "%D", aDisc.toUpperCase(Locale.US));
240 newFileName = FileUtil.correctFileName(newFileName);
241 newFileName = aDirectory + SystemUtils.FILE_SEPARATOR + newFileName + "." + ImageFilter.JPG;
242 ImageIcon imageIcon = getScaledImage(imageFile.toString(), size, size);
243 Image newImage = imageIcon.getImage();
244 saveImage(newImage, newFileName);
245 result = imageFile.getAbsolutePath();
246 }
247 else {
248 result = imageFile.getAbsolutePath();
249 }
250 return result;
251 }
252
253
254
255
256
257
258
259 private static ImageIcon getIcon(final String aFileName) {
260 return new ImageIcon(ClassLoader.getSystemResource(aFileName));
261 }
262
263
264 public static String standardImageFileName(final String aArtist,
265 final String aDisc,
266 final String aYear) {
267 String newFileName = settings.getFileFormatImage();
268 String aDirectory = settings.getImagesLocation().toString();
269
270 newFileName = StringUtils.replace(newFileName, "%y", aYear);
271 newFileName = StringUtils.replace(newFileName, "%a", aArtist);
272 newFileName = StringUtils.replace(newFileName, "%d", aDisc);
273 newFileName = StringUtils.replace(newFileName, "%A", aArtist.toUpperCase(Locale.US));
274 newFileName = StringUtils.replace(newFileName, "%D", aDisc.toUpperCase(Locale.US));
275 newFileName = FileUtil.correctFileName(newFileName);
276 newFileName = aDirectory + SystemUtils.FILE_SEPARATOR + newFileName + "." + ImageFilter.JPG;
277 return newFileName;
278 }
279 }