Camera.java revision 16b67137b893f32491a38ab81d49368322e1ae92
1/* 2 * Copyright (C) 2008 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 android.hardware; 18 19import android.annotation.SdkConstant; 20import android.annotation.SdkConstant.SdkConstantType; 21import android.graphics.ImageFormat; 22import android.graphics.Point; 23import android.graphics.Rect; 24import android.graphics.SurfaceTexture; 25import android.os.Handler; 26import android.os.Looper; 27import android.os.Message; 28import android.util.Log; 29import android.view.Surface; 30import android.view.SurfaceHolder; 31 32import java.io.IOException; 33import java.lang.ref.WeakReference; 34import java.util.ArrayList; 35import java.util.HashMap; 36import java.util.List; 37import java.util.StringTokenizer; 38 39 40/** 41 * The Camera class is used to set image capture settings, start/stop preview, 42 * snap pictures, and retrieve frames for encoding for video. This class is a 43 * client for the Camera service, which manages the actual camera hardware. 44 * 45 * <p>To access the device camera, you must declare the 46 * {@link android.Manifest.permission#CAMERA} permission in your Android 47 * Manifest. Also be sure to include the 48 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> 49 * manifest element to declare camera features used by your application. 50 * For example, if you use the camera and auto-focus feature, your Manifest 51 * should include the following:</p> 52 * <pre> <uses-permission android:name="android.permission.CAMERA" /> 53 * <uses-feature android:name="android.hardware.camera" /> 54 * <uses-feature android:name="android.hardware.camera.autofocus" /></pre> 55 * 56 * <p>To take pictures with this class, use the following steps:</p> 57 * 58 * <ol> 59 * <li>Obtain an instance of Camera from {@link #open(int)}. 60 * 61 * <li>Get existing (default) settings with {@link #getParameters()}. 62 * 63 * <li>If necessary, modify the returned {@link Camera.Parameters} object and call 64 * {@link #setParameters(Camera.Parameters)}. 65 * 66 * <li>If desired, call {@link #setDisplayOrientation(int)}. 67 * 68 * <li><b>Important</b>: Pass a fully initialized {@link SurfaceHolder} to 69 * {@link #setPreviewDisplay(SurfaceHolder)}. Without a surface, the camera 70 * will be unable to start the preview. 71 * 72 * <li><b>Important</b>: Call {@link #startPreview()} to start updating the 73 * preview surface. Preview must be started before you can take a picture. 74 * 75 * <li>When you want, call {@link #takePicture(Camera.ShutterCallback, 76 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)} to 77 * capture a photo. Wait for the callbacks to provide the actual image data. 78 * 79 * <li>After taking a picture, preview display will have stopped. To take more 80 * photos, call {@link #startPreview()} again first. 81 * 82 * <li>Call {@link #stopPreview()} to stop updating the preview surface. 83 * 84 * <li><b>Important:</b> Call {@link #release()} to release the camera for 85 * use by other applications. Applications should release the camera 86 * immediately in {@link android.app.Activity#onPause()} (and re-{@link #open()} 87 * it in {@link android.app.Activity#onResume()}). 88 * </ol> 89 * 90 * <p>To quickly switch to video recording mode, use these steps:</p> 91 * 92 * <ol> 93 * <li>Obtain and initialize a Camera and start preview as described above. 94 * 95 * <li>Call {@link #unlock()} to allow the media process to access the camera. 96 * 97 * <li>Pass the camera to {@link android.media.MediaRecorder#setCamera(Camera)}. 98 * See {@link android.media.MediaRecorder} information about video recording. 99 * 100 * <li>When finished recording, call {@link #reconnect()} to re-acquire 101 * and re-lock the camera. 102 * 103 * <li>If desired, restart preview and take more photos or videos. 104 * 105 * <li>Call {@link #stopPreview()} and {@link #release()} as described above. 106 * </ol> 107 * 108 * <p>This class is not thread-safe, and is meant for use from one event thread. 109 * Most long-running operations (preview, focus, photo capture, etc) happen 110 * asynchronously and invoke callbacks as necessary. Callbacks will be invoked 111 * on the event thread {@link #open(int)} was called from. This class's methods 112 * must never be called from multiple threads at once.</p> 113 * 114 * <p class="caution"><strong>Caution:</strong> Different Android-powered devices 115 * may have different hardware specifications, such as megapixel ratings and 116 * auto-focus capabilities. In order for your application to be compatible with 117 * more devices, you should not make assumptions about the device camera 118 * specifications.</p> 119 */ 120public class Camera { 121 private static final String TAG = "Camera"; 122 123 // These match the enums in frameworks/base/include/camera/Camera.h 124 private static final int CAMERA_MSG_ERROR = 0x001; 125 private static final int CAMERA_MSG_SHUTTER = 0x002; 126 private static final int CAMERA_MSG_FOCUS = 0x004; 127 private static final int CAMERA_MSG_ZOOM = 0x008; 128 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010; 129 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020; 130 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040; 131 private static final int CAMERA_MSG_RAW_IMAGE = 0x080; 132 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100; 133 private static final int CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x200; 134 private static final int CAMERA_MSG_PREVIEW_METADATA = 0x400; 135 private static final int CAMERA_MSG_ALL_MSGS = 0x4FF; 136 137 private int mNativeContext; // accessed by native methods 138 private EventHandler mEventHandler; 139 private ShutterCallback mShutterCallback; 140 private PictureCallback mRawImageCallback; 141 private PictureCallback mJpegCallback; 142 private PreviewCallback mPreviewCallback; 143 private PictureCallback mPostviewCallback; 144 private AutoFocusCallback mAutoFocusCallback; 145 private OnZoomChangeListener mZoomListener; 146 private FaceDetectionListener mFaceListener; 147 private ErrorCallback mErrorCallback; 148 private boolean mOneShot; 149 private boolean mWithBuffer; 150 private boolean mFaceDetectionRunning = false; 151 152 /** 153 * Broadcast Action: A new picture is taken by the camera, and the entry of 154 * the picture has been added to the media store. 155 * {@link android.content.Intent#getData} is URI of the picture. 156 */ 157 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 158 public static final String ACTION_NEW_PICTURE = "android.hardware.action.NEW_PICTURE"; 159 160 /** 161 * Broadcast Action: A new video is recorded by the camera, and the entry 162 * of the video has been added to the media store. 163 * {@link android.content.Intent#getData} is URI of the video. 164 */ 165 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 166 public static final String ACTION_NEW_VIDEO = "android.hardware.action.NEW_VIDEO"; 167 168 /** 169 * Hardware face detection. It does not use much CPU. 170 */ 171 private static final int CAMERA_FACE_DETECTION_HW = 0; 172 173 /** 174 * Software face detection. It uses some CPU. 175 */ 176 private static final int CAMERA_FACE_DETECTION_SW = 1; 177 178 /** 179 * Returns the number of physical cameras available on this device. 180 */ 181 public native static int getNumberOfCameras(); 182 183 /** 184 * Returns the information about a particular camera. 185 * If {@link #getNumberOfCameras()} returns N, the valid id is 0 to N-1. 186 */ 187 public native static void getCameraInfo(int cameraId, CameraInfo cameraInfo); 188 189 /** 190 * Information about a camera 191 */ 192 public static class CameraInfo { 193 /** 194 * The facing of the camera is opposite to that of the screen. 195 */ 196 public static final int CAMERA_FACING_BACK = 0; 197 198 /** 199 * The facing of the camera is the same as that of the screen. 200 */ 201 public static final int CAMERA_FACING_FRONT = 1; 202 203 /** 204 * The direction that the camera faces to. It should be 205 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT. 206 */ 207 public int facing; 208 209 /** 210 * <p>The orientation of the camera image. The value is the angle that the 211 * camera image needs to be rotated clockwise so it shows correctly on 212 * the display in its natural orientation. It should be 0, 90, 180, or 270.</p> 213 * 214 * <p>For example, suppose a device has a naturally tall screen. The 215 * back-facing camera sensor is mounted in landscape. You are looking at 216 * the screen. If the top side of the camera sensor is aligned with the 217 * right edge of the screen in natural orientation, the value should be 218 * 90. If the top side of a front-facing camera sensor is aligned with 219 * the right of the screen, the value should be 270.</p> 220 * 221 * @see #setDisplayOrientation(int) 222 * @see Parameters#setRotation(int) 223 * @see Parameters#setPreviewSize(int, int) 224 * @see Parameters#setPictureSize(int, int) 225 * @see Parameters#setJpegThumbnailSize(int, int) 226 */ 227 public int orientation; 228 }; 229 230 /** 231 * Creates a new Camera object to access a particular hardware camera. 232 * 233 * <p>You must call {@link #release()} when you are done using the camera, 234 * otherwise it will remain locked and be unavailable to other applications. 235 * 236 * <p>Your application should only have one Camera object active at a time 237 * for a particular hardware camera. 238 * 239 * <p>Callbacks from other methods are delivered to the event loop of the 240 * thread which called open(). If this thread has no event loop, then 241 * callbacks are delivered to the main application event loop. If there 242 * is no main application event loop, callbacks are not delivered. 243 * 244 * <p class="caution"><b>Caution:</b> On some devices, this method may 245 * take a long time to complete. It is best to call this method from a 246 * worker thread (possibly using {@link android.os.AsyncTask}) to avoid 247 * blocking the main application UI thread. 248 * 249 * @param cameraId the hardware camera to access, between 0 and 250 * {@link #getNumberOfCameras()}-1. 251 * @return a new Camera object, connected, locked and ready for use. 252 * @throws RuntimeException if connection to the camera service fails (for 253 * example, if the camera is in use by another process or device policy 254 * manager has disabled the camera). 255 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName) 256 */ 257 public static Camera open(int cameraId) { 258 return new Camera(cameraId); 259 } 260 261 /** 262 * Creates a new Camera object to access the first back-facing camera on the 263 * device. If the device does not have a back-facing camera, this returns 264 * null. 265 * @see #open(int) 266 */ 267 public static Camera open() { 268 int numberOfCameras = getNumberOfCameras(); 269 CameraInfo cameraInfo = new CameraInfo(); 270 for (int i = 0; i < numberOfCameras; i++) { 271 getCameraInfo(i, cameraInfo); 272 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) { 273 return new Camera(i); 274 } 275 } 276 return null; 277 } 278 279 Camera(int cameraId) { 280 mShutterCallback = null; 281 mRawImageCallback = null; 282 mJpegCallback = null; 283 mPreviewCallback = null; 284 mPostviewCallback = null; 285 mZoomListener = null; 286 287 Looper looper; 288 if ((looper = Looper.myLooper()) != null) { 289 mEventHandler = new EventHandler(this, looper); 290 } else if ((looper = Looper.getMainLooper()) != null) { 291 mEventHandler = new EventHandler(this, looper); 292 } else { 293 mEventHandler = null; 294 } 295 296 native_setup(new WeakReference<Camera>(this), cameraId); 297 } 298 299 protected void finalize() { 300 native_release(); 301 } 302 303 private native final void native_setup(Object camera_this, int cameraId); 304 private native final void native_release(); 305 306 307 /** 308 * Disconnects and releases the Camera object resources. 309 * 310 * <p>You must call this as soon as you're done with the Camera object.</p> 311 */ 312 public final void release() { 313 native_release(); 314 mFaceDetectionRunning = false; 315 } 316 317 /** 318 * Unlocks the camera to allow another process to access it. 319 * Normally, the camera is locked to the process with an active Camera 320 * object until {@link #release()} is called. To allow rapid handoff 321 * between processes, you can call this method to release the camera 322 * temporarily for another process to use; once the other process is done 323 * you can call {@link #reconnect()} to reclaim the camera. 324 * 325 * <p>This must be done before calling 326 * {@link android.media.MediaRecorder#setCamera(Camera)}. This cannot be 327 * called after recording starts. 328 * 329 * <p>If you are not recording video, you probably do not need this method. 330 * 331 * @throws RuntimeException if the camera cannot be unlocked. 332 */ 333 public native final void unlock(); 334 335 /** 336 * Re-locks the camera to prevent other processes from accessing it. 337 * Camera objects are locked by default unless {@link #unlock()} is 338 * called. Normally {@link #reconnect()} is used instead. 339 * 340 * <p>Since API level 13, camera is automatically locked for applications in 341 * {@link android.media.MediaRecorder#start()}. Applications can use the 342 * camera (ex: zoom) after recording starts. There is no need to call this 343 * after recording starts or stops. 344 * 345 * <p>If you are not recording video, you probably do not need this method. 346 * 347 * @throws RuntimeException if the camera cannot be re-locked (for 348 * example, if the camera is still in use by another process). 349 */ 350 public native final void lock(); 351 352 /** 353 * Reconnects to the camera service after another process used it. 354 * After {@link #unlock()} is called, another process may use the 355 * camera; when the process is done, you must reconnect to the camera, 356 * which will re-acquire the lock and allow you to continue using the 357 * camera. 358 * 359 * <p>Since API level 13, camera is automatically locked for applications in 360 * {@link android.media.MediaRecorder#start()}. Applications can use the 361 * camera (ex: zoom) after recording starts. There is no need to call this 362 * after recording starts or stops. 363 * 364 * <p>If you are not recording video, you probably do not need this method. 365 * 366 * @throws IOException if a connection cannot be re-established (for 367 * example, if the camera is still in use by another process). 368 */ 369 public native final void reconnect() throws IOException; 370 371 /** 372 * Sets the {@link Surface} to be used for live preview. 373 * Either a surface or surface texture is necessary for preview, and 374 * preview is necessary to take pictures. The same surface can be re-set 375 * without harm. Setting a preview surface will un-set any preview surface 376 * texture that was set via {@link #setPreviewTexture}. 377 * 378 * <p>The {@link SurfaceHolder} must already contain a surface when this 379 * method is called. If you are using {@link android.view.SurfaceView}, 380 * you will need to register a {@link SurfaceHolder.Callback} with 381 * {@link SurfaceHolder#addCallback(SurfaceHolder.Callback)} and wait for 382 * {@link SurfaceHolder.Callback#surfaceCreated(SurfaceHolder)} before 383 * calling setPreviewDisplay() or starting preview. 384 * 385 * <p>This method must be called before {@link #startPreview()}. The 386 * one exception is that if the preview surface is not set (or set to null) 387 * before startPreview() is called, then this method may be called once 388 * with a non-null parameter to set the preview surface. (This allows 389 * camera setup and surface creation to happen in parallel, saving time.) 390 * The preview surface may not otherwise change while preview is running. 391 * 392 * @param holder containing the Surface on which to place the preview, 393 * or null to remove the preview surface 394 * @throws IOException if the method fails (for example, if the surface 395 * is unavailable or unsuitable). 396 */ 397 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException { 398 if (holder != null) { 399 setPreviewDisplay(holder.getSurface()); 400 } else { 401 setPreviewDisplay((Surface)null); 402 } 403 } 404 405 private native final void setPreviewDisplay(Surface surface) throws IOException; 406 407 /** 408 * Sets the {@link SurfaceTexture} to be used for live preview. 409 * Either a surface or surface texture is necessary for preview, and 410 * preview is necessary to take pictures. The same surface texture can be 411 * re-set without harm. Setting a preview surface texture will un-set any 412 * preview surface that was set via {@link #setPreviewDisplay}. 413 * 414 * <p>This method must be called before {@link #startPreview()}. The 415 * one exception is that if the preview surface texture is not set (or set 416 * to null) before startPreview() is called, then this method may be called 417 * once with a non-null parameter to set the preview surface. (This allows 418 * camera setup and surface creation to happen in parallel, saving time.) 419 * The preview surface texture may not otherwise change while preview is 420 * running. 421 * 422 * <p>The timestamps provided by {@link SurfaceTexture#getTimestamp()} for a 423 * SurfaceTexture set as the preview texture have an unspecified zero point, 424 * and cannot be directly compared between different cameras or different 425 * instances of the same camera, or across multiple runs of the same 426 * program. 427 * 428 * @param surfaceTexture the {@link SurfaceTexture} to which the preview 429 * images are to be sent or null to remove the current preview surface 430 * texture 431 * @throws IOException if the method fails (for example, if the surface 432 * texture is unavailable or unsuitable). 433 */ 434 public native final void setPreviewTexture(SurfaceTexture surfaceTexture) throws IOException; 435 436 /** 437 * Callback interface used to deliver copies of preview frames as 438 * they are displayed. 439 * 440 * @see #setPreviewCallback(Camera.PreviewCallback) 441 * @see #setOneShotPreviewCallback(Camera.PreviewCallback) 442 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback) 443 * @see #startPreview() 444 */ 445 public interface PreviewCallback 446 { 447 /** 448 * Called as preview frames are displayed. This callback is invoked 449 * on the event thread {@link #open(int)} was called from. 450 * 451 * @param data the contents of the preview frame in the format defined 452 * by {@link android.graphics.ImageFormat}, which can be queried 453 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}. 454 * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)} 455 * is never called, the default will be the YCbCr_420_SP 456 * (NV21) format. 457 * @param camera the Camera service object. 458 */ 459 void onPreviewFrame(byte[] data, Camera camera); 460 }; 461 462 /** 463 * Starts capturing and drawing preview frames to the screen. 464 * Preview will not actually start until a surface is supplied 465 * with {@link #setPreviewDisplay(SurfaceHolder)} or 466 * {@link #setPreviewTexture(SurfaceTexture)}. 467 * 468 * <p>If {@link #setPreviewCallback(Camera.PreviewCallback)}, 469 * {@link #setOneShotPreviewCallback(Camera.PreviewCallback)}, or 470 * {@link #setPreviewCallbackWithBuffer(Camera.PreviewCallback)} were 471 * called, {@link Camera.PreviewCallback#onPreviewFrame(byte[], Camera)} 472 * will be called when preview data becomes available. 473 */ 474 public native final void startPreview(); 475 476 /** 477 * Stops capturing and drawing preview frames to the surface, and 478 * resets the camera for a future call to {@link #startPreview()}. 479 */ 480 public final void stopPreview() { 481 _stopPreview(); 482 mFaceDetectionRunning = false; 483 } 484 485 private native final void _stopPreview(); 486 487 /** 488 * Return current preview state. 489 * 490 * FIXME: Unhide before release 491 * @hide 492 */ 493 public native final boolean previewEnabled(); 494 495 /** 496 * Installs a callback to be invoked for every preview frame in addition 497 * to displaying them on the screen. The callback will be repeatedly called 498 * for as long as preview is active. This method can be called at any time, 499 * even while preview is live. Any other preview callbacks are overridden. 500 * 501 * @param cb a callback object that receives a copy of each preview frame, 502 * or null to stop receiving callbacks. 503 */ 504 public final void setPreviewCallback(PreviewCallback cb) { 505 mPreviewCallback = cb; 506 mOneShot = false; 507 mWithBuffer = false; 508 // Always use one-shot mode. We fake camera preview mode by 509 // doing one-shot preview continuously. 510 setHasPreviewCallback(cb != null, false); 511 } 512 513 /** 514 * Installs a callback to be invoked for the next preview frame in addition 515 * to displaying it on the screen. After one invocation, the callback is 516 * cleared. This method can be called any time, even when preview is live. 517 * Any other preview callbacks are overridden. 518 * 519 * @param cb a callback object that receives a copy of the next preview frame, 520 * or null to stop receiving callbacks. 521 */ 522 public final void setOneShotPreviewCallback(PreviewCallback cb) { 523 mPreviewCallback = cb; 524 mOneShot = true; 525 mWithBuffer = false; 526 setHasPreviewCallback(cb != null, false); 527 } 528 529 private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer); 530 531 /** 532 * Installs a callback to be invoked for every preview frame, using buffers 533 * supplied with {@link #addCallbackBuffer(byte[])}, in addition to 534 * displaying them on the screen. The callback will be repeatedly called 535 * for as long as preview is active and buffers are available. 536 * Any other preview callbacks are overridden. 537 * 538 * <p>The purpose of this method is to improve preview efficiency and frame 539 * rate by allowing preview frame memory reuse. You must call 540 * {@link #addCallbackBuffer(byte[])} at some point -- before or after 541 * calling this method -- or no callbacks will received. 542 * 543 * The buffer queue will be cleared if this method is called with a null 544 * callback, {@link #setPreviewCallback(Camera.PreviewCallback)} is called, 545 * or {@link #setOneShotPreviewCallback(Camera.PreviewCallback)} is called. 546 * 547 * @param cb a callback object that receives a copy of the preview frame, 548 * or null to stop receiving callbacks and clear the buffer queue. 549 * @see #addCallbackBuffer(byte[]) 550 */ 551 public final void setPreviewCallbackWithBuffer(PreviewCallback cb) { 552 mPreviewCallback = cb; 553 mOneShot = false; 554 mWithBuffer = true; 555 setHasPreviewCallback(cb != null, true); 556 } 557 558 /** 559 * Adds a pre-allocated buffer to the preview callback buffer queue. 560 * Applications can add one or more buffers to the queue. When a preview 561 * frame arrives and there is still at least one available buffer, the 562 * buffer will be used and removed from the queue. Then preview callback is 563 * invoked with the buffer. If a frame arrives and there is no buffer left, 564 * the frame is discarded. Applications should add buffers back when they 565 * finish processing the data in them. 566 * 567 * <p>The size of the buffer is determined by multiplying the preview 568 * image width, height, and bytes per pixel. The width and height can be 569 * read from {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel 570 * can be computed from 571 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8, 572 * using the image format from {@link Camera.Parameters#getPreviewFormat()}. 573 * 574 * <p>This method is only necessary when 575 * {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When 576 * {@link #setPreviewCallback(PreviewCallback)} or 577 * {@link #setOneShotPreviewCallback(PreviewCallback)} are used, buffers 578 * are automatically allocated. When a supplied buffer is too small to 579 * hold the preview frame data, preview callback will return null and 580 * the buffer will be removed from the buffer queue. 581 * 582 * @param callbackBuffer the buffer to add to the queue. 583 * The size should be width * height * bits_per_pixel / 8. 584 * @see #setPreviewCallbackWithBuffer(PreviewCallback) 585 */ 586 public final void addCallbackBuffer(byte[] callbackBuffer) 587 { 588 _addCallbackBuffer(callbackBuffer, CAMERA_MSG_PREVIEW_FRAME); 589 } 590 591 /** 592 * Adds a pre-allocated buffer to the raw image callback buffer queue. 593 * Applications can add one or more buffers to the queue. When a raw image 594 * frame arrives and there is still at least one available buffer, the 595 * buffer will be used to hold the raw image data and removed from the 596 * queue. Then raw image callback is invoked with the buffer. If a raw 597 * image frame arrives but there is no buffer left, the frame is 598 * discarded. Applications should add buffers back when they finish 599 * processing the data in them by calling this method again in order 600 * to avoid running out of raw image callback buffers. 601 * 602 * <p>The size of the buffer is determined by multiplying the raw image 603 * width, height, and bytes per pixel. The width and height can be 604 * read from {@link Camera.Parameters#getPictureSize()}. Bytes per pixel 605 * can be computed from 606 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8, 607 * using the image format from {@link Camera.Parameters#getPreviewFormat()}. 608 * 609 * <p>This method is only necessary when the PictureCallbck for raw image 610 * is used while calling {@link #takePicture(Camera.ShutterCallback, 611 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}. 612 * 613 * <p>Please note that by calling this method, the mode for 614 * application-managed callback buffers is triggered. If this method has 615 * never been called, null will be returned by the raw image callback since 616 * there is no image callback buffer available. Furthermore, When a supplied 617 * buffer is too small to hold the raw image data, raw image callback will 618 * return null and the buffer will be removed from the buffer queue. 619 * 620 * @param callbackBuffer the buffer to add to the raw image callback buffer 621 * queue. The size should be width * height * (bits per pixel) / 8. An 622 * null callbackBuffer will be ignored and won't be added to the queue. 623 * 624 * @see #takePicture(Camera.ShutterCallback, 625 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}. 626 * 627 * {@hide} 628 */ 629 public final void addRawImageCallbackBuffer(byte[] callbackBuffer) 630 { 631 addCallbackBuffer(callbackBuffer, CAMERA_MSG_RAW_IMAGE); 632 } 633 634 private final void addCallbackBuffer(byte[] callbackBuffer, int msgType) 635 { 636 // CAMERA_MSG_VIDEO_FRAME may be allowed in the future. 637 if (msgType != CAMERA_MSG_PREVIEW_FRAME && 638 msgType != CAMERA_MSG_RAW_IMAGE) { 639 throw new IllegalArgumentException( 640 "Unsupported message type: " + msgType); 641 } 642 643 _addCallbackBuffer(callbackBuffer, msgType); 644 } 645 646 private native final void _addCallbackBuffer( 647 byte[] callbackBuffer, int msgType); 648 649 private class EventHandler extends Handler 650 { 651 private Camera mCamera; 652 653 public EventHandler(Camera c, Looper looper) { 654 super(looper); 655 mCamera = c; 656 } 657 658 @Override 659 public void handleMessage(Message msg) { 660 switch(msg.what) { 661 case CAMERA_MSG_SHUTTER: 662 if (mShutterCallback != null) { 663 mShutterCallback.onShutter(); 664 } 665 return; 666 667 case CAMERA_MSG_RAW_IMAGE: 668 if (mRawImageCallback != null) { 669 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera); 670 } 671 return; 672 673 case CAMERA_MSG_COMPRESSED_IMAGE: 674 if (mJpegCallback != null) { 675 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera); 676 } 677 return; 678 679 case CAMERA_MSG_PREVIEW_FRAME: 680 if (mPreviewCallback != null) { 681 PreviewCallback cb = mPreviewCallback; 682 if (mOneShot) { 683 // Clear the callback variable before the callback 684 // in case the app calls setPreviewCallback from 685 // the callback function 686 mPreviewCallback = null; 687 } else if (!mWithBuffer) { 688 // We're faking the camera preview mode to prevent 689 // the app from being flooded with preview frames. 690 // Set to oneshot mode again. 691 setHasPreviewCallback(true, false); 692 } 693 cb.onPreviewFrame((byte[])msg.obj, mCamera); 694 } 695 return; 696 697 case CAMERA_MSG_POSTVIEW_FRAME: 698 if (mPostviewCallback != null) { 699 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera); 700 } 701 return; 702 703 case CAMERA_MSG_FOCUS: 704 if (mAutoFocusCallback != null) { 705 mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera); 706 } 707 return; 708 709 case CAMERA_MSG_ZOOM: 710 if (mZoomListener != null) { 711 mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera); 712 } 713 return; 714 715 case CAMERA_MSG_PREVIEW_METADATA: 716 if (mFaceListener != null) { 717 mFaceListener.onFaceDetection((Face[])msg.obj, mCamera); 718 } 719 return; 720 721 case CAMERA_MSG_ERROR : 722 Log.e(TAG, "Error " + msg.arg1); 723 if (mErrorCallback != null) { 724 mErrorCallback.onError(msg.arg1, mCamera); 725 } 726 return; 727 728 default: 729 Log.e(TAG, "Unknown message type " + msg.what); 730 return; 731 } 732 } 733 } 734 735 private static void postEventFromNative(Object camera_ref, 736 int what, int arg1, int arg2, Object obj) 737 { 738 Camera c = (Camera)((WeakReference)camera_ref).get(); 739 if (c == null) 740 return; 741 742 if (c.mEventHandler != null) { 743 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj); 744 c.mEventHandler.sendMessage(m); 745 } 746 } 747 748 /** 749 * Callback interface used to notify on completion of camera auto focus. 750 * 751 * <p>Devices that do not support auto-focus will receive a "fake" 752 * callback to this interface. If your application needs auto-focus and 753 * should not be installed on devices <em>without</em> auto-focus, you must 754 * declare that your app uses the 755 * {@code android.hardware.camera.autofocus} feature, in the 756 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> 757 * manifest element.</p> 758 * 759 * @see #autoFocus(AutoFocusCallback) 760 */ 761 public interface AutoFocusCallback 762 { 763 /** 764 * Called when the camera auto focus completes. If the camera 765 * does not support auto-focus and autoFocus is called, 766 * onAutoFocus will be called immediately with a fake value of 767 * <code>success</code> set to <code>true</code>. 768 * 769 * The auto-focus routine may lock auto-exposure and auto-white balance 770 * after it completes. To check for the state of these locks, use the 771 * {@link android.hardware.Camera.Parameters#getAutoExposureLock()} and 772 * {@link android.hardware.Camera.Parameters#getAutoWhiteBalanceLock()} 773 * methods. If such locking is undesirable, use 774 * {@link android.hardware.Camera.Parameters#setAutoExposureLock(boolean)} 775 * and 776 * {@link android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)} 777 * to release the locks. 778 * 779 * @param success true if focus was successful, false if otherwise 780 * @param camera the Camera service object 781 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean) 782 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean) 783 */ 784 void onAutoFocus(boolean success, Camera camera); 785 }; 786 787 /** 788 * Starts camera auto-focus and registers a callback function to run when 789 * the camera is focused. This method is only valid when preview is active 790 * (between {@link #startPreview()} and before {@link #stopPreview()}). 791 * 792 * <p>Callers should check 793 * {@link android.hardware.Camera.Parameters#getFocusMode()} to determine if 794 * this method should be called. If the camera does not support auto-focus, 795 * it is a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)} 796 * callback will be called immediately. 797 * 798 * <p>If your application should not be installed 799 * on devices without auto-focus, you must declare that your application 800 * uses auto-focus with the 801 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> 802 * manifest element.</p> 803 * 804 * <p>If the current flash mode is not 805 * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be 806 * fired during auto-focus, depending on the driver and camera hardware.<p> 807 * 808 * The auto-focus routine may lock auto-exposure and auto-white balance 809 * after it completes. To check for the state of these locks, use the 810 * {@link android.hardware.Camera.Parameters#getAutoExposureLock()} and 811 * {@link android.hardware.Camera.Parameters#getAutoWhiteBalanceLock()} 812 * methods after the {@link AutoFocusCallback#onAutoFocus(boolean, Camera)} 813 * callback is invoked. If such locking is undesirable, use 814 * {@link android.hardware.Camera.Parameters#setAutoExposureLock(boolean)} 815 * and 816 * {@link android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)} 817 * to release the locks. 818 * 819 * @param cb the callback to run 820 * @see #cancelAutoFocus() 821 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean) 822 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean) 823 */ 824 public final void autoFocus(AutoFocusCallback cb) 825 { 826 mAutoFocusCallback = cb; 827 native_autoFocus(); 828 } 829 private native final void native_autoFocus(); 830 831 /** 832 * Cancels any auto-focus function in progress. 833 * Whether or not auto-focus is currently in progress, 834 * this function will return the focus position to the default. 835 * If the camera does not support auto-focus, this is a no-op. 836 * 837 * Canceling auto-focus will return the auto-exposure lock and auto-white 838 * balance lock to their state before {@link #autoFocus(AutoFocusCallback)} 839 * was called. 840 * 841 * @see #autoFocus(Camera.AutoFocusCallback) 842 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean) 843 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean) 844 */ 845 public final void cancelAutoFocus() 846 { 847 mAutoFocusCallback = null; 848 native_cancelAutoFocus(); 849 } 850 private native final void native_cancelAutoFocus(); 851 852 /** 853 * Callback interface used to signal the moment of actual image capture. 854 * 855 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback) 856 */ 857 public interface ShutterCallback 858 { 859 /** 860 * Called as near as possible to the moment when a photo is captured 861 * from the sensor. This is a good opportunity to play a shutter sound 862 * or give other feedback of camera operation. This may be some time 863 * after the photo was triggered, but some time before the actual data 864 * is available. 865 */ 866 void onShutter(); 867 } 868 869 /** 870 * Callback interface used to supply image data from a photo capture. 871 * 872 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback) 873 */ 874 public interface PictureCallback { 875 /** 876 * Called when image data is available after a picture is taken. 877 * The format of the data depends on the context of the callback 878 * and {@link Camera.Parameters} settings. 879 * 880 * @param data a byte array of the picture data 881 * @param camera the Camera service object 882 */ 883 void onPictureTaken(byte[] data, Camera camera); 884 }; 885 886 /** 887 * Equivalent to takePicture(shutter, raw, null, jpeg). 888 * 889 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback) 890 */ 891 public final void takePicture(ShutterCallback shutter, PictureCallback raw, 892 PictureCallback jpeg) { 893 takePicture(shutter, raw, null, jpeg); 894 } 895 private native final void native_takePicture(int msgType); 896 897 /** 898 * Triggers an asynchronous image capture. The camera service will initiate 899 * a series of callbacks to the application as the image capture progresses. 900 * The shutter callback occurs after the image is captured. This can be used 901 * to trigger a sound to let the user know that image has been captured. The 902 * raw callback occurs when the raw image data is available (NOTE: the data 903 * will be null if there is no raw image callback buffer available or the 904 * raw image callback buffer is not large enough to hold the raw image). 905 * The postview callback occurs when a scaled, fully processed postview 906 * image is available (NOTE: not all hardware supports this). The jpeg 907 * callback occurs when the compressed image is available. If the 908 * application does not need a particular callback, a null can be passed 909 * instead of a callback method. 910 * 911 * <p>This method is only valid when preview is active (after 912 * {@link #startPreview()}). Preview will be stopped after the image is 913 * taken; callers must call {@link #startPreview()} again if they want to 914 * re-start preview or take more pictures. This should not be called between 915 * {@link android.media.MediaRecorder#start()} and 916 * {@link android.media.MediaRecorder#stop()}. 917 * 918 * <p>After calling this method, you must not call {@link #startPreview()} 919 * or take another picture until the JPEG callback has returned. 920 * 921 * @param shutter the callback for image capture moment, or null 922 * @param raw the callback for raw (uncompressed) image data, or null 923 * @param postview callback with postview image data, may be null 924 * @param jpeg the callback for JPEG image data, or null 925 */ 926 public final void takePicture(ShutterCallback shutter, PictureCallback raw, 927 PictureCallback postview, PictureCallback jpeg) { 928 mShutterCallback = shutter; 929 mRawImageCallback = raw; 930 mPostviewCallback = postview; 931 mJpegCallback = jpeg; 932 933 // If callback is not set, do not send me callbacks. 934 int msgType = 0; 935 if (mShutterCallback != null) { 936 msgType |= CAMERA_MSG_SHUTTER; 937 } 938 if (mRawImageCallback != null) { 939 msgType |= CAMERA_MSG_RAW_IMAGE; 940 } 941 if (mPostviewCallback != null) { 942 msgType |= CAMERA_MSG_POSTVIEW_FRAME; 943 } 944 if (mJpegCallback != null) { 945 msgType |= CAMERA_MSG_COMPRESSED_IMAGE; 946 } 947 948 native_takePicture(msgType); 949 } 950 951 /** 952 * Zooms to the requested value smoothly. The driver will notify {@link 953 * OnZoomChangeListener} of the zoom value and whether zoom is stopped at 954 * the time. For example, suppose the current zoom is 0 and startSmoothZoom 955 * is called with value 3. The 956 * {@link Camera.OnZoomChangeListener#onZoomChange(int, boolean, Camera)} 957 * method will be called three times with zoom values 1, 2, and 3. 958 * Applications can call {@link #stopSmoothZoom} to stop the zoom earlier. 959 * Applications should not call startSmoothZoom again or change the zoom 960 * value before zoom stops. If the supplied zoom value equals to the current 961 * zoom value, no zoom callback will be generated. This method is supported 962 * if {@link android.hardware.Camera.Parameters#isSmoothZoomSupported} 963 * returns true. 964 * 965 * @param value zoom value. The valid range is 0 to {@link 966 * android.hardware.Camera.Parameters#getMaxZoom}. 967 * @throws IllegalArgumentException if the zoom value is invalid. 968 * @throws RuntimeException if the method fails. 969 * @see #setZoomChangeListener(OnZoomChangeListener) 970 */ 971 public native final void startSmoothZoom(int value); 972 973 /** 974 * Stops the smooth zoom. Applications should wait for the {@link 975 * OnZoomChangeListener} to know when the zoom is actually stopped. This 976 * method is supported if {@link 977 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true. 978 * 979 * @throws RuntimeException if the method fails. 980 */ 981 public native final void stopSmoothZoom(); 982 983 /** 984 * Set the clockwise rotation of preview display in degrees. This affects 985 * the preview frames and the picture displayed after snapshot. This method 986 * is useful for portrait mode applications. Note that preview display of 987 * front-facing cameras is flipped horizontally before the rotation, that 988 * is, the image is reflected along the central vertical axis of the camera 989 * sensor. So the users can see themselves as looking into a mirror. 990 * 991 * <p>This does not affect the order of byte array passed in {@link 992 * PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This 993 * method is not allowed to be called during preview. 994 * 995 * <p>If you want to make the camera image show in the same orientation as 996 * the display, you can use the following code. 997 * <pre> 998 * public static void setCameraDisplayOrientation(Activity activity, 999 * int cameraId, android.hardware.Camera camera) { 1000 * android.hardware.Camera.CameraInfo info = 1001 * new android.hardware.Camera.CameraInfo(); 1002 * android.hardware.Camera.getCameraInfo(cameraId, info); 1003 * int rotation = activity.getWindowManager().getDefaultDisplay() 1004 * .getRotation(); 1005 * int degrees = 0; 1006 * switch (rotation) { 1007 * case Surface.ROTATION_0: degrees = 0; break; 1008 * case Surface.ROTATION_90: degrees = 90; break; 1009 * case Surface.ROTATION_180: degrees = 180; break; 1010 * case Surface.ROTATION_270: degrees = 270; break; 1011 * } 1012 * 1013 * int result; 1014 * if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 1015 * result = (info.orientation + degrees) % 360; 1016 * result = (360 - result) % 360; // compensate the mirror 1017 * } else { // back-facing 1018 * result = (info.orientation - degrees + 360) % 360; 1019 * } 1020 * camera.setDisplayOrientation(result); 1021 * } 1022 * </pre> 1023 * @param degrees the angle that the picture will be rotated clockwise. 1024 * Valid values are 0, 90, 180, and 270. The starting 1025 * position is 0 (landscape). 1026 * @see #setPreviewDisplay(SurfaceHolder) 1027 */ 1028 public native final void setDisplayOrientation(int degrees); 1029 1030 /** 1031 * Callback interface for zoom changes during a smooth zoom operation. 1032 * 1033 * @see #setZoomChangeListener(OnZoomChangeListener) 1034 * @see #startSmoothZoom(int) 1035 */ 1036 public interface OnZoomChangeListener 1037 { 1038 /** 1039 * Called when the zoom value has changed during a smooth zoom. 1040 * 1041 * @param zoomValue the current zoom value. In smooth zoom mode, camera 1042 * calls this for every new zoom value. 1043 * @param stopped whether smooth zoom is stopped. If the value is true, 1044 * this is the last zoom update for the application. 1045 * @param camera the Camera service object 1046 */ 1047 void onZoomChange(int zoomValue, boolean stopped, Camera camera); 1048 }; 1049 1050 /** 1051 * Registers a listener to be notified when the zoom value is updated by the 1052 * camera driver during smooth zoom. 1053 * 1054 * @param listener the listener to notify 1055 * @see #startSmoothZoom(int) 1056 */ 1057 public final void setZoomChangeListener(OnZoomChangeListener listener) 1058 { 1059 mZoomListener = listener; 1060 } 1061 1062 /** 1063 * Callback interface for face detected in the preview frame. 1064 * 1065 */ 1066 public interface FaceDetectionListener 1067 { 1068 /** 1069 * Notify the listener of the detected faces in the preview frame. 1070 * 1071 * @param faces the detected faces. The list is sorted by the score. 1072 * The highest score is the first element. 1073 * @param camera the Camera service object 1074 */ 1075 void onFaceDetection(Face[] faces, Camera camera); 1076 } 1077 1078 /** 1079 * Registers a listener to be notified about the faces detected in the 1080 * preview frame. 1081 * 1082 * @param listener the listener to notify 1083 * @see #startFaceDetection() 1084 */ 1085 public final void setFaceDetectionListener(FaceDetectionListener listener) 1086 { 1087 mFaceListener = listener; 1088 } 1089 1090 /** 1091 * Starts the face detection. This should be called after preview is started. 1092 * The camera will notify {@link FaceDetectionListener} of the detected 1093 * faces in the preview frame. The detected faces may be the same as the 1094 * previous ones. Applications should call {@link #stopFaceDetection} to 1095 * stop the face detection. This method is supported if {@link 1096 * Parameters#getMaxNumDetectedFaces()} returns a number larger than 0. 1097 * If the face detection has started, apps should not call this again. 1098 * 1099 * When the face detection is running, {@link Parameters#setWhiteBalance(String)}, 1100 * {@link Parameters#setFocusAreas(List)}, and {@link Parameters#setMeteringAreas(List)} 1101 * have no effect. 1102 * 1103 * @throws IllegalArgumentException if the face detection is unsupported. 1104 * @throws RuntimeException if the method fails or the face detection is 1105 * already running. 1106 * @see FaceDetectionListener 1107 * @see #stopFaceDetection() 1108 * @see Parameters#getMaxNumDetectedFaces() 1109 */ 1110 public final void startFaceDetection() { 1111 if (mFaceDetectionRunning) { 1112 throw new RuntimeException("Face detection is already running"); 1113 } 1114 _startFaceDetection(CAMERA_FACE_DETECTION_HW); 1115 mFaceDetectionRunning = true; 1116 } 1117 1118 /** 1119 * Stops the face detection. 1120 * 1121 * @see #startFaceDetection(int) 1122 */ 1123 public final void stopFaceDetection() { 1124 _stopFaceDetection(); 1125 mFaceDetectionRunning = false; 1126 } 1127 1128 private native final void _startFaceDetection(int type); 1129 private native final void _stopFaceDetection(); 1130 1131 /** 1132 * The information of a face from camera face detection. 1133 * 1134 */ 1135 public static class Face { 1136 /** 1137 * Create an empty face. 1138 */ 1139 public Face() { 1140 } 1141 1142 /** 1143 * Bounds of the face. (-1000, -1000) represents the top-left of the 1144 * camera field of view, and (1000, 1000) represents the bottom-right of 1145 * the field of view. For example, suppose the size of the viewfinder UI 1146 * is 800x480. The rect passed from the driver is (-1000, -1000, 0, 0). 1147 * The corresponding viewfinder rect should be (0, 0, 400, 240). The 1148 * width and height of the rect will not be 0 or negative. 1149 * 1150 * <p>The direction is relative to the sensor orientation, that is, what 1151 * the sensor sees. The direction is not affected by the rotation or 1152 * mirroring of {@link #setDisplayOrientation(int)}.</p> 1153 * 1154 * @see #startFaceDetection(int) 1155 */ 1156 public Rect rect; 1157 1158 /** 1159 * The confidence level of the face. The range is 1 to 100. 100 is the 1160 * highest confidence. 1161 * 1162 * @see #startFaceDetection(int) 1163 */ 1164 public int score; 1165 } 1166 1167 // Error codes match the enum in include/ui/Camera.h 1168 1169 /** 1170 * Unspecified camera error. 1171 * @see Camera.ErrorCallback 1172 */ 1173 public static final int CAMERA_ERROR_UNKNOWN = 1; 1174 1175 /** 1176 * Media server died. In this case, the application must release the 1177 * Camera object and instantiate a new one. 1178 * @see Camera.ErrorCallback 1179 */ 1180 public static final int CAMERA_ERROR_SERVER_DIED = 100; 1181 1182 /** 1183 * Callback interface for camera error notification. 1184 * 1185 * @see #setErrorCallback(ErrorCallback) 1186 */ 1187 public interface ErrorCallback 1188 { 1189 /** 1190 * Callback for camera errors. 1191 * @param error error code: 1192 * <ul> 1193 * <li>{@link #CAMERA_ERROR_UNKNOWN} 1194 * <li>{@link #CAMERA_ERROR_SERVER_DIED} 1195 * </ul> 1196 * @param camera the Camera service object 1197 */ 1198 void onError(int error, Camera camera); 1199 }; 1200 1201 /** 1202 * Registers a callback to be invoked when an error occurs. 1203 * @param cb The callback to run 1204 */ 1205 public final void setErrorCallback(ErrorCallback cb) 1206 { 1207 mErrorCallback = cb; 1208 } 1209 1210 private native final void native_setParameters(String params); 1211 private native final String native_getParameters(); 1212 1213 /** 1214 * Changes the settings for this Camera service. 1215 * 1216 * @param params the Parameters to use for this Camera service 1217 * @throws RuntimeException if any parameter is invalid or not supported. 1218 * @see #getParameters() 1219 */ 1220 public void setParameters(Parameters params) { 1221 native_setParameters(params.flatten()); 1222 } 1223 1224 /** 1225 * Returns the current settings for this Camera service. 1226 * If modifications are made to the returned Parameters, they must be passed 1227 * to {@link #setParameters(Camera.Parameters)} to take effect. 1228 * 1229 * @see #setParameters(Camera.Parameters) 1230 */ 1231 public Parameters getParameters() { 1232 Parameters p = new Parameters(); 1233 String s = native_getParameters(); 1234 p.unflatten(s); 1235 return p; 1236 } 1237 1238 /** 1239 * Image size (width and height dimensions). 1240 */ 1241 public class Size { 1242 /** 1243 * Sets the dimensions for pictures. 1244 * 1245 * @param w the photo width (pixels) 1246 * @param h the photo height (pixels) 1247 */ 1248 public Size(int w, int h) { 1249 width = w; 1250 height = h; 1251 } 1252 /** 1253 * Compares {@code obj} to this size. 1254 * 1255 * @param obj the object to compare this size with. 1256 * @return {@code true} if the width and height of {@code obj} is the 1257 * same as those of this size. {@code false} otherwise. 1258 */ 1259 @Override 1260 public boolean equals(Object obj) { 1261 if (!(obj instanceof Size)) { 1262 return false; 1263 } 1264 Size s = (Size) obj; 1265 return width == s.width && height == s.height; 1266 } 1267 @Override 1268 public int hashCode() { 1269 return width * 32713 + height; 1270 } 1271 /** width of the picture */ 1272 public int width; 1273 /** height of the picture */ 1274 public int height; 1275 }; 1276 1277 /** 1278 * <p>The Area class is used for choosing specific metering and focus areas for 1279 * the camera to use when calculating auto-exposure, auto-white balance, and 1280 * auto-focus.</p> 1281 * 1282 * <p>To find out how many simultaneous areas a given camera supports, use 1283 * {@link Parameters#getMaxNumMeteringAreas()} and 1284 * {@link Parameters#getMaxNumFocusAreas()}. If metering or focusing area 1285 * selection is unsupported, these methods will return 0.</p> 1286 * 1287 * <p>Each Area consists of a rectangle specifying its bounds, and a weight 1288 * that determines its importance. The bounds are relative to the camera's 1289 * current field of view. The coordinates are mapped so that (-1000, -1000) 1290 * is always the top-left corner of the current field of view, and (1000, 1291 * 1000) is always the bottom-right corner of the current field of 1292 * view. Setting Areas with bounds outside that range is not allowed. Areas 1293 * with zero or negative width or height are not allowed.</p> 1294 * 1295 * <p>The weight must range from 1 to 1000, and represents a weight for 1296 * every pixel in the area. This means that a large metering area with 1297 * the same weight as a smaller area will have more effect in the 1298 * metering result. Metering areas can overlap and the driver 1299 * will add the weights in the overlap region.</p> 1300 * 1301 * @see Parameters#setFocusAreas(List) 1302 * @see Parameters#getFocusAreas() 1303 * @see Parameters#getMaxNumFocusAreas() 1304 * @see Parameters#setMeteringAreas(List) 1305 * @see Parameters#getMeteringAreas() 1306 * @see Parameters#getMaxNumMeteringAreas() 1307 */ 1308 public static class Area { 1309 /** 1310 * Create an area with specified rectangle and weight. 1311 * 1312 * @param rect the bounds of the area. 1313 * @param weight the weight of the area. 1314 */ 1315 public Area(Rect rect, int weight) { 1316 this.rect = rect; 1317 this.weight = weight; 1318 } 1319 /** 1320 * Compares {@code obj} to this area. 1321 * 1322 * @param obj the object to compare this area with. 1323 * @return {@code true} if the rectangle and weight of {@code obj} is 1324 * the same as those of this area. {@code false} otherwise. 1325 */ 1326 @Override 1327 public boolean equals(Object obj) { 1328 if (!(obj instanceof Area)) { 1329 return false; 1330 } 1331 Area a = (Area) obj; 1332 if (rect == null) { 1333 if (a.rect != null) return false; 1334 } else { 1335 if (!rect.equals(a.rect)) return false; 1336 } 1337 return weight == a.weight; 1338 } 1339 1340 /** 1341 * Bounds of the area. (-1000, -1000) represents the top-left of the 1342 * camera field of view, and (1000, 1000) represents the bottom-right of 1343 * the field of view. Setting bounds outside that range is not 1344 * allowed. Bounds with zero or negative width or height are not 1345 * allowed. 1346 * 1347 * @see Parameters#getFocusAreas() 1348 * @see Parameters#getMeteringAreas() 1349 */ 1350 public Rect rect; 1351 1352 /** 1353 * Weight of the area. The weight must range from 1 to 1000, and 1354 * represents a weight for every pixel in the area. This means that a 1355 * large metering area with the same weight as a smaller area will have 1356 * more effect in the metering result. Metering areas can overlap and 1357 * the driver will add the weights in the overlap region. 1358 * 1359 * @see Parameters#getFocusAreas() 1360 * @see Parameters#getMeteringAreas() 1361 */ 1362 public int weight; 1363 } 1364 1365 /** 1366 * Camera service settings. 1367 * 1368 * <p>To make camera parameters take effect, applications have to call 1369 * {@link Camera#setParameters(Camera.Parameters)}. For example, after 1370 * {@link Camera.Parameters#setWhiteBalance} is called, white balance is not 1371 * actually changed until {@link Camera#setParameters(Camera.Parameters)} 1372 * is called with the changed parameters object. 1373 * 1374 * <p>Different devices may have different camera capabilities, such as 1375 * picture size or flash modes. The application should query the camera 1376 * capabilities before setting parameters. For example, the application 1377 * should call {@link Camera.Parameters#getSupportedColorEffects()} before 1378 * calling {@link Camera.Parameters#setColorEffect(String)}. If the 1379 * camera does not support color effects, 1380 * {@link Camera.Parameters#getSupportedColorEffects()} will return null. 1381 */ 1382 public class Parameters { 1383 // Parameter keys to communicate with the camera driver. 1384 private static final String KEY_PREVIEW_SIZE = "preview-size"; 1385 private static final String KEY_PREVIEW_FORMAT = "preview-format"; 1386 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate"; 1387 private static final String KEY_PREVIEW_FPS_RANGE = "preview-fps-range"; 1388 private static final String KEY_PICTURE_SIZE = "picture-size"; 1389 private static final String KEY_PICTURE_FORMAT = "picture-format"; 1390 private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size"; 1391 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width"; 1392 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height"; 1393 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality"; 1394 private static final String KEY_JPEG_QUALITY = "jpeg-quality"; 1395 private static final String KEY_ROTATION = "rotation"; 1396 private static final String KEY_GPS_LATITUDE = "gps-latitude"; 1397 private static final String KEY_GPS_LONGITUDE = "gps-longitude"; 1398 private static final String KEY_GPS_ALTITUDE = "gps-altitude"; 1399 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp"; 1400 private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method"; 1401 private static final String KEY_WHITE_BALANCE = "whitebalance"; 1402 private static final String KEY_EFFECT = "effect"; 1403 private static final String KEY_ANTIBANDING = "antibanding"; 1404 private static final String KEY_SCENE_MODE = "scene-mode"; 1405 private static final String KEY_FLASH_MODE = "flash-mode"; 1406 private static final String KEY_FOCUS_MODE = "focus-mode"; 1407 private static final String KEY_FOCUS_AREAS = "focus-areas"; 1408 private static final String KEY_MAX_NUM_FOCUS_AREAS = "max-num-focus-areas"; 1409 private static final String KEY_FOCAL_LENGTH = "focal-length"; 1410 private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle"; 1411 private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle"; 1412 private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation"; 1413 private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation"; 1414 private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation"; 1415 private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step"; 1416 private static final String KEY_AUTO_EXPOSURE_LOCK = "auto-exposure-lock"; 1417 private static final String KEY_AUTO_EXPOSURE_LOCK_SUPPORTED = "auto-exposure-lock-supported"; 1418 private static final String KEY_AUTO_WHITEBALANCE_LOCK = "auto-whitebalance-lock"; 1419 private static final String KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED = "auto-whitebalance-lock-supported"; 1420 private static final String KEY_METERING_AREAS = "metering-areas"; 1421 private static final String KEY_MAX_NUM_METERING_AREAS = "max-num-metering-areas"; 1422 private static final String KEY_ZOOM = "zoom"; 1423 private static final String KEY_MAX_ZOOM = "max-zoom"; 1424 private static final String KEY_ZOOM_RATIOS = "zoom-ratios"; 1425 private static final String KEY_ZOOM_SUPPORTED = "zoom-supported"; 1426 private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported"; 1427 private static final String KEY_FOCUS_DISTANCES = "focus-distances"; 1428 private static final String KEY_VIDEO_SIZE = "video-size"; 1429 private static final String KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO = 1430 "preferred-preview-size-for-video"; 1431 private static final String KEY_MAX_NUM_DETECTED_FACES_HW = "max-num-detected-faces-hw"; 1432 private static final String KEY_MAX_NUM_DETECTED_FACES_SW = "max-num-detected-faces-sw"; 1433 private static final String KEY_RECORDING_HINT = "recording-hint"; 1434 1435 // Parameter key suffix for supported values. 1436 private static final String SUPPORTED_VALUES_SUFFIX = "-values"; 1437 1438 private static final String TRUE = "true"; 1439 private static final String FALSE = "false"; 1440 1441 // Values for white balance settings. 1442 public static final String WHITE_BALANCE_AUTO = "auto"; 1443 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent"; 1444 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent"; 1445 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent"; 1446 public static final String WHITE_BALANCE_DAYLIGHT = "daylight"; 1447 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight"; 1448 public static final String WHITE_BALANCE_TWILIGHT = "twilight"; 1449 public static final String WHITE_BALANCE_SHADE = "shade"; 1450 1451 // Values for color effect settings. 1452 public static final String EFFECT_NONE = "none"; 1453 public static final String EFFECT_MONO = "mono"; 1454 public static final String EFFECT_NEGATIVE = "negative"; 1455 public static final String EFFECT_SOLARIZE = "solarize"; 1456 public static final String EFFECT_SEPIA = "sepia"; 1457 public static final String EFFECT_POSTERIZE = "posterize"; 1458 public static final String EFFECT_WHITEBOARD = "whiteboard"; 1459 public static final String EFFECT_BLACKBOARD = "blackboard"; 1460 public static final String EFFECT_AQUA = "aqua"; 1461 1462 // Values for antibanding settings. 1463 public static final String ANTIBANDING_AUTO = "auto"; 1464 public static final String ANTIBANDING_50HZ = "50hz"; 1465 public static final String ANTIBANDING_60HZ = "60hz"; 1466 public static final String ANTIBANDING_OFF = "off"; 1467 1468 // Values for flash mode settings. 1469 /** 1470 * Flash will not be fired. 1471 */ 1472 public static final String FLASH_MODE_OFF = "off"; 1473 1474 /** 1475 * Flash will be fired automatically when required. The flash may be fired 1476 * during preview, auto-focus, or snapshot depending on the driver. 1477 */ 1478 public static final String FLASH_MODE_AUTO = "auto"; 1479 1480 /** 1481 * Flash will always be fired during snapshot. The flash may also be 1482 * fired during preview or auto-focus depending on the driver. 1483 */ 1484 public static final String FLASH_MODE_ON = "on"; 1485 1486 /** 1487 * Flash will be fired in red-eye reduction mode. 1488 */ 1489 public static final String FLASH_MODE_RED_EYE = "red-eye"; 1490 1491 /** 1492 * Constant emission of light during preview, auto-focus and snapshot. 1493 * This can also be used for video recording. 1494 */ 1495 public static final String FLASH_MODE_TORCH = "torch"; 1496 1497 /** 1498 * Scene mode is off. 1499 */ 1500 public static final String SCENE_MODE_AUTO = "auto"; 1501 1502 /** 1503 * Take photos of fast moving objects. Same as {@link 1504 * #SCENE_MODE_SPORTS}. 1505 */ 1506 public static final String SCENE_MODE_ACTION = "action"; 1507 1508 /** 1509 * Take people pictures. 1510 */ 1511 public static final String SCENE_MODE_PORTRAIT = "portrait"; 1512 1513 /** 1514 * Take pictures on distant objects. 1515 */ 1516 public static final String SCENE_MODE_LANDSCAPE = "landscape"; 1517 1518 /** 1519 * Take photos at night. 1520 */ 1521 public static final String SCENE_MODE_NIGHT = "night"; 1522 1523 /** 1524 * Take people pictures at night. 1525 */ 1526 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait"; 1527 1528 /** 1529 * Take photos in a theater. Flash light is off. 1530 */ 1531 public static final String SCENE_MODE_THEATRE = "theatre"; 1532 1533 /** 1534 * Take pictures on the beach. 1535 */ 1536 public static final String SCENE_MODE_BEACH = "beach"; 1537 1538 /** 1539 * Take pictures on the snow. 1540 */ 1541 public static final String SCENE_MODE_SNOW = "snow"; 1542 1543 /** 1544 * Take sunset photos. 1545 */ 1546 public static final String SCENE_MODE_SUNSET = "sunset"; 1547 1548 /** 1549 * Avoid blurry pictures (for example, due to hand shake). 1550 */ 1551 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto"; 1552 1553 /** 1554 * For shooting firework displays. 1555 */ 1556 public static final String SCENE_MODE_FIREWORKS = "fireworks"; 1557 1558 /** 1559 * Take photos of fast moving objects. Same as {@link 1560 * #SCENE_MODE_ACTION}. 1561 */ 1562 public static final String SCENE_MODE_SPORTS = "sports"; 1563 1564 /** 1565 * Take indoor low-light shot. 1566 */ 1567 public static final String SCENE_MODE_PARTY = "party"; 1568 1569 /** 1570 * Capture the naturally warm color of scenes lit by candles. 1571 */ 1572 public static final String SCENE_MODE_CANDLELIGHT = "candlelight"; 1573 1574 /** 1575 * Applications are looking for a barcode. Camera driver will be 1576 * optimized for barcode reading. 1577 */ 1578 public static final String SCENE_MODE_BARCODE = "barcode"; 1579 1580 /** 1581 * Auto-focus mode. Applications should call {@link 1582 * #autoFocus(AutoFocusCallback)} to start the focus in this mode. 1583 */ 1584 public static final String FOCUS_MODE_AUTO = "auto"; 1585 1586 /** 1587 * Focus is set at infinity. Applications should not call 1588 * {@link #autoFocus(AutoFocusCallback)} in this mode. 1589 */ 1590 public static final String FOCUS_MODE_INFINITY = "infinity"; 1591 1592 /** 1593 * Macro (close-up) focus mode. Applications should call 1594 * {@link #autoFocus(AutoFocusCallback)} to start the focus in this 1595 * mode. 1596 */ 1597 public static final String FOCUS_MODE_MACRO = "macro"; 1598 1599 /** 1600 * Focus is fixed. The camera is always in this mode if the focus is not 1601 * adjustable. If the camera has auto-focus, this mode can fix the 1602 * focus, which is usually at hyperfocal distance. Applications should 1603 * not call {@link #autoFocus(AutoFocusCallback)} in this mode. 1604 */ 1605 public static final String FOCUS_MODE_FIXED = "fixed"; 1606 1607 /** 1608 * Extended depth of field (EDOF). Focusing is done digitally and 1609 * continuously. Applications should not call {@link 1610 * #autoFocus(AutoFocusCallback)} in this mode. 1611 */ 1612 public static final String FOCUS_MODE_EDOF = "edof"; 1613 1614 /** 1615 * Continuous auto focus mode intended for video recording. The camera 1616 * continuously tries to focus. This is the best choice for video 1617 * recording because the focus changes smoothly . Applications still can 1618 * call {@link #takePicture(Camera.ShutterCallback, 1619 * Camera.PictureCallback, Camera.PictureCallback)} in this mode but the 1620 * subject may not be in focus. Auto focus starts when the parameter is 1621 * set. Applications should not call {@link 1622 * #autoFocus(AutoFocusCallback)} in this mode. To stop continuous 1623 * focus, applications should change the focus mode to other modes. 1624 */ 1625 public static final String FOCUS_MODE_CONTINUOUS_VIDEO = "continuous-video"; 1626 1627 /** 1628 * Continuous auto focus mode intended for taking pictures. The camera 1629 * continuously tries to focus. The speed of focus change is more 1630 * aggressive than {@link #FOCUS_MODE_CONTINUOUS_VIDEO}. Auto focus 1631 * starts when the parameter is set. If applications call {@link 1632 * #autoFocus(AutoFocusCallback)} in this mode, the focus callback will 1633 * immediately return with a boolean that indicates whether the focus is 1634 * sharp or not. The apps can then decide if they want to take a picture 1635 * immediately or to change the focus mode to auto, and run a full 1636 * autofocus cycle. To stop continuous focus, applications should change 1637 * the focus mode to other modes. 1638 * 1639 * @see #FOCUS_MODE_CONTINUOUS_VIDEO 1640 * @hide 1641 */ 1642 public static final String FOCUS_MODE_CONTINUOUS_PICTURE = "continuous-picture"; 1643 1644 // Indices for focus distance array. 1645 /** 1646 * The array index of near focus distance for use with 1647 * {@link #getFocusDistances(float[])}. 1648 */ 1649 public static final int FOCUS_DISTANCE_NEAR_INDEX = 0; 1650 1651 /** 1652 * The array index of optimal focus distance for use with 1653 * {@link #getFocusDistances(float[])}. 1654 */ 1655 public static final int FOCUS_DISTANCE_OPTIMAL_INDEX = 1; 1656 1657 /** 1658 * The array index of far focus distance for use with 1659 * {@link #getFocusDistances(float[])}. 1660 */ 1661 public static final int FOCUS_DISTANCE_FAR_INDEX = 2; 1662 1663 /** 1664 * The array index of minimum preview fps for use with {@link 1665 * #getPreviewFpsRange(int[])} or {@link 1666 * #getSupportedPreviewFpsRange()}. 1667 */ 1668 public static final int PREVIEW_FPS_MIN_INDEX = 0; 1669 1670 /** 1671 * The array index of maximum preview fps for use with {@link 1672 * #getPreviewFpsRange(int[])} or {@link 1673 * #getSupportedPreviewFpsRange()}. 1674 */ 1675 public static final int PREVIEW_FPS_MAX_INDEX = 1; 1676 1677 // Formats for setPreviewFormat and setPictureFormat. 1678 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp"; 1679 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp"; 1680 private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv"; 1681 private static final String PIXEL_FORMAT_YUV420P = "yuv420p"; 1682 private static final String PIXEL_FORMAT_RGB565 = "rgb565"; 1683 private static final String PIXEL_FORMAT_JPEG = "jpeg"; 1684 private static final String PIXEL_FORMAT_BAYER_RGGB = "bayer-rggb"; 1685 1686 private HashMap<String, String> mMap; 1687 1688 private Parameters() { 1689 mMap = new HashMap<String, String>(); 1690 } 1691 1692 /** 1693 * Writes the current Parameters to the log. 1694 * @hide 1695 * @deprecated 1696 */ 1697 public void dump() { 1698 Log.e(TAG, "dump: size=" + mMap.size()); 1699 for (String k : mMap.keySet()) { 1700 Log.e(TAG, "dump: " + k + "=" + mMap.get(k)); 1701 } 1702 } 1703 1704 /** 1705 * Creates a single string with all the parameters set in 1706 * this Parameters object. 1707 * <p>The {@link #unflatten(String)} method does the reverse.</p> 1708 * 1709 * @return a String with all values from this Parameters object, in 1710 * semi-colon delimited key-value pairs 1711 */ 1712 public String flatten() { 1713 StringBuilder flattened = new StringBuilder(); 1714 for (String k : mMap.keySet()) { 1715 flattened.append(k); 1716 flattened.append("="); 1717 flattened.append(mMap.get(k)); 1718 flattened.append(";"); 1719 } 1720 // chop off the extra semicolon at the end 1721 flattened.deleteCharAt(flattened.length()-1); 1722 return flattened.toString(); 1723 } 1724 1725 /** 1726 * Takes a flattened string of parameters and adds each one to 1727 * this Parameters object. 1728 * <p>The {@link #flatten()} method does the reverse.</p> 1729 * 1730 * @param flattened a String of parameters (key-value paired) that 1731 * are semi-colon delimited 1732 */ 1733 public void unflatten(String flattened) { 1734 mMap.clear(); 1735 1736 StringTokenizer tokenizer = new StringTokenizer(flattened, ";"); 1737 while (tokenizer.hasMoreElements()) { 1738 String kv = tokenizer.nextToken(); 1739 int pos = kv.indexOf('='); 1740 if (pos == -1) { 1741 continue; 1742 } 1743 String k = kv.substring(0, pos); 1744 String v = kv.substring(pos + 1); 1745 mMap.put(k, v); 1746 } 1747 } 1748 1749 public void remove(String key) { 1750 mMap.remove(key); 1751 } 1752 1753 /** 1754 * Sets a String parameter. 1755 * 1756 * @param key the key name for the parameter 1757 * @param value the String value of the parameter 1758 */ 1759 public void set(String key, String value) { 1760 if (key.indexOf('=') != -1 || key.indexOf(';') != -1) { 1761 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ;)"); 1762 return; 1763 } 1764 if (value.indexOf('=') != -1 || value.indexOf(';') != -1) { 1765 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ;)"); 1766 return; 1767 } 1768 1769 mMap.put(key, value); 1770 } 1771 1772 /** 1773 * Sets an integer parameter. 1774 * 1775 * @param key the key name for the parameter 1776 * @param value the int value of the parameter 1777 */ 1778 public void set(String key, int value) { 1779 mMap.put(key, Integer.toString(value)); 1780 } 1781 1782 private void set(String key, List<Area> areas) { 1783 if (areas == null) { 1784 set(key, "(0,0,0,0,0)"); 1785 } else { 1786 StringBuilder buffer = new StringBuilder(); 1787 for (int i = 0; i < areas.size(); i++) { 1788 Area area = areas.get(i); 1789 Rect rect = area.rect; 1790 buffer.append('('); 1791 buffer.append(rect.left); 1792 buffer.append(','); 1793 buffer.append(rect.top); 1794 buffer.append(','); 1795 buffer.append(rect.right); 1796 buffer.append(','); 1797 buffer.append(rect.bottom); 1798 buffer.append(','); 1799 buffer.append(area.weight); 1800 buffer.append(')'); 1801 if (i != areas.size() - 1) buffer.append(','); 1802 } 1803 set(key, buffer.toString()); 1804 } 1805 } 1806 1807 /** 1808 * Returns the value of a String parameter. 1809 * 1810 * @param key the key name for the parameter 1811 * @return the String value of the parameter 1812 */ 1813 public String get(String key) { 1814 return mMap.get(key); 1815 } 1816 1817 /** 1818 * Returns the value of an integer parameter. 1819 * 1820 * @param key the key name for the parameter 1821 * @return the int value of the parameter 1822 */ 1823 public int getInt(String key) { 1824 return Integer.parseInt(mMap.get(key)); 1825 } 1826 1827 /** 1828 * Sets the dimensions for preview pictures. If the preview has already 1829 * started, applications should stop the preview first before changing 1830 * preview size. 1831 * 1832 * The sides of width and height are based on camera orientation. That 1833 * is, the preview size is the size before it is rotated by display 1834 * orientation. So applications need to consider the display orientation 1835 * while setting preview size. For example, suppose the camera supports 1836 * both 480x320 and 320x480 preview sizes. The application wants a 3:2 1837 * preview ratio. If the display orientation is set to 0 or 180, preview 1838 * size should be set to 480x320. If the display orientation is set to 1839 * 90 or 270, preview size should be set to 320x480. The display 1840 * orientation should also be considered while setting picture size and 1841 * thumbnail size. 1842 * 1843 * @param width the width of the pictures, in pixels 1844 * @param height the height of the pictures, in pixels 1845 * @see #setDisplayOrientation(int) 1846 * @see #getCameraInfo(int, CameraInfo) 1847 * @see #setPictureSize(int, int) 1848 * @see #setJpegThumbnailSize(int, int) 1849 */ 1850 public void setPreviewSize(int width, int height) { 1851 String v = Integer.toString(width) + "x" + Integer.toString(height); 1852 set(KEY_PREVIEW_SIZE, v); 1853 } 1854 1855 /** 1856 * Returns the dimensions setting for preview pictures. 1857 * 1858 * @return a Size object with the width and height setting 1859 * for the preview picture 1860 */ 1861 public Size getPreviewSize() { 1862 String pair = get(KEY_PREVIEW_SIZE); 1863 return strToSize(pair); 1864 } 1865 1866 /** 1867 * Gets the supported preview sizes. 1868 * 1869 * @return a list of Size object. This method will always return a list 1870 * with at least one element. 1871 */ 1872 public List<Size> getSupportedPreviewSizes() { 1873 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX); 1874 return splitSize(str); 1875 } 1876 1877 /** 1878 * <p>Gets the supported video frame sizes that can be used by 1879 * MediaRecorder.</p> 1880 * 1881 * <p>If the returned list is not null, the returned list will contain at 1882 * least one Size and one of the sizes in the returned list must be 1883 * passed to MediaRecorder.setVideoSize() for camcorder application if 1884 * camera is used as the video source. In this case, the size of the 1885 * preview can be different from the resolution of the recorded video 1886 * during video recording.</p> 1887 * 1888 * @return a list of Size object if camera has separate preview and 1889 * video output; otherwise, null is returned. 1890 * @see #getPreferredPreviewSizeForVideo() 1891 */ 1892 public List<Size> getSupportedVideoSizes() { 1893 String str = get(KEY_VIDEO_SIZE + SUPPORTED_VALUES_SUFFIX); 1894 return splitSize(str); 1895 } 1896 1897 /** 1898 * Returns the preferred or recommended preview size (width and height) 1899 * in pixels for video recording. Camcorder applications should 1900 * set the preview size to a value that is not larger than the 1901 * preferred preview size. In other words, the product of the width 1902 * and height of the preview size should not be larger than that of 1903 * the preferred preview size. In addition, we recommend to choose a 1904 * preview size that has the same aspect ratio as the resolution of 1905 * video to be recorded. 1906 * 1907 * @return the preferred preview size (width and height) in pixels for 1908 * video recording if getSupportedVideoSizes() does not return 1909 * null; otherwise, null is returned. 1910 * @see #getSupportedVideoSizes() 1911 */ 1912 public Size getPreferredPreviewSizeForVideo() { 1913 String pair = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO); 1914 return strToSize(pair); 1915 } 1916 1917 /** 1918 * <p>Sets the dimensions for EXIF thumbnail in Jpeg picture. If 1919 * applications set both width and height to 0, EXIF will not contain 1920 * thumbnail.</p> 1921 * 1922 * <p>Applications need to consider the display orientation. See {@link 1923 * #setPreviewSize(int,int)} for reference.</p> 1924 * 1925 * @param width the width of the thumbnail, in pixels 1926 * @param height the height of the thumbnail, in pixels 1927 * @see #setPreviewSize(int,int) 1928 */ 1929 public void setJpegThumbnailSize(int width, int height) { 1930 set(KEY_JPEG_THUMBNAIL_WIDTH, width); 1931 set(KEY_JPEG_THUMBNAIL_HEIGHT, height); 1932 } 1933 1934 /** 1935 * Returns the dimensions for EXIF thumbnail in Jpeg picture. 1936 * 1937 * @return a Size object with the height and width setting for the EXIF 1938 * thumbnails 1939 */ 1940 public Size getJpegThumbnailSize() { 1941 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH), 1942 getInt(KEY_JPEG_THUMBNAIL_HEIGHT)); 1943 } 1944 1945 /** 1946 * Gets the supported jpeg thumbnail sizes. 1947 * 1948 * @return a list of Size object. This method will always return a list 1949 * with at least two elements. Size 0,0 (no thumbnail) is always 1950 * supported. 1951 */ 1952 public List<Size> getSupportedJpegThumbnailSizes() { 1953 String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX); 1954 return splitSize(str); 1955 } 1956 1957 /** 1958 * Sets the quality of the EXIF thumbnail in Jpeg picture. 1959 * 1960 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1 1961 * to 100, with 100 being the best. 1962 */ 1963 public void setJpegThumbnailQuality(int quality) { 1964 set(KEY_JPEG_THUMBNAIL_QUALITY, quality); 1965 } 1966 1967 /** 1968 * Returns the quality setting for the EXIF thumbnail in Jpeg picture. 1969 * 1970 * @return the JPEG quality setting of the EXIF thumbnail. 1971 */ 1972 public int getJpegThumbnailQuality() { 1973 return getInt(KEY_JPEG_THUMBNAIL_QUALITY); 1974 } 1975 1976 /** 1977 * Sets Jpeg quality of captured picture. 1978 * 1979 * @param quality the JPEG quality of captured picture. The range is 1 1980 * to 100, with 100 being the best. 1981 */ 1982 public void setJpegQuality(int quality) { 1983 set(KEY_JPEG_QUALITY, quality); 1984 } 1985 1986 /** 1987 * Returns the quality setting for the JPEG picture. 1988 * 1989 * @return the JPEG picture quality setting. 1990 */ 1991 public int getJpegQuality() { 1992 return getInt(KEY_JPEG_QUALITY); 1993 } 1994 1995 /** 1996 * Sets the rate at which preview frames are received. This is the 1997 * target frame rate. The actual frame rate depends on the driver. 1998 * 1999 * @param fps the frame rate (frames per second) 2000 * @deprecated replaced by {@link #setPreviewFpsRange(int,int)} 2001 */ 2002 @Deprecated 2003 public void setPreviewFrameRate(int fps) { 2004 set(KEY_PREVIEW_FRAME_RATE, fps); 2005 } 2006 2007 /** 2008 * Returns the setting for the rate at which preview frames are 2009 * received. This is the target frame rate. The actual frame rate 2010 * depends on the driver. 2011 * 2012 * @return the frame rate setting (frames per second) 2013 * @deprecated replaced by {@link #getPreviewFpsRange(int[])} 2014 */ 2015 @Deprecated 2016 public int getPreviewFrameRate() { 2017 return getInt(KEY_PREVIEW_FRAME_RATE); 2018 } 2019 2020 /** 2021 * Gets the supported preview frame rates. 2022 * 2023 * @return a list of supported preview frame rates. null if preview 2024 * frame rate setting is not supported. 2025 * @deprecated replaced by {@link #getSupportedPreviewFpsRange()} 2026 */ 2027 @Deprecated 2028 public List<Integer> getSupportedPreviewFrameRates() { 2029 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX); 2030 return splitInt(str); 2031 } 2032 2033 /** 2034 * Sets the maximum and maximum preview fps. This controls the rate of 2035 * preview frames received in {@link PreviewCallback}. The minimum and 2036 * maximum preview fps must be one of the elements from {@link 2037 * #getSupportedPreviewFpsRange}. 2038 * 2039 * @param min the minimum preview fps (scaled by 1000). 2040 * @param max the maximum preview fps (scaled by 1000). 2041 * @throws RuntimeException if fps range is invalid. 2042 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback) 2043 * @see #getSupportedPreviewFpsRange() 2044 */ 2045 public void setPreviewFpsRange(int min, int max) { 2046 set(KEY_PREVIEW_FPS_RANGE, "" + min + "," + max); 2047 } 2048 2049 /** 2050 * Returns the current minimum and maximum preview fps. The values are 2051 * one of the elements returned by {@link #getSupportedPreviewFpsRange}. 2052 * 2053 * @return range the minimum and maximum preview fps (scaled by 1000). 2054 * @see #PREVIEW_FPS_MIN_INDEX 2055 * @see #PREVIEW_FPS_MAX_INDEX 2056 * @see #getSupportedPreviewFpsRange() 2057 */ 2058 public void getPreviewFpsRange(int[] range) { 2059 if (range == null || range.length != 2) { 2060 throw new IllegalArgumentException( 2061 "range must be an array with two elements."); 2062 } 2063 splitInt(get(KEY_PREVIEW_FPS_RANGE), range); 2064 } 2065 2066 /** 2067 * Gets the supported preview fps (frame-per-second) ranges. Each range 2068 * contains a minimum fps and maximum fps. If minimum fps equals to 2069 * maximum fps, the camera outputs frames in fixed frame rate. If not, 2070 * the camera outputs frames in auto frame rate. The actual frame rate 2071 * fluctuates between the minimum and the maximum. The values are 2072 * multiplied by 1000 and represented in integers. For example, if frame 2073 * rate is 26.623 frames per second, the value is 26623. 2074 * 2075 * @return a list of supported preview fps ranges. This method returns a 2076 * list with at least one element. Every element is an int array 2077 * of two values - minimum fps and maximum fps. The list is 2078 * sorted from small to large (first by maximum fps and then 2079 * minimum fps). 2080 * @see #PREVIEW_FPS_MIN_INDEX 2081 * @see #PREVIEW_FPS_MAX_INDEX 2082 */ 2083 public List<int[]> getSupportedPreviewFpsRange() { 2084 String str = get(KEY_PREVIEW_FPS_RANGE + SUPPORTED_VALUES_SUFFIX); 2085 return splitRange(str); 2086 } 2087 2088 /** 2089 * Sets the image format for preview pictures. 2090 * <p>If this is never called, the default format will be 2091 * {@link android.graphics.ImageFormat#NV21}, which 2092 * uses the NV21 encoding format.</p> 2093 * 2094 * @param pixel_format the desired preview picture format, defined 2095 * by one of the {@link android.graphics.ImageFormat} constants. 2096 * (E.g., <var>ImageFormat.NV21</var> (default), 2097 * <var>ImageFormat.RGB_565</var>, or 2098 * <var>ImageFormat.JPEG</var>) 2099 * @see android.graphics.ImageFormat 2100 */ 2101 public void setPreviewFormat(int pixel_format) { 2102 String s = cameraFormatForPixelFormat(pixel_format); 2103 if (s == null) { 2104 throw new IllegalArgumentException( 2105 "Invalid pixel_format=" + pixel_format); 2106 } 2107 2108 set(KEY_PREVIEW_FORMAT, s); 2109 } 2110 2111 /** 2112 * Returns the image format for preview frames got from 2113 * {@link PreviewCallback}. 2114 * 2115 * @return the preview format. 2116 * @see android.graphics.ImageFormat 2117 */ 2118 public int getPreviewFormat() { 2119 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT)); 2120 } 2121 2122 /** 2123 * Gets the supported preview formats. {@link android.graphics.ImageFormat#NV21} 2124 * is always supported. {@link android.graphics.ImageFormat#YV12} 2125 * is always supported since API level 12. 2126 * 2127 * @return a list of supported preview formats. This method will always 2128 * return a list with at least one element. 2129 * @see android.graphics.ImageFormat 2130 */ 2131 public List<Integer> getSupportedPreviewFormats() { 2132 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX); 2133 ArrayList<Integer> formats = new ArrayList<Integer>(); 2134 for (String s : split(str)) { 2135 int f = pixelFormatForCameraFormat(s); 2136 if (f == ImageFormat.UNKNOWN) continue; 2137 formats.add(f); 2138 } 2139 return formats; 2140 } 2141 2142 /** 2143 * <p>Sets the dimensions for pictures.</p> 2144 * 2145 * <p>Applications need to consider the display orientation. See {@link 2146 * #setPreviewSize(int,int)} for reference.</p> 2147 * 2148 * @param width the width for pictures, in pixels 2149 * @param height the height for pictures, in pixels 2150 * @see #setPreviewSize(int,int) 2151 * 2152 */ 2153 public void setPictureSize(int width, int height) { 2154 String v = Integer.toString(width) + "x" + Integer.toString(height); 2155 set(KEY_PICTURE_SIZE, v); 2156 } 2157 2158 /** 2159 * Returns the dimension setting for pictures. 2160 * 2161 * @return a Size object with the height and width setting 2162 * for pictures 2163 */ 2164 public Size getPictureSize() { 2165 String pair = get(KEY_PICTURE_SIZE); 2166 return strToSize(pair); 2167 } 2168 2169 /** 2170 * Gets the supported picture sizes. 2171 * 2172 * @return a list of supported picture sizes. This method will always 2173 * return a list with at least one element. 2174 */ 2175 public List<Size> getSupportedPictureSizes() { 2176 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX); 2177 return splitSize(str); 2178 } 2179 2180 /** 2181 * Sets the image format for pictures. 2182 * 2183 * @param pixel_format the desired picture format 2184 * (<var>ImageFormat.NV21</var>, 2185 * <var>ImageFormat.RGB_565</var>, or 2186 * <var>ImageFormat.JPEG</var>) 2187 * @see android.graphics.ImageFormat 2188 */ 2189 public void setPictureFormat(int pixel_format) { 2190 String s = cameraFormatForPixelFormat(pixel_format); 2191 if (s == null) { 2192 throw new IllegalArgumentException( 2193 "Invalid pixel_format=" + pixel_format); 2194 } 2195 2196 set(KEY_PICTURE_FORMAT, s); 2197 } 2198 2199 /** 2200 * Returns the image format for pictures. 2201 * 2202 * @return the picture format 2203 * @see android.graphics.ImageFormat 2204 */ 2205 public int getPictureFormat() { 2206 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT)); 2207 } 2208 2209 /** 2210 * Gets the supported picture formats. 2211 * 2212 * @return supported picture formats. This method will always return a 2213 * list with at least one element. 2214 * @see android.graphics.ImageFormat 2215 */ 2216 public List<Integer> getSupportedPictureFormats() { 2217 String str = get(KEY_PICTURE_FORMAT + SUPPORTED_VALUES_SUFFIX); 2218 ArrayList<Integer> formats = new ArrayList<Integer>(); 2219 for (String s : split(str)) { 2220 int f = pixelFormatForCameraFormat(s); 2221 if (f == ImageFormat.UNKNOWN) continue; 2222 formats.add(f); 2223 } 2224 return formats; 2225 } 2226 2227 private String cameraFormatForPixelFormat(int pixel_format) { 2228 switch(pixel_format) { 2229 case ImageFormat.NV16: return PIXEL_FORMAT_YUV422SP; 2230 case ImageFormat.NV21: return PIXEL_FORMAT_YUV420SP; 2231 case ImageFormat.YUY2: return PIXEL_FORMAT_YUV422I; 2232 case ImageFormat.YV12: return PIXEL_FORMAT_YUV420P; 2233 case ImageFormat.RGB_565: return PIXEL_FORMAT_RGB565; 2234 case ImageFormat.JPEG: return PIXEL_FORMAT_JPEG; 2235 case ImageFormat.BAYER_RGGB: return PIXEL_FORMAT_BAYER_RGGB; 2236 default: return null; 2237 } 2238 } 2239 2240 private int pixelFormatForCameraFormat(String format) { 2241 if (format == null) 2242 return ImageFormat.UNKNOWN; 2243 2244 if (format.equals(PIXEL_FORMAT_YUV422SP)) 2245 return ImageFormat.NV16; 2246 2247 if (format.equals(PIXEL_FORMAT_YUV420SP)) 2248 return ImageFormat.NV21; 2249 2250 if (format.equals(PIXEL_FORMAT_YUV422I)) 2251 return ImageFormat.YUY2; 2252 2253 if (format.equals(PIXEL_FORMAT_YUV420P)) 2254 return ImageFormat.YV12; 2255 2256 if (format.equals(PIXEL_FORMAT_RGB565)) 2257 return ImageFormat.RGB_565; 2258 2259 if (format.equals(PIXEL_FORMAT_JPEG)) 2260 return ImageFormat.JPEG; 2261 2262 return ImageFormat.UNKNOWN; 2263 } 2264 2265 /** 2266 * Sets the rotation angle in degrees relative to the orientation of 2267 * the camera. This affects the pictures returned from JPEG {@link 2268 * PictureCallback}. The camera driver may set orientation in the 2269 * EXIF header without rotating the picture. Or the driver may rotate 2270 * the picture and the EXIF thumbnail. If the Jpeg picture is rotated, 2271 * the orientation in the EXIF header will be missing or 1 (row #0 is 2272 * top and column #0 is left side). 2273 * 2274 * <p>If applications want to rotate the picture to match the orientation 2275 * of what users see, apps should use {@link 2276 * android.view.OrientationEventListener} and {@link CameraInfo}. 2277 * The value from OrientationEventListener is relative to the natural 2278 * orientation of the device. CameraInfo.orientation is the angle 2279 * between camera orientation and natural device orientation. The sum 2280 * of the two is the rotation angle for back-facing camera. The 2281 * difference of the two is the rotation angle for front-facing camera. 2282 * Note that the JPEG pictures of front-facing cameras are not mirrored 2283 * as in preview display. 2284 * 2285 * <p>For example, suppose the natural orientation of the device is 2286 * portrait. The device is rotated 270 degrees clockwise, so the device 2287 * orientation is 270. Suppose a back-facing camera sensor is mounted in 2288 * landscape and the top side of the camera sensor is aligned with the 2289 * right edge of the display in natural orientation. So the camera 2290 * orientation is 90. The rotation should be set to 0 (270 + 90). 2291 * 2292 * <p>The reference code is as follows. 2293 * 2294 * <pre> 2295 * public void public void onOrientationChanged(int orientation) { 2296 * if (orientation == ORIENTATION_UNKNOWN) return; 2297 * android.hardware.Camera.CameraInfo info = 2298 * new android.hardware.Camera.CameraInfo(); 2299 * android.hardware.Camera.getCameraInfo(cameraId, info); 2300 * orientation = (orientation + 45) / 90 * 90; 2301 * int rotation = 0; 2302 * if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 2303 * rotation = (info.orientation - orientation + 360) % 360; 2304 * } else { // back-facing camera 2305 * rotation = (info.orientation + orientation) % 360; 2306 * } 2307 * mParameters.setRotation(rotation); 2308 * } 2309 * </pre> 2310 * 2311 * @param rotation The rotation angle in degrees relative to the 2312 * orientation of the camera. Rotation can only be 0, 2313 * 90, 180 or 270. 2314 * @throws IllegalArgumentException if rotation value is invalid. 2315 * @see android.view.OrientationEventListener 2316 * @see #getCameraInfo(int, CameraInfo) 2317 */ 2318 public void setRotation(int rotation) { 2319 if (rotation == 0 || rotation == 90 || rotation == 180 2320 || rotation == 270) { 2321 set(KEY_ROTATION, Integer.toString(rotation)); 2322 } else { 2323 throw new IllegalArgumentException( 2324 "Invalid rotation=" + rotation); 2325 } 2326 } 2327 2328 /** 2329 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF 2330 * header. 2331 * 2332 * @param latitude GPS latitude coordinate. 2333 */ 2334 public void setGpsLatitude(double latitude) { 2335 set(KEY_GPS_LATITUDE, Double.toString(latitude)); 2336 } 2337 2338 /** 2339 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF 2340 * header. 2341 * 2342 * @param longitude GPS longitude coordinate. 2343 */ 2344 public void setGpsLongitude(double longitude) { 2345 set(KEY_GPS_LONGITUDE, Double.toString(longitude)); 2346 } 2347 2348 /** 2349 * Sets GPS altitude. This will be stored in JPEG EXIF header. 2350 * 2351 * @param altitude GPS altitude in meters. 2352 */ 2353 public void setGpsAltitude(double altitude) { 2354 set(KEY_GPS_ALTITUDE, Double.toString(altitude)); 2355 } 2356 2357 /** 2358 * Sets GPS timestamp. This will be stored in JPEG EXIF header. 2359 * 2360 * @param timestamp GPS timestamp (UTC in seconds since January 1, 2361 * 1970). 2362 */ 2363 public void setGpsTimestamp(long timestamp) { 2364 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp)); 2365 } 2366 2367 /** 2368 * Sets GPS processing method. It will store up to 32 characters 2369 * in JPEG EXIF header. 2370 * 2371 * @param processing_method The processing method to get this location. 2372 */ 2373 public void setGpsProcessingMethod(String processing_method) { 2374 set(KEY_GPS_PROCESSING_METHOD, processing_method); 2375 } 2376 2377 /** 2378 * Removes GPS latitude, longitude, altitude, and timestamp from the 2379 * parameters. 2380 */ 2381 public void removeGpsData() { 2382 remove(KEY_GPS_LATITUDE); 2383 remove(KEY_GPS_LONGITUDE); 2384 remove(KEY_GPS_ALTITUDE); 2385 remove(KEY_GPS_TIMESTAMP); 2386 remove(KEY_GPS_PROCESSING_METHOD); 2387 } 2388 2389 /** 2390 * Gets the current white balance setting. 2391 * 2392 * @return current white balance. null if white balance setting is not 2393 * supported. 2394 * @see #WHITE_BALANCE_AUTO 2395 * @see #WHITE_BALANCE_INCANDESCENT 2396 * @see #WHITE_BALANCE_FLUORESCENT 2397 * @see #WHITE_BALANCE_WARM_FLUORESCENT 2398 * @see #WHITE_BALANCE_DAYLIGHT 2399 * @see #WHITE_BALANCE_CLOUDY_DAYLIGHT 2400 * @see #WHITE_BALANCE_TWILIGHT 2401 * @see #WHITE_BALANCE_SHADE 2402 * 2403 */ 2404 public String getWhiteBalance() { 2405 return get(KEY_WHITE_BALANCE); 2406 } 2407 2408 /** 2409 * Sets the white balance. Changing the setting will release the 2410 * auto-white balance lock. 2411 * 2412 * @param value new white balance. 2413 * @see #getWhiteBalance() 2414 * @see #setAutoWhiteBalanceLock() 2415 */ 2416 public void setWhiteBalance(String value) { 2417 set(KEY_WHITE_BALANCE, value); 2418 set(KEY_AUTO_WHITEBALANCE_LOCK, FALSE); 2419 } 2420 2421 /** 2422 * Gets the supported white balance. 2423 * 2424 * @return a list of supported white balance. null if white balance 2425 * setting is not supported. 2426 * @see #getWhiteBalance() 2427 */ 2428 public List<String> getSupportedWhiteBalance() { 2429 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX); 2430 return split(str); 2431 } 2432 2433 /** 2434 * Gets the current color effect setting. 2435 * 2436 * @return current color effect. null if color effect 2437 * setting is not supported. 2438 * @see #EFFECT_NONE 2439 * @see #EFFECT_MONO 2440 * @see #EFFECT_NEGATIVE 2441 * @see #EFFECT_SOLARIZE 2442 * @see #EFFECT_SEPIA 2443 * @see #EFFECT_POSTERIZE 2444 * @see #EFFECT_WHITEBOARD 2445 * @see #EFFECT_BLACKBOARD 2446 * @see #EFFECT_AQUA 2447 */ 2448 public String getColorEffect() { 2449 return get(KEY_EFFECT); 2450 } 2451 2452 /** 2453 * Sets the current color effect setting. 2454 * 2455 * @param value new color effect. 2456 * @see #getColorEffect() 2457 */ 2458 public void setColorEffect(String value) { 2459 set(KEY_EFFECT, value); 2460 } 2461 2462 /** 2463 * Gets the supported color effects. 2464 * 2465 * @return a list of supported color effects. null if color effect 2466 * setting is not supported. 2467 * @see #getColorEffect() 2468 */ 2469 public List<String> getSupportedColorEffects() { 2470 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX); 2471 return split(str); 2472 } 2473 2474 2475 /** 2476 * Gets the current antibanding setting. 2477 * 2478 * @return current antibanding. null if antibanding setting is not 2479 * supported. 2480 * @see #ANTIBANDING_AUTO 2481 * @see #ANTIBANDING_50HZ 2482 * @see #ANTIBANDING_60HZ 2483 * @see #ANTIBANDING_OFF 2484 */ 2485 public String getAntibanding() { 2486 return get(KEY_ANTIBANDING); 2487 } 2488 2489 /** 2490 * Sets the antibanding. 2491 * 2492 * @param antibanding new antibanding value. 2493 * @see #getAntibanding() 2494 */ 2495 public void setAntibanding(String antibanding) { 2496 set(KEY_ANTIBANDING, antibanding); 2497 } 2498 2499 /** 2500 * Gets the supported antibanding values. 2501 * 2502 * @return a list of supported antibanding values. null if antibanding 2503 * setting is not supported. 2504 * @see #getAntibanding() 2505 */ 2506 public List<String> getSupportedAntibanding() { 2507 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX); 2508 return split(str); 2509 } 2510 2511 /** 2512 * Gets the current scene mode setting. 2513 * 2514 * @return one of SCENE_MODE_XXX string constant. null if scene mode 2515 * setting is not supported. 2516 * @see #SCENE_MODE_AUTO 2517 * @see #SCENE_MODE_ACTION 2518 * @see #SCENE_MODE_PORTRAIT 2519 * @see #SCENE_MODE_LANDSCAPE 2520 * @see #SCENE_MODE_NIGHT 2521 * @see #SCENE_MODE_NIGHT_PORTRAIT 2522 * @see #SCENE_MODE_THEATRE 2523 * @see #SCENE_MODE_BEACH 2524 * @see #SCENE_MODE_SNOW 2525 * @see #SCENE_MODE_SUNSET 2526 * @see #SCENE_MODE_STEADYPHOTO 2527 * @see #SCENE_MODE_FIREWORKS 2528 * @see #SCENE_MODE_SPORTS 2529 * @see #SCENE_MODE_PARTY 2530 * @see #SCENE_MODE_CANDLELIGHT 2531 */ 2532 public String getSceneMode() { 2533 return get(KEY_SCENE_MODE); 2534 } 2535 2536 /** 2537 * Sets the scene mode. Changing scene mode may override other 2538 * parameters (such as flash mode, focus mode, white balance). For 2539 * example, suppose originally flash mode is on and supported flash 2540 * modes are on/off. In night scene mode, both flash mode and supported 2541 * flash mode may be changed to off. After setting scene mode, 2542 * applications should call getParameters to know if some parameters are 2543 * changed. 2544 * 2545 * @param value scene mode. 2546 * @see #getSceneMode() 2547 */ 2548 public void setSceneMode(String value) { 2549 set(KEY_SCENE_MODE, value); 2550 } 2551 2552 /** 2553 * Gets the supported scene modes. 2554 * 2555 * @return a list of supported scene modes. null if scene mode setting 2556 * is not supported. 2557 * @see #getSceneMode() 2558 */ 2559 public List<String> getSupportedSceneModes() { 2560 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX); 2561 return split(str); 2562 } 2563 2564 /** 2565 * Gets the current flash mode setting. 2566 * 2567 * @return current flash mode. null if flash mode setting is not 2568 * supported. 2569 * @see #FLASH_MODE_OFF 2570 * @see #FLASH_MODE_AUTO 2571 * @see #FLASH_MODE_ON 2572 * @see #FLASH_MODE_RED_EYE 2573 * @see #FLASH_MODE_TORCH 2574 */ 2575 public String getFlashMode() { 2576 return get(KEY_FLASH_MODE); 2577 } 2578 2579 /** 2580 * Sets the flash mode. 2581 * 2582 * @param value flash mode. 2583 * @see #getFlashMode() 2584 */ 2585 public void setFlashMode(String value) { 2586 set(KEY_FLASH_MODE, value); 2587 } 2588 2589 /** 2590 * Gets the supported flash modes. 2591 * 2592 * @return a list of supported flash modes. null if flash mode setting 2593 * is not supported. 2594 * @see #getFlashMode() 2595 */ 2596 public List<String> getSupportedFlashModes() { 2597 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX); 2598 return split(str); 2599 } 2600 2601 /** 2602 * Gets the current focus mode setting. 2603 * 2604 * @return current focus mode. This method will always return a non-null 2605 * value. Applications should call {@link 2606 * #autoFocus(AutoFocusCallback)} to start the focus if focus 2607 * mode is FOCUS_MODE_AUTO or FOCUS_MODE_MACRO. 2608 * @see #FOCUS_MODE_AUTO 2609 * @see #FOCUS_MODE_INFINITY 2610 * @see #FOCUS_MODE_MACRO 2611 * @see #FOCUS_MODE_FIXED 2612 * @see #FOCUS_MODE_EDOF 2613 * @see #FOCUS_MODE_CONTINUOUS_VIDEO 2614 */ 2615 public String getFocusMode() { 2616 return get(KEY_FOCUS_MODE); 2617 } 2618 2619 /** 2620 * Sets the focus mode. 2621 * 2622 * @param value focus mode. 2623 * @see #getFocusMode() 2624 */ 2625 public void setFocusMode(String value) { 2626 set(KEY_FOCUS_MODE, value); 2627 } 2628 2629 /** 2630 * Gets the supported focus modes. 2631 * 2632 * @return a list of supported focus modes. This method will always 2633 * return a list with at least one element. 2634 * @see #getFocusMode() 2635 */ 2636 public List<String> getSupportedFocusModes() { 2637 String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX); 2638 return split(str); 2639 } 2640 2641 /** 2642 * Gets the focal length (in millimeter) of the camera. 2643 * 2644 * @return the focal length. This method will always return a valid 2645 * value. 2646 */ 2647 public float getFocalLength() { 2648 return Float.parseFloat(get(KEY_FOCAL_LENGTH)); 2649 } 2650 2651 /** 2652 * Gets the horizontal angle of view in degrees. 2653 * 2654 * @return horizontal angle of view. This method will always return a 2655 * valid value. 2656 */ 2657 public float getHorizontalViewAngle() { 2658 return Float.parseFloat(get(KEY_HORIZONTAL_VIEW_ANGLE)); 2659 } 2660 2661 /** 2662 * Gets the vertical angle of view in degrees. 2663 * 2664 * @return vertical angle of view. This method will always return a 2665 * valid value. 2666 */ 2667 public float getVerticalViewAngle() { 2668 return Float.parseFloat(get(KEY_VERTICAL_VIEW_ANGLE)); 2669 } 2670 2671 /** 2672 * Gets the current exposure compensation index. 2673 * 2674 * @return current exposure compensation index. The range is {@link 2675 * #getMinExposureCompensation} to {@link 2676 * #getMaxExposureCompensation}. 0 means exposure is not 2677 * adjusted. 2678 */ 2679 public int getExposureCompensation() { 2680 return getInt(KEY_EXPOSURE_COMPENSATION, 0); 2681 } 2682 2683 /** 2684 * Sets the exposure compensation index. 2685 * 2686 * @param value exposure compensation index. The valid value range is 2687 * from {@link #getMinExposureCompensation} (inclusive) to {@link 2688 * #getMaxExposureCompensation} (inclusive). 0 means exposure is 2689 * not adjusted. Application should call 2690 * getMinExposureCompensation and getMaxExposureCompensation to 2691 * know if exposure compensation is supported. 2692 */ 2693 public void setExposureCompensation(int value) { 2694 set(KEY_EXPOSURE_COMPENSATION, value); 2695 } 2696 2697 /** 2698 * Gets the maximum exposure compensation index. 2699 * 2700 * @return maximum exposure compensation index (>=0). If both this 2701 * method and {@link #getMinExposureCompensation} return 0, 2702 * exposure compensation is not supported. 2703 */ 2704 public int getMaxExposureCompensation() { 2705 return getInt(KEY_MAX_EXPOSURE_COMPENSATION, 0); 2706 } 2707 2708 /** 2709 * Gets the minimum exposure compensation index. 2710 * 2711 * @return minimum exposure compensation index (<=0). If both this 2712 * method and {@link #getMaxExposureCompensation} return 0, 2713 * exposure compensation is not supported. 2714 */ 2715 public int getMinExposureCompensation() { 2716 return getInt(KEY_MIN_EXPOSURE_COMPENSATION, 0); 2717 } 2718 2719 /** 2720 * Gets the exposure compensation step. 2721 * 2722 * @return exposure compensation step. Applications can get EV by 2723 * multiplying the exposure compensation index and step. Ex: if 2724 * exposure compensation index is -6 and step is 0.333333333, EV 2725 * is -2. 2726 */ 2727 public float getExposureCompensationStep() { 2728 return getFloat(KEY_EXPOSURE_COMPENSATION_STEP, 0); 2729 } 2730 2731 /** 2732 * <p>Sets the auto-exposure lock state. Applications should check 2733 * {@link #isAutoExposureLockSupported} before using this method.</p> 2734 * 2735 * <p>If set to true, the camera auto-exposure routine will immediately 2736 * pause until the lock is set to false. Exposure compensation settings 2737 * changes will still take effect while auto-exposure is locked.</p> 2738 * 2739 * <p>If auto-exposure is already locked, setting this to true again has 2740 * no effect (the driver will not recalculate exposure values).</p> 2741 * 2742 * <p>Stopping preview with {@link #stopPreview()}, or triggering still 2743 * image capture with {@link #takePicture(Camera.ShutterCallback, 2744 * Camera.PictureCallback, Camera.PictureCallback)}, will automatically 2745 * set the lock to false. However, the lock can be re-enabled before 2746 * preview is re-started to keep the same AE parameters.</p> 2747 * 2748 * <p>Exposure compensation, in conjunction with re-enabling the AE and 2749 * AWB locks after each still capture, can be used to capture an 2750 * exposure-bracketed burst of images, for example.</p> 2751 * 2752 * <p>Auto-exposure state, including the lock state, will not be 2753 * maintained after camera {@link #release()} is called. Locking 2754 * auto-exposure after {@link #open()} but before the first call to 2755 * {@link #startPreview()} will not allow the auto-exposure routine to 2756 * run at all, and may result in severely over- or under-exposed 2757 * images.</p> 2758 * 2759 * <p>The driver may also independently lock auto-exposure after 2760 * auto-focus completes. If this is undesirable, be sure to always set 2761 * the auto-exposure lock to false after the 2762 * {@link AutoFocusCallback#onAutoFocus(boolean, Camera)} callback is 2763 * received. The {@link #getAutoExposureLock()} method can be used after 2764 * the callback to determine if the camera has locked auto-exposure 2765 * independently.</p> 2766 * 2767 * @param toggle new state of the auto-exposure lock. True means that 2768 * auto-exposure is locked, false means that the auto-exposure 2769 * routine is free to run normally. 2770 * 2771 * @see #getAutoExposureLock() 2772 */ 2773 public void setAutoExposureLock(boolean toggle) { 2774 set(KEY_AUTO_EXPOSURE_LOCK, toggle ? TRUE : FALSE); 2775 } 2776 2777 /** 2778 * Gets the state of the auto-exposure lock. Applications should check 2779 * {@link #isAutoExposureLockSupported} before using this method. See 2780 * {@link #setAutoExposureLock} for details about the lock. 2781 * 2782 * @return State of the auto-exposure lock. Returns true if 2783 * auto-exposure is currently locked, and false otherwise. The 2784 * auto-exposure lock may be independently enabled by the camera 2785 * subsystem when auto-focus has completed. This method can be 2786 * used after the {@link AutoFocusCallback#onAutoFocus(boolean, 2787 * Camera)} callback to determine if the camera has locked AE. 2788 * 2789 * @see #setAutoExposureLock(boolean) 2790 * 2791 */ 2792 public boolean getAutoExposureLock() { 2793 String str = get(KEY_AUTO_EXPOSURE_LOCK); 2794 return TRUE.equals(str); 2795 } 2796 2797 /** 2798 * Returns true if auto-exposure locking is supported. Applications 2799 * should call this before trying to lock auto-exposure. See 2800 * {@link #setAutoExposureLock} for details about the lock. 2801 * 2802 * @return true if auto-exposure lock is supported. 2803 * @see #setAutoExposureLock(boolean) 2804 * 2805 */ 2806 public boolean isAutoExposureLockSupported() { 2807 String str = get(KEY_AUTO_EXPOSURE_LOCK_SUPPORTED); 2808 return TRUE.equals(str); 2809 } 2810 2811 /** 2812 * <p>Sets the auto-white balance lock state. Applications should check 2813 * {@link #isAutoWhiteBalanceLockSupported} before using this 2814 * method.</p> 2815 * 2816 * <p>If set to true, the camera auto-white balance routine will 2817 * immediately pause until the lock is set to false.</p> 2818 * 2819 * <p>If auto-white balance is already locked, setting this to true 2820 * again has no effect (the driver will not recalculate white balance 2821 * values).</p> 2822 * 2823 * <p>Stopping preview with {@link #stopPreview()}, or triggering still 2824 * image capture with {@link #takePicture(Camera.ShutterCallback, 2825 * Camera.PictureCallback, Camera.PictureCallback)}, will automatically 2826 * set the lock to false. However, the lock can be re-enabled before 2827 * preview is re-started to keep the same white balance parameters.</p> 2828 * 2829 * <p> Changing the white balance mode with {@link #setWhiteBalance} 2830 * will release the auto-white balance lock if it is set.</p> 2831 * 2832 * <p>Exposure compensation, in conjunction with re-enabling the AE and 2833 * AWB locks after each still capture, can be used to capture an 2834 * exposure-bracketed burst of images, for example. Auto-white balance 2835 * state, including the lock state, will not be maintained after camera 2836 * {@link #release()} is called. Locking auto-white balance after 2837 * {@link #open()} but before the first call to {@link #startPreview()} 2838 * will not allow the auto-white balance routine to run at all, and may 2839 * result in severely incorrect color in captured images.</p> 2840 * 2841 * <p>The driver may also independently lock auto-white balance after 2842 * auto-focus completes. If this is undesirable, be sure to always set 2843 * the auto-white balance lock to false after the 2844 * {@link AutoFocusCallback#onAutoFocus(boolean, Camera)} callback is 2845 * received. The {@link #getAutoWhiteBalanceLock()} method can be used 2846 * after the callback to determine if the camera has locked auto-white 2847 * balance independently.</p> 2848 * 2849 * @param toggle new state of the auto-white balance lock. True means 2850 * that auto-white balance is locked, false means that the 2851 * auto-white balance routine is free to run normally. 2852 * 2853 * @see #getAutoWhiteBalanceLock() 2854 * @see #setWhiteBalance(String) 2855 */ 2856 public void setAutoWhiteBalanceLock(boolean toggle) { 2857 set(KEY_AUTO_WHITEBALANCE_LOCK, toggle ? TRUE : FALSE); 2858 } 2859 2860 /** 2861 * Gets the state of the auto-white balance lock. Applications should 2862 * check {@link #isAutoWhiteBalanceLockSupported} before using this 2863 * method. See {@link #setAutoWhiteBalanceLock} for details about the 2864 * lock. 2865 * 2866 * @return State of the auto-white balance lock. Returns true if 2867 * auto-white balance is currently locked, and false 2868 * otherwise. The auto-white balance lock may be independently 2869 * enabled by the camera subsystem when auto-focus has 2870 * completed. This method can be used after the 2871 * {@link AutoFocusCallback#onAutoFocus(boolean, Camera)} 2872 * callback to determine if the camera has locked AWB. 2873 * 2874 * @see #setAutoWhiteBalanceLock(boolean) 2875 * 2876 */ 2877 public boolean getAutoWhiteBalanceLock() { 2878 String str = get(KEY_AUTO_WHITEBALANCE_LOCK); 2879 return TRUE.equals(str); 2880 } 2881 2882 /** 2883 * Returns true if auto-white balance locking is supported. Applications 2884 * should call this before trying to lock auto-white balance. See 2885 * {@link #setAutoWhiteBalanceLock} for details about the lock. 2886 * 2887 * @return true if auto-white balance lock is supported. 2888 * @see #setAutoWhiteBalanceLock(boolean) 2889 * 2890 */ 2891 public boolean isAutoWhiteBalanceLockSupported() { 2892 String str = get(KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED); 2893 return TRUE.equals(str); 2894 } 2895 2896 /** 2897 * Gets current zoom value. This also works when smooth zoom is in 2898 * progress. Applications should check {@link #isZoomSupported} before 2899 * using this method. 2900 * 2901 * @return the current zoom value. The range is 0 to {@link 2902 * #getMaxZoom}. 0 means the camera is not zoomed. 2903 */ 2904 public int getZoom() { 2905 return getInt(KEY_ZOOM, 0); 2906 } 2907 2908 /** 2909 * Sets current zoom value. If the camera is zoomed (value > 0), the 2910 * actual picture size may be smaller than picture size setting. 2911 * Applications can check the actual picture size after picture is 2912 * returned from {@link PictureCallback}. The preview size remains the 2913 * same in zoom. Applications should check {@link #isZoomSupported} 2914 * before using this method. 2915 * 2916 * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}. 2917 */ 2918 public void setZoom(int value) { 2919 set(KEY_ZOOM, value); 2920 } 2921 2922 /** 2923 * Returns true if zoom is supported. Applications should call this 2924 * before using other zoom methods. 2925 * 2926 * @return true if zoom is supported. 2927 */ 2928 public boolean isZoomSupported() { 2929 String str = get(KEY_ZOOM_SUPPORTED); 2930 return TRUE.equals(str); 2931 } 2932 2933 /** 2934 * Gets the maximum zoom value allowed for snapshot. This is the maximum 2935 * value that applications can set to {@link #setZoom(int)}. 2936 * Applications should call {@link #isZoomSupported} before using this 2937 * method. This value may change in different preview size. Applications 2938 * should call this again after setting preview size. 2939 * 2940 * @return the maximum zoom value supported by the camera. 2941 */ 2942 public int getMaxZoom() { 2943 return getInt(KEY_MAX_ZOOM, 0); 2944 } 2945 2946 /** 2947 * Gets the zoom ratios of all zoom values. Applications should check 2948 * {@link #isZoomSupported} before using this method. 2949 * 2950 * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is 2951 * returned as 320. The number of elements is {@link 2952 * #getMaxZoom} + 1. The list is sorted from small to large. The 2953 * first element is always 100. The last element is the zoom 2954 * ratio of the maximum zoom value. 2955 */ 2956 public List<Integer> getZoomRatios() { 2957 return splitInt(get(KEY_ZOOM_RATIOS)); 2958 } 2959 2960 /** 2961 * Returns true if smooth zoom is supported. Applications should call 2962 * this before using other smooth zoom methods. 2963 * 2964 * @return true if smooth zoom is supported. 2965 */ 2966 public boolean isSmoothZoomSupported() { 2967 String str = get(KEY_SMOOTH_ZOOM_SUPPORTED); 2968 return TRUE.equals(str); 2969 } 2970 2971 /** 2972 * <p>Gets the distances from the camera to where an object appears to be 2973 * in focus. The object is sharpest at the optimal focus distance. The 2974 * depth of field is the far focus distance minus near focus distance.</p> 2975 * 2976 * <p>Focus distances may change after calling {@link 2977 * #autoFocus(AutoFocusCallback)}, {@link #cancelAutoFocus}, or {@link 2978 * #startPreview()}. Applications can call {@link #getParameters()} 2979 * and this method anytime to get the latest focus distances. If the 2980 * focus mode is FOCUS_MODE_CONTINUOUS_VIDEO, focus distances may change 2981 * from time to time.</p> 2982 * 2983 * <p>This method is intended to estimate the distance between the camera 2984 * and the subject. After autofocus, the subject distance may be within 2985 * near and far focus distance. However, the precision depends on the 2986 * camera hardware, autofocus algorithm, the focus area, and the scene. 2987 * The error can be large and it should be only used as a reference.</p> 2988 * 2989 * <p>Far focus distance >= optimal focus distance >= near focus distance. 2990 * If the focus distance is infinity, the value will be 2991 * {@code Float.POSITIVE_INFINITY}.</p> 2992 * 2993 * @param output focus distances in meters. output must be a float 2994 * array with three elements. Near focus distance, optimal focus 2995 * distance, and far focus distance will be filled in the array. 2996 * @see #FOCUS_DISTANCE_NEAR_INDEX 2997 * @see #FOCUS_DISTANCE_OPTIMAL_INDEX 2998 * @see #FOCUS_DISTANCE_FAR_INDEX 2999 */ 3000 public void getFocusDistances(float[] output) { 3001 if (output == null || output.length != 3) { 3002 throw new IllegalArgumentException( 3003 "output must be an float array with three elements."); 3004 } 3005 splitFloat(get(KEY_FOCUS_DISTANCES), output); 3006 } 3007 3008 /** 3009 * Gets the maximum number of focus areas supported. This is the maximum 3010 * length of the list in {@link #setFocusAreas(List)} and 3011 * {@link #getFocusAreas()}. 3012 * 3013 * @return the maximum number of focus areas supported by the camera. 3014 * @see #getFocusAreas() 3015 */ 3016 public int getMaxNumFocusAreas() { 3017 return getInt(KEY_MAX_NUM_FOCUS_AREAS, 0); 3018 } 3019 3020 /** 3021 * <p>Gets the current focus areas. Camera driver uses the areas to decide 3022 * focus.</p> 3023 * 3024 * <p>Before using this API or {@link #setFocusAreas(List)}, apps should 3025 * call {@link #getMaxNumFocusAreas()} to know the maximum number of 3026 * focus areas first. If the value is 0, focus area is not supported.</p> 3027 * 3028 * <p>Each focus area is a rectangle with specified weight. The direction 3029 * is relative to the sensor orientation, that is, what the sensor sees. 3030 * The direction is not affected by the rotation or mirroring of 3031 * {@link #setDisplayOrientation(int)}. Coordinates of the rectangle 3032 * range from -1000 to 1000. (-1000, -1000) is the upper left point. 3033 * (1000, 1000) is the lower right point. The width and height of focus 3034 * areas cannot be 0 or negative.</p> 3035 * 3036 * <p>The weight must range from 1 to 1000. The weight should be 3037 * interpreted as a per-pixel weight - all pixels in the area have the 3038 * specified weight. This means a small area with the same weight as a 3039 * larger area will have less influence on the focusing than the larger 3040 * area. Focus areas can partially overlap and the driver will add the 3041 * weights in the overlap region.</p> 3042 * 3043 * <p>A special case of a {@code null} focus area list means the driver is 3044 * free to select focus targets as it wants. For example, the driver may 3045 * use more signals to select focus areas and change them 3046 * dynamically. Apps can set the focus area list to {@code null} if they 3047 * want the driver to completely control focusing.</p> 3048 * 3049 * <p>Focus areas are relative to the current field of view 3050 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000) 3051 * represents the top of the currently visible camera frame. The focus 3052 * area cannot be set to be outside the current field of view, even 3053 * when using zoom.</p> 3054 * 3055 * <p>Focus area only has effect if the current focus mode is 3056 * {@link #FOCUS_MODE_AUTO}, {@link #FOCUS_MODE_MACRO}, or 3057 * {@link #FOCUS_MODE_CONTINUOUS_VIDEO}.</p> 3058 * 3059 * @return a list of current focus areas 3060 */ 3061 public List<Area> getFocusAreas() { 3062 return splitArea(get(KEY_FOCUS_AREAS)); 3063 } 3064 3065 /** 3066 * Sets focus areas. See {@link #getFocusAreas()} for documentation. 3067 * 3068 * @param focusAreas the focus areas 3069 * @see #getFocusAreas() 3070 */ 3071 public void setFocusAreas(List<Area> focusAreas) { 3072 set(KEY_FOCUS_AREAS, focusAreas); 3073 } 3074 3075 /** 3076 * Gets the maximum number of metering areas supported. This is the 3077 * maximum length of the list in {@link #setMeteringAreas(List)} and 3078 * {@link #getMeteringAreas()}. 3079 * 3080 * @return the maximum number of metering areas supported by the camera. 3081 * @see #getMeteringAreas() 3082 */ 3083 public int getMaxNumMeteringAreas() { 3084 return getInt(KEY_MAX_NUM_METERING_AREAS, 0); 3085 } 3086 3087 /** 3088 * <p>Gets the current metering areas. Camera driver uses these areas to 3089 * decide exposure.</p> 3090 * 3091 * <p>Before using this API or {@link #setMeteringAreas(List)}, apps should 3092 * call {@link #getMaxNumMeteringAreas()} to know the maximum number of 3093 * metering areas first. If the value is 0, metering area is not 3094 * supported.</p> 3095 * 3096 * <p>Each metering area is a rectangle with specified weight. The 3097 * direction is relative to the sensor orientation, that is, what the 3098 * sensor sees. The direction is not affected by the rotation or 3099 * mirroring of {@link #setDisplayOrientation(int)}. Coordinates of the 3100 * rectangle range from -1000 to 1000. (-1000, -1000) is the upper left 3101 * point. (1000, 1000) is the lower right point. The width and height of 3102 * metering areas cannot be 0 or negative.</p> 3103 * 3104 * <p>The weight must range from 1 to 1000, and represents a weight for 3105 * every pixel in the area. This means that a large metering area with 3106 * the same weight as a smaller area will have more effect in the 3107 * metering result. Metering areas can partially overlap and the driver 3108 * will add the weights in the overlap region.</p> 3109 * 3110 * <p>A special case of a {@code null} metering area list means the driver 3111 * is free to meter as it chooses. For example, the driver may use more 3112 * signals to select metering areas and change them dynamically. Apps 3113 * can set the metering area list to {@code null} if they want the 3114 * driver to completely control metering.</p> 3115 * 3116 * <p>Metering areas are relative to the current field of view 3117 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000) 3118 * represents the top of the currently visible camera frame. The 3119 * metering area cannot be set to be outside the current field of view, 3120 * even when using zoom.</p> 3121 * 3122 * <p>No matter what metering areas are, the final exposure are compensated 3123 * by {@link #setExposureCompensation(int)}.</p> 3124 * 3125 * @return a list of current metering areas 3126 */ 3127 public List<Area> getMeteringAreas() { 3128 return splitArea(get(KEY_METERING_AREAS)); 3129 } 3130 3131 /** 3132 * Sets metering areas. See {@link #getMeteringAreas()} for 3133 * documentation. 3134 * 3135 * @param meteringAreas the metering areas 3136 * @see #getMeteringAreas() 3137 */ 3138 public void setMeteringAreas(List<Area> meteringAreas) { 3139 set(KEY_METERING_AREAS, meteringAreas); 3140 } 3141 3142 /** 3143 * Gets the maximum number of detected faces supported. This is the 3144 * maximum length of the list returned from {@link FaceDetectionListener}. 3145 * If the return value is 0, face detection of the specified type is not 3146 * supported. 3147 * 3148 * @return the maximum number of detected face supported by the camera. 3149 * @see #startFaceDetection(int) 3150 */ 3151 public int getMaxNumDetectedFaces() { 3152 return getInt(KEY_MAX_NUM_DETECTED_FACES_HW, 0); 3153 } 3154 3155 /** 3156 * Sets recording mode hint. This tells the camera that the intent of 3157 * the application is to record videos {@link 3158 * android.media.MediaRecorder#start()}, not to take still pictures 3159 * {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback, 3160 * Camera.PictureCallback, Camera.PictureCallback)}. Using this hint can 3161 * allow MediaRecorder.start() to start faster or with fewer glitches on 3162 * output. This should be called before starting preview for the best 3163 * result, but can be changed while the preview is active. The default 3164 * value is false. 3165 * 3166 * The app can still call takePicture() when the hint is true or call 3167 * MediaRecorder.start() when the hint is false. But the performance may 3168 * be worse. 3169 * 3170 * @param hint true if the apps intend to record videos using 3171 * {@link android.media.MediaRecorder}. 3172 */ 3173 public void setRecordingHint(boolean hint) { 3174 set(KEY_RECORDING_HINT, hint ? TRUE : FALSE); 3175 } 3176 3177 // Splits a comma delimited string to an ArrayList of String. 3178 // Return null if the passing string is null or the size is 0. 3179 private ArrayList<String> split(String str) { 3180 if (str == null) return null; 3181 3182 // Use StringTokenizer because it is faster than split. 3183 StringTokenizer tokenizer = new StringTokenizer(str, ","); 3184 ArrayList<String> substrings = new ArrayList<String>(); 3185 while (tokenizer.hasMoreElements()) { 3186 substrings.add(tokenizer.nextToken()); 3187 } 3188 return substrings; 3189 } 3190 3191 // Splits a comma delimited string to an ArrayList of Integer. 3192 // Return null if the passing string is null or the size is 0. 3193 private ArrayList<Integer> splitInt(String str) { 3194 if (str == null) return null; 3195 3196 StringTokenizer tokenizer = new StringTokenizer(str, ","); 3197 ArrayList<Integer> substrings = new ArrayList<Integer>(); 3198 while (tokenizer.hasMoreElements()) { 3199 String token = tokenizer.nextToken(); 3200 substrings.add(Integer.parseInt(token)); 3201 } 3202 if (substrings.size() == 0) return null; 3203 return substrings; 3204 } 3205 3206 private void splitInt(String str, int[] output) { 3207 if (str == null) return; 3208 3209 StringTokenizer tokenizer = new StringTokenizer(str, ","); 3210 int index = 0; 3211 while (tokenizer.hasMoreElements()) { 3212 String token = tokenizer.nextToken(); 3213 output[index++] = Integer.parseInt(token); 3214 } 3215 } 3216 3217 // Splits a comma delimited string to an ArrayList of Float. 3218 private void splitFloat(String str, float[] output) { 3219 if (str == null) return; 3220 3221 StringTokenizer tokenizer = new StringTokenizer(str, ","); 3222 int index = 0; 3223 while (tokenizer.hasMoreElements()) { 3224 String token = tokenizer.nextToken(); 3225 output[index++] = Float.parseFloat(token); 3226 } 3227 } 3228 3229 // Returns the value of a float parameter. 3230 private float getFloat(String key, float defaultValue) { 3231 try { 3232 return Float.parseFloat(mMap.get(key)); 3233 } catch (NumberFormatException ex) { 3234 return defaultValue; 3235 } 3236 } 3237 3238 // Returns the value of a integer parameter. 3239 private int getInt(String key, int defaultValue) { 3240 try { 3241 return Integer.parseInt(mMap.get(key)); 3242 } catch (NumberFormatException ex) { 3243 return defaultValue; 3244 } 3245 } 3246 3247 // Splits a comma delimited string to an ArrayList of Size. 3248 // Return null if the passing string is null or the size is 0. 3249 private ArrayList<Size> splitSize(String str) { 3250 if (str == null) return null; 3251 3252 StringTokenizer tokenizer = new StringTokenizer(str, ","); 3253 ArrayList<Size> sizeList = new ArrayList<Size>(); 3254 while (tokenizer.hasMoreElements()) { 3255 Size size = strToSize(tokenizer.nextToken()); 3256 if (size != null) sizeList.add(size); 3257 } 3258 if (sizeList.size() == 0) return null; 3259 return sizeList; 3260 } 3261 3262 // Parses a string (ex: "480x320") to Size object. 3263 // Return null if the passing string is null. 3264 private Size strToSize(String str) { 3265 if (str == null) return null; 3266 3267 int pos = str.indexOf('x'); 3268 if (pos != -1) { 3269 String width = str.substring(0, pos); 3270 String height = str.substring(pos + 1); 3271 return new Size(Integer.parseInt(width), 3272 Integer.parseInt(height)); 3273 } 3274 Log.e(TAG, "Invalid size parameter string=" + str); 3275 return null; 3276 } 3277 3278 // Splits a comma delimited string to an ArrayList of int array. 3279 // Example string: "(10000,26623),(10000,30000)". Return null if the 3280 // passing string is null or the size is 0. 3281 private ArrayList<int[]> splitRange(String str) { 3282 if (str == null || str.charAt(0) != '(' 3283 || str.charAt(str.length() - 1) != ')') { 3284 Log.e(TAG, "Invalid range list string=" + str); 3285 return null; 3286 } 3287 3288 ArrayList<int[]> rangeList = new ArrayList<int[]>(); 3289 int endIndex, fromIndex = 1; 3290 do { 3291 int[] range = new int[2]; 3292 endIndex = str.indexOf("),(", fromIndex); 3293 if (endIndex == -1) endIndex = str.length() - 1; 3294 splitInt(str.substring(fromIndex, endIndex), range); 3295 rangeList.add(range); 3296 fromIndex = endIndex + 3; 3297 } while (endIndex != str.length() - 1); 3298 3299 if (rangeList.size() == 0) return null; 3300 return rangeList; 3301 } 3302 3303 // Splits a comma delimited string to an ArrayList of Area objects. 3304 // Example string: "(-10,-10,0,0,300),(0,0,10,10,700)". Return null if 3305 // the passing string is null or the size is 0 or (0,0,0,0,0). 3306 private ArrayList<Area> splitArea(String str) { 3307 if (str == null || str.charAt(0) != '(' 3308 || str.charAt(str.length() - 1) != ')') { 3309 Log.e(TAG, "Invalid area string=" + str); 3310 return null; 3311 } 3312 3313 ArrayList<Area> result = new ArrayList<Area>(); 3314 int endIndex, fromIndex = 1; 3315 int[] array = new int[5]; 3316 do { 3317 endIndex = str.indexOf("),(", fromIndex); 3318 if (endIndex == -1) endIndex = str.length() - 1; 3319 splitInt(str.substring(fromIndex, endIndex), array); 3320 Rect rect = new Rect(array[0], array[1], array[2], array[3]); 3321 result.add(new Area(rect, array[4])); 3322 fromIndex = endIndex + 3; 3323 } while (endIndex != str.length() - 1); 3324 3325 if (result.size() == 0) return null; 3326 3327 if (result.size() == 1) { 3328 Area area = result.get(0); 3329 Rect rect = area.rect; 3330 if (rect.left == 0 && rect.top == 0 && rect.right == 0 3331 && rect.bottom == 0 && area.weight == 0) { 3332 return null; 3333 } 3334 } 3335 3336 return result; 3337 } 3338 }; 3339} 3340