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