KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > Image


1
2 package de.jwi.jgallery;
3
4 /*
5  * jGallery - Java web application to display image galleries
6  *
7  * Copyright (C) 2004 Juergen Weber
8  *
9  * This file is part of jGallery.
10  *
11  * jGallery is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * jGallery is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with jGallery; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
24  */

25
26 import imageinfo.ImageInfo;
27
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.Serializable JavaDoc;
32 import java.text.DateFormat JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.List JavaDoc;
35
36 /**
37  * @author Jürgen Weber Source file created on 22.02.2004
38  */

39 public class Image implements Serializable JavaDoc
40 {
41
42     private boolean isRepresentsSubdirectory = false;
43
44     private String JavaDoc name;
45
46     private Folder folder;
47
48
49     private String JavaDoc fileSize = "";
50
51     private String JavaDoc thumbWidth;
52
53     private String JavaDoc thumbHeight;
54
55     private String JavaDoc fileDate = "";
56
57     private long lastModified;
58
59     private String JavaDoc imageWidth; // Width of image in Pixels
60

61     private String JavaDoc imageHeight; // Height of image in pixels
62

63     private String JavaDoc comment;
64
65     private EXIFInfo exif;
66
67     private ThumbNailInfo thumbNailInfo;
68
69     Image(String JavaDoc name, boolean isRepresentsSubdirectory, Folder folder,
70             IImageAccessor imageAccessor, ThumbNailInfo thumbNailInfo)
71             throws GalleryException
72     {
73         this.name = name;
74         this.folder = folder;
75         this.isRepresentsSubdirectory = isRepresentsSubdirectory;
76         this.thumbNailInfo = thumbNailInfo;
77
78         lastModified = imageAccessor.getLastModified();
79
80         fileDate = DateFormat.getDateInstance().format(new Date JavaDoc(lastModified));
81
82         InputStream JavaDoc imageInputStream = null;
83         InputStream JavaDoc thumbInputStream = null;
84
85         if (!isRepresentsSubdirectory)
86         {
87             try
88             {
89                 // get image size using ImageInfo class
90

91                 imageInputStream = imageAccessor.getImageInputStream();
92                 ImageInfo ii = new ImageInfo();
93                 ii.setInput(imageInputStream);
94                 if (!ii.check())
95                 {
96                     throw new GalleryException(
97                             "Not a supported image file format.");
98                 }
99
100                 imageWidth = Integer.toString(ii.getWidth());
101                 imageHeight = Integer.toString(ii.getHeight());
102
103                 fileSize = Long.toString(imageAccessor.getLength());
104
105                 // get thumbnail image size using ImageInfo class
106

107                 thumbInputStream = imageAccessor.getThumbInputStream();
108
109                 ii = new ImageInfo();
110                 ii.setInput(thumbInputStream);
111                 if (!ii.check())
112                 {
113                     throw new GalleryException(
114                             "Not a supported image file format.");
115                 }
116
117                 thumbWidth = Integer.toString(ii.getWidth());
118                 thumbHeight = Integer.toString(ii.getHeight());
119
120
121             }
122             catch (FileNotFoundException JavaDoc e)
123             {
124                 throw new GalleryException(e.getMessage());
125             }
126             finally
127             {
128                 if (imageInputStream != null)
129                 {
130                     try
131                     {
132                         imageInputStream.close();
133                     }
134                     catch (IOException JavaDoc e)
135                     {
136                         // NOP
137
}
138                 }
139                 if (thumbInputStream != null)
140                 {
141                     try
142                     {
143                         thumbInputStream.close();
144                     }
145                     catch (IOException JavaDoc e)
146                     {
147                         // NOP
148
}
149                 }
150             }
151
152
153             try
154             {
155                 imageInputStream = imageAccessor.getImageInputStream();
156             }
157             catch (FileNotFoundException JavaDoc e)
158             {
159                 throw new GalleryException(e.getMessage());
160             }
161
162             try
163             {
164                 exif = new EXIFInfo(imageInputStream);
165             }
166             catch (GalleryException e1)
167             {
168                 exif = null;
169             }
170         }
171
172     }
173
174     public String JavaDoc toString()
175     {
176         return name;
177     }
178
179     public String JavaDoc getName()
180     {
181         return name;
182     }
183
184     private String JavaDoc labelfile;
185
186     public String JavaDoc getLabelfile()
187             throws FileNotFoundException JavaDoc, IOException JavaDoc, GalleryException
188     {
189         if (null == labelfile)
190         {
191             labelfile = folder.getFileContent(getLabel() + ".txt");
192         }
193
194         return labelfile;
195     }
196
197     public String JavaDoc getLabel()
198     {
199         int p = name.indexOf('.');
200
201         return (Folder.isJPEGExtension(name) & (p > -1))
202                 ? name.substring(0, p)
203                 : name;
204     }
205
206     public String JavaDoc getFileName()
207     {
208         return name;
209     }
210
211     public String JavaDoc getCloseupPath()
212     {
213         String JavaDoc n;
214         if (isRepresentsSubdirectory)
215         {
216             n = name + "/index." + folder.getUrlExtention();
217         }
218         else
219         {
220             n = name.substring(0, name.indexOf('.')) + "."
221                     + folder.getUrlExtention();
222         }
223         return folder.getHTMLBase() + n;
224     }
225
226     public String JavaDoc getIconPath()
227     {
228         return isRepresentsSubdirectory ? thumbNailInfo.getThumbPath() : "";
229     }
230
231     public String JavaDoc getImagePath()
232     {
233         return folder.getImageBasePath() + "/" + name;
234     }
235
236     public String JavaDoc getThumbHeight()
237     {
238         return isRepresentsSubdirectory
239                 ? thumbNailInfo.getThumbHeight()
240                 : thumbHeight;
241     }
242
243     public String JavaDoc getThumbPath()
244     {
245         return isRepresentsSubdirectory ? thumbNailInfo.getThumbPath() : folder.getThumbsPath() + "/" + name;
246         
247 // return folder.getThumbsPath() + "/" + name;
248
}
249
250     public String JavaDoc getThumbBound()
251     {
252         int h = Integer.parseInt(isRepresentsSubdirectory ? thumbNailInfo
253                 .getThumbHeight() : thumbHeight);
254         int w = Integer.parseInt(isRepresentsSubdirectory ? thumbNailInfo
255                 .getThumbWidth() : thumbWidth);
256
257         return Integer.toString(Math.max(h, w));
258     }
259
260
261     public List JavaDoc getNeighbourImages() throws GalleryException
262     {
263         return folder.getNeighbourImages(this);
264     }
265
266     public String JavaDoc getThumbWidth()
267     {
268         return isRepresentsSubdirectory
269                 ? thumbNailInfo.getThumbWidth()
270                 : thumbWidth;
271     }
272
273     /**
274      * @return Path to original file. Only defined if linking is "Link to
275      * originals via scaled images". If linking is "Link to originals"
276      * then imagePath links to the original image
277      */

278     public String JavaDoc getOriginalPath()
279     {
280         return "";
281     }
282
283     public String JavaDoc getImageHeight()
284     {
285         return imageHeight;
286     }
287
288     public String JavaDoc getImageWidth()
289     {
290         return imageWidth;
291     }
292
293     public Folder getFolder()
294     {
295         return folder;
296     }
297
298     public void setFolder(Folder folder)
299     {
300         this.folder = folder;
301     }
302
303
304     public void setName(String JavaDoc name)
305     {
306         this.name = name;
307     }
308
309     public String JavaDoc getFileDate()
310     {
311         return fileDate;
312     }
313
314     public EXIFInfo getExif()
315     {
316         return exif;
317     }
318
319     public String JavaDoc getComment()
320     {
321         return comment;
322     }
323
324     public void setComment(String JavaDoc comment)
325     {
326         this.comment = comment;
327     }
328
329     public String JavaDoc getFileSize()
330     {
331         return fileSize;
332     }
333
334     public long getLastModified()
335     {
336         return lastModified;
337     }
338
339     public String JavaDoc getCounter()
340     {
341         String JavaDoc rc = folder.getImageCounter(name);
342
343         return rc;
344     }
345
346     public boolean getRepresentsSubdirectory()
347     {
348         return isRepresentsSubdirectory;
349     }
350
351 }
Popular Tags