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