]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Photo.java
Version 13, August 2011
[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 extends MediaObject
12 {
13         /** Size of original image */
14         private Dimension _size = null;
15         /** rotation flag (clockwise from 0 to 3) */
16         private int _rotation = 0;
17         // TODO: Need to store caption for image?
18         /** Bearing, if any */
19         private double _bearing = -1.0;
20         /** thumbnail for image (from exif) */
21         private byte[] _exifThumbnail = null;
22
23         /**
24          * Constructor
25          * @param inFile File object for photo
26          */
27         public Photo(File inFile)
28         {
29                 super(inFile, null);
30         }
31
32         /**
33          * Constructor using data, eg from zip file or URL
34          * @param inData data as byte array
35          * @param inName name of file from which it came
36          * @param inUrl url from which it came (or null)
37          */
38         public Photo(byte[] inData, String inName, String inUrl)
39         {
40                 super(inData, inName, inUrl);
41         }
42
43         /**
44          * Calculate the size of the image (slow)
45          */
46         private void calculateSize()
47         {
48                 ImageIcon icon = null;
49                 if (_file != null)
50                         icon = new ImageIcon(_file.getAbsolutePath());
51                 else
52                         icon = new ImageIcon(_data);
53                 int width = icon.getIconWidth();
54                 int height = icon.getIconHeight();
55                 if (width > 0 && height > 0)
56                 {
57                         _size = new Dimension(width, height);
58                 }
59         }
60
61         /**
62          * @return size of image as Dimension object
63          */
64         public Dimension getSize()
65         {
66                 if (_size == null) {
67                         calculateSize();
68                 }
69                 return _size;
70         }
71
72         /**
73          * @return width of the image, if known
74          */
75         public int getWidth()
76         {
77                 if (getSize() == null) {return -1;}
78                 return _size.width;
79         }
80
81         /**
82          * @return height of the image, if known
83          */
84         public int getHeight()
85         {
86                 if (getSize() == null) {return -1;}
87                 return _size.height;
88         }
89
90         /**
91          * @return byte array of thumbnail data
92          */
93         public byte[] getExifThumbnail()
94         {
95                 return _exifThumbnail;
96         }
97
98         /**
99          * @param inBytes byte array from exif
100          */
101         public void setExifThumbnail(byte[] inBytes)
102         {
103                 _exifThumbnail = inBytes;
104         }
105
106         /**
107          * Delete the cached data when the Photo is no longer needed
108          */
109         public void resetCachedData()
110         {
111                 _size = null;
112                 // remove thumbnail too
113         }
114
115         /**
116          * @param inRotation initial rotation value (from exif)
117          */
118         public void setRotation(int inRotation)
119         {
120                 if (inRotation >= 0 && inRotation <= 3) {
121                         _rotation = inRotation;
122                 }
123         }
124
125         /**
126          * Rotate the image by 90 degrees
127          * @param inRight true to rotate right, false for left
128          */
129         public void rotate(boolean inRight)
130         {
131                 int dir = inRight?1:3;
132                 _rotation = (_rotation + dir) % 4;
133         }
134
135         /**
136          * @return rotation status
137          */
138         public int getRotationDegrees()
139         {
140                 return _rotation * 90;
141         }
142
143         /**
144          * @return a new image icon for the whole image
145          */
146         public ImageIcon createImageIcon()
147         {
148                 if (_file != null) {
149                         return new ImageIcon(_file.getAbsolutePath());
150                 }
151                 if (_data != null) {
152                         return new ImageIcon(_data);
153                 }
154                 return null;
155         }
156
157         /**
158          * @param inValue bearing in degrees, 0 to 360
159          */
160         public void setBearing(double inValue) {
161                 _bearing = inValue;
162         }
163
164         /** @return bearing in degrees */
165         public double getBearing() {
166                 return _bearing;
167         }
168 }