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