| The Android Open Source Project | 1d4c750 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.camera; |
| 18 | |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Iterator; |
| 21 | |
| 22 | import android.util.Config; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | // Wrapper for native Exif library |
| 26 | |
| 27 | public class ExifInterface { |
| 28 | |
| 29 | private String mFilename; |
| 30 | |
| 31 | // Constants used for the Orientation Exif tag. |
| 32 | static final int ORIENTATION_UNDEFINED = 0; |
| 33 | static final int ORIENTATION_NORMAL = 1; |
| 34 | static final int ORIENTATION_FLIP_HORIZONTAL = 2; // left right reversed mirror |
| 35 | static final int ORIENTATION_ROTATE_180 = 3; |
| 36 | static final int ORIENTATION_FLIP_VERTICAL = 4; // upside down mirror |
| 37 | static final int ORIENTATION_TRANSPOSE = 5; // flipped about top-left <--> bottom-right axis |
| 38 | static final int ORIENTATION_ROTATE_90 = 6; // rotate 90 cw to right it |
| 39 | static final int ORIENTATION_TRANSVERSE = 7; // flipped about top-right <--> bottom-left axis |
| 40 | static final int ORIENTATION_ROTATE_270 = 8; // rotate 270 to right it |
| 41 | |
| 42 | // The Exif tag names |
| 43 | static final String TAG_ORIENTATION = "Orientation"; |
| 44 | static final String TAG_DATE_TIME_ORIGINAL = "DateTimeOriginal"; |
| 45 | static final String TAG_MAKE = "Make"; |
| 46 | static final String TAG_MODEL = "Model"; |
| 47 | static final String TAG_FLASH = "Flash"; |
| 48 | static final String TAG_IMAGE_WIDTH = "ImageWidth"; |
| 49 | static final String TAG_IMAGE_LENGTH = "ImageLength"; |
| 50 | |
| 51 | static final String TAG_GPS_LATITUDE = "GPSLatitude"; |
| 52 | static final String TAG_GPS_LONGITUDE = "GPSLongitude"; |
| 53 | |
| 54 | static final String TAG_GPS_LATITUDE_REF = "GPSLatitudeRef"; |
| 55 | static final String TAG_GPS_LONGITUDE_REF = "GPSLongitudeRef"; |
| 56 | |
| 57 | private boolean mSavedAttributes = false; |
| 58 | private boolean mHasThumbnail = false; |
| 59 | private HashMap<String, String> mCachedAttributes = null; |
| 60 | |
| 61 | static { |
| The Android Open Source Project | 0eaec58 | 2008-12-17 18:05:54 -0800 | [diff] [blame^] | 62 | System.loadLibrary("exif"); |
| The Android Open Source Project | 1d4c750 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | public ExifInterface(String fileName) { |
| 66 | mFilename = fileName; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Given a HashMap of Exif tags and associated values, an Exif section in the JPG file |
| 71 | * is created and loaded with the tag data. saveAttributes() is expensive because it involves |
| 72 | * copying all the JPG data from one file to another and deleting the old file and renaming the other. |
| 73 | * It's best to collect all the attributes to write and make a single call rather than multiple |
| 74 | * calls for each attribute. You must call "commitChanges()" at some point to commit the changes. |
| 75 | */ |
| 76 | public void saveAttributes(HashMap<String, String> attributes) { |
| 77 | // format of string passed to native C code: |
| 78 | // "attrCnt attr1=valueLen value1attr2=value2Len value2..." |
| 79 | // example: "4 attrPtr ImageLength=4 1024Model=6 FooImageWidth=4 1280Make=3 FOO" |
| 80 | StringBuilder sb = new StringBuilder(); |
| 81 | int size = attributes.size(); |
| 82 | if (attributes.containsKey("hasThumbnail")) { |
| 83 | --size; |
| 84 | } |
| 85 | sb.append(size + " "); |
| 86 | Iterator keyIterator = attributes.keySet().iterator(); |
| 87 | while (keyIterator.hasNext()) { |
| 88 | String key = (String)keyIterator.next(); |
| 89 | if (key.equals("hasThumbnail")) { |
| 90 | continue; // this is a fake attribute not saved as an exif tag |
| 91 | } |
| 92 | String val = (String)attributes.get(key); |
| 93 | sb.append(key + "="); |
| 94 | sb.append(val.length() + " "); |
| 95 | sb.append(val); |
| 96 | } |
| 97 | String s = sb.toString(); |
| 98 | if (android.util.Config.LOGV) |
| 99 | android.util.Log.v("camera", "saving exif data: " + s); |
| 100 | saveAttributesNative(mFilename, s); |
| 101 | mSavedAttributes = true; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns a HashMap loaded with the Exif attributes of the file. The key is the standard |
| 106 | * tag name and the value is the tag's value: e.g. Model -> Nikon. Numeric values are |
| 107 | * returned as strings. |
| 108 | */ |
| 109 | public HashMap<String, String> getAttributes() { |
| 110 | if (mCachedAttributes != null) { |
| 111 | return mCachedAttributes; |
| 112 | } |
| 113 | // format of string passed from native C code: |
| 114 | // "attrCnt attr1=valueLen value1attr2=value2Len value2..." |
| 115 | // example: "4 attrPtr ImageLength=4 1024Model=6 FooImageWidth=4 1280Make=3 FOO" |
| 116 | mCachedAttributes = new HashMap<String, String>(); |
| 117 | |
| 118 | String attrStr = getAttributesNative(mFilename); |
| 119 | |
| 120 | // get count |
| 121 | int ptr = attrStr.indexOf(' '); |
| 122 | int count = Integer.parseInt(attrStr.substring(0, ptr)); |
| 123 | ++ptr; // skip past the space between item count and the rest of the attributes |
| 124 | |
| 125 | for (int i = 0; i < count; i++) { |
| 126 | // extract the attribute name |
| 127 | int equalPos = attrStr.indexOf('=', ptr); |
| 128 | String attrName = attrStr.substring(ptr, equalPos); |
| 129 | ptr = equalPos + 1; // skip past = |
| 130 | |
| 131 | // extract the attribute value length |
| 132 | int lenPos = attrStr.indexOf(' ', ptr); |
| 133 | int attrLen = Integer.parseInt(attrStr.substring(ptr, lenPos)); |
| 134 | ptr = lenPos + 1; // skip pas the space |
| 135 | |
| 136 | // extract the attribute value |
| 137 | String attrValue = attrStr.substring(ptr, ptr + attrLen); |
| 138 | ptr += attrLen; |
| 139 | |
| 140 | if (attrName.equals("hasThumbnail")) { |
| 141 | mHasThumbnail = attrValue.equalsIgnoreCase("true"); |
| 142 | } else { |
| 143 | mCachedAttributes.put(attrName, attrValue); |
| 144 | } |
| 145 | } |
| 146 | return mCachedAttributes; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Given a numerical orientation, return a human-readable string describing the orientation. |
| 151 | */ |
| 152 | static public String orientationToString(int orientation) { |
| 153 | // TODO: this function needs to be localized and use string resource ids rather than strings |
| 154 | String orientationString; |
| 155 | switch (orientation) { |
| 156 | case ORIENTATION_NORMAL: orientationString = "Normal"; break; |
| 157 | case ORIENTATION_FLIP_HORIZONTAL: orientationString = "Flipped horizontal"; break; |
| 158 | case ORIENTATION_ROTATE_180: orientationString = "Rotated 180 degrees"; break; |
| 159 | case ORIENTATION_FLIP_VERTICAL: orientationString = "Upside down mirror"; break; |
| 160 | case ORIENTATION_TRANSPOSE: orientationString = "Transposed"; break; |
| 161 | case ORIENTATION_ROTATE_90: orientationString = "Rotated 90 degrees"; break; |
| 162 | case ORIENTATION_TRANSVERSE: orientationString = "Transversed"; break; |
| 163 | case ORIENTATION_ROTATE_270: orientationString = "Rotated 270 degrees"; break; |
| 164 | default: orientationString = "Undefined"; break; |
| 165 | } |
| 166 | return orientationString; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Copies the thumbnail data out of the filename and puts it in the Exif data associated |
| 171 | * with the file used to create this object. You must call "commitChanges()" at some point |
| 172 | * to commit the changes. |
| 173 | */ |
| 174 | public boolean appendThumbnail(String thumbnailFileName) { |
| 175 | if (!mSavedAttributes) { |
| 176 | throw new RuntimeException("Must call saveAttributes before calling appendThumbnail"); |
| 177 | } |
| 178 | mHasThumbnail = appendThumbnailNative(mFilename, thumbnailFileName); |
| 179 | return mHasThumbnail; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Saves the changes (added Exif tags, added thumbnail) to the JPG file. You have to call |
| 184 | * saveAttributes() before committing the changes. |
| 185 | */ |
| 186 | public void commitChanges() { |
| 187 | if (!mSavedAttributes) { |
| 188 | throw new RuntimeException("Must call saveAttributes before calling commitChanges"); |
| 189 | } |
| 190 | commitChangesNative(mFilename); |
| 191 | } |
| 192 | |
| 193 | public boolean hasThumbnail() { |
| 194 | if (!mSavedAttributes) { |
| 195 | getAttributes(); |
| 196 | } |
| 197 | return mHasThumbnail; |
| 198 | } |
| 199 | |
| 200 | public byte[] getThumbnail() { |
| 201 | return getThumbnailNative(mFilename); |
| 202 | } |
| 203 | |
| 204 | static public String convertRationalLatLonToDecimalString(String rationalString, String ref, boolean usePositiveNegative) { |
| 205 | try { |
| 206 | String [] parts = rationalString.split(","); |
| 207 | |
| 208 | String [] pair; |
| 209 | pair = parts[0].split("/"); |
| 210 | int degrees = (int) (Float.parseFloat(pair[0].trim()) / Float.parseFloat(pair[1].trim())); |
| 211 | |
| 212 | pair = parts[1].split("/"); |
| 213 | int minutes = (int) ((Float.parseFloat(pair[0].trim()) / Float.parseFloat(pair[1].trim()))); |
| 214 | |
| 215 | pair = parts[2].split("/"); |
| 216 | float seconds = Float.parseFloat(pair[0].trim()) / Float.parseFloat(pair[1].trim()); |
| 217 | |
| 218 | float result = degrees + (minutes/60F) + (seconds/(60F*60F)); |
| 219 | |
| 220 | String preliminaryResult = String.valueOf(result); |
| 221 | if (usePositiveNegative) { |
| 222 | String neg = (ref.equals("S") || ref.equals("E")) ? "-" : ""; |
| 223 | return neg + preliminaryResult; |
| 224 | } else { |
| 225 | return preliminaryResult + String.valueOf((char)186) + " " + ref; |
| 226 | } |
| 227 | } catch (Exception ex) { |
| 228 | // if for whatever reason we can't parse the lat long then return null |
| 229 | return null; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static public String makeLatLongString(double d) { |
| 234 | d = Math.abs(d); |
| 235 | |
| 236 | int degrees = (int) d; |
| 237 | |
| 238 | double remainder = d - (double)degrees; |
| 239 | int minutes = (int) (remainder * 60D); |
| 240 | int seconds = (int) (((remainder * 60D) - minutes) * 60D * 1000D); // really seconds * 1000 |
| 241 | |
| 242 | String retVal = degrees + "/1," + minutes + "/1," + (int)seconds + "/1000"; |
| 243 | return retVal; |
| 244 | } |
| 245 | |
| 246 | static public String makeLatStringRef(double lat) { |
| 247 | return lat >= 0D ? "N" : "S"; |
| 248 | } |
| 249 | |
| 250 | static public String makeLonStringRef(double lon) { |
| 251 | return lon >= 0D ? "W" : "E"; |
| 252 | } |
| 253 | |
| 254 | private native boolean appendThumbnailNative(String fileName, String thumbnailFileName); |
| 255 | |
| 256 | private native void saveAttributesNative(String fileName, String compressedAttributes); |
| 257 | |
| 258 | private native String getAttributesNative(String fileName); |
| 259 | |
| 260 | private native void commitChangesNative(String fileName); |
| 261 | |
| 262 | private native byte[] getThumbnailNative(String fileName); |
| 263 | } |