]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Photo.java
c9046743a704ca2629e8341ebef1d4497f878384
[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 MediaFile
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         // thumbnail for image (from exif)
19         private byte[] _exifThumbnail = null;
20
21         /**
22          * Constructor
23          * @param inFile File object for photo
24          */
25         public Photo(File inFile)
26         {
27                 super(inFile, null);
28         }
29
30         /**
31          * Calculate the size of the image (slow)
32          */
33         private void calculateSize()
34         {
35                 ImageIcon icon = new ImageIcon(_file.getAbsolutePath());
36                 int width = icon.getIconWidth();
37                 int height = icon.getIconHeight();
38                 if (width > 0 && height > 0)
39                 {
40                         _size = new Dimension(width, height);
41                 }
42         }
43
44         /**
45          * @return size of image as Dimension object
46          */
47         public Dimension getSize()
48         {
49                 if (_size == null) {
50                         calculateSize();
51                 }
52                 return _size;
53         }
54
55         /**
56          * @return width of the image, if known
57          */
58         public int getWidth()
59         {
60                 if (_size == null)
61                 {
62                         calculateSize();
63                         if (_size == null) {return -1;}
64                 }
65                 return _size.width;
66         }
67
68         /**
69          * @return height of the image, if known
70          */
71         public int getHeight()
72         {
73                 if (_size == null)
74                 {
75                         calculateSize();
76                         if (_size == null) {return -1;}
77                 }
78                 return _size.height;
79         }
80
81         /**
82          * @return byte array of thumbnail data
83          */
84         public byte[] getExifThumbnail()
85         {
86                 return _exifThumbnail;
87         }
88
89         /**
90          * @param inBytes byte array from exif
91          */
92         public void setExifThumbnail(byte[] inBytes)
93         {
94                 _exifThumbnail = inBytes;
95         }
96
97         /**
98          * Delete the cached data when the Photo is no longer needed
99          */
100         public void resetCachedData()
101         {
102                 _size = null;
103                 // remove thumbnail too
104         }
105
106         /**
107          * @param inRotation initial rotation value (from exif)
108          */
109         public void setRotation(int inRotation)
110         {
111                 if (inRotation >= 0 && inRotation <= 3) {
112                         _rotation = inRotation;
113                 }
114         }
115
116         /**
117          * Rotate the image by 90 degrees
118          * @param inRight true to rotate right, false for left
119          */
120         public void rotate(boolean inRight)
121         {
122                 int dir = inRight?1:3;
123                 _rotation = (_rotation + dir) % 4;
124         }
125
126         /**
127          * @return rotation status
128          */
129         public int getRotationDegrees()
130         {
131                 return _rotation * 90;
132         }
133 }