1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.jdiskcatalog.analysis;
24
25 import java.awt.Graphics;
26 import java.awt.Image;
27 import java.awt.image.BufferedImage;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import javax.imageio.ImageIO;
34
35 import net.sf.jdiskcatalog.api.NodeInfo;
36 import net.sf.jdiskcatalog.api.StreamAnalyser;
37 import net.sf.jdiskcatalog.config.Configurable;
38 import net.sf.jdiskcatalog.config.Configuration;
39
40
41
42
43
44
45
46
47 public class ImageThumbnail implements StreamAnalyser, Configurable
48 {
49 public static final String KEY_THUMBNAIL = "thumbnail";
50 public static final String KEY_IMAGE_HEIGHT = "image.height";
51 public static final String KEY_IMAGE_WIDTH = "image.width";
52
53 public static final String OPTION_THUMBNAIL_WIDTH = "thumbnail.width";
54 public static final String OPTION_THUMBNAIL_HEIGHT = "thumbnail.height";
55 private static final Map<String, Object> DECLARED_PROPERTIES = new HashMap<String, Object>();
56 static
57 {
58 DECLARED_PROPERTIES.put(OPTION_THUMBNAIL_WIDTH, 160);
59 DECLARED_PROPERTIES.put(OPTION_THUMBNAIL_HEIGHT, 160);
60 }
61
62 private Configuration config;
63
64 public Map<String, Object> analyse(InputStream stream, NodeInfo nodeInfo) throws IOException
65 {
66 String filename = nodeInfo.getName().toLowerCase();
67 if (!filename.endsWith(".jpg") && !filename.endsWith(".jpeg") && !filename.endsWith(".gif") && !filename.endsWith(".png") && !filename.endsWith(".bmp"))
68 return null;
69 Map<String, Object> properties = new HashMap<String, Object>();
70
71 BufferedImage image;
72 try
73 {
74 image = ImageIO.read(stream);
75 }
76 catch (IllegalArgumentException e)
77 {
78
79 throw new IOException(e);
80 }
81 if (image == null)
82 return null;
83
84 BufferedImage thumb = image;
85 int imageWidth = image.getWidth(null);
86 int imageHeight = image.getHeight(null);
87 int thumbWidth = config.getInt(OPTION_THUMBNAIL_WIDTH);
88 int thumbHeight = config.getInt(OPTION_THUMBNAIL_HEIGHT);
89
90 if (imageWidth > thumbWidth || imageHeight > thumbHeight)
91 {
92 if ((double)thumbWidth / thumbHeight < (double)imageWidth / imageHeight)
93 thumbHeight = thumbWidth * imageHeight / imageWidth;
94 else
95 thumbWidth = thumbHeight * imageWidth / imageHeight;
96
97
98 Image scaled = image.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH);
99
100 thumb = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
101 Graphics g = thumb.getGraphics();
102 g.drawImage(scaled, 0, 0, null);
103 }
104
105 properties.put(KEY_THUMBNAIL, new JpegImage(thumb));
106 properties.put(KEY_IMAGE_WIDTH, imageWidth);
107 properties.put(KEY_IMAGE_HEIGHT, imageHeight);
108 return properties;
109 }
110
111 public void setConfiguration(Configuration config)
112 {
113 this.config = config;
114 }
115
116 public Map<String, Object> getDeclaredProperties()
117 {
118 return DECLARED_PROPERTIES;
119 }
120 }