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