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