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