Camera.java revision 055c986ab841f8f758398841730f1e90313b132a
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 java.lang.ref.WeakReference; 20import java.util.ArrayList; 21import java.util.HashMap; 22import java.util.List; 23import java.util.StringTokenizer; 24import java.io.IOException; 25 26import android.util.Log; 27import android.view.Surface; 28import android.view.SurfaceHolder; 29import android.graphics.ImageFormat; 30import android.os.Handler; 31import android.os.Looper; 32import android.os.Message; 33 34/** 35 * The Camera class is used to connect/disconnect with the camera service, 36 * set capture settings, start/stop preview, snap a picture, and retrieve 37 * frames for encoding for video. 38 * <p>There is no default constructor for this class. Use {@link #open()} to 39 * get a Camera object.</p> 40 * 41 * <p>In order to use 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 in order 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 class="caution"><strong>Caution:</strong> Different Android-powered devices 53 * may have different hardware specifications, such as megapixel ratings and 54 * auto-focus capabilities. In order for your application to be compatible with 55 * more devices, you should not make assumptions about the device camera 56 * specifications.</p> 57 */ 58public class Camera { 59 private static final String TAG = "Camera"; 60 61 // These match the enums in frameworks/base/include/ui/Camera.h 62 private static final int CAMERA_MSG_ERROR = 0x001; 63 private static final int CAMERA_MSG_SHUTTER = 0x002; 64 private static final int CAMERA_MSG_FOCUS = 0x004; 65 private static final int CAMERA_MSG_ZOOM = 0x008; 66 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010; 67 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020; 68 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040; 69 private static final int CAMERA_MSG_RAW_IMAGE = 0x080; 70 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100; 71 private static final int CAMERA_MSG_ALL_MSGS = 0x1FF; 72 73 private int mNativeContext; // accessed by native methods 74 private EventHandler mEventHandler; 75 private ShutterCallback mShutterCallback; 76 private PictureCallback mRawImageCallback; 77 private PictureCallback mJpegCallback; 78 private PreviewCallback mPreviewCallback; 79 private PictureCallback mPostviewCallback; 80 private AutoFocusCallback mAutoFocusCallback; 81 private ZoomCallback mZoomCallback; 82 private ErrorCallback mErrorCallback; 83 private boolean mOneShot; 84 private boolean mWithBuffer; 85 86 /** 87 * Returns a new Camera object. 88 */ 89 public static Camera open() { 90 return new Camera(); 91 } 92 93 Camera() { 94 mShutterCallback = null; 95 mRawImageCallback = null; 96 mJpegCallback = null; 97 mPreviewCallback = null; 98 mPostviewCallback = null; 99 mZoomCallback = null; 100 101 Looper looper; 102 if ((looper = Looper.myLooper()) != null) { 103 mEventHandler = new EventHandler(this, looper); 104 } else if ((looper = Looper.getMainLooper()) != null) { 105 mEventHandler = new EventHandler(this, looper); 106 } else { 107 mEventHandler = null; 108 } 109 110 native_setup(new WeakReference<Camera>(this)); 111 } 112 113 protected void finalize() { 114 native_release(); 115 } 116 117 private native final void native_setup(Object camera_this); 118 private native final void native_release(); 119 120 121 /** 122 * Disconnects and releases the Camera object resources. 123 * <p>It is recommended that you call this as soon as you're done with the 124 * Camera object.</p> 125 */ 126 public final void release() { 127 native_release(); 128 } 129 130 /** 131 * Reconnect to the camera after passing it to MediaRecorder. To save 132 * setup/teardown time, a client of Camera can pass an initialized Camera 133 * object to a MediaRecorder to use for video recording. Once the 134 * MediaRecorder is done with the Camera, this method can be used to 135 * re-establish a connection with the camera hardware. NOTE: The Camera 136 * object must first be unlocked by the process that owns it before it 137 * can be connected to another process. 138 * 139 * @throws IOException if the method fails. 140 * 141 * @hide 142 */ 143 public native final void reconnect() throws IOException; 144 145 /** 146 * Lock the camera to prevent other processes from accessing it. To save 147 * setup/teardown time, a client of Camera can pass an initialized Camera 148 * object to another process. This method is used to re-lock the Camera 149 * object prevent other processes from accessing it. By default, the 150 * Camera object is locked. Locking it again from the same process will 151 * have no effect. Attempting to lock it from another process if it has 152 * not been unlocked will fail. 153 * 154 * @throws RuntimeException if the method fails. 155 */ 156 public native final void lock(); 157 158 /** 159 * Unlock the camera to allow another process to access it. To save 160 * setup/teardown time, a client of Camera can pass an initialized Camera 161 * object to another process. This method is used to unlock the Camera 162 * object before handing off the Camera object to the other process. 163 * 164 * @throws RuntimeException if the method fails. 165 */ 166 public native final void unlock(); 167 168 /** 169 * Sets the SurfaceHolder to be used for a picture preview. If the surface 170 * changed since the last call, the screen will blank. Nothing happens 171 * if the same surface is re-set. 172 * 173 * @param holder the SurfaceHolder upon which to place the picture preview 174 * @throws IOException if the method fails. 175 */ 176 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException { 177 if (holder != null) { 178 setPreviewDisplay(holder.getSurface()); 179 } else { 180 setPreviewDisplay((Surface)null); 181 } 182 } 183 184 private native final void setPreviewDisplay(Surface surface); 185 186 /** 187 * Used to get a copy of each preview frame. 188 */ 189 public interface PreviewCallback 190 { 191 /** 192 * The callback that delivers the preview frames. 193 * 194 * @param data The contents of the preview frame in the format defined 195 * by {@link android.graphics.ImageFormat}, which can be queried 196 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}. 197 * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)} 198 * is never called, the default will be the YCbCr_420_SP 199 * (NV21) format. 200 * @param camera The Camera service object. 201 */ 202 void onPreviewFrame(byte[] data, Camera camera); 203 }; 204 205 /** 206 * Start drawing preview frames to the surface. 207 */ 208 public native final void startPreview(); 209 210 /** 211 * Stop drawing preview frames to the surface. 212 */ 213 public native final void stopPreview(); 214 215 /** 216 * Return current preview state. 217 * 218 * FIXME: Unhide before release 219 * @hide 220 */ 221 public native final boolean previewEnabled(); 222 223 /** 224 * Can be called at any time to instruct the camera to use a callback for 225 * each preview frame in addition to displaying it. 226 * 227 * @param cb A callback object that receives a copy of each preview frame. 228 * Pass null to stop receiving callbacks at any time. 229 */ 230 public final void setPreviewCallback(PreviewCallback cb) { 231 mPreviewCallback = cb; 232 mOneShot = false; 233 mWithBuffer = false; 234 // Always use one-shot mode. We fake camera preview mode by 235 // doing one-shot preview continuously. 236 setHasPreviewCallback(cb != null, false); 237 } 238 239 /** 240 * Installs a callback to retrieve a single preview frame, after which the 241 * callback is cleared. 242 * 243 * @param cb A callback object that receives a copy of the preview frame. 244 */ 245 public final void setOneShotPreviewCallback(PreviewCallback cb) { 246 mPreviewCallback = cb; 247 mOneShot = true; 248 mWithBuffer = false; 249 setHasPreviewCallback(cb != null, false); 250 } 251 252 private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer); 253 254 /** 255 * Installs a callback which will get called as long as there are buffers in the 256 * preview buffer queue, which minimizes dynamic allocation of preview buffers. 257 * 258 * Apps must call addCallbackBuffer to explicitly register the buffers to use, or no callbacks 259 * will be received. addCallbackBuffer may be safely called before or after 260 * a call to setPreviewCallbackWithBuffer with a non-null callback parameter. 261 * 262 * The buffer queue will be cleared upon any calls to setOneShotPreviewCallback, 263 * setPreviewCallback, or to this method with a null callback parameter. 264 * 265 * @param cb A callback object that receives a copy of the preview frame. A null value will clear the queue. 266 * @hide 267 */ 268 public final void setPreviewCallbackWithBuffer(PreviewCallback cb) { 269 mPreviewCallback = cb; 270 mOneShot = false; 271 mWithBuffer = true; 272 setHasPreviewCallback(cb != null, true); 273 } 274 275 /** 276 * Adds a pre-allocated buffer to the callback buffer queue. 277 * Preview width and height can be determined from getPreviewSize, and bitsPerPixel can be 278 * found from from {@link android.hardware.Camera.Parameters#getPreviewFormat()} and 279 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} 280 * 281 * Alternatively, a buffer from a previous callback may be passed in or used 282 * to determine the size of new preview frame buffers. 283 * 284 * @param callbackBuffer The buffer to register. Size should be width * height * bitsPerPixel / 8. 285 * @hide 286 */ 287 public native final void addCallbackBuffer(byte[] callbackBuffer); 288 289 private class EventHandler extends Handler 290 { 291 private Camera mCamera; 292 293 public EventHandler(Camera c, Looper looper) { 294 super(looper); 295 mCamera = c; 296 } 297 298 @Override 299 public void handleMessage(Message msg) { 300 switch(msg.what) { 301 case CAMERA_MSG_SHUTTER: 302 if (mShutterCallback != null) { 303 mShutterCallback.onShutter(); 304 } 305 return; 306 307 case CAMERA_MSG_RAW_IMAGE: 308 if (mRawImageCallback != null) { 309 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera); 310 } 311 return; 312 313 case CAMERA_MSG_COMPRESSED_IMAGE: 314 if (mJpegCallback != null) { 315 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera); 316 } 317 return; 318 319 case CAMERA_MSG_PREVIEW_FRAME: 320 if (mPreviewCallback != null) { 321 PreviewCallback cb = mPreviewCallback; 322 if (mOneShot) { 323 // Clear the callback variable before the callback 324 // in case the app calls setPreviewCallback from 325 // the callback function 326 mPreviewCallback = null; 327 } else if (!mWithBuffer) { 328 // We're faking the camera preview mode to prevent 329 // the app from being flooded with preview frames. 330 // Set to oneshot mode again. 331 setHasPreviewCallback(true, false); 332 } 333 cb.onPreviewFrame((byte[])msg.obj, mCamera); 334 } 335 return; 336 337 case CAMERA_MSG_POSTVIEW_FRAME: 338 if (mPostviewCallback != null) { 339 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera); 340 } 341 return; 342 343 case CAMERA_MSG_FOCUS: 344 if (mAutoFocusCallback != null) { 345 mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera); 346 } 347 return; 348 349 case CAMERA_MSG_ZOOM: 350 if (mZoomCallback != null) { 351 mZoomCallback.onZoomUpdate(msg.arg1, msg.arg2 != 0, mCamera); 352 } 353 return; 354 355 case CAMERA_MSG_ERROR : 356 Log.e(TAG, "Error " + msg.arg1); 357 if (mErrorCallback != null) { 358 mErrorCallback.onError(msg.arg1, mCamera); 359 } 360 return; 361 362 default: 363 Log.e(TAG, "Unknown message type " + msg.what); 364 return; 365 } 366 } 367 } 368 369 private static void postEventFromNative(Object camera_ref, 370 int what, int arg1, int arg2, Object obj) 371 { 372 Camera c = (Camera)((WeakReference)camera_ref).get(); 373 if (c == null) 374 return; 375 376 if (c.mEventHandler != null) { 377 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj); 378 c.mEventHandler.sendMessage(m); 379 } 380 } 381 382 /** 383 * Handles the callback for the camera auto focus. 384 * <p>Devices that do not support auto-focus will receive a "fake" 385 * callback to this interface. If your application needs auto-focus and 386 * should not be installed on devices <em>without</em> auto-focus, you must 387 * declare that your app uses the 388 * {@code android.hardware.camera.autofocus} feature, in the 389 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> 390 * manifest element.</p> 391 */ 392 public interface AutoFocusCallback 393 { 394 /** 395 * Callback for the camera auto focus. If the camera does not support 396 * auto-focus and autoFocus is called, onAutoFocus will be called 397 * immediately with success. 398 * 399 * @param success true if focus was successful, false if otherwise 400 * @param camera the Camera service object 401 */ 402 void onAutoFocus(boolean success, Camera camera); 403 }; 404 405 /** 406 * Starts auto-focus function and registers a callback function to run when 407 * camera is focused. Only valid after startPreview() has been called. 408 * Applications should call {@link 409 * android.hardware.Camera.Parameters#getFocusMode()} to determine if this 410 * method should be called. If the camera does not support auto-focus, it is 411 * a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)} 412 * callback will be called immediately. 413 * <p>If your application should not be installed 414 * on devices without auto-focus, you must declare that your application 415 * uses auto-focus with the 416 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> 417 * manifest element.</p> 418 * <p>If the current flash mode is not 419 * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be 420 * fired during auto-focus depending on the driver.<p> 421 * 422 * @param cb the callback to run 423 */ 424 public final void autoFocus(AutoFocusCallback cb) 425 { 426 mAutoFocusCallback = cb; 427 native_autoFocus(); 428 } 429 private native final void native_autoFocus(); 430 431 /** 432 * Cancels auto-focus function. If the auto-focus is still in progress, 433 * this function will cancel it. Whether the auto-focus is in progress 434 * or not, this function will return the focus position to the default. 435 * If the camera does not support auto-focus, this is a no-op. 436 */ 437 public final void cancelAutoFocus() 438 { 439 mAutoFocusCallback = null; 440 native_cancelAutoFocus(); 441 } 442 private native final void native_cancelAutoFocus(); 443 444 /** 445 * An interface which contains a callback for the shutter closing after taking a picture. 446 */ 447 public interface ShutterCallback 448 { 449 /** 450 * Can be used to play a shutter sound as soon as the image has been captured, but before 451 * the data is available. 452 */ 453 void onShutter(); 454 } 455 456 /** 457 * Handles the callback for when a picture is taken. 458 */ 459 public interface PictureCallback { 460 /** 461 * Callback for when a picture is taken. 462 * 463 * @param data a byte array of the picture data 464 * @param camera the Camera service object 465 */ 466 void onPictureTaken(byte[] data, Camera camera); 467 }; 468 469 /** 470 * Triggers an asynchronous image capture. The camera service will initiate 471 * a series of callbacks to the application as the image capture progresses. 472 * The shutter callback occurs after the image is captured. This can be used 473 * to trigger a sound to let the user know that image has been captured. The 474 * raw callback occurs when the raw image data is available (NOTE: the data 475 * may be null if the hardware does not have enough memory to make a copy). 476 * The jpeg callback occurs when the compressed image is available. If the 477 * application does not need a particular callback, a null can be passed 478 * instead of a callback method. 479 * 480 * This method will stop the preview. Applications should not call {@link 481 * #stopPreview()} before this. After jpeg callback is received, 482 * applications can call {@link #startPreview()} to restart the preview. 483 * 484 * @param shutter callback after the image is captured, may be null 485 * @param raw callback with raw image data, may be null 486 * @param jpeg callback with jpeg image data, may be null 487 */ 488 public final void takePicture(ShutterCallback shutter, PictureCallback raw, 489 PictureCallback jpeg) { 490 takePicture(shutter, raw, null, jpeg); 491 } 492 private native final void native_takePicture(); 493 494 /** 495 * Triggers an asynchronous image capture. The camera service will initiate 496 * a series of callbacks to the application as the image capture progresses. 497 * The shutter callback occurs after the image is captured. This can be used 498 * to trigger a sound to let the user know that image has been captured. The 499 * raw callback occurs when the raw image data is available (NOTE: the data 500 * may be null if the hardware does not have enough memory to make a copy). 501 * The postview callback occurs when a scaled, fully processed postview 502 * image is available (NOTE: not all hardware supports this). The jpeg 503 * callback occurs when the compressed image is available. If the 504 * application does not need a particular callback, a null can be passed 505 * instead of a callback method. 506 * 507 * This method will stop the preview. Applications should not call {@link 508 * #stopPreview()} before this. After jpeg callback is received, 509 * applications can call {@link #startPreview()} to restart the preview. 510 * 511 * @param shutter callback after the image is captured, may be null 512 * @param raw callback with raw image data, may be null 513 * @param postview callback with postview image data, may be null 514 * @param jpeg callback with jpeg image data, may be null 515 */ 516 public final void takePicture(ShutterCallback shutter, PictureCallback raw, 517 PictureCallback postview, PictureCallback jpeg) { 518 mShutterCallback = shutter; 519 mRawImageCallback = raw; 520 mPostviewCallback = postview; 521 mJpegCallback = jpeg; 522 native_takePicture(); 523 } 524 525 /** 526 * Zooms to the requested value smoothly. Driver will generate {@link 527 * ZoomCallback} for the zoom value and whether zoom is stopped at the 528 * time. For example, suppose the current zoom is 0 and startSmoothZoom is 529 * called with value 3. Three ZoomCallback will be generated with zoom value 530 * 1, 2, and 3. The applications can call {@link #stopSmoothZoom} to stop 531 * the zoom earlier. The applications should not call startSmoothZoom again 532 * or change the zoom value before zoom stops. This method is supported if 533 * {@link android.hardware.Camera.Parameters#isSmoothZoomSupported} is true. 534 * 535 * @param value zoom value. The valid range is 0 to {@link 536 * android.hardware.Camera.Parameters#getMaxZoom}. 537 */ 538 public native final void startSmoothZoom(int value); 539 540 /** 541 * Stops the smooth zoom. The applications should wait for the {@link 542 * ZoomCallback} to know when the zoom is actually stopped. This method is 543 * supported if {@link 544 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true. 545 */ 546 public native final void stopSmoothZoom(); 547 548 /** 549 * Set the display orientation. This affects the preview frames and the 550 * picture displayed after snapshot. This method is useful for portrait 551 * mode applications. 552 * 553 * This does not affect the order of byte array passed in 554 * {@link PreviewCallback#onPreviewFrame}. This method is not allowed to 555 * be called during preview. 556 * 557 * @param degrees the angle that the picture will be rotated clockwise. 558 * Valid values are 0, 90, 180, and 270. The starting 559 * position is 0 (landscape). 560 */ 561 public native final void setDisplayOrientation(int degrees); 562 563 /** 564 * Handles the zoom callback. 565 * 566 */ 567 public interface ZoomCallback 568 { 569 /** 570 * Callback for zoom updates 571 * 572 * @param zoomValue the current zoom value. In smooth zoom mode, camera 573 * generates this callback for every new zoom value. 574 * @param stopped whether smooth zoom is stopped. If the value is true, 575 * this is the last zoom update for the application. 576 * 577 * @param camera the Camera service object 578 * @see #startSmoothZoom(int) 579 */ 580 void onZoomUpdate(int zoomValue, boolean stopped, Camera camera); 581 }; 582 583 /** 584 * Registers a callback to be invoked when the zoom value is updated by the 585 * camera driver during smooth zoom. 586 * 587 * @param cb the callback to run 588 * @see #startSmoothZoom(int) 589 */ 590 public final void setZoomCallback(ZoomCallback cb) 591 { 592 mZoomCallback = cb; 593 } 594 595 // These match the enum in include/ui/Camera.h 596 /** Unspecified camerar error. @see #ErrorCallback */ 597 public static final int CAMERA_ERROR_UNKNOWN = 1; 598 /** Media server died. In this case, the application must release the 599 * Camera object and instantiate a new one. @see #ErrorCallback */ 600 public static final int CAMERA_ERROR_SERVER_DIED = 100; 601 602 /** 603 * Handles the camera error callback. 604 */ 605 public interface ErrorCallback 606 { 607 /** 608 * Callback for camera errors. 609 * @param error error code: 610 * <ul> 611 * <li>{@link #CAMERA_ERROR_UNKNOWN} 612 * <li>{@link #CAMERA_ERROR_SERVER_DIED} 613 * </ul> 614 * @param camera the Camera service object 615 */ 616 void onError(int error, Camera camera); 617 }; 618 619 /** 620 * Registers a callback to be invoked when an error occurs. 621 * @param cb the callback to run 622 */ 623 public final void setErrorCallback(ErrorCallback cb) 624 { 625 mErrorCallback = cb; 626 } 627 628 private native final void native_setParameters(String params); 629 private native final String native_getParameters(); 630 631 /** 632 * Sets the Parameters for pictures from this Camera service. 633 * 634 * @param params the Parameters to use for this Camera service 635 */ 636 public void setParameters(Parameters params) { 637 native_setParameters(params.flatten()); 638 } 639 640 /** 641 * Returns the picture Parameters for this Camera service. 642 */ 643 public Parameters getParameters() { 644 Parameters p = new Parameters(); 645 String s = native_getParameters(); 646 p.unflatten(s); 647 return p; 648 } 649 650 /** 651 * Handles the picture size (dimensions). 652 */ 653 public class Size { 654 /** 655 * Sets the dimensions for pictures. 656 * 657 * @param w the photo width (pixels) 658 * @param h the photo height (pixels) 659 */ 660 public Size(int w, int h) { 661 width = w; 662 height = h; 663 } 664 /** 665 * Compares {@code obj} to this size. 666 * 667 * @param obj the object to compare this size with. 668 * @return {@code true} if the width and height of {@code obj} is the 669 * same as those of this size. {@code false} otherwise. 670 */ 671 @Override 672 public boolean equals(Object obj) { 673 if (!(obj instanceof Size)) { 674 return false; 675 } 676 Size s = (Size) obj; 677 return width == s.width && height == s.height; 678 } 679 @Override 680 public int hashCode() { 681 return width * 32713 + height; 682 } 683 /** width of the picture */ 684 public int width; 685 /** height of the picture */ 686 public int height; 687 }; 688 689 /** 690 * Handles the parameters for pictures created by a Camera service. 691 * 692 * <p>To make camera parameters take effect, applications have to call 693 * Camera.setParameters. For example, after setWhiteBalance is called, white 694 * balance is not changed until Camera.setParameters() is called. 695 * 696 * <p>Different devices may have different camera capabilities, such as 697 * picture size or flash modes. The application should query the camera 698 * capabilities before setting parameters. For example, the application 699 * should call getSupportedColorEffects before calling setEffect. If the 700 * camera does not support color effects, getSupportedColorEffects will 701 * return null. 702 */ 703 public class Parameters { 704 // Parameter keys to communicate with the camera driver. 705 private static final String KEY_PREVIEW_SIZE = "preview-size"; 706 private static final String KEY_PREVIEW_FORMAT = "preview-format"; 707 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate"; 708 private static final String KEY_PICTURE_SIZE = "picture-size"; 709 private static final String KEY_PICTURE_FORMAT = "picture-format"; 710 private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size"; 711 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width"; 712 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height"; 713 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality"; 714 private static final String KEY_JPEG_QUALITY = "jpeg-quality"; 715 private static final String KEY_ROTATION = "rotation"; 716 private static final String KEY_GPS_LATITUDE = "gps-latitude"; 717 private static final String KEY_GPS_LONGITUDE = "gps-longitude"; 718 private static final String KEY_GPS_ALTITUDE = "gps-altitude"; 719 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp"; 720 private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method"; 721 private static final String KEY_WHITE_BALANCE = "whitebalance"; 722 private static final String KEY_EFFECT = "effect"; 723 private static final String KEY_ANTIBANDING = "antibanding"; 724 private static final String KEY_SCENE_MODE = "scene-mode"; 725 private static final String KEY_FLASH_MODE = "flash-mode"; 726 private static final String KEY_FOCUS_MODE = "focus-mode"; 727 private static final String KEY_FOCAL_LENGTH = "focal-length"; 728 private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle"; 729 private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle"; 730 private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation"; 731 private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation"; 732 private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation"; 733 private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step"; 734 private static final String KEY_ZOOM = "zoom"; 735 private static final String KEY_MAX_ZOOM = "max-zoom"; 736 private static final String KEY_ZOOM_RATIOS = "zoom-ratios"; 737 private static final String KEY_ZOOM_SUPPORTED = "zoom-supported"; 738 private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported"; 739 // Parameter key suffix for supported values. 740 private static final String SUPPORTED_VALUES_SUFFIX = "-values"; 741 742 private static final String TRUE = "true"; 743 744 // Values for white balance settings. 745 public static final String WHITE_BALANCE_AUTO = "auto"; 746 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent"; 747 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent"; 748 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent"; 749 public static final String WHITE_BALANCE_DAYLIGHT = "daylight"; 750 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight"; 751 public static final String WHITE_BALANCE_TWILIGHT = "twilight"; 752 public static final String WHITE_BALANCE_SHADE = "shade"; 753 754 // Values for color effect settings. 755 public static final String EFFECT_NONE = "none"; 756 public static final String EFFECT_MONO = "mono"; 757 public static final String EFFECT_NEGATIVE = "negative"; 758 public static final String EFFECT_SOLARIZE = "solarize"; 759 public static final String EFFECT_SEPIA = "sepia"; 760 public static final String EFFECT_POSTERIZE = "posterize"; 761 public static final String EFFECT_WHITEBOARD = "whiteboard"; 762 public static final String EFFECT_BLACKBOARD = "blackboard"; 763 public static final String EFFECT_AQUA = "aqua"; 764 765 // Values for antibanding settings. 766 public static final String ANTIBANDING_AUTO = "auto"; 767 public static final String ANTIBANDING_50HZ = "50hz"; 768 public static final String ANTIBANDING_60HZ = "60hz"; 769 public static final String ANTIBANDING_OFF = "off"; 770 771 // Values for flash mode settings. 772 /** 773 * Flash will not be fired. 774 */ 775 public static final String FLASH_MODE_OFF = "off"; 776 777 /** 778 * Flash will be fired automatically when required. The flash may be fired 779 * during preview, auto-focus, or snapshot depending on the driver. 780 */ 781 public static final String FLASH_MODE_AUTO = "auto"; 782 783 /** 784 * Flash will always be fired during snapshot. The flash may also be 785 * fired during preview or auto-focus depending on the driver. 786 */ 787 public static final String FLASH_MODE_ON = "on"; 788 789 /** 790 * Flash will be fired in red-eye reduction mode. 791 */ 792 public static final String FLASH_MODE_RED_EYE = "red-eye"; 793 794 /** 795 * Constant emission of light during preview, auto-focus and snapshot. 796 * This can also be used for video recording. 797 */ 798 public static final String FLASH_MODE_TORCH = "torch"; 799 800 // Values for scene mode settings. 801 public static final String SCENE_MODE_AUTO = "auto"; 802 public static final String SCENE_MODE_ACTION = "action"; 803 public static final String SCENE_MODE_PORTRAIT = "portrait"; 804 public static final String SCENE_MODE_LANDSCAPE = "landscape"; 805 public static final String SCENE_MODE_NIGHT = "night"; 806 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait"; 807 public static final String SCENE_MODE_THEATRE = "theatre"; 808 public static final String SCENE_MODE_BEACH = "beach"; 809 public static final String SCENE_MODE_SNOW = "snow"; 810 public static final String SCENE_MODE_SUNSET = "sunset"; 811 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto"; 812 public static final String SCENE_MODE_FIREWORKS = "fireworks"; 813 public static final String SCENE_MODE_SPORTS = "sports"; 814 public static final String SCENE_MODE_PARTY = "party"; 815 public static final String SCENE_MODE_CANDLELIGHT = "candlelight"; 816 817 // Values for focus mode settings. 818 /** 819 * Auto-focus mode. 820 */ 821 public static final String FOCUS_MODE_AUTO = "auto"; 822 823 /** 824 * Focus is set at infinity. Applications should not call 825 * {@link #autoFocus(AutoFocusCallback)} in this mode. 826 */ 827 public static final String FOCUS_MODE_INFINITY = "infinity"; 828 public static final String FOCUS_MODE_MACRO = "macro"; 829 830 /** 831 * Focus is fixed. The camera is always in this mode if the focus is not 832 * adjustable. If the camera has auto-focus, this mode can fix the 833 * focus, which is usually at hyperfocal distance. Applications should 834 * not call {@link #autoFocus(AutoFocusCallback)} in this mode. 835 */ 836 public static final String FOCUS_MODE_FIXED = "fixed"; 837 838 // Formats for setPreviewFormat and setPictureFormat. 839 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp"; 840 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp"; 841 private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv"; 842 private static final String PIXEL_FORMAT_RGB565 = "rgb565"; 843 private static final String PIXEL_FORMAT_JPEG = "jpeg"; 844 845 private HashMap<String, String> mMap; 846 847 private Parameters() { 848 mMap = new HashMap<String, String>(); 849 } 850 851 /** 852 * Writes the current Parameters to the log. 853 * @hide 854 * @deprecated 855 */ 856 public void dump() { 857 Log.e(TAG, "dump: size=" + mMap.size()); 858 for (String k : mMap.keySet()) { 859 Log.e(TAG, "dump: " + k + "=" + mMap.get(k)); 860 } 861 } 862 863 /** 864 * Creates a single string with all the parameters set in 865 * this Parameters object. 866 * <p>The {@link #unflatten(String)} method does the reverse.</p> 867 * 868 * @return a String with all values from this Parameters object, in 869 * semi-colon delimited key-value pairs 870 */ 871 public String flatten() { 872 StringBuilder flattened = new StringBuilder(); 873 for (String k : mMap.keySet()) { 874 flattened.append(k); 875 flattened.append("="); 876 flattened.append(mMap.get(k)); 877 flattened.append(";"); 878 } 879 // chop off the extra semicolon at the end 880 flattened.deleteCharAt(flattened.length()-1); 881 return flattened.toString(); 882 } 883 884 /** 885 * Takes a flattened string of parameters and adds each one to 886 * this Parameters object. 887 * <p>The {@link #flatten()} method does the reverse.</p> 888 * 889 * @param flattened a String of parameters (key-value paired) that 890 * are semi-colon delimited 891 */ 892 public void unflatten(String flattened) { 893 mMap.clear(); 894 895 StringTokenizer tokenizer = new StringTokenizer(flattened, ";"); 896 while (tokenizer.hasMoreElements()) { 897 String kv = tokenizer.nextToken(); 898 int pos = kv.indexOf('='); 899 if (pos == -1) { 900 continue; 901 } 902 String k = kv.substring(0, pos); 903 String v = kv.substring(pos + 1); 904 mMap.put(k, v); 905 } 906 } 907 908 public void remove(String key) { 909 mMap.remove(key); 910 } 911 912 /** 913 * Sets a String parameter. 914 * 915 * @param key the key name for the parameter 916 * @param value the String value of the parameter 917 */ 918 public void set(String key, String value) { 919 if (key.indexOf('=') != -1 || key.indexOf(';') != -1) { 920 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ;)"); 921 return; 922 } 923 if (value.indexOf('=') != -1 || value.indexOf(';') != -1) { 924 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ;)"); 925 return; 926 } 927 928 mMap.put(key, value); 929 } 930 931 /** 932 * Sets an integer parameter. 933 * 934 * @param key the key name for the parameter 935 * @param value the int value of the parameter 936 */ 937 public void set(String key, int value) { 938 mMap.put(key, Integer.toString(value)); 939 } 940 941 /** 942 * Returns the value of a String parameter. 943 * 944 * @param key the key name for the parameter 945 * @return the String value of the parameter 946 */ 947 public String get(String key) { 948 return mMap.get(key); 949 } 950 951 /** 952 * Returns the value of an integer parameter. 953 * 954 * @param key the key name for the parameter 955 * @return the int value of the parameter 956 */ 957 public int getInt(String key) { 958 return Integer.parseInt(mMap.get(key)); 959 } 960 961 /** 962 * Sets the dimensions for preview pictures. 963 * 964 * @param width the width of the pictures, in pixels 965 * @param height the height of the pictures, in pixels 966 */ 967 public void setPreviewSize(int width, int height) { 968 String v = Integer.toString(width) + "x" + Integer.toString(height); 969 set(KEY_PREVIEW_SIZE, v); 970 } 971 972 /** 973 * Returns the dimensions setting for preview pictures. 974 * 975 * @return a Size object with the height and width setting 976 * for the preview picture 977 */ 978 public Size getPreviewSize() { 979 String pair = get(KEY_PREVIEW_SIZE); 980 return strToSize(pair); 981 } 982 983 /** 984 * Gets the supported preview sizes. 985 * 986 * @return a List of Size object. This method will always return a list 987 * with at least one element. 988 */ 989 public List<Size> getSupportedPreviewSizes() { 990 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX); 991 return splitSize(str); 992 } 993 994 /** 995 * Sets the dimensions for EXIF thumbnail in Jpeg picture. If 996 * applications set both width and height to 0, EXIF will not contain 997 * thumbnail. 998 * 999 * @param width the width of the thumbnail, in pixels 1000 * @param height the height of the thumbnail, in pixels 1001 */ 1002 public void setJpegThumbnailSize(int width, int height) { 1003 set(KEY_JPEG_THUMBNAIL_WIDTH, width); 1004 set(KEY_JPEG_THUMBNAIL_HEIGHT, height); 1005 } 1006 1007 /** 1008 * Returns the dimensions for EXIF thumbnail in Jpeg picture. 1009 * 1010 * @return a Size object with the height and width setting for the EXIF 1011 * thumbnails 1012 */ 1013 public Size getJpegThumbnailSize() { 1014 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH), 1015 getInt(KEY_JPEG_THUMBNAIL_HEIGHT)); 1016 } 1017 1018 /** 1019 * Gets the supported jpeg thumbnail sizes. 1020 * 1021 * @return a List of Size object. This method will always return a list 1022 * with at least two elements. Size 0,0 (no thumbnail) is always 1023 * supported. 1024 */ 1025 public List<Size> getSupportedJpegThumbnailSizes() { 1026 String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX); 1027 return splitSize(str); 1028 } 1029 1030 /** 1031 * Sets the quality of the EXIF thumbnail in Jpeg picture. 1032 * 1033 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1 1034 * to 100, with 100 being the best. 1035 */ 1036 public void setJpegThumbnailQuality(int quality) { 1037 set(KEY_JPEG_THUMBNAIL_QUALITY, quality); 1038 } 1039 1040 /** 1041 * Returns the quality setting for the EXIF thumbnail in Jpeg picture. 1042 * 1043 * @return the JPEG quality setting of the EXIF thumbnail. 1044 */ 1045 public int getJpegThumbnailQuality() { 1046 return getInt(KEY_JPEG_THUMBNAIL_QUALITY); 1047 } 1048 1049 /** 1050 * Sets Jpeg quality of captured picture. 1051 * 1052 * @param quality the JPEG quality of captured picture. The range is 1 1053 * to 100, with 100 being the best. 1054 */ 1055 public void setJpegQuality(int quality) { 1056 set(KEY_JPEG_QUALITY, quality); 1057 } 1058 1059 /** 1060 * Returns the quality setting for the JPEG picture. 1061 * 1062 * @return the JPEG picture quality setting. 1063 */ 1064 public int getJpegQuality() { 1065 return getInt(KEY_JPEG_QUALITY); 1066 } 1067 1068 /** 1069 * Sets the rate at which preview frames are received. This is the 1070 * target frame rate. The actual frame rate depends on the driver. 1071 * 1072 * @param fps the frame rate (frames per second) 1073 */ 1074 public void setPreviewFrameRate(int fps) { 1075 set(KEY_PREVIEW_FRAME_RATE, fps); 1076 } 1077 1078 /** 1079 * Returns the setting for the rate at which preview frames are 1080 * received. This is the target frame rate. The actual frame rate 1081 * depends on the driver. 1082 * 1083 * @return the frame rate setting (frames per second) 1084 */ 1085 public int getPreviewFrameRate() { 1086 return getInt(KEY_PREVIEW_FRAME_RATE); 1087 } 1088 1089 /** 1090 * Gets the supported preview frame rates. 1091 * 1092 * @return a List of Integer objects (preview frame rates). null if 1093 * preview frame rate setting is not supported. 1094 */ 1095 public List<Integer> getSupportedPreviewFrameRates() { 1096 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX); 1097 return splitInt(str); 1098 } 1099 1100 /** 1101 * Sets the image format for preview pictures. 1102 * <p>If this is never called, the default format will be 1103 * {@link android.graphics.ImageFormat#NV21}, which 1104 * uses the NV21 encoding format.</p> 1105 * 1106 * @param pixel_format the desired preview picture format, defined 1107 * by one of the {@link android.graphics.ImageFormat} constants. 1108 * (E.g., <var>ImageFormat.NV21</var> (default), 1109 * <var>ImageFormat.RGB_565</var>, or 1110 * <var>ImageFormat.JPEG</var>) 1111 * @see android.graphics.ImageFormat 1112 */ 1113 public void setPreviewFormat(int pixel_format) { 1114 String s = cameraFormatForPixelFormat(pixel_format); 1115 if (s == null) { 1116 throw new IllegalArgumentException( 1117 "Invalid pixel_format=" + pixel_format); 1118 } 1119 1120 set(KEY_PREVIEW_FORMAT, s); 1121 } 1122 1123 /** 1124 * Returns the image format for preview pictures got from 1125 * {@link PreviewCallback}. 1126 * 1127 * @return the {@link android.graphics.ImageFormat} int representing 1128 * the preview picture format. 1129 */ 1130 public int getPreviewFormat() { 1131 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT)); 1132 } 1133 1134 /** 1135 * Gets the supported preview formats. 1136 * 1137 * @return a List of Integer objects. This method will always return a 1138 * list with at least one element. 1139 */ 1140 public List<Integer> getSupportedPreviewFormats() { 1141 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX); 1142 ArrayList<Integer> formats = new ArrayList<Integer>(); 1143 for (String s : split(str)) { 1144 int f = pixelFormatForCameraFormat(s); 1145 if (f == ImageFormat.UNKNOWN) continue; 1146 formats.add(f); 1147 } 1148 return formats; 1149 } 1150 1151 /** 1152 * Sets the dimensions for pictures. 1153 * 1154 * @param width the width for pictures, in pixels 1155 * @param height the height for pictures, in pixels 1156 */ 1157 public void setPictureSize(int width, int height) { 1158 String v = Integer.toString(width) + "x" + Integer.toString(height); 1159 set(KEY_PICTURE_SIZE, v); 1160 } 1161 1162 /** 1163 * Returns the dimension setting for pictures. 1164 * 1165 * @return a Size object with the height and width setting 1166 * for pictures 1167 */ 1168 public Size getPictureSize() { 1169 String pair = get(KEY_PICTURE_SIZE); 1170 return strToSize(pair); 1171 } 1172 1173 /** 1174 * Gets the supported picture sizes. 1175 * 1176 * @return a List of Size objects. This method will always return a list 1177 * with at least one element. 1178 */ 1179 public List<Size> getSupportedPictureSizes() { 1180 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX); 1181 return splitSize(str); 1182 } 1183 1184 /** 1185 * Sets the image format for pictures. 1186 * 1187 * @param pixel_format the desired picture format 1188 * (<var>ImageFormat.NV21</var>, 1189 * <var>ImageFormat.RGB_565</var>, or 1190 * <var>ImageFormat.JPEG</var>) 1191 * @see android.graphics.ImageFormat 1192 */ 1193 public void setPictureFormat(int pixel_format) { 1194 String s = cameraFormatForPixelFormat(pixel_format); 1195 if (s == null) { 1196 throw new IllegalArgumentException( 1197 "Invalid pixel_format=" + pixel_format); 1198 } 1199 1200 set(KEY_PICTURE_FORMAT, s); 1201 } 1202 1203 /** 1204 * Returns the image format for pictures. 1205 * 1206 * @return the ImageFormat int representing the picture format 1207 */ 1208 public int getPictureFormat() { 1209 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT)); 1210 } 1211 1212 /** 1213 * Gets the supported picture formats. 1214 * 1215 * @return a List of Integer objects (values are ImageFormat.XXX). This 1216 * method will always return a list with at least one element. 1217 */ 1218 public List<Integer> getSupportedPictureFormats() { 1219 String str = get(KEY_PICTURE_FORMAT + SUPPORTED_VALUES_SUFFIX); 1220 ArrayList<Integer> formats = new ArrayList<Integer>(); 1221 for (String s : split(str)) { 1222 int f = pixelFormatForCameraFormat(s); 1223 if (f == ImageFormat.UNKNOWN) continue; 1224 formats.add(f); 1225 } 1226 return formats; 1227 } 1228 1229 private String cameraFormatForPixelFormat(int pixel_format) { 1230 switch(pixel_format) { 1231 case ImageFormat.NV16: return PIXEL_FORMAT_YUV422SP; 1232 case ImageFormat.NV21: return PIXEL_FORMAT_YUV420SP; 1233 case ImageFormat.YUY2: return PIXEL_FORMAT_YUV422I; 1234 case ImageFormat.RGB_565: return PIXEL_FORMAT_RGB565; 1235 case ImageFormat.JPEG: return PIXEL_FORMAT_JPEG; 1236 default: return null; 1237 } 1238 } 1239 1240 private int pixelFormatForCameraFormat(String format) { 1241 if (format == null) 1242 return ImageFormat.UNKNOWN; 1243 1244 if (format.equals(PIXEL_FORMAT_YUV422SP)) 1245 return ImageFormat.NV16; 1246 1247 if (format.equals(PIXEL_FORMAT_YUV420SP)) 1248 return ImageFormat.NV21; 1249 1250 if (format.equals(PIXEL_FORMAT_YUV422I)) 1251 return ImageFormat.YUY2; 1252 1253 if (format.equals(PIXEL_FORMAT_RGB565)) 1254 return ImageFormat.RGB_565; 1255 1256 if (format.equals(PIXEL_FORMAT_JPEG)) 1257 return ImageFormat.JPEG; 1258 1259 return ImageFormat.UNKNOWN; 1260 } 1261 1262 /** 1263 * Sets the orientation of the device in degrees. For example, suppose 1264 * the natural position of the device is landscape. If the user takes a 1265 * picture in landscape mode in 2048x1536 resolution, the rotation 1266 * should be set to 0. If the user rotates the phone 90 degrees 1267 * clockwise, the rotation should be set to 90. Applications can use 1268 * {@link android.view.OrientationEventListener} to set this parameter. 1269 * 1270 * The camera driver may set orientation in the EXIF header without 1271 * rotating the picture. Or the driver may rotate the picture and 1272 * the EXIF thumbnail. If the Jpeg picture is rotated, the orientation 1273 * in the EXIF header will be missing or 1 (row #0 is top and column #0 1274 * is left side). 1275 * 1276 * @param rotation The orientation of the device in degrees. Rotation 1277 * can only be 0, 90, 180 or 270. 1278 * @throws IllegalArgumentException if rotation value is invalid. 1279 * @see android.view.OrientationEventListener 1280 */ 1281 public void setRotation(int rotation) { 1282 if (rotation == 0 || rotation == 90 || rotation == 180 1283 || rotation == 270) { 1284 set(KEY_ROTATION, Integer.toString(rotation)); 1285 } else { 1286 throw new IllegalArgumentException( 1287 "Invalid rotation=" + rotation); 1288 } 1289 } 1290 1291 /** 1292 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF 1293 * header. 1294 * 1295 * @param latitude GPS latitude coordinate. 1296 */ 1297 public void setGpsLatitude(double latitude) { 1298 set(KEY_GPS_LATITUDE, Double.toString(latitude)); 1299 } 1300 1301 /** 1302 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF 1303 * header. 1304 * 1305 * @param longitude GPS longitude coordinate. 1306 */ 1307 public void setGpsLongitude(double longitude) { 1308 set(KEY_GPS_LONGITUDE, Double.toString(longitude)); 1309 } 1310 1311 /** 1312 * Sets GPS altitude. This will be stored in JPEG EXIF header. 1313 * 1314 * @param altitude GPS altitude in meters. 1315 */ 1316 public void setGpsAltitude(double altitude) { 1317 set(KEY_GPS_ALTITUDE, Double.toString(altitude)); 1318 } 1319 1320 /** 1321 * Sets GPS timestamp. This will be stored in JPEG EXIF header. 1322 * 1323 * @param timestamp GPS timestamp (UTC in seconds since January 1, 1324 * 1970). 1325 */ 1326 public void setGpsTimestamp(long timestamp) { 1327 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp)); 1328 } 1329 1330 /** 1331 * Sets GPS processing method. It will store up to 100 characters 1332 * in JPEG EXIF header. 1333 * 1334 * @param processing_method The processing method to get this location. 1335 */ 1336 public void setGpsProcessingMethod(String processing_method) { 1337 set(KEY_GPS_PROCESSING_METHOD, processing_method); 1338 } 1339 1340 /** 1341 * Removes GPS latitude, longitude, altitude, and timestamp from the 1342 * parameters. 1343 */ 1344 public void removeGpsData() { 1345 remove(KEY_GPS_LATITUDE); 1346 remove(KEY_GPS_LONGITUDE); 1347 remove(KEY_GPS_ALTITUDE); 1348 remove(KEY_GPS_TIMESTAMP); 1349 remove(KEY_GPS_PROCESSING_METHOD); 1350 } 1351 1352 /** 1353 * Gets the current white balance setting. 1354 * 1355 * @return one of WHITE_BALANCE_XXX string constant. null if white 1356 * balance setting is not supported. 1357 */ 1358 public String getWhiteBalance() { 1359 return get(KEY_WHITE_BALANCE); 1360 } 1361 1362 /** 1363 * Sets the white balance. 1364 * 1365 * @param value WHITE_BALANCE_XXX string constant. 1366 */ 1367 public void setWhiteBalance(String value) { 1368 set(KEY_WHITE_BALANCE, value); 1369 } 1370 1371 /** 1372 * Gets the supported white balance. 1373 * 1374 * @return a List of WHITE_BALANCE_XXX string constants. null if white 1375 * balance setting is not supported. 1376 */ 1377 public List<String> getSupportedWhiteBalance() { 1378 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX); 1379 return split(str); 1380 } 1381 1382 /** 1383 * Gets the current color effect setting. 1384 * 1385 * @return one of EFFECT_XXX string constant. null if color effect 1386 * setting is not supported. 1387 */ 1388 public String getColorEffect() { 1389 return get(KEY_EFFECT); 1390 } 1391 1392 /** 1393 * Sets the current color effect setting. 1394 * 1395 * @param value EFFECT_XXX string constants. 1396 */ 1397 public void setColorEffect(String value) { 1398 set(KEY_EFFECT, value); 1399 } 1400 1401 /** 1402 * Gets the supported color effects. 1403 * 1404 * @return a List of EFFECT_XXX string constants. null if color effect 1405 * setting is not supported. 1406 */ 1407 public List<String> getSupportedColorEffects() { 1408 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX); 1409 return split(str); 1410 } 1411 1412 1413 /** 1414 * Gets the current antibanding setting. 1415 * 1416 * @return one of ANTIBANDING_XXX string constant. null if antibanding 1417 * setting is not supported. 1418 */ 1419 public String getAntibanding() { 1420 return get(KEY_ANTIBANDING); 1421 } 1422 1423 /** 1424 * Sets the antibanding. 1425 * 1426 * @param antibanding ANTIBANDING_XXX string constant. 1427 */ 1428 public void setAntibanding(String antibanding) { 1429 set(KEY_ANTIBANDING, antibanding); 1430 } 1431 1432 /** 1433 * Gets the supported antibanding values. 1434 * 1435 * @return a List of ANTIBANDING_XXX string constants. null if 1436 * antibanding setting is not supported. 1437 */ 1438 public List<String> getSupportedAntibanding() { 1439 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX); 1440 return split(str); 1441 } 1442 1443 /** 1444 * Gets the current scene mode setting. 1445 * 1446 * @return one of SCENE_MODE_XXX string constant. null if scene mode 1447 * setting is not supported. 1448 */ 1449 public String getSceneMode() { 1450 return get(KEY_SCENE_MODE); 1451 } 1452 1453 /** 1454 * Sets the scene mode. Other parameters may be changed after changing 1455 * scene mode. For example, flash and supported flash mode may be 1456 * changed to "off" in night scene mode. After setting scene mode, 1457 * applications should call getParameters to know if some parameters are 1458 * changed. 1459 * 1460 * @param value SCENE_MODE_XXX string constants. 1461 */ 1462 public void setSceneMode(String value) { 1463 set(KEY_SCENE_MODE, value); 1464 } 1465 1466 /** 1467 * Gets the supported scene modes. 1468 * 1469 * @return a List of SCENE_MODE_XXX string constant. null if scene mode 1470 * setting is not supported. 1471 */ 1472 public List<String> getSupportedSceneModes() { 1473 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX); 1474 return split(str); 1475 } 1476 1477 /** 1478 * Gets the current flash mode setting. 1479 * 1480 * @return one of FLASH_MODE_XXX string constant. null if flash mode 1481 * setting is not supported. 1482 */ 1483 public String getFlashMode() { 1484 return get(KEY_FLASH_MODE); 1485 } 1486 1487 /** 1488 * Sets the flash mode. 1489 * 1490 * @param value FLASH_MODE_XXX string constants. 1491 */ 1492 public void setFlashMode(String value) { 1493 set(KEY_FLASH_MODE, value); 1494 } 1495 1496 /** 1497 * Gets the supported flash modes. 1498 * 1499 * @return a List of FLASH_MODE_XXX string constants. null if flash mode 1500 * setting is not supported. 1501 */ 1502 public List<String> getSupportedFlashModes() { 1503 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX); 1504 return split(str); 1505 } 1506 1507 /** 1508 * Gets the current focus mode setting. 1509 * 1510 * @return one of FOCUS_MODE_XXX string constant. If the camera does not 1511 * support auto-focus, this should return {@link 1512 * #FOCUS_MODE_FIXED}. If the focus mode is not FOCUS_MODE_FIXED 1513 * or {@link #FOCUS_MODE_INFINITY}, applications should call 1514 * {@link #autoFocus(AutoFocusCallback)} to start the focus. 1515 */ 1516 public String getFocusMode() { 1517 return get(KEY_FOCUS_MODE); 1518 } 1519 1520 /** 1521 * Sets the focus mode. 1522 * 1523 * @param value FOCUS_MODE_XXX string constants. 1524 */ 1525 public void setFocusMode(String value) { 1526 set(KEY_FOCUS_MODE, value); 1527 } 1528 1529 /** 1530 * Gets the supported focus modes. 1531 * 1532 * @return a List of FOCUS_MODE_XXX string constants. This method will 1533 * always return a list with at least one element. 1534 */ 1535 public List<String> getSupportedFocusModes() { 1536 String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX); 1537 return split(str); 1538 } 1539 1540 /** 1541 * Gets the focal length (in millimeter) of the camera. 1542 * 1543 * @return the focal length. This method will always return a valid 1544 * value. 1545 */ 1546 public float getFocalLength() { 1547 return Float.parseFloat(get(KEY_FOCAL_LENGTH)); 1548 } 1549 1550 /** 1551 * Gets the horizontal angle of view in degrees. 1552 * 1553 * @return horizontal angle of view. This method will always return a 1554 * valid value. 1555 */ 1556 public float getHorizontalViewAngle() { 1557 return Float.parseFloat(get(KEY_HORIZONTAL_VIEW_ANGLE)); 1558 } 1559 1560 /** 1561 * Gets the vertical angle of view in degrees. 1562 * 1563 * @return vertical angle of view. This method will always return a 1564 * valid value. 1565 */ 1566 public float getVerticalViewAngle() { 1567 return Float.parseFloat(get(KEY_VERTICAL_VIEW_ANGLE)); 1568 } 1569 1570 /** 1571 * Gets the current exposure compensation index. 1572 * 1573 * @return current exposure compensation index. The range is {@link 1574 * #getMinExposureCompensation} to {@link 1575 * #getMaxExposureCompensation}. 0 means exposure is not 1576 * adjusted. 1577 */ 1578 public int getExposureCompensation() { 1579 return getInt(KEY_EXPOSURE_COMPENSATION, 0); 1580 } 1581 1582 /** 1583 * Sets the exposure compensation index. 1584 * 1585 * @param value exposure compensation index. The valid value range is 1586 * from {@link #getMinExposureCompensation} (inclusive) to {@link 1587 * #getMaxExposureCompensation} (inclusive). 0 means exposure is 1588 * not adjusted. Application should call 1589 * getMinExposureCompensation and getMaxExposureCompensation to 1590 * know if exposure compensation is supported. 1591 */ 1592 public void setExposureCompensation(int value) { 1593 set(KEY_EXPOSURE_COMPENSATION, value); 1594 } 1595 1596 /** 1597 * Gets the maximum exposure compensation index. 1598 * 1599 * @return maximum exposure compensation index (>=0). If both this 1600 * method and {@link #getMinExposureCompensation} return 0, 1601 * exposure compensation is not supported. 1602 */ 1603 public int getMaxExposureCompensation() { 1604 return getInt(KEY_MAX_EXPOSURE_COMPENSATION, 0); 1605 } 1606 1607 /** 1608 * Gets the minimum exposure compensation index. 1609 * 1610 * @return minimum exposure compensation index (<=0). If both this 1611 * method and {@link #getMaxExposureCompensation} return 0, 1612 * exposure compensation is not supported. 1613 */ 1614 public int getMinExposureCompensation() { 1615 return getInt(KEY_MIN_EXPOSURE_COMPENSATION, 0); 1616 } 1617 1618 /** 1619 * Gets the exposure compensation step. 1620 * 1621 * @return exposure compensation step. Applications can get EV by 1622 * multiplying the exposure compensation index and step. Ex: if 1623 * exposure compensation index is -6 and step is 0.333333333, EV 1624 * is -2. 1625 */ 1626 public float getExposureCompensationStep() { 1627 return getFloat(KEY_EXPOSURE_COMPENSATION_STEP, 0); 1628 } 1629 1630 /** 1631 * Gets current zoom value. This also works when smooth zoom is in 1632 * progress. Applications should check {@link #isZoomSupported} before 1633 * using this method. 1634 * 1635 * @return the current zoom value. The range is 0 to {@link 1636 * #getMaxZoom}. 0 means the camera is not zoomed. 1637 */ 1638 public int getZoom() { 1639 return getInt(KEY_ZOOM, 0); 1640 } 1641 1642 /** 1643 * Sets current zoom value. If the camera is zoomed (value > 0), the 1644 * actual picture size may be smaller than picture size setting. 1645 * Applications can check the actual picture size after picture is 1646 * returned from {@link PictureCallback}. The preview size remains the 1647 * same in zoom. Applications should check {@link #isZoomSupported} 1648 * before using this method. 1649 * 1650 * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}. 1651 */ 1652 public void setZoom(int value) { 1653 set(KEY_ZOOM, value); 1654 } 1655 1656 /** 1657 * Returns true if zoom is supported. Applications should call this 1658 * before using other zoom methods. 1659 * 1660 * @return true if zoom is supported. 1661 */ 1662 public boolean isZoomSupported() { 1663 String str = get(KEY_ZOOM_SUPPORTED); 1664 return TRUE.equals(str); 1665 } 1666 1667 /** 1668 * Gets the maximum zoom value allowed for snapshot. This is the maximum 1669 * value that applications can set to {@link #setZoom(int)}. 1670 * Applications should call {@link #isZoomSupported} before using this 1671 * method. This value may change in different preview size. Applications 1672 * should call this again after setting preview size. 1673 * 1674 * @return the maximum zoom value supported by the camera. 1675 */ 1676 public int getMaxZoom() { 1677 return getInt(KEY_MAX_ZOOM, 0); 1678 } 1679 1680 /** 1681 * Gets the zoom ratios of all zoom values. Applications should check 1682 * {@link #isZoomSupported} before using this method. 1683 * 1684 * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is 1685 * returned as 320. The number of elements is {@link 1686 * #getMaxZoom} + 1. The list is sorted from small to large. The 1687 * first element is always 100. The last element is the zoom 1688 * ratio of the maximum zoom value. 1689 */ 1690 public List<Integer> getZoomRatios() { 1691 return splitInt(get(KEY_ZOOM_RATIOS)); 1692 } 1693 1694 /** 1695 * Returns true if smooth zoom is supported. Applications should call 1696 * this before using other smooth zoom methods. 1697 * 1698 * @return true if smooth zoom is supported. 1699 */ 1700 public boolean isSmoothZoomSupported() { 1701 String str = get(KEY_SMOOTH_ZOOM_SUPPORTED); 1702 return TRUE.equals(str); 1703 } 1704 1705 // Splits a comma delimited string to an ArrayList of String. 1706 // Return null if the passing string is null or the size is 0. 1707 private ArrayList<String> split(String str) { 1708 if (str == null) return null; 1709 1710 // Use StringTokenizer because it is faster than split. 1711 StringTokenizer tokenizer = new StringTokenizer(str, ","); 1712 ArrayList<String> substrings = new ArrayList<String>(); 1713 while (tokenizer.hasMoreElements()) { 1714 substrings.add(tokenizer.nextToken()); 1715 } 1716 return substrings; 1717 } 1718 1719 // Splits a comma delimited string to an ArrayList of Integer. 1720 // Return null if the passing string is null or the size is 0. 1721 private ArrayList<Integer> splitInt(String str) { 1722 if (str == null) return null; 1723 1724 StringTokenizer tokenizer = new StringTokenizer(str, ","); 1725 ArrayList<Integer> substrings = new ArrayList<Integer>(); 1726 while (tokenizer.hasMoreElements()) { 1727 String token = tokenizer.nextToken(); 1728 substrings.add(Integer.parseInt(token)); 1729 } 1730 if (substrings.size() == 0) return null; 1731 return substrings; 1732 } 1733 1734 // Returns the value of a float parameter. 1735 private float getFloat(String key, float defaultValue) { 1736 try { 1737 return Float.parseFloat(mMap.get(key)); 1738 } catch (NumberFormatException ex) { 1739 return defaultValue; 1740 } 1741 } 1742 1743 // Returns the value of a integer parameter. 1744 private int getInt(String key, int defaultValue) { 1745 try { 1746 return Integer.parseInt(mMap.get(key)); 1747 } catch (NumberFormatException ex) { 1748 return defaultValue; 1749 } 1750 } 1751 1752 // Splits a comma delimited string to an ArrayList of Size. 1753 // Return null if the passing string is null or the size is 0. 1754 private ArrayList<Size> splitSize(String str) { 1755 if (str == null) return null; 1756 1757 StringTokenizer tokenizer = new StringTokenizer(str, ","); 1758 ArrayList<Size> sizeList = new ArrayList<Size>(); 1759 while (tokenizer.hasMoreElements()) { 1760 Size size = strToSize(tokenizer.nextToken()); 1761 if (size != null) sizeList.add(size); 1762 } 1763 if (sizeList.size() == 0) return null; 1764 return sizeList; 1765 } 1766 1767 // Parses a string (ex: "480x320") to Size object. 1768 // Return null if the passing string is null. 1769 private Size strToSize(String str) { 1770 if (str == null) return null; 1771 1772 int pos = str.indexOf('x'); 1773 if (pos != -1) { 1774 String width = str.substring(0, pos); 1775 String height = str.substring(pos + 1); 1776 return new Size(Integer.parseInt(width), 1777 Integer.parseInt(height)); 1778 } 1779 Log.e(TAG, "Invalid size parameter string=" + str); 1780 return null; 1781 } 1782 }; 1783} 1784