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