CameraSettings.java revision 9d8668449376fa47bc6528c7a61b04d6a0f691b3
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
17package com.android.ex.camera2.portability;
18
19import android.hardware.Camera;
20
21import com.android.ex.camera2.portability.debug.Log;
22
23import java.util.ArrayList;
24import java.util.List;
25import java.util.Map;
26import java.util.TreeMap;
27
28/**
29 * A class which stores the camera settings.
30 */
31public abstract class CameraSettings {
32    private static final Log.Tag TAG = new Log.Tag("CamSet");
33
34    // Attempts to provide a value outside this range will be ignored.
35    private static final int MIN_JPEG_COMPRESSION_QUALITY = 1;
36    private static final int MAX_JPEG_COMPRESSION_QUALITY = 100;
37
38    protected final Map<String, String> mGeneralSetting = new TreeMap<>();
39    protected final List<Camera.Area> mMeteringAreas = new ArrayList<>();
40    protected final List<Camera.Area> mFocusAreas = new ArrayList<>();
41    protected int mPreviewFpsRangeMin;
42    protected int mPreviewFpsRangeMax;
43    protected int mPreviewFrameRate;
44    protected Size mCurrentPreviewSize;
45    private int mCurrentPreviewFormat;
46    protected Size mCurrentPhotoSize;
47    protected byte mJpegCompressQuality;
48    protected int mCurrentPhotoFormat;
49    protected float mCurrentZoomRatio;
50    protected int mExposureCompensationIndex;
51    protected CameraCapabilities.FlashMode mCurrentFlashMode;
52    protected CameraCapabilities.FocusMode mCurrentFocusMode;
53    protected CameraCapabilities.SceneMode mCurrentSceneMode;
54    protected CameraCapabilities.WhiteBalance mWhiteBalance;
55    protected boolean mVideoStabilizationEnabled;
56    protected boolean mAutoExposureLocked;
57    protected boolean mAutoWhiteBalanceLocked;
58    protected boolean mRecordingHintEnabled;
59    protected GpsData mGpsData;
60    protected Size mExifThumbnailSize = new Size(0,0);
61
62    /**
63     * An immutable class storing GPS related information.
64     * <p>It's a hack since we always use GPS time stamp but does not use other
65     * fields sometimes. Setting processing method to null means the other
66     * fields should not be used.</p>
67     */
68    public static class GpsData {
69        public final double latitude;
70        public final double longitude;
71        public final double altitude;
72        public final long timeStamp;
73        public final String processingMethod;
74
75        /** Constructor. */
76        public GpsData(double latitude, double longitude, double altitude, long timeStamp,
77                String processingMethod) {
78            this.latitude = latitude;
79            this.longitude = longitude;
80            this.altitude = altitude;
81            this.timeStamp = timeStamp;
82            this.processingMethod = processingMethod;
83        }
84
85        /** Copy constructor. */
86        public GpsData(GpsData src) {
87            this.latitude = src.latitude;
88            this.longitude = src.longitude;
89            this.altitude = src.altitude;
90            this.timeStamp = src.timeStamp;
91            this.processingMethod = src.processingMethod;
92        }
93    }
94
95    protected CameraSettings() {
96    }
97
98    /**
99     * Copy constructor.
100     *
101     * @param src The source settings.
102     * @return The copy of the source.
103     */
104    protected CameraSettings(CameraSettings src) {
105        mGeneralSetting.putAll(src.mGeneralSetting);
106        mMeteringAreas.addAll(src.mMeteringAreas);
107        mFocusAreas.addAll(src.mFocusAreas);
108        mPreviewFpsRangeMin = src.mPreviewFpsRangeMin;
109        mPreviewFpsRangeMax = src.mPreviewFpsRangeMax;
110        mPreviewFrameRate = src.mPreviewFrameRate;
111        mCurrentPreviewSize =
112                (src.mCurrentPreviewSize == null ? null : new Size(src.mCurrentPreviewSize));
113        mCurrentPreviewFormat = src.mCurrentPreviewFormat;
114        mCurrentPhotoSize =
115                (src.mCurrentPhotoSize == null ? null : new Size(src.mCurrentPhotoSize));
116        mJpegCompressQuality = src.mJpegCompressQuality;
117        mCurrentPhotoFormat = src.mCurrentPhotoFormat;
118        mCurrentZoomRatio = src.mCurrentZoomRatio;
119        mExposureCompensationIndex = src.mExposureCompensationIndex;
120        mCurrentFlashMode = src.mCurrentFlashMode;
121        mCurrentFocusMode = src.mCurrentFocusMode;
122        mCurrentSceneMode = src.mCurrentSceneMode;
123        mWhiteBalance = src.mWhiteBalance;
124        mVideoStabilizationEnabled = src.mVideoStabilizationEnabled;
125        mAutoExposureLocked = src.mAutoExposureLocked;
126        mAutoWhiteBalanceLocked = src.mAutoWhiteBalanceLocked;
127        mRecordingHintEnabled = src.mRecordingHintEnabled;
128        mGpsData = src.mGpsData;
129        mExifThumbnailSize = src.mExifThumbnailSize;
130    }
131
132    /**
133     * @return A copy of this object, as an instance of the implementing class.
134     */
135    public abstract CameraSettings copy();
136
137    /** General setting **/
138    @Deprecated
139    public void setSetting(String key, String value) {
140        mGeneralSetting.put(key, value);
141    }
142
143    /**  Preview **/
144
145    /**
146     * Sets the preview FPS range. This call will invalidate prior calls to
147     * {@link #setPreviewFrameRate(int)}.
148     *
149     * @param min The min FPS.
150     * @param max The max FPS.
151     */
152    public void setPreviewFpsRange(int min, int max) {
153        if (min > max) {
154            int temp = max;
155            max = min;
156            min = temp;
157        }
158        mPreviewFpsRangeMax = max;
159        mPreviewFpsRangeMin = min;
160        mPreviewFrameRate = -1;
161    }
162
163    /**
164     * @return The min of the preview FPS range.
165     */
166    public int getPreviewFpsRangeMin() {
167        return mPreviewFpsRangeMin;
168    }
169
170    /**
171     * @return The max of the preview FPS range.
172     */
173    public int getPreviewFpsRangeMax() {
174        return mPreviewFpsRangeMax;
175    }
176
177    /**
178     * Sets the preview FPS. This call will invalidate prior calls to
179     * {@link #setPreviewFpsRange(int, int)}.
180     *
181     * @param frameRate The target frame rate.
182     */
183    public void setPreviewFrameRate(int frameRate) {
184        if (frameRate > 0) {
185            mPreviewFrameRate = frameRate;
186            mPreviewFpsRangeMax = frameRate;
187            mPreviewFpsRangeMin = frameRate;
188        }
189    }
190
191    public int getPreviewFrameRate() {
192        return mPreviewFrameRate;
193    }
194
195    /**
196     * @return The current preview size.
197     */
198    public Size getCurrentPreviewSize() {
199        return new Size(mCurrentPreviewSize);
200    }
201
202    /**
203     * @param previewSize The size to use for preview.
204     */
205    public void setPreviewSize(Size previewSize) {
206        mCurrentPreviewSize = new Size(previewSize);
207    }
208
209    /**
210     * Sets the preview format.
211     *
212     * @param format
213     * @see {@link android.graphics.ImageFormat}.
214     */
215    public void setPreviewFormat(int format) {
216        mCurrentPreviewFormat = format;
217    }
218
219    /**
220     * @return The preview format.
221     * @see {@link android.graphics.ImageFormat}.
222     */
223    public int getCurrentPreviewFormat() {
224        return mCurrentPreviewFormat;
225    }
226
227    /** Picture **/
228
229    /**
230     * @return The current photo size.
231     */
232    public Size getCurrentPhotoSize() {
233        return new Size(mCurrentPhotoSize);
234    }
235
236    /**
237     * Sets the size for the photo.
238     *
239     * @param photoSize The photo size.
240     */
241    public void setPhotoSize(Size photoSize) {
242        mCurrentPhotoSize = new Size(photoSize);
243    }
244
245    /**
246     * Sets the format for the photo.
247     *
248     * @param format The format for the photos taken.
249     * @see {@link android.graphics.ImageFormat}.
250     */
251    public void setPhotoFormat(int format) {
252        mCurrentPhotoFormat = format;
253    }
254
255    /**
256     * @return The format for the photos taken.
257     * @see {@link android.graphics.ImageFormat}.
258     */
259    public int getCurrentPhotoFormat() {
260        return mCurrentPhotoFormat;
261    }
262
263    /**
264     * Sets the JPEG compression quality.
265     *
266     * @param quality The quality for JPEG.
267     */
268    public void setPhotoJpegCompressionQuality(int quality) {
269        if (quality < MIN_JPEG_COMPRESSION_QUALITY || quality > MAX_JPEG_COMPRESSION_QUALITY) {
270            Log.w(TAG, "Ignoring JPEG quality that falls outside the expected range");
271            return;
272        }
273        // This is safe because the positive numbers go up to 127.
274        mJpegCompressQuality = (byte) quality;
275    }
276
277    public int getPhotoJpegCompressionQuality() {
278        return mJpegCompressQuality;
279    }
280
281    /** Zoom **/
282
283    /**
284     * @return The current zoom ratio. The min is 1.0f.
285     */
286    public float getCurrentZoomRatio() {
287        return mCurrentZoomRatio;
288    }
289
290    /**
291     * Sets the zoom ratio.
292     * @param ratio The new zoom ratio. Should be in the range between 1.0 to
293     *              the value returned from {@link
294     *              com.android.camera.cameradevice.CameraCapabilities#getMaxZoomRatio()}.
295     * @throws java.lang.UnsupportedOperationException if the ratio is not
296     *         supported.
297     */
298    public void setZoomRatio(float ratio) {
299        mCurrentZoomRatio = ratio;
300    }
301
302    /** Exposure **/
303
304    public void setExposureCompensationIndex(int index) {
305        mExposureCompensationIndex = index;
306    }
307
308    /**
309     * @return The exposure compensation, with 0 meaning unadjusted.
310     */
311    public int getExposureCompensationIndex() {
312        return mExposureCompensationIndex;
313    }
314
315    public void setAutoExposureLock(boolean locked) {
316        mAutoExposureLocked = locked;
317    }
318
319    public boolean isAutoExposureLocked() {
320        return mAutoExposureLocked;
321    }
322
323    /**
324     * @param areas The areas for autoexposure. The coordinate system has domain
325     *              and range [-1000,1000], measured relative to the visible
326     *              preview image, with orientation matching that of the sensor.
327     *              This means the coordinates must be transformed to account
328     *              for the devices rotation---but not the zoom level---before
329     *              being passed into this method.
330     */
331    public void setMeteringAreas(List<Camera.Area> areas) {
332        mMeteringAreas.clear();
333        if (areas != null) {
334            mMeteringAreas.addAll(areas);
335        }
336    }
337
338    public List<Camera.Area> getMeteringAreas() {
339        return new ArrayList<Camera.Area>(mMeteringAreas);
340    }
341
342    /** Flash **/
343
344    public CameraCapabilities.FlashMode getCurrentFlashMode() {
345        return mCurrentFlashMode;
346    }
347
348    public void setFlashMode(CameraCapabilities.FlashMode flashMode) {
349        mCurrentFlashMode = flashMode;
350    }
351
352    /** Focus **/
353
354    /**
355     * Sets the focus mode.
356     * @param focusMode The focus mode to use.
357     */
358    public void setFocusMode(CameraCapabilities.FocusMode focusMode) {
359        mCurrentFocusMode = focusMode;
360    }
361
362    /**
363     * @return The current focus mode.
364     */
365    public CameraCapabilities.FocusMode getCurrentFocusMode() {
366        return mCurrentFocusMode;
367    }
368
369    /**
370     * @param areas The areas to focus. The coordinate system has domain and
371     *              range [-1000,1000], measured relative to the visible preview
372     *              image, with orientation matching that of the sensor. This
373     *              means the coordinates must be transformed to account for
374     *              the devices rotation---but not the zoom level---before being
375     *              passed into this method.
376     */
377    public void setFocusAreas(List<Camera.Area> areas) {
378        mFocusAreas.clear();
379        if (areas != null) {
380            mFocusAreas.addAll(areas);
381        }
382    }
383
384    public List<Camera.Area> getFocusAreas() {
385        return new ArrayList<Camera.Area>(mFocusAreas);
386    }
387
388    /** White balance **/
389
390    public void setWhiteBalance(CameraCapabilities.WhiteBalance whiteBalance) {
391        mWhiteBalance = whiteBalance;
392    }
393
394    public CameraCapabilities.WhiteBalance getWhiteBalance() {
395        return mWhiteBalance;
396    }
397
398    public void setAutoWhiteBalanceLock(boolean locked) {
399        mAutoWhiteBalanceLocked = locked;
400    }
401
402    public boolean isAutoWhiteBalanceLocked() {
403        return mAutoWhiteBalanceLocked;
404    }
405
406    /** Scene mode **/
407
408    /**
409     * @return The current scene mode.
410     */
411    public CameraCapabilities.SceneMode getCurrentSceneMode() {
412        return mCurrentSceneMode;
413    }
414
415    /**
416     * Sets the scene mode for capturing.
417     *
418     * @param sceneMode The scene mode to use.
419     * @throws java.lang.UnsupportedOperationException if it's not supported.
420     */
421    public void setSceneMode(CameraCapabilities.SceneMode sceneMode) {
422        mCurrentSceneMode = sceneMode;
423    }
424
425    /** Other Features **/
426
427    public void setVideoStabilization(boolean enabled) {
428        mVideoStabilizationEnabled = enabled;
429    }
430
431    public boolean isVideoStabilizationEnabled() {
432        return mVideoStabilizationEnabled;
433    }
434
435    public void setRecordingHintEnabled(boolean hintEnabled) {
436        mRecordingHintEnabled = hintEnabled;
437    }
438
439    public boolean isRecordingHintEnabled() {
440        return mRecordingHintEnabled;
441    }
442
443    public void setGpsData(GpsData data) {
444        mGpsData = new GpsData(data);
445    }
446
447    public GpsData getGpsData() {
448        return (mGpsData == null ? null : new GpsData(mGpsData));
449    }
450
451    public void clearGpsData() {
452        mGpsData = null;
453    }
454
455    /**
456     * Sets the size of the thumbnail in EXIF header.
457     *
458     * @param s The size for the thumbnail. {@code null} will clear the size to
459     *          (0,0).
460     */
461    public void setExifThumbnailSize(Size s) {
462        if (s != null) {
463            mExifThumbnailSize = s;
464        } else {
465            mExifThumbnailSize = new Size(0,0);
466        }
467    }
468
469    public Size getExifThumbnailSize() {
470        return new Size(mExifThumbnailSize);
471    }
472}
473