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