summaryrefslogtreecommitdiffstats
path: root/camera2/portability/src/com/android/ex/camera2/portability/CameraSettings.java
blob: ccd980a6407c44e698a395120a4a0d27ab809428 (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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * 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.ex.camera2.portability;

import android.hardware.Camera;

import com.android.ex.camera2.portability.debug.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * A class which stores the camera settings.
 */
public abstract class CameraSettings {
    private static final Log.Tag TAG = new Log.Tag("CamSet");

    // Attempts to provide a value outside this range will be ignored.
    private static final int MIN_JPEG_COMPRESSION_QUALITY = 1;
    private static final int MAX_JPEG_COMPRESSION_QUALITY = 100;

    protected final Map<String, String> mGeneralSetting = new TreeMap<>();
    protected final List<Camera.Area> mMeteringAreas = new ArrayList<>();
    protected final List<Camera.Area> mFocusAreas = new ArrayList<>();
    protected boolean mSizesLocked;
    protected int mPreviewFpsRangeMin;
    protected int mPreviewFpsRangeMax;
    protected int mPreviewFrameRate;
    protected Size mCurrentPreviewSize;
    private int mCurrentPreviewFormat;
    protected Size mCurrentPhotoSize;
    protected byte mJpegCompressQuality;
    protected int mCurrentPhotoFormat;
    protected float mCurrentZoomRatio;
    protected int mExposureCompensationIndex;
    protected CameraCapabilities.FlashMode mCurrentFlashMode;
    protected CameraCapabilities.FocusMode mCurrentFocusMode;
    protected CameraCapabilities.SceneMode mCurrentSceneMode;
    protected CameraCapabilities.WhiteBalance mWhiteBalance;
    protected boolean mVideoStabilizationEnabled;
    protected boolean mAutoExposureLocked;
    protected boolean mAutoWhiteBalanceLocked;
    protected boolean mRecordingHintEnabled;
    protected GpsData mGpsData;
    protected Size mExifThumbnailSize;

    /**
     * An immutable class storing GPS related information.
     * <p>It's a hack since we always use GPS time stamp but does not use other
     * fields sometimes. Setting processing method to null means the other
     * fields should not be used.</p>
     */
    public static class GpsData {
        public final double latitude;
        public final double longitude;
        public final double altitude;
        public final long timeStamp;
        public final String processingMethod;

        /**
         * Construct what may or may not actually represent a location,
         * depending on the value of {@code processingMethod}.
         *
         * <p>Setting {@code processingMethod} to {@code null} means that
         * {@code latitude}, {@code longitude}, and {@code altitude} will be
         * completely ignored.</p>
         */
        public GpsData(double latitude, double longitude, double altitude, long timeStamp,
                String processingMethod) {
            if (processingMethod == null &&
                    (latitude != 0.0 || longitude != 0.0 || altitude != 0.0)) {
                Log.w(TAG, "GpsData's nonzero data will be ignored due to null processingMethod");
            }
            this.latitude = latitude;
            this.longitude = longitude;
            this.altitude = altitude;
            this.timeStamp = timeStamp;
            this.processingMethod = processingMethod;
        }

        /** Copy constructor. */
        public GpsData(GpsData src) {
            this.latitude = src.latitude;
            this.longitude = src.longitude;
            this.altitude = src.altitude;
            this.timeStamp = src.timeStamp;
            this.processingMethod = src.processingMethod;
        }
    }

    protected CameraSettings() {
    }

    /**
     * Copy constructor.
     *
     * @param src The source settings.
     * @return The copy of the source.
     */
    protected CameraSettings(CameraSettings src) {
        mGeneralSetting.putAll(src.mGeneralSetting);
        mMeteringAreas.addAll(src.mMeteringAreas);
        mFocusAreas.addAll(src.mFocusAreas);
        mSizesLocked = src.mSizesLocked;
        mPreviewFpsRangeMin = src.mPreviewFpsRangeMin;
        mPreviewFpsRangeMax = src.mPreviewFpsRangeMax;
        mPreviewFrameRate = src.mPreviewFrameRate;
        mCurrentPreviewSize =
                (src.mCurrentPreviewSize == null ? null : new Size(src.mCurrentPreviewSize));
        mCurrentPreviewFormat = src.mCurrentPreviewFormat;
        mCurrentPhotoSize =
                (src.mCurrentPhotoSize == null ? null : new Size(src.mCurrentPhotoSize));
        mJpegCompressQuality = src.mJpegCompressQuality;
        mCurrentPhotoFormat = src.mCurrentPhotoFormat;
        mCurrentZoomRatio = src.mCurrentZoomRatio;
        mExposureCompensationIndex = src.mExposureCompensationIndex;
        mCurrentFlashMode = src.mCurrentFlashMode;
        mCurrentFocusMode = src.mCurrentFocusMode;
        mCurrentSceneMode = src.mCurrentSceneMode;
        mWhiteBalance = src.mWhiteBalance;
        mVideoStabilizationEnabled = src.mVideoStabilizationEnabled;
        mAutoExposureLocked = src.mAutoExposureLocked;
        mAutoWhiteBalanceLocked = src.mAutoWhiteBalanceLocked;
        mRecordingHintEnabled = src.mRecordingHintEnabled;
        mGpsData = src.mGpsData;
        mExifThumbnailSize = src.mExifThumbnailSize;
    }

    /**
     * @return A copy of this object, as an instance of the implementing class.
     */
    public abstract CameraSettings copy();

    /** General setting **/
    @Deprecated
    public void setSetting(String key, String value) {
        mGeneralSetting.put(key, value);
    }

    /**
     * Changes whether classes outside this class are allowed to set the preview
     * and photo capture sizes.
     *
     * @param locked Whether to prevent changes to these fields.
     *
     * @see #setPhotoSize
     * @see #setPreviewSize
     */
    /*package*/ void setSizesLocked(boolean locked) {
        mSizesLocked = locked;
    }

    /**  Preview **/

    /**
     * Sets the preview FPS range. This call will invalidate prior calls to
     * {@link #setPreviewFrameRate(int)}.
     *
     * @param min The min FPS.
     * @param max The max FPS.
     */
    public void setPreviewFpsRange(int min, int max) {
        if (min > max) {
            int temp = max;
            max = min;
            min = temp;
        }
        mPreviewFpsRangeMax = max;
        mPreviewFpsRangeMin = min;
        mPreviewFrameRate = -1;
    }

    /**
     * @return The min of the preview FPS range.
     */
    public int getPreviewFpsRangeMin() {
        return mPreviewFpsRangeMin;
    }

    /**
     * @return The max of the preview FPS range.
     */
    public int getPreviewFpsRangeMax() {
        return mPreviewFpsRangeMax;
    }

    /**
     * Sets the preview FPS. This call will invalidate prior calls to
     * {@link #setPreviewFpsRange(int, int)}.
     *
     * @param frameRate The target frame rate.
     */
    public void setPreviewFrameRate(int frameRate) {
        if (frameRate > 0) {
            mPreviewFrameRate = frameRate;
            mPreviewFpsRangeMax = frameRate;
            mPreviewFpsRangeMin = frameRate;
        }
    }

    public int getPreviewFrameRate() {
        return mPreviewFrameRate;
    }

    /**
     * @return The current preview size.
     */
    public Size getCurrentPreviewSize() {
        return new Size(mCurrentPreviewSize);
    }

    /**
     * @param previewSize The size to use for preview.
     * @return Whether the operation was allowed (i.e. the sizes are unlocked).
     */
    public boolean setPreviewSize(Size previewSize) {
        if (mSizesLocked) {
            Log.w(TAG, "Attempt to change preview size while locked");
            return false;
        }

        mCurrentPreviewSize = new Size(previewSize);
        return true;
    }

    /**
     * Sets the preview format.
     *
     * @param format
     * @see {@link android.graphics.ImageFormat}.
     */
    public void setPreviewFormat(int format) {
        mCurrentPreviewFormat = format;
    }

    /**
     * @return The preview format.
     * @see {@link android.graphics.ImageFormat}.
     */
    public int getCurrentPreviewFormat() {
        return mCurrentPreviewFormat;
    }

    /** Picture **/

    /**
     * @return The current photo size.
     */
    public Size getCurrentPhotoSize() {
        return new Size(mCurrentPhotoSize);
    }

    /**
     * @param photoSize The size to use for preview.
     * @return Whether the operation was allowed (i.e. the sizes are unlocked).
     */
    public boolean setPhotoSize(Size photoSize) {
        if (mSizesLocked) {
            Log.w(TAG, "Attempt to change photo size while locked");
            return false;
        }

        mCurrentPhotoSize = new Size(photoSize);
        return true;
    }

    /**
     * Sets the format for the photo.
     *
     * @param format The format for the photos taken.
     * @see {@link android.graphics.ImageFormat}.
     */
    public void setPhotoFormat(int format) {
        mCurrentPhotoFormat = format;
    }

    /**
     * @return The format for the photos taken.
     * @see {@link android.graphics.ImageFormat}.
     */
    public int getCurrentPhotoFormat() {
        return mCurrentPhotoFormat;
    }

    /**
     * Sets the JPEG compression quality.
     *
     * @param quality The quality for JPEG.
     */
    public void setPhotoJpegCompressionQuality(int quality) {
        if (quality < MIN_JPEG_COMPRESSION_QUALITY || quality > MAX_JPEG_COMPRESSION_QUALITY) {
            Log.w(TAG, "Ignoring JPEG quality that falls outside the expected range");
            return;
        }
        // This is safe because the positive numbers go up to 127.
        mJpegCompressQuality = (byte) quality;
    }

    public int getPhotoJpegCompressionQuality() {
        return mJpegCompressQuality;
    }

    /** Zoom **/

    /**
     * @return The current zoom ratio. The min is 1.0f.
     */
    public float getCurrentZoomRatio() {
        return mCurrentZoomRatio;
    }

    /**
     * Sets the zoom ratio.
     * @param ratio The new zoom ratio. Should be in the range between 1.0 to
     *              the value returned from {@link
     *              com.android.camera.cameradevice.CameraCapabilities#getMaxZoomRatio()}.
     * @throws java.lang.UnsupportedOperationException if the ratio is not
     *         supported.
     */
    public void setZoomRatio(float ratio) {
        mCurrentZoomRatio = ratio;
    }

    /** Exposure **/

    public void setExposureCompensationIndex(int index) {
        mExposureCompensationIndex = index;
    }

    /**
     * @return The exposure compensation, with 0 meaning unadjusted.
     */
    public int getExposureCompensationIndex() {
        return mExposureCompensationIndex;
    }

    public void setAutoExposureLock(boolean locked) {
        mAutoExposureLocked = locked;
    }

    public boolean isAutoExposureLocked() {
        return mAutoExposureLocked;
    }

    /**
     * @param areas The areas for autoexposure. The coordinate system has domain
     *              and range [-1000,1000], measured relative to the visible
     *              preview image, with orientation matching that of the sensor.
     *              This means the coordinates must be transformed to account
     *              for the devices rotation---but not the zoom level---before
     *              being passed into this method.
     */
    public void setMeteringAreas(List<Camera.Area> areas) {
        mMeteringAreas.clear();
        if (areas != null) {
            mMeteringAreas.addAll(areas);
        }
    }

    public List<Camera.Area> getMeteringAreas() {
        return new ArrayList<Camera.Area>(mMeteringAreas);
    }

    /** Flash **/

    public CameraCapabilities.FlashMode getCurrentFlashMode() {
        return mCurrentFlashMode;
    }

    public void setFlashMode(CameraCapabilities.FlashMode flashMode) {
        mCurrentFlashMode = flashMode;
    }

    /** Focus **/

    /**
     * Sets the focus mode.
     * @param focusMode The focus mode to use.
     */
    public void setFocusMode(CameraCapabilities.FocusMode focusMode) {
        mCurrentFocusMode = focusMode;
    }

    /**
     * @return The current focus mode.
     */
    public CameraCapabilities.FocusMode getCurrentFocusMode() {
        return mCurrentFocusMode;
    }

    /**
     * @param areas The areas to focus. The coordinate system has domain and
     *              range [-1000,1000], measured relative to the visible preview
     *              image, with orientation matching that of the sensor. This
     *              means the coordinates must be transformed to account for
     *              the devices rotation---but not the zoom level---before being
     *              passed into this method.
     */
    public void setFocusAreas(List<Camera.Area> areas) {
        mFocusAreas.clear();
        if (areas != null) {
            mFocusAreas.addAll(areas);
        }
    }

    public List<Camera.Area> getFocusAreas() {
        return new ArrayList<Camera.Area>(mFocusAreas);
    }

    /** White balance **/

    public void setWhiteBalance(CameraCapabilities.WhiteBalance whiteBalance) {
        mWhiteBalance = whiteBalance;
    }

    public CameraCapabilities.WhiteBalance getWhiteBalance() {
        return mWhiteBalance;
    }

    public void setAutoWhiteBalanceLock(boolean locked) {
        mAutoWhiteBalanceLocked = locked;
    }

    public boolean isAutoWhiteBalanceLocked() {
        return mAutoWhiteBalanceLocked;
    }

    /** Scene mode **/

    /**
     * @return The current scene mode.
     */
    public CameraCapabilities.SceneMode getCurrentSceneMode() {
        return mCurrentSceneMode;
    }

    /**
     * Sets the scene mode for capturing.
     *
     * @param sceneMode The scene mode to use.
     * @throws java.lang.UnsupportedOperationException if it's not supported.
     */
    public void setSceneMode(CameraCapabilities.SceneMode sceneMode) {
        mCurrentSceneMode = sceneMode;
    }

    /** Other Features **/

    public void setVideoStabilization(boolean enabled) {
        mVideoStabilizationEnabled = enabled;
    }

    public boolean isVideoStabilizationEnabled() {
        return mVideoStabilizationEnabled;
    }

    public void setRecordingHintEnabled(boolean hintEnabled) {
        mRecordingHintEnabled = hintEnabled;
    }

    public boolean isRecordingHintEnabled() {
        return mRecordingHintEnabled;
    }

    public void setGpsData(GpsData data) {
        mGpsData = new GpsData(data);
    }

    public GpsData getGpsData() {
        return (mGpsData == null ? null : new GpsData(mGpsData));
    }

    public void clearGpsData() {
        mGpsData = null;
    }

    /**
     * Sets the size of the thumbnail in EXIF header. To suppress thumbnail
     * generation, set a size of (0,0).
     *
     * @param s The size for the thumbnail. If {@code null}, agent will not
     *          set a thumbnail size.
     */
    public void setExifThumbnailSize(Size s) {
        mExifThumbnailSize = s;
    }

    /**
     * Gets the size of the thumbnail in EXIF header.
     *
     * @return desired thumbnail size, or null if no size was set
     */
    public Size getExifThumbnailSize() {
        return (mExifThumbnailSize == null) ? null : new Size(mExifThumbnailSize);
    }
}