]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/drew/jpeg/JpegData.java
Version 9, February 2010
[GpsPrune.git] / tim / prune / drew / jpeg / JpegData.java
1 package tim.prune.drew.jpeg;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 /**
7  * Class to hold the GPS data extracted from a Jpeg including position and time
8  */
9 public class JpegData
10 {
11         private boolean _exifDataPresent = false;
12         private char _latitudeRef = '\0';
13         private char _longitudeRef = '\0';
14         private byte _altitudeRef = 0;
15         private Rational[] _latitude = null;
16         private Rational[] _longitude = null;
17         private Rational   _altitude = null;
18         private Rational[] _gpsTimestamp = null;
19         private Rational[] _gpsDatestamp = null;
20         private String _originalTimestamp = null;
21         private int _orientationCode = -1;
22         private byte[] _thumbnail = null;
23         private ArrayList<String> _errors = null;
24
25
26         /**
27          * Set the exif data present flag
28          */
29         public void setExifDataPresent()
30         {
31                 _exifDataPresent = true;
32         }
33         /**
34          * @return true if exif data found
35          */
36         public boolean getExifDataPresent()
37         {
38                 return _exifDataPresent;
39         }
40
41         /**
42          * Set the latitude reference (N/S)
43          * @param inChar character representing reference
44          */
45         public void setLatitudeRef(char inChar)
46         {
47                 _latitudeRef = inChar;
48         }
49
50         /**
51          * Set the latitude reference (N/S)
52          * @param inString string containing reference
53          */
54         public void setLatitudeRef(String inString)
55         {
56                 if (inString != null && inString.length() == 1)
57                         setLatitudeRef(inString.charAt(0));
58         }
59
60         /**
61          * Set the latitude
62          * @param inValues array of three Rationals for deg-min-sec
63          */
64         public void setLatitude(Rational[] inValues)
65         {
66                 if (inValues != null && inValues.length == 3)
67                         _latitude = inValues;
68         }
69
70         /**
71          * Set the longitude reference (E/W)
72          * @param inChar character representing reference
73          */
74         public void setLongitudeRef(char inChar)
75         {
76                 _longitudeRef = inChar;
77         }
78
79         /**
80          * Set the longitude reference (E/W)
81          * @param inString string containing reference
82          */
83         public void setLongitudeRef(String inString)
84         {
85                 if (inString != null && inString.length() == 1)
86                         setLongitudeRef(inString.charAt(0));
87         }
88
89         /**
90          * Set the longitude
91          * @param inValues array of three Rationals for deg-min-sec
92          */
93         public void setLongitude(Rational[] inValues)
94         {
95                 if (inValues != null && inValues.length == 3)
96                         _longitude = inValues;
97         }
98
99         /**
100          * Set the altitude reference (sea level / not)
101          * @param inByte byte representing reference
102          */
103         public void setAltitudeRef(byte inByte)
104         {
105                 _altitudeRef = inByte;
106         }
107
108         /**
109          * Set the altitude
110          * @param inRational Rational number representing altitude
111          */
112         public void setAltitude(Rational inRational)
113         {
114                 _altitude = inRational;
115         }
116
117         /**
118          * Set the Gps timestamp
119          * @param inValues array of Rationals holding timestamp
120          */
121         public void setGpsTimestamp(Rational[] inValues)
122         {
123                 _gpsTimestamp = inValues;
124         }
125
126         /**
127          * Set the Gps datestamp
128          * @param inValues array of Rationals holding datestamp
129          */
130         public void setGpsDatestamp(Rational[] inValues)
131         {
132                 _gpsDatestamp = inValues;
133         }
134
135         /**
136          * Set the original timestamp
137          * @param inStamp original timestamp of photo
138          */
139         public void setOriginalTimestamp(String inStamp)
140         {
141                 _originalTimestamp = inStamp;
142         }
143
144         /**
145          * Set the orientation code
146          * @param inCode code from exif (1 to 8)
147          */
148         public void setOrientationCode(int inCode)
149         {
150                 if (inCode >= 1 && inCode <= 8) {
151                         _orientationCode = inCode;
152                 }
153         }
154
155         /** @return latitude ref as char */
156         public char getLatitudeRef() { return _latitudeRef; }
157         /** @return latitude as array of 3 Rationals */
158         public Rational[] getLatitude() { return _latitude; }
159         /** @return longitude ref as char */
160         public char getLongitudeRef() { return _longitudeRef; }
161         /** @return longitude as array of 3 Rationals */
162         public Rational[] getLongitude() { return _longitude; }
163         /** @return altitude ref as byte (should be 0) */
164         public byte getAltitudeRef() { return _altitudeRef; }
165         /** @return altitude as Rational */
166         public Rational getAltitude() { return _altitude; }
167         /** @return Gps timestamp as array of 3 Rationals */
168         public Rational[] getGpsTimestamp() { return _gpsTimestamp; }
169         /** @return Gps datestamp as array of 3 Rationals */
170         public Rational[] getGpsDatestamp() { return _gpsDatestamp; }
171         /** @return orientation code (1 to 8) */
172         public int getOrientationCode() { return _orientationCode; }
173         /** @return original timestamp as string */
174         public String getOriginalTimestamp() { return _originalTimestamp; }
175
176         /**
177          * Set the thumbnail
178          * @param inBytes byte array containing thumbnail
179          */
180         public void setThumbnailImage(byte[] inBytes) {
181                 _thumbnail = inBytes;
182         }
183         /** @return thumbnail as byte array */
184         public byte[] getThumbnailImage() {
185                 return _thumbnail;
186         }
187
188         /**
189          * @return rotation required to display photo properly (0 to 3)
190          */
191         public int getRequiredRotation()
192         {
193                 if (_orientationCode <= 2) { return 0; } // no rotation required
194                 if (_orientationCode <= 4) { return 2; } // 180 degrees
195                 if (_orientationCode <= 6) { return 1; } // 270 degrees, so need to rotate by 90
196                 return 3; // 90 degrees, so need to rotate by 270
197         }
198
199         /**
200          * @return true if data looks valid, ie has at least lat and long
201          *  (altitude and timestamp optional).
202          */
203         public boolean isValid()
204         {
205                 return (_latitudeRef == 'N' || _latitudeRef == 'n' || _latitudeRef == 'S' || _latitudeRef == 's')
206                         && _latitude != null
207                         && (_longitudeRef == 'E' || _longitudeRef == 'e' || _longitudeRef == 'W' || _longitudeRef == 'w')
208                         && _longitude != null;
209         }
210
211         /**
212          * Add the given error message to the list of errors
213          * @param inError String containing error message
214          */
215         public void addError(String inError)
216         {
217                 if (_errors == null) _errors = new ArrayList<String>();
218                 _errors.add(inError);
219         }
220
221         /**
222          * @return the number of errors, if any
223          */
224         public int getNumErrors()
225         {
226                 if (_errors == null) return 0;
227                 return _errors.size();
228         }
229
230         /**
231          * @return true if errors occurred
232          */
233         public boolean hasErrors()
234         {
235                 return getNumErrors() > 0;
236         }
237
238         /**
239          * @return all errors as a list
240          */
241         public List<String> getErrors()
242         {
243                 return _errors;
244         }
245 }