]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Photo.java
9eacac2706a87d1c2ef451c879ddc717b6c565d6
[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                 {
70                         setCurrentStatus(Status.NOT_CONNECTED);
71                 }
72                 else
73                 {
74                         setCurrentStatus(Status.CONNECTED);
75                 }
76         }
77
78         /**
79          * @return the DataPoint object
80          */
81         public DataPoint getDataPoint()
82         {
83                 return _dataPoint;
84         }
85
86         /**
87          * @param inTimestamp Timestamp of photo
88          */
89         public void setTimestamp(Timestamp inTimestamp)
90         {
91                 _timestamp = inTimestamp;
92         }
93
94         /**
95          * @return timestamp of photo
96          */
97         public Timestamp getTimestamp()
98         {
99                 return _timestamp;
100         }
101
102         /**
103          * Calculate the size of the image (slow)
104          */
105         private void calculateSize()
106         {
107                 ImageIcon icon = new ImageIcon(_file.getAbsolutePath());
108                 int width = icon.getIconWidth();
109                 int height = icon.getIconHeight();
110                 if (width > 0 && height > 0)
111                 {
112                         _size = new Dimension(width, height);
113                 }
114         }
115
116         /**
117          * @return size of image as Dimension object
118          */
119         public Dimension getSize()
120         {
121                 if (_size == null)
122                 {
123                         calculateSize();
124                 }
125                 return _size;
126         }
127
128         /**
129          * @return width of the image, if known
130          */
131         public int getWidth()
132         {
133                 if (_size == null)
134                 {
135                         calculateSize();
136                         if (_size == null) {return -1;}
137                 }
138                 return _size.width;
139         }
140
141         /**
142          * @return height of the image, if known
143          */
144         public int getHeight()
145         {
146                 if (_size == null)
147                 {
148                         calculateSize();
149                         if (_size == null) {return -1;}
150                 }
151                 return _size.height;
152         }
153
154         /**
155          * @param inStatus status of photo when loaded
156          */
157         public void setOriginalStatus(Status inStatus)
158         {
159                 _originalStatus = inStatus;
160                 _currentStatus = inStatus;
161         }
162
163         /**
164          * @return status of photo when it was loaded
165          */
166         public Status getOriginalStatus()
167         {
168                 return _originalStatus;
169         }
170
171         /**
172          * @return current status of photo
173          */
174         public Status getCurrentStatus()
175         {
176                 return _currentStatus;
177         }
178         /**
179          * @param inStatus current status of photo
180          */
181         public void setCurrentStatus(Status inStatus)
182         {
183                 _currentStatus = inStatus;
184         }
185
186         /**
187          * @return true if photo is connected to a point
188          */
189         public boolean isConnected()
190         {
191                 return _currentStatus != Status.NOT_CONNECTED;
192         }
193
194         /**
195          * @return byte array of thumbnail data
196          */
197         public byte[] getExifThumbnail()
198         {
199                 return _exifThumbnail;
200         }
201
202         /**
203          * @param inBytes byte array from exif
204          */
205         public void setExifThumbnail(byte[] inBytes)
206         {
207                 _exifThumbnail = inBytes;
208         }
209
210         /**
211          * Delete the cached data when the Photo is no longer needed
212          */
213         public void resetCachedData()
214         {
215                 _size = null;
216                 // remove thumbnail too
217         }
218
219         /**
220          * Check if a Photo object refers to the same File as another
221          * @param inOther other Photo object
222          * @return true if the Files are the same
223          */
224         public boolean equals(Photo inOther)
225         {
226                 return (inOther != null && inOther.getFile() != null && getFile() != null
227                         && inOther.getFile().equals(getFile()));
228         }
229
230         /**
231          * @param inRotation initial rotation value (from exif)
232          */
233         public void setRotation(int inRotation)
234         {
235                 if (inRotation >= 0 && inRotation <= 3) {
236                         _rotation = inRotation;
237                 }
238         }
239
240         /**
241          * Rotate the image by 90 degrees
242          * @param inRight true to rotate right, false for left
243          */
244         public void rotate(boolean inRight)
245         {
246                 int dir = inRight?1:3;
247                 _rotation = (_rotation + dir) % 4;
248         }
249
250         /**
251          * @return rotation status
252          */
253         public int getRotationDegrees()
254         {
255                 return _rotation * 90;
256         }
257 }