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