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