1 /* 2 * Copyright (C) 2014 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.util; 18 19 import android.hardware.camera2.CaptureResult; 20 import android.location.Location; 21 import android.os.Build; 22 23 import com.android.camera.debug.DebugPropertyHelper; 24 import com.android.camera.exif.ExifInterface; 25 import com.android.camera.exif.Rational; 26 import com.android.camera.one.v2.camera2proxy.CaptureResultProxy; 27 import com.android.camera.processing.imagebackend.TaskImageContainer; 28 import com.google.common.base.Optional; 29 30 import java.text.DecimalFormat; 31 import java.util.Calendar; 32 import java.util.Date; 33 import java.util.TimeZone; 34 35 /** 36 * Exif utility functions. 37 */ 38 public class ExifUtil { 39 private static final double LOG_2 = Math.log(2); // natural log of 2 40 41 private final ExifInterface mExif; 42 43 /** 44 * Construct a new ExifUtil object. 45 * @param exif The EXIF object to populate. 46 */ ExifUtil(ExifInterface exif)47 public ExifUtil(ExifInterface exif) { 48 mExif = exif; 49 } 50 51 /** 52 * Populate the EXIF object with info pulled from a given capture result. 53 * 54 * @param image A {@link TaskImageContainer.TaskImage} from which to extract info from. 55 * @param captureResult A {@link CaptureResultProxy} from which to extract info from. 56 * @param location optionally a location that should be added to the EXIF. 57 */ populateExif(Optional<TaskImageContainer.TaskImage> image, Optional<CaptureResultProxy> captureResult, Optional<Location> location)58 public void populateExif(Optional<TaskImageContainer.TaskImage> image, 59 Optional<CaptureResultProxy> captureResult, 60 Optional<Location> location) { 61 addExifVersionToExif(); 62 addTimestampToExif(); 63 addMakeAndModelToExif(); 64 if (image.isPresent()) { 65 addImageDataToExif(image.get()); 66 } 67 if (captureResult.isPresent()) { 68 addCaptureResultToExif(captureResult.get()); 69 } 70 if (location.isPresent()) { 71 addLocationToExif(location.get()); 72 } 73 } 74 75 /** 76 * Adds the given location to the EXIF object. 77 * 78 * @param location The location to add. 79 */ addLocationToExif(Location location)80 public void addLocationToExif(Location location) { 81 final Long ALTITUDE_PRECISION = 1L; // GPS altitude isn't particularly accurate (determined empirically) 82 83 mExif.addGpsTags(location.getLatitude(), location.getLongitude()); 84 mExif.addGpsDateTimeStampTag(location.getTime()); 85 86 if (location.hasAltitude()) { 87 double altitude = location.getAltitude(); 88 addExifTag(ExifInterface.TAG_GPS_ALTITUDE, rational(altitude, ALTITUDE_PRECISION)); 89 short altitudeRef = altitude < 0 ? ExifInterface.GpsAltitudeRef.SEA_LEVEL_NEGATIVE 90 : ExifInterface.GpsAltitudeRef.SEA_LEVEL; 91 addExifTag(ExifInterface.TAG_GPS_ALTITUDE_REF, altitudeRef); 92 } 93 } 94 95 private void addExifVersionToExif() { 96 addExifTag(ExifInterface.TAG_EXIF_VERSION, ExifInterface.EXIF_VERSION); 97 } 98 99 private void addTimestampToExif() { 100 final Long MS_TO_S = 1000L; // Milliseconds per second 101 final String subSecondFormat = "000"; 102 103 Long timestampMs = System.currentTimeMillis(); 104 TimeZone timezone = Calendar.getInstance().getTimeZone(); 105 mExif.addDateTimeStampTag(ExifInterface.TAG_DATE_TIME, timestampMs, timezone); 106 mExif.addDateTimeStampTag(ExifInterface.TAG_DATE_TIME_DIGITIZED, timestampMs, timezone); 107 mExif.addDateTimeStampTag(ExifInterface.TAG_DATE_TIME_ORIGINAL, timestampMs, timezone); 108 109 Long subSeconds = timestampMs % MS_TO_S; 110 String subSecondsString = new DecimalFormat(subSecondFormat).format(subSeconds); 111 addExifTag(ExifInterface.TAG_SUB_SEC_TIME, subSecondsString); 112 addExifTag(ExifInterface.TAG_SUB_SEC_TIME_ORIGINAL, subSecondsString); 113 addExifTag(ExifInterface.TAG_SUB_SEC_TIME_DIGITIZED, subSecondsString); 114 } 115 116 private void addMakeAndModelToExif() { 117 if (DebugPropertyHelper.isRedactExifEnabled()) { 118 addExifTag(ExifInterface.TAG_MAKE, "CAM_YY"); 119 addExifTag(ExifInterface.TAG_MODEL, "CAM_XX"); 120 } else { 121 addExifTag(ExifInterface.TAG_MAKE, Build.MANUFACTURER); 122 addExifTag(ExifInterface.TAG_MODEL, Build.MODEL); 123 } 124 } 125 126 private void addImageDataToExif(TaskImageContainer.TaskImage image) { 127 mExif.setTag(mExif.buildTag(ExifInterface.TAG_PIXEL_X_DIMENSION, image.width)); 128 mExif.setTag(mExif.buildTag(ExifInterface.TAG_PIXEL_Y_DIMENSION, image.height)); 129 mExif.setTag(mExif.buildTag(ExifInterface.TAG_IMAGE_WIDTH, image.width)); 130 mExif.setTag(mExif.buildTag(ExifInterface.TAG_IMAGE_LENGTH, image.height)); 131 mExif.setTag(mExif.buildTag(ExifInterface.TAG_ORIENTATION, 132 ExifInterface.getOrientationValueForRotation(image.orientation.getDegrees()))); 133 } 134 135 private void addCaptureResultToExif(CaptureResultProxy result) { 136 final Long NS_TO_S = 1000000000L; // Nanoseconds per second 137 final Long SHUTTER_SPEED_VALUE_PRECISION = 1000L; 138 final Long F_NUMBER_PRECISION = 100L; 139 final Long APERTURE_VALUE_PRECISION = 100L; 140 final Long FOCAL_LENGTH_PRECISION = 1000L; // micrometer precision 141 142 // Exposure time 143 Long exposureTimeNs = result.get(CaptureResult.SENSOR_EXPOSURE_TIME); 144 addExifTag(ExifInterface.TAG_EXPOSURE_TIME, ratio(exposureTimeNs, NS_TO_S)); 145 146 // Shutter speed value (APEX unit, see Jeita EXIF 2.2 spec Annex C). 147 if (exposureTimeNs != null) { 148 Double exposureTime = (double) exposureTimeNs / NS_TO_S; 149 Double shutterSpeedValue = -log2(exposureTime); 150 addExifTag(ExifInterface.TAG_SHUTTER_SPEED_VALUE, rational(shutterSpeedValue, SHUTTER_SPEED_VALUE_PRECISION)); 151 } 152 153 // ISO 154 addExifTag(ExifInterface.TAG_ISO_SPEED_RATINGS, result.get(CaptureResult.SENSOR_SENSITIVITY)); 155 156 // F-stop number 157 Float fNumber = result.get(CaptureResult.LENS_APERTURE); 158 addExifTag(ExifInterface.TAG_F_NUMBER, rational(fNumber, F_NUMBER_PRECISION)); 159 160 // Aperture value (APEX unit, see Jeita EXIF 2.2 spec Annex C). 161 if (fNumber != null) { 162 Double apertureValue = 2 * log2(fNumber); 163 addExifTag(ExifInterface.TAG_APERTURE_VALUE, rational(apertureValue, APERTURE_VALUE_PRECISION)); 164 } 165 166 // Focal length 167 Float focalLength = result.get(CaptureResult.LENS_FOCAL_LENGTH); 168 addExifTag(ExifInterface.TAG_FOCAL_LENGTH, rational(focalLength, FOCAL_LENGTH_PRECISION)); 169 170 // Flash mode 171 Integer flashMode = result.get(CaptureResult.FLASH_MODE); 172 if (flashMode == CaptureResult.FLASH_MODE_OFF) { 173 addExifTag(ExifInterface.TAG_FLASH, ExifInterface.Flash.DID_NOT_FIRE); 174 } else { 175 addExifTag(ExifInterface.TAG_FLASH, ExifInterface.Flash.FIRED); 176 } 177 178 // White balance 179 Integer whiteBalanceMode = result.get(CaptureResult.CONTROL_AWB_MODE); 180 if (whiteBalanceMode == CaptureResult.CONTROL_AWB_MODE_OFF) { 181 addExifTag(ExifInterface.TAG_WHITE_BALANCE, ExifInterface.WhiteBalance.MANUAL); 182 } else { 183 addExifTag(ExifInterface.TAG_WHITE_BALANCE, ExifInterface.WhiteBalance.AUTO); 184 } 185 186 } 187 188 private void addExifTag(int tagId, Object val) { 189 if (val != null) { 190 mExif.setTag(mExif.buildTag(tagId, val)); 191 } 192 } 193 194 private Rational ratio(Long numerator, Long denominator) { 195 if (numerator != null && denominator != null) { 196 return new Rational(numerator, denominator); 197 } 198 return null; 199 } 200 private Rational rational(Float value, Long precision) { 201 if (value != null && precision != null) { 202 return new Rational((long) (value * precision), precision); 203 } 204 return null; 205 } 206 207 private Rational rational(Double value, Long precision) { 208 if (value != null && precision != null) { 209 return new Rational((long) (value * precision), precision); 210 } 211 return null; 212 } 213 214 private Double log2(Float value) { 215 if (value != null) { 216 return Math.log(value) / LOG_2; 217 } 218 return null; 219 } 220 221 private Double log2(Double value) { 222 if (value != null) { 223 return Math.log(value) / LOG_2; 224 } 225 return null; 226 } 227 } 228