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