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