summaryrefslogtreecommitdiff
path: root/src/com/android/camera/util/ExifUtil.java
blob: 5fae249840bb1ab132769724786fadfe6db50562 (plain)
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
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.util;

import android.hardware.camera2.CaptureResult;
import android.location.Location;
import android.os.Build;

import com.android.camera.debug.DebugPropertyHelper;
import com.android.camera.exif.ExifInterface;
import com.android.camera.exif.Rational;
import com.android.camera.one.v2.camera2proxy.CaptureResultProxy;
import com.android.camera.processing.imagebackend.TaskImageContainer;
import com.google.common.base.Optional;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Exif utility functions.
 */
public class ExifUtil {
    private static final double LOG_2 = Math.log(2); // natural log of 2

    private final ExifInterface mExif;

    /**
     * Construct a new ExifUtil object.
     * @param exif The EXIF object to populate.
     */
    public ExifUtil(ExifInterface exif) {
        mExif = exif;
    }

    /**
     * Populate the EXIF object with info pulled from a given capture result.
     *
     * @param image A {@link TaskImageContainer.TaskImage} from which to extract info from.
     * @param captureResult A {@link CaptureResultProxy} from which to extract info from.
     * @param location optionally a location that should be added to the EXIF.
     */
    public void populateExif(Optional<TaskImageContainer.TaskImage> image,
                             Optional<CaptureResultProxy> captureResult,
                             Optional<Location> location) {
        addExifVersionToExif();
        addTimestampToExif();
        addMakeAndModelToExif();
        if (image.isPresent()) {
            addImageDataToExif(image.get());
        }
        if (captureResult.isPresent()) {
            addCaptureResultToExif(captureResult.get());
        }
        if (location.isPresent()) {
            addLocationToExif(location.get());
        }
    }

    /**
     * Adds the given location to the EXIF object.
     *
     * @param location The location to add.
     */
    public void addLocationToExif(Location location) {
        final Long ALTITUDE_PRECISION = 1L; // GPS altitude isn't particularly accurate (determined empirically)

        mExif.addGpsTags(location.getLatitude(), location.getLongitude());
        mExif.addGpsDateTimeStampTag(location.getTime());

        if (location.hasAltitude()) {
            double altitude = location.getAltitude();
            addExifTag(ExifInterface.TAG_GPS_ALTITUDE, rational(altitude, ALTITUDE_PRECISION));
            short altitudeRef = altitude < 0 ? ExifInterface.GpsAltitudeRef.SEA_LEVEL_NEGATIVE
                    : ExifInterface.GpsAltitudeRef.SEA_LEVEL;
            addExifTag(ExifInterface.TAG_GPS_ALTITUDE_REF, altitudeRef);
        }
    }

    private void addExifVersionToExif() {
        addExifTag(ExifInterface.TAG_EXIF_VERSION, ExifInterface.EXIF_VERSION);
    }

    private void addTimestampToExif() {
        final Long MS_TO_S = 1000L; // Milliseconds per second
        final String subSecondFormat = "000";

        Long timestampMs = System.currentTimeMillis();
        TimeZone timezone = Calendar.getInstance().getTimeZone();
        mExif.addDateTimeStampTag(ExifInterface.TAG_DATE_TIME, timestampMs, timezone);
        mExif.addDateTimeStampTag(ExifInterface.TAG_DATE_TIME_DIGITIZED, timestampMs, timezone);
        mExif.addDateTimeStampTag(ExifInterface.TAG_DATE_TIME_ORIGINAL, timestampMs, timezone);

        Long subSeconds = timestampMs % MS_TO_S;
        String subSecondsString = new DecimalFormat(subSecondFormat).format(subSeconds);
        addExifTag(ExifInterface.TAG_SUB_SEC_TIME, subSecondsString);
        addExifTag(ExifInterface.TAG_SUB_SEC_TIME_ORIGINAL, subSecondsString);
        addExifTag(ExifInterface.TAG_SUB_SEC_TIME_DIGITIZED, subSecondsString);
    }

    private void addMakeAndModelToExif() {
        if (DebugPropertyHelper.isRedactExifEnabled()) {
            addExifTag(ExifInterface.TAG_MAKE, "CAM_YY");
            addExifTag(ExifInterface.TAG_MODEL, "CAM_XX");
        } else {
            addExifTag(ExifInterface.TAG_MAKE, Build.MANUFACTURER);
            addExifTag(ExifInterface.TAG_MODEL, Build.MODEL);
        }
    }

    private void addImageDataToExif(TaskImageContainer.TaskImage image) {
        mExif.setTag(mExif.buildTag(ExifInterface.TAG_PIXEL_X_DIMENSION, image.width));
        mExif.setTag(mExif.buildTag(ExifInterface.TAG_PIXEL_Y_DIMENSION, image.height));
        mExif.setTag(mExif.buildTag(ExifInterface.TAG_IMAGE_WIDTH, image.width));
        mExif.setTag(mExif.buildTag(ExifInterface.TAG_IMAGE_LENGTH, image.height));
        mExif.setTag(mExif.buildTag(ExifInterface.TAG_ORIENTATION,
                ExifInterface.getOrientationValueForRotation(image.orientation.getDegrees())));
    }

    private void addCaptureResultToExif(CaptureResultProxy result) {
        final Long NS_TO_S = 1000000000L; // Nanoseconds per second
        final Long SHUTTER_SPEED_VALUE_PRECISION = 1000L;
        final Long F_NUMBER_PRECISION = 100L;
        final Long APERTURE_VALUE_PRECISION = 100L;
        final Long FOCAL_LENGTH_PRECISION = 1000L; // micrometer precision

        // Exposure time
        Long exposureTimeNs = result.get(CaptureResult.SENSOR_EXPOSURE_TIME);
        addExifTag(ExifInterface.TAG_EXPOSURE_TIME, ratio(exposureTimeNs, NS_TO_S));

        // Shutter speed value (APEX unit, see Jeita EXIF 2.2 spec Annex C).
        if (exposureTimeNs != null) {
            Double exposureTime = (double) exposureTimeNs / NS_TO_S;
            Double shutterSpeedValue = -log2(exposureTime);
            addExifTag(ExifInterface.TAG_SHUTTER_SPEED_VALUE, rational(shutterSpeedValue, SHUTTER_SPEED_VALUE_PRECISION));
        }

        // ISO
        addExifTag(ExifInterface.TAG_ISO_SPEED_RATINGS, result.get(CaptureResult.SENSOR_SENSITIVITY));

        // F-stop number
        Float fNumber = result.get(CaptureResult.LENS_APERTURE);
        addExifTag(ExifInterface.TAG_F_NUMBER, rational(fNumber, F_NUMBER_PRECISION));

        // Aperture value (APEX unit, see Jeita EXIF 2.2 spec Annex C).
        if (fNumber != null) {
            Double apertureValue = 2 * log2(fNumber);
            addExifTag(ExifInterface.TAG_APERTURE_VALUE, rational(apertureValue, APERTURE_VALUE_PRECISION));
        }

        // Focal length
        Float focalLength = result.get(CaptureResult.LENS_FOCAL_LENGTH);
        addExifTag(ExifInterface.TAG_FOCAL_LENGTH, rational(focalLength, FOCAL_LENGTH_PRECISION));

        // Flash mode
        Integer flashMode = result.get(CaptureResult.FLASH_MODE);
        if (flashMode == CaptureResult.FLASH_MODE_OFF) {
            addExifTag(ExifInterface.TAG_FLASH, ExifInterface.Flash.DID_NOT_FIRE);
        } else {
            addExifTag(ExifInterface.TAG_FLASH, ExifInterface.Flash.FIRED);
        }

        // White balance
        Integer whiteBalanceMode = result.get(CaptureResult.CONTROL_AWB_MODE);
        if (whiteBalanceMode == CaptureResult.CONTROL_AWB_MODE_OFF) {
            addExifTag(ExifInterface.TAG_WHITE_BALANCE, ExifInterface.WhiteBalance.MANUAL);
        } else {
            addExifTag(ExifInterface.TAG_WHITE_BALANCE, ExifInterface.WhiteBalance.AUTO);
        }

    }

    private void addExifTag(int tagId, Object val) {
        if (val != null) {
            mExif.setTag(mExif.buildTag(tagId, val));
        }
    }

    private Rational ratio(Long numerator, Long denominator) {
        if (numerator != null && denominator != null) {
            return new Rational(numerator, denominator);
        }
        return null;
    }
    private Rational rational(Float value, Long precision) {
        if (value != null && precision != null) {
            return new Rational((long) (value * precision), precision);
        }
        return null;
    }

    private Rational rational(Double value, Long precision) {
        if (value != null && precision != null) {
            return new Rational((long) (value * precision), precision);
        }
        return null;
    }

    private Double log2(Float value) {
        if (value != null) {
            return Math.log(value) / LOG_2;
        }
        return null;
    }

    private Double log2(Double value) {
        if (value != null) {
            return Math.log(value) / LOG_2;
        }
        return null;
    }
}