]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Photo.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / data / Photo.java
1 package tim.prune.data;
2
3 import java.awt.Dimension;
4 import java.io.File;
5
6 import javax.swing.ImageIcon;
7
8 /**
9  * Class to represent a photo and link to DataPoint
10  */
11 public class Photo
12 {
13         /** File where photo is stored */
14         private File _file = null;
15         /** Timestamp, if any */
16         private Timestamp _timestamp = null;
17         /** Associated DataPoint if correlated */
18         private DataPoint _dataPoint = null;
19         /** Size of original image */
20         private Dimension _size = null;
21         /** Status of photo when loaded */
22         private Status _originalStatus = Status.NOT_CONNECTED;
23         /** Current photo status */
24         private Status _currentStatus = Status.NOT_CONNECTED;
25         /** rotation flag (clockwise from 0 to 3) */
26         private int _rotation = 0;
27         // TODO: Need to store caption for image?
28         // thumbnail for image (from exif)
29         private byte[] _exifThumbnail = null;
30
31         /** Photo status */
32         public enum Status {
33                 /** Photo is not connected to any point */
34                 NOT_CONNECTED,
35                 /** Photo has been connected to a point since it was loaded */
36                 TAGGED,
37                 /** Photo is connected to a point */
38                 CONNECTED
39         };
40
41         /**
42          * Constructor
43          * @param inFile File object for photo
44          */
45         public Photo(File inFile)
46         {
47                 _file = inFile;
48         }
49
50
51         /**
52          * @return File object where photo resides
53          */
54         public File getFile()
55         {
56                 return _file;
57         }
58
59
60         /**
61          * Set the data point associated with the photo
62          * @param inPoint DataPoint with coordinates etc
63          */
64         public void setDataPoint(DataPoint inPoint)
65         {
66                 _dataPoint = inPoint;
67                 // set status according to point
68                 if (inPoint == null) {
69                         setCurrentStatus(Status.NOT_CONNECTED);
70                 }
71                 else {
72                         setCurrentStatus(Status.CONNECTED);
73                 }
74         }
75
76         /**
77          * @return the DataPoint object
78          */
79         public DataPoint getDataPoint()
80         {
81                 return _dataPoint;
82         }
83
84         /**
85          * @param inTimestamp Timestamp of photo
86          */
87         public void setTimestamp(Timestamp inTimestamp)
88         {
89                 _timestamp = inTimestamp;
90         }
91
92         /**
93          * @return timestamp of photo
94          */
95         public Timestamp getTimestamp()
96         {
97                 return _timestamp;
98         }
99
100         /**
101          * Calculate the size of the image (slow)
102          */
103         private void calculateSize()
104         {
105                 ImageIcon icon = new ImageIcon(_file.getAbsolutePath());
106                 int width = icon.getIconWidth();
107                 int height = icon.getIconHeight();
108                 if (width > 0 && height > 0)
109                 {
110                         _size = new Dimension(width, height);
111                 }
112         }
113
114         /**
115          * @return size of image as Dimension object
116          */
117         public Dimension getSize()
118         {
119                 if (_size == null)
120                 {
121                         calculateSize();
122                 }
123                 return _size;
124         }
125
126         /**
127          * @return width of the image, if known
128          */
129         public int getWidth()
130         {
131                 if (_size == null)
132                 {
133                         calculateSize();
134                         if (_size == null) {return -1;}
135                 }
136                 return _size.width;
137         }
138
139         /**
140          * @return height of the image, if known
141          */
142         public int getHeight()
143         {
144                 if (_size == null)
145                 {
146                         calculateSize();
147                         if (_size == null) {return -1;}
148                 }
149                 return _size.height;
150         }
151
152         /**
153          * @param inStatus status of photo when loaded
154          */
155         public void setOriginalStatus(Status inStatus)
156         {
157                 _originalStatus = inStatus;
158                 _currentStatus = inStatus;
159         }
160
161         /**
162          * @return status of photo when it was loaded
163          */
164         public Status getOriginalStatus()
165         {
166                 return _originalStatus;
167         }
168
169         /**
170          * @return current status of photo
171          */
172         public Status getCurrentStatus()
173         {
174                 return _currentStatus;
175         }
176         /**
177          * @param inStatus current status of photo
178          */
179         public void setCurrentStatus(Status inStatus)
180         {
181                 _currentStatus = inStatus;
182         }
183
184         /**
185          * @return true if photo is connected to a point
186          */
187         public boolean isConnected()
188         {
189                 return _currentStatus != Status.NOT_CONNECTED;
190         }
191
192         /**
193          * @return byte array of thumbnail data
194          */
195         public byte[] getExifThumbnail()
196         {
197                 return _exifThumbnail;
198         }
199
200         /**
201          * @param inBytes byte array from exif
202          */
203         public void setExifThumbnail(byte[] inBytes)
204         {
205                 _exifThumbnail = inBytes;
206         }
207
208         /**
209          * Delete the cached data when the Photo is no longer needed
210          */
211         public void resetCachedData()
212         {
213                 _size = null;
214                 // remove thumbnail too
215         }
216
217         /**
218          * Check if a Photo object refers to the same File as another
219          * @param inOther other Photo object
220          * @return true if the Files are the same
221          */
222         public boolean equals(Photo inOther)
223         {
224                 return (inOther != null && inOther.getFile() != null && getFile() != null
225                         && inOther.getFile().equals(getFile()));
226         }
227
228         /**
229          * @param inRotation initial rotation value (from exif)
230          */
231         public void setRotation(int inRotation)
232         {
233                 if (inRotation >= 0 && inRotation <= 3) {
234                         _rotation = inRotation;
235                 }
236         }
237
238         /**
239          * Rotate the image by 90 degrees
240          * @param inRight true to rotate right, false for left
241          */
242         public void rotate(boolean inRight)
243         {
244                 int dir = inRight?1:3;
245                 _rotation = (_rotation + dir) % 4;
246         }
247
248         /**
249          * @return rotation status
250          */
251         public int getRotationDegrees()
252         {
253                 return _rotation * 90;
254         }
255 }