View Javadoc

1   /*
2    * JDiskCatalog
3    *
4    * Copyright 2007 Przemek Więch
5    *
6    * This file is part of JDiskCatalog.
7    *
8    * JDiskCatalog is free software; you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17  
18   * You should have received a copy of the GNU General Public License
19   * along with this program; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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   * Retrieves a thumbnail from images using java's ImageIO.
43   *
44   * @author Przemek Więch <pwiech@losthive.org>
45   * @version $Id$
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  			// Reading image failed
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  			// scale
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 }