Camera.java revision 6bbda22e1c5604b0866068913c7466fa23d47818
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 android.app.ActivityThread; 20import android.annotation.SdkConstant; 21import android.annotation.SdkConstant.SdkConstantType; 22import android.content.Context; 23import android.graphics.ImageFormat; 24import android.graphics.Point; 25import android.graphics.Rect; 26import android.graphics.SurfaceTexture; 27import android.media.IAudioService; 28import android.os.Handler; 29import android.os.IBinder; 30import android.os.Looper; 31import android.os.Message; 32import android.os.RemoteException; 33import android.os.ServiceManager; 34import android.renderscript.Allocation; 35import android.renderscript.Element; 36import android.renderscript.RenderScript; 37import android.renderscript.RSIllegalArgumentException; 38import android.renderscript.Type; 39import android.util.Log; 40import android.text.TextUtils; 41import android.view.Surface; 42import android.view.SurfaceHolder; 43 44import java.io.IOException; 45import java.lang.ref.WeakReference; 46import java.util.ArrayList; 47import java.util.HashMap; 48import java.util.List; 49 50/** 51 * The Camera class is used to set image capture settings, start/stop preview, 52 * snap pictures, and retrieve frames for encoding for video. This class is a 53 * client for the Camera service, which manages the actual camera hardware. 54 * 55 * <p>To access the device camera, you must declare the 56 * {@link android.Manifest.permission#CAMERA} permission in your Android 57 * Manifest. Also be sure to include the 58 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> 59 * manifest element to declare camera features used by your application. 60 * For example, if you use the camera and auto-focus feature, your Manifest 61 * should include the following:</p> 62 * <pre> <uses-permission android:name="android.permission.CAMERA" /> 63 * <uses-feature android:name="android.hardware.camera" /> 64 * <uses-feature android:name="android.hardware.camera.autofocus" /></pre> 65 * 66 * <p>To take pictures with this class, use the following steps:</p> 67 * 68 * <ol> 69 * <li>Obtain an instance of Camera from {@link #open(int)}. 70 * 71 * <li>Get existing (default) settings with {@link #getParameters()}. 72 * 73 * <li>If necessary, modify the returned {@link Camera.Parameters} object and call 74 * {@link #setParameters(Camera.Parameters)}. 75 * 76 * <li>If desired, call {@link #setDisplayOrientation(int)}. 77 * 78 * <li><b>Important</b>: Pass a fully initialized {@link SurfaceHolder} to 79 * {@link #setPreviewDisplay(SurfaceHolder)}. Without a surface, the camera 80 * will be unable to start the preview. 81 * 82 * <li><b>Important</b>: Call {@link #startPreview()} to start updating the 83 * preview surface. Preview must be started before you can take a picture. 84 * 85 * <li>When you want, call {@link #takePicture(Camera.ShutterCallback, 86 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)} to 87 * capture a photo. Wait for the callbacks to provide the actual image data. 88 * 89 * <li>After taking a picture, preview display will have stopped. To take more 90 * photos, call {@link #startPreview()} again first. 91 * 92 * <li>Call {@link #stopPreview()} to stop updating the preview surface. 93 * 94 * <li><b>Important:</b> Call {@link #release()} to release the camera for 95 * use by other applications. Applications should release the camera 96 * immediately in {@link android.app.Activity#onPause()} (and re-{@link #open()} 97 * it in {@link android.app.Activity#onResume()}). 98 * </ol> 99 * 100 * <p>To quickly switch to video recording mode, use these steps:</p> 101 * 102 * <ol> 103 * <li>Obtain and initialize a Camera and start preview as described above. 104 * 105 * <li>Call {@link #unlock()} to allow the media process to access the camera. 106 * 107 * <li>Pass the camera to {@link android.media.MediaRecorder#setCamera(Camera)}. 108 * See {@link android.media.MediaRecorder} information about video recording. 109 * 110 * <li>When finished recording, call {@link #reconnect()} to re-acquire 111 * and re-lock the camera. 112 * 113 * <li>If desired, restart preview and take more photos or videos. 114 * 115 * <li>Call {@link #stopPreview()} and {@link #release()} as described above. 116 * </ol> 117 * 118 * <p>This class is not thread-safe, and is meant for use from one event thread. 119 * Most long-running operations (preview, focus, photo capture, etc) happen 120 * asynchronously and invoke callbacks as necessary. Callbacks will be invoked 121 * on the event thread {@link #open(int)} was called from. This class's methods 122 * must never be called from multiple threads at once.</p> 123 * 124 * <p class="caution"><strong>Caution:</strong> Different Android-powered devices 125 * may have different hardware specifications, such as megapixel ratings and 126 * auto-focus capabilities. In order for your application to be compatible with 127 * more devices, you should not make assumptions about the device camera 128 * specifications.</p> 129 * 130 * <div class="special reference"> 131 * <h3>Developer Guides</h3> 132 * <p>For more information about using cameras, read the 133 * <a href="{@docRoot}guide/topics/media/camera.html">Camera</a> developer guide.</p> 134 * </div> 135 */ 136public class Camera { 137 private static final String TAG = "Camera"; 138 139 // These match the enums in frameworks/base/include/camera/Camera.h 140 private static final int CAMERA_MSG_ERROR = 0x001; 141 private static final int CAMERA_MSG_SHUTTER = 0x002; 142 private static final int CAMERA_MSG_FOCUS = 0x004; 143 private static final int CAMERA_MSG_ZOOM = 0x008; 144 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010; 145 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020; 146 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040; 147 private static final int CAMERA_MSG_RAW_IMAGE = 0x080; 148 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100; 149 private static final int CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x200; 150 private static final int CAMERA_MSG_PREVIEW_METADATA = 0x400; 151 private static final int CAMERA_MSG_FOCUS_MOVE = 0x800; 152 153 private long mNativeContext; // accessed by native methods 154 private EventHandler mEventHandler; 155 private ShutterCallback mShutterCallback; 156 private PictureCallback mRawImageCallback; 157 private PictureCallback mJpegCallback; 158 private PreviewCallback mPreviewCallback; 159 private boolean mUsingPreviewAllocation; 160 private PictureCallback mPostviewCallback; 161 private AutoFocusCallback mAutoFocusCallback; 162 private AutoFocusMoveCallback mAutoFocusMoveCallback; 163 private OnZoomChangeListener mZoomListener; 164 private FaceDetectionListener mFaceListener; 165 private ErrorCallback mErrorCallback; 166 private boolean mOneShot; 167 private boolean mWithBuffer; 168 private boolean mFaceDetectionRunning = false; 169 private Object mAutoFocusCallbackLock = new Object(); 170 171 /** 172 * Broadcast Action: A new picture is taken by the camera, and the entry of 173 * the picture has been added to the media store. 174 * {@link android.content.Intent#getData} is URI of the picture. 175 */ 176 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 177 public static final String ACTION_NEW_PICTURE = "android.hardware.action.NEW_PICTURE"; 178 179 /** 180 * Broadcast Action: A new video is recorded by the camera, and the entry 181 * of the video has been added to the media store. 182 * {@link android.content.Intent#getData} is URI of the video. 183 */ 184 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 185 public static final String ACTION_NEW_VIDEO = "android.hardware.action.NEW_VIDEO"; 186 187 /** 188 * Hardware face detection. It does not use much CPU. 189 */ 190 private static final int CAMERA_FACE_DETECTION_HW = 0; 191 192 /** 193 * Software face detection. It uses some CPU. 194 */ 195 private static final int CAMERA_FACE_DETECTION_SW = 1; 196 197 /** 198 * Returns the number of physical cameras available on this device. 199 */ 200 public native static int getNumberOfCameras(); 201 202 /** 203 * Returns the information about a particular camera. 204 * If {@link #getNumberOfCameras()} returns N, the valid id is 0 to N-1. 205 */ 206 public static void getCameraInfo(int cameraId, CameraInfo cameraInfo) { 207 _getCameraInfo(cameraId, cameraInfo); 208 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE); 209 IAudioService audioService = IAudioService.Stub.asInterface(b); 210 try { 211 if (audioService.isCameraSoundForced()) { 212 // Only set this when sound is forced; otherwise let native code 213 // decide. 214 cameraInfo.canDisableShutterSound = false; 215 } 216 } catch (RemoteException e) { 217 Log.e(TAG, "Audio service is unavailable for queries"); 218 } 219 } 220 private native static void _getCameraInfo(int cameraId, CameraInfo cameraInfo); 221 222 /** 223 * Information about a camera 224 */ 225 public static class CameraInfo { 226 /** 227 * The facing of the camera is opposite to that of the screen. 228 */ 229 public static final int CAMERA_FACING_BACK = 0; 230 231 /** 232 * The facing of the camera is the same as that of the screen. 233 */ 234 public static final int CAMERA_FACING_FRONT = 1; 235 236 /** 237 * The direction that the camera faces. It should be 238 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT. 239 */ 240 public int facing; 241 242 /** 243 * <p>The orientation of the camera image. The value is the angle that the 244 * camera image needs to be rotated clockwise so it shows correctly on 245 * the display in its natural orientation. It should be 0, 90, 180, or 270.</p> 246 * 247 * <p>For example, suppose a device has a naturally tall screen. The 248 * back-facing camera sensor is mounted in landscape. You are looking at 249 * the screen. If the top side of the camera sensor is aligned with the 250 * right edge of the screen in natural orientation, the value should be 251 * 90. If the top side of a front-facing camera sensor is aligned with 252 * the right of the screen, the value should be 270.</p> 253 * 254 * @see #setDisplayOrientation(int) 255 * @see Parameters#setRotation(int) 256 * @see Parameters#setPreviewSize(int, int) 257 * @see Parameters#setPictureSize(int, int) 258 * @see Parameters#setJpegThumbnailSize(int, int) 259 */ 260 public int orientation; 261 262 /** 263 * <p>Whether the shutter sound can be disabled.</p> 264 * 265 * <p>On some devices, the camera shutter sound cannot be turned off 266 * through {@link #enableShutterSound enableShutterSound}. This field 267 * can be used to determine whether a call to disable the shutter sound 268 * will succeed.</p> 269 * 270 * <p>If this field is set to true, then a call of 271 * {@code enableShutterSound(false)} will be successful. If set to 272 * false, then that call will fail, and the shutter sound will be played 273 * when {@link Camera#takePicture takePicture} is called.</p> 274 */ 275 public boolean canDisableShutterSound; 276 }; 277 278 /** 279 * Creates a new Camera object to access a particular hardware camera. If 280 * the same camera is opened by other applications, this will throw a 281 * RuntimeException. 282 * 283 * <p>You must call {@link #release()} when you are done using the camera, 284 * otherwise it will remain locked and be unavailable to other applications. 285 * 286 * <p>Your application should only have one Camera object active at a time 287 * for a particular hardware camera. 288 * 289 * <p>Callbacks from other methods are delivered to the event loop of the 290 * thread which called open(). If this thread has no event loop, then 291 * callbacks are delivered to the main application event loop. If there 292 * is no main application event loop, callbacks are not delivered. 293 * 294 * <p class="caution"><b>Caution:</b> On some devices, this method may 295 * take a long time to complete. It is best to call this method from a 296 * worker thread (possibly using {@link android.os.AsyncTask}) to avoid 297 * blocking the main application UI thread. 298 * 299 * @param cameraId the hardware camera to access, between 0 and 300 * {@link #getNumberOfCameras()}-1. 301 * @return a new Camera object, connected, locked and ready for use. 302 * @throws RuntimeException if opening the camera fails (for example, if the 303 * camera is in use by another process or device policy manager has 304 * disabled the camera). 305 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName) 306 */ 307 public static Camera open(int cameraId) { 308 return new Camera(cameraId); 309 } 310 311 /** 312 * Creates a new Camera object to access the first back-facing camera on the 313 * device. If the device does not have a back-facing camera, this returns 314 * null. 315 * @see #open(int) 316 */ 317 public static Camera open() { 318 int numberOfCameras = getNumberOfCameras(); 319 CameraInfo cameraInfo = new CameraInfo(); 320 for (int i = 0; i < numberOfCameras; i++) { 321 getCameraInfo(i, cameraInfo); 322 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) { 323 return new Camera(i); 324 } 325 } 326 return null; 327 } 328 329 Camera(int cameraId) { 330 mShutterCallback = null; 331 mRawImageCallback = null; 332 mJpegCallback = null; 333 mPreviewCallback = null; 334 mPostviewCallback = null; 335 mUsingPreviewAllocation = false; 336 mZoomListener = null; 337 338 Looper looper; 339 if ((looper = Looper.myLooper()) != null) { 340 mEventHandler = new EventHandler(this, looper); 341 } else if ((looper = Looper.getMainLooper()) != null) { 342 mEventHandler = new EventHandler(this, looper); 343 } else { 344 mEventHandler = null; 345 } 346 347 String packageName = ActivityThread.currentPackageName(); 348 349 native_setup(new WeakReference<Camera>(this), cameraId, packageName); 350 } 351 352 /** 353 * An empty Camera for testing purpose. 354 */ 355 Camera() { 356 } 357 358 protected void finalize() { 359 release(); 360 } 361 362 private native final void native_setup(Object camera_this, int cameraId, 363 String packageName); 364 365 private native final void native_release(); 366 367 368 /** 369 * Disconnects and releases the Camera object resources. 370 * 371 * <p>You must call this as soon as you're done with the Camera object.</p> 372 */ 373 public final void release() { 374 native_release(); 375 mFaceDetectionRunning = false; 376 } 377 378 /** 379 * Unlocks the camera to allow another process to access it. 380 * Normally, the camera is locked to the process with an active Camera 381 * object until {@link #release()} is called. To allow rapid handoff 382 * between processes, you can call this method to release the camera 383 * temporarily for another process to use; once the other process is done 384 * you can call {@link #reconnect()} to reclaim the camera. 385 * 386 * <p>This must be done before calling 387 * {@link android.media.MediaRecorder#setCamera(Camera)}. This cannot be 388 * called after recording starts. 389 * 390 * <p>If you are not recording video, you probably do not need this method. 391 * 392 * @throws RuntimeException if the camera cannot be unlocked. 393 */ 394 public native final void unlock(); 395 396 /** 397 * Re-locks the camera to prevent other processes from accessing it. 398 * Camera objects are locked by default unless {@link #unlock()} is 399 * called. Normally {@link #reconnect()} is used instead. 400 * 401 * <p>Since API level 14, camera is automatically locked for applications in 402 * {@link android.media.MediaRecorder#start()}. Applications can use the 403 * camera (ex: zoom) after recording starts. There is no need to call this 404 * after recording starts or stops. 405 * 406 * <p>If you are not recording video, you probably do not need this method. 407 * 408 * @throws RuntimeException if the camera cannot be re-locked (for 409 * example, if the camera is still in use by another process). 410 */ 411 public native final void lock(); 412 413 /** 414 * Reconnects to the camera service after another process used it. 415 * After {@link #unlock()} is called, another process may use the 416 * camera; when the process is done, you must reconnect to the camera, 417 * which will re-acquire the lock and allow you to continue using the 418 * camera. 419 * 420 * <p>Since API level 14, camera is automatically locked for applications in 421 * {@link android.media.MediaRecorder#start()}. Applications can use the 422 * camera (ex: zoom) after recording starts. There is no need to call this 423 * after recording starts or stops. 424 * 425 * <p>If you are not recording video, you probably do not need this method. 426 * 427 * @throws IOException if a connection cannot be re-established (for 428 * example, if the camera is still in use by another process). 429 */ 430 public native final void reconnect() throws IOException; 431 432 /** 433 * Sets the {@link Surface} to be used for live preview. 434 * Either a surface or surface texture is necessary for preview, and 435 * preview is necessary to take pictures. The same surface can be re-set 436 * without harm. Setting a preview surface will un-set any preview surface 437 * texture that was set via {@link #setPreviewTexture}. 438 * 439 * <p>The {@link SurfaceHolder} must already contain a surface when this 440 * method is called. If you are using {@link android.view.SurfaceView}, 441 * you will need to register a {@link SurfaceHolder.Callback} with 442 * {@link SurfaceHolder#addCallback(SurfaceHolder.Callback)} and wait for 443 * {@link SurfaceHolder.Callback#surfaceCreated(SurfaceHolder)} before 444 * calling setPreviewDisplay() or starting preview. 445 * 446 * <p>This method must be called before {@link #startPreview()}. The 447 * one exception is that if the preview surface is not set (or set to null) 448 * before startPreview() is called, then this method may be called once 449 * with a non-null parameter to set the preview surface. (This allows 450 * camera setup and surface creation to happen in parallel, saving time.) 451 * The preview surface may not otherwise change while preview is running. 452 * 453 * @param holder containing the Surface on which to place the preview, 454 * or null to remove the preview surface 455 * @throws IOException if the method fails (for example, if the surface 456 * is unavailable or unsuitable). 457 */ 458 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException { 459 if (holder != null) { 460 setPreviewDisplay(holder.getSurface()); 461 } else { 462 setPreviewDisplay((Surface)null); 463 } 464 } 465 466 private native final void setPreviewDisplay(Surface surface) throws IOException; 467 468 /** 469 * Sets the {@link SurfaceTexture} to be used for live preview. 470 * Either a surface or surface texture is necessary for preview, and 471 * preview is necessary to take pictures. The same surface texture can be 472 * re-set without harm. Setting a preview surface texture will un-set any 473 * preview surface that was set via {@link #setPreviewDisplay}. 474 * 475 * <p>This method must be called before {@link #startPreview()}. The 476 * one exception is that if the preview surface texture is not set (or set 477 * to null) before startPreview() is called, then this method may be called 478 * once with a non-null parameter to set the preview surface. (This allows 479 * camera setup and surface creation to happen in parallel, saving time.) 480 * The preview surface texture may not otherwise change while preview is 481 * running. 482 * 483 * <p>The timestamps provided by {@link SurfaceTexture#getTimestamp()} for a 484 * SurfaceTexture set as the preview texture have an unspecified zero point, 485 * and cannot be directly compared between different cameras or different 486 * instances of the same camera, or across multiple runs of the same 487 * program. 488 * 489 * <p>If you are using the preview data to create video or still images, 490 * strongly consider using {@link android.media.MediaActionSound} to 491 * properly indicate image capture or recording start/stop to the user.</p> 492 * 493 * @see android.media.MediaActionSound 494 * @see android.graphics.SurfaceTexture 495 * @see android.view.TextureView 496 * @param surfaceTexture the {@link SurfaceTexture} to which the preview 497 * images are to be sent or null to remove the current preview surface 498 * texture 499 * @throws IOException if the method fails (for example, if the surface 500 * texture is unavailable or unsuitable). 501 */ 502 public native final void setPreviewTexture(SurfaceTexture surfaceTexture) throws IOException; 503 504 /** 505 * Callback interface used to deliver copies of preview frames as 506 * they are displayed. 507 * 508 * @see #setPreviewCallback(Camera.PreviewCallback) 509 * @see #setOneShotPreviewCallback(Camera.PreviewCallback) 510 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback) 511 * @see #startPreview() 512 */ 513 public interface PreviewCallback 514 { 515 /** 516 * Called as preview frames are displayed. This callback is invoked 517 * on the event thread {@link #open(int)} was called from. 518 * 519 * <p>If using the {@link android.graphics.ImageFormat#YV12} format, 520 * refer to the equations in {@link Camera.Parameters#setPreviewFormat} 521 * for the arrangement of the pixel data in the preview callback 522 * buffers. 523 * 524 * @param data the contents of the preview frame in the format defined 525 * by {@link android.graphics.ImageFormat}, which can be queried 526 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}. 527 * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)} 528 * is never called, the default will be the YCbCr_420_SP 529 * (NV21) format. 530 * @param camera the Camera service object. 531 */ 532 void onPreviewFrame(byte[] data, Camera camera); 533 }; 534 535 /** 536 * Starts capturing and drawing preview frames to the screen. 537 * Preview will not actually start until a surface is supplied 538 * with {@link #setPreviewDisplay(SurfaceHolder)} or 539 * {@link #setPreviewTexture(SurfaceTexture)}. 540 * 541 * <p>If {@link #setPreviewCallback(Camera.PreviewCallback)}, 542 * {@link #setOneShotPreviewCallback(Camera.PreviewCallback)}, or 543 * {@link #setPreviewCallbackWithBuffer(Camera.PreviewCallback)} were 544 * called, {@link Camera.PreviewCallback#onPreviewFrame(byte[], Camera)} 545 * will be called when preview data becomes available. 546 */ 547 public native final void startPreview(); 548 549 /** 550 * Stops capturing and drawing preview frames to the surface, and 551 * resets the camera for a future call to {@link #startPreview()}. 552 */ 553 public final void stopPreview() { 554 _stopPreview(); 555 mFaceDetectionRunning = false; 556 557 mShutterCallback = null; 558 mRawImageCallback = null; 559 mPostviewCallback = null; 560 mJpegCallback = null; 561 synchronized (mAutoFocusCallbackLock) { 562 mAutoFocusCallback = null; 563 } 564 mAutoFocusMoveCallback = null; 565 } 566 567 private native final void _stopPreview(); 568 569 /** 570 * Return current preview state. 571 * 572 * FIXME: Unhide before release 573 * @hide 574 */ 575 public native final boolean previewEnabled(); 576 577 /** 578 * <p>Installs a callback to be invoked for every preview frame in addition 579 * to displaying them on the screen. The callback will be repeatedly called 580 * for as long as preview is active. This method can be called at any time, 581 * even while preview is live. Any other preview callbacks are 582 * overridden.</p> 583 * 584 * <p>If you are using the preview data to create video or still images, 585 * strongly consider using {@link android.media.MediaActionSound} to 586 * properly indicate image capture or recording start/stop to the user.</p> 587 * 588 * @param cb a callback object that receives a copy of each preview frame, 589 * or null to stop receiving callbacks. 590 * @see android.media.MediaActionSound 591 */ 592 public final void setPreviewCallback(PreviewCallback cb) { 593 mPreviewCallback = cb; 594 mOneShot = false; 595 mWithBuffer = false; 596 if (cb != null) { 597 mUsingPreviewAllocation = false; 598 } 599 // Always use one-shot mode. We fake camera preview mode by 600 // doing one-shot preview continuously. 601 setHasPreviewCallback(cb != null, false); 602 } 603 604 /** 605 * <p>Installs a callback to be invoked for the next preview frame in 606 * addition to displaying it on the screen. After one invocation, the 607 * callback is cleared. This method can be called any time, even when 608 * preview is live. Any other preview callbacks are overridden.</p> 609 * 610 * <p>If you are using the preview data to create video or still images, 611 * strongly consider using {@link android.media.MediaActionSound} to 612 * properly indicate image capture or recording start/stop to the user.</p> 613 * 614 * @param cb a callback object that receives a copy of the next preview frame, 615 * or null to stop receiving callbacks. 616 * @see android.media.MediaActionSound 617 */ 618 public final void setOneShotPreviewCallback(PreviewCallback cb) { 619 mPreviewCallback = cb; 620 mOneShot = true; 621 mWithBuffer = false; 622 if (cb != null) { 623 mUsingPreviewAllocation = false; 624 } 625 setHasPreviewCallback(cb != null, false); 626 } 627 628 private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer); 629 630 /** 631 * <p>Installs a callback to be invoked for every preview frame, using 632 * buffers supplied with {@link #addCallbackBuffer(byte[])}, in addition to 633 * displaying them on the screen. The callback will be repeatedly called 634 * for as long as preview is active and buffers are available. Any other 635 * preview callbacks are overridden.</p> 636 * 637 * <p>The purpose of this method is to improve preview efficiency and frame 638 * rate by allowing preview frame memory reuse. You must call 639 * {@link #addCallbackBuffer(byte[])} at some point -- before or after 640 * calling this method -- or no callbacks will received.</p> 641 * 642 * <p>The buffer queue will be cleared if this method is called with a null 643 * callback, {@link #setPreviewCallback(Camera.PreviewCallback)} is called, 644 * or {@link #setOneShotPreviewCallback(Camera.PreviewCallback)} is 645 * called.</p> 646 * 647 * <p>If you are using the preview data to create video or still images, 648 * strongly consider using {@link android.media.MediaActionSound} to 649 * properly indicate image capture or recording start/stop to the user.</p> 650 * 651 * @param cb a callback object that receives a copy of the preview frame, 652 * or null to stop receiving callbacks and clear the buffer queue. 653 * @see #addCallbackBuffer(byte[]) 654 * @see android.media.MediaActionSound 655 */ 656 public final void setPreviewCallbackWithBuffer(PreviewCallback cb) { 657 mPreviewCallback = cb; 658 mOneShot = false; 659 mWithBuffer = true; 660 if (cb != null) { 661 mUsingPreviewAllocation = false; 662 } 663 setHasPreviewCallback(cb != null, true); 664 } 665 666 /** 667 * Adds a pre-allocated buffer to the preview callback buffer queue. 668 * Applications can add one or more buffers to the queue. When a preview 669 * frame arrives and there is still at least one available buffer, the 670 * buffer will be used and removed from the queue. Then preview callback is 671 * invoked with the buffer. If a frame arrives and there is no buffer left, 672 * the frame is discarded. Applications should add buffers back when they 673 * finish processing the data in them. 674 * 675 * <p>For formats besides YV12, the size of the buffer is determined by 676 * multiplying the preview image width, height, and bytes per pixel. The 677 * width and height can be read from 678 * {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel can be 679 * computed from {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 680 * 8, using the image format from 681 * {@link Camera.Parameters#getPreviewFormat()}. 682 * 683 * <p>If using the {@link android.graphics.ImageFormat#YV12} format, the 684 * size can be calculated using the equations listed in 685 * {@link Camera.Parameters#setPreviewFormat}. 686 * 687 * <p>This method is only necessary when 688 * {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When 689 * {@link #setPreviewCallback(PreviewCallback)} or 690 * {@link #setOneShotPreviewCallback(PreviewCallback)} are used, buffers 691 * are automatically allocated. When a supplied buffer is too small to 692 * hold the preview frame data, preview callback will return null and 693 * the buffer will be removed from the buffer queue. 694 * 695 * @param callbackBuffer the buffer to add to the queue. The size of the 696 * buffer must match the values described above. 697 * @see #setPreviewCallbackWithBuffer(PreviewCallback) 698 */ 699 public final void addCallbackBuffer(byte[] callbackBuffer) 700 { 701 _addCallbackBuffer(callbackBuffer, CAMERA_MSG_PREVIEW_FRAME); 702 } 703 704 /** 705 * Adds a pre-allocated buffer to the raw image callback buffer queue. 706 * Applications can add one or more buffers to the queue. When a raw image 707 * frame arrives and there is still at least one available buffer, the 708 * buffer will be used to hold the raw image data and removed from the 709 * queue. Then raw image callback is invoked with the buffer. If a raw 710 * image frame arrives but there is no buffer left, the frame is 711 * discarded. Applications should add buffers back when they finish 712 * processing the data in them by calling this method again in order 713 * to avoid running out of raw image callback buffers. 714 * 715 * <p>The size of the buffer is determined by multiplying the raw image 716 * width, height, and bytes per pixel. The width and height can be 717 * read from {@link Camera.Parameters#getPictureSize()}. Bytes per pixel 718 * can be computed from 719 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8, 720 * using the image format from {@link Camera.Parameters#getPreviewFormat()}. 721 * 722 * <p>This method is only necessary when the PictureCallbck for raw image 723 * is used while calling {@link #takePicture(Camera.ShutterCallback, 724 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}. 725 * 726 * <p>Please note that by calling this method, the mode for 727 * application-managed callback buffers is triggered. If this method has 728 * never been called, null will be returned by the raw image callback since 729 * there is no image callback buffer available. Furthermore, When a supplied 730 * buffer is too small to hold the raw image data, raw image callback will 731 * return null and the buffer will be removed from the buffer queue. 732 * 733 * @param callbackBuffer the buffer to add to the raw image callback buffer 734 * queue. The size should be width * height * (bits per pixel) / 8. An 735 * null callbackBuffer will be ignored and won't be added to the queue. 736 * 737 * @see #takePicture(Camera.ShutterCallback, 738 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}. 739 * 740 * {@hide} 741 */ 742 public final void addRawImageCallbackBuffer(byte[] callbackBuffer) 743 { 744 addCallbackBuffer(callbackBuffer, CAMERA_MSG_RAW_IMAGE); 745 } 746 747 private final void addCallbackBuffer(byte[] callbackBuffer, int msgType) 748 { 749 // CAMERA_MSG_VIDEO_FRAME may be allowed in the future. 750 if (msgType != CAMERA_MSG_PREVIEW_FRAME && 751 msgType != CAMERA_MSG_RAW_IMAGE) { 752 throw new IllegalArgumentException( 753 "Unsupported message type: " + msgType); 754 } 755 756 _addCallbackBuffer(callbackBuffer, msgType); 757 } 758 759 private native final void _addCallbackBuffer( 760 byte[] callbackBuffer, int msgType); 761 762 /** 763 * <p>Create a {@link android.renderscript RenderScript} 764 * {@link android.renderscript.Allocation Allocation} to use as a 765 * destination of preview callback frames. Use 766 * {@link #setPreviewCallbackAllocation setPreviewCallbackAllocation} to use 767 * the created Allocation as a destination for camera preview frames.</p> 768 * 769 * <p>The Allocation will be created with a YUV type, and its contents must 770 * be accessed within Renderscript with the {@code rsGetElementAtYuv_*} 771 * accessor methods. Its size will be based on the current 772 * {@link Parameters#getPreviewSize preview size} configured for this 773 * camera.</p> 774 * 775 * @param rs the RenderScript context for this Allocation. 776 * @param usage additional usage flags to set for the Allocation. The usage 777 * flag {@link android.renderscript.Allocation#USAGE_IO_INPUT} will always 778 * be set on the created Allocation, but additional flags may be provided 779 * here. 780 * @return a new YUV-type Allocation with dimensions equal to the current 781 * preview size. 782 * @throws RSIllegalArgumentException if the usage flags are not compatible 783 * with an YUV Allocation. 784 * @see #setPreviewCallbackAllocation 785 * @hide 786 */ 787 public final Allocation createPreviewAllocation(RenderScript rs, int usage) 788 throws RSIllegalArgumentException { 789 Parameters p = getParameters(); 790 Size previewSize = p.getPreviewSize(); 791 Type.Builder yuvBuilder = new Type.Builder(rs, 792 Element.createPixel(rs, 793 Element.DataType.UNSIGNED_8, 794 Element.DataKind.PIXEL_YUV)); 795 // Use YV12 for wide compatibility. Changing this requires also 796 // adjusting camera service's format selection. 797 yuvBuilder.setYuvFormat(ImageFormat.YV12); 798 yuvBuilder.setX(previewSize.width); 799 yuvBuilder.setY(previewSize.height); 800 801 Allocation a = Allocation.createTyped(rs, yuvBuilder.create(), 802 usage | Allocation.USAGE_IO_INPUT); 803 804 return a; 805 } 806 807 /** 808 * <p>Set an {@link android.renderscript.Allocation Allocation} as the 809 * target of preview callback data. Use this method for efficient processing 810 * of camera preview data with RenderScript. The Allocation must be created 811 * with the {@link #createPreviewAllocation createPreviewAllocation } 812 * method.</p> 813 * 814 * <p>Setting a preview allocation will disable any active preview callbacks 815 * set by {@link #setPreviewCallback setPreviewCallback} or 816 * {@link #setPreviewCallbackWithBuffer setPreviewCallbackWithBuffer}, and 817 * vice versa. Using a preview allocation still requires an active standard 818 * preview target to be set, either with 819 * {@link #setPreviewTexture setPreviewTexture} or 820 * {@link #setPreviewDisplay setPreviewDisplay}.</p> 821 * 822 * <p>To be notified when new frames are available to the Allocation, use 823 * {@link android.renderscript.Allocation#setIoInputNotificationHandler Allocation.setIoInputNotificationHandler}. To 824 * update the frame currently accessible from the Allocation to the latest 825 * preview frame, call 826 * {@link android.renderscript.Allocation#ioReceive Allocation.ioReceive}.</p> 827 * 828 * <p>To disable preview into the Allocation, call this method with a 829 * {@code null} parameter.</p> 830 * 831 * <p>Once a preview allocation is set, the preview size set by 832 * {@link Parameters#setPreviewSize setPreviewSize} cannot be changed. If 833 * you wish to change the preview size, first remove the preview allocation 834 * by calling {@code setPreviewCallbackAllocation(null)}, then change the 835 * preview size, create a new preview Allocation with 836 * {@link #createPreviewAllocation createPreviewAllocation}, and set it as 837 * the new preview callback allocation target.</p> 838 * 839 * <p>If you are using the preview data to create video or still images, 840 * strongly consider using {@link android.media.MediaActionSound} to 841 * properly indicate image capture or recording start/stop to the user.</p> 842 * 843 * @param previewAllocation the allocation to use as destination for preview 844 * @throws IOException if configuring the camera to use the Allocation for 845 * preview fails. 846 * @throws IllegalArgumentException if the Allocation's dimensions or other 847 * parameters don't meet the requirements. 848 * @see #createPreviewAllocation 849 * @see #setPreviewCallback 850 * @see #setPreviewCallbackWithBuffer 851 * @hide 852 */ 853 public final void setPreviewCallbackAllocation(Allocation previewAllocation) 854 throws IOException { 855 Surface previewSurface = null; 856 if (previewAllocation != null) { 857 Parameters p = getParameters(); 858 Size previewSize = p.getPreviewSize(); 859 if (previewSize.width != previewAllocation.getType().getX() || 860 previewSize.height != previewAllocation.getType().getY()) { 861 throw new IllegalArgumentException( 862 "Allocation dimensions don't match preview dimensions: " + 863 "Allocation is " + 864 previewAllocation.getType().getX() + 865 ", " + 866 previewAllocation.getType().getY() + 867 ". Preview is " + previewSize.width + ", " + 868 previewSize.height); 869 } 870 if ((previewAllocation.getUsage() & 871 Allocation.USAGE_IO_INPUT) == 0) { 872 throw new IllegalArgumentException( 873 "Allocation usage does not include USAGE_IO_INPUT"); 874 } 875 if (previewAllocation.getType().getElement().getDataKind() != 876 Element.DataKind.PIXEL_YUV) { 877 throw new IllegalArgumentException( 878 "Allocation is not of a YUV type"); 879 } 880 previewSurface = previewAllocation.getSurface(); 881 mUsingPreviewAllocation = true; 882 } else { 883 mUsingPreviewAllocation = false; 884 } 885 setPreviewCallbackSurface(previewSurface); 886 } 887 888 private native final void setPreviewCallbackSurface(Surface s); 889 890 private class EventHandler extends Handler 891 { 892 private Camera mCamera; 893 894 public EventHandler(Camera c, Looper looper) { 895 super(looper); 896 mCamera = c; 897 } 898 899 @Override 900 public void handleMessage(Message msg) { 901 switch(msg.what) { 902 case CAMERA_MSG_SHUTTER: 903 if (mShutterCallback != null) { 904 mShutterCallback.onShutter(); 905 } 906 return; 907 908 case CAMERA_MSG_RAW_IMAGE: 909 if (mRawImageCallback != null) { 910 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera); 911 } 912 return; 913 914 case CAMERA_MSG_COMPRESSED_IMAGE: 915 if (mJpegCallback != null) { 916 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera); 917 } 918 return; 919 920 case CAMERA_MSG_PREVIEW_FRAME: 921 PreviewCallback pCb = mPreviewCallback; 922 if (pCb != null) { 923 if (mOneShot) { 924 // Clear the callback variable before the callback 925 // in case the app calls setPreviewCallback from 926 // the callback function 927 mPreviewCallback = null; 928 } else if (!mWithBuffer) { 929 // We're faking the camera preview mode to prevent 930 // the app from being flooded with preview frames. 931 // Set to oneshot mode again. 932 setHasPreviewCallback(true, false); 933 } 934 pCb.onPreviewFrame((byte[])msg.obj, mCamera); 935 } 936 return; 937 938 case CAMERA_MSG_POSTVIEW_FRAME: 939 if (mPostviewCallback != null) { 940 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera); 941 } 942 return; 943 944 case CAMERA_MSG_FOCUS: 945 AutoFocusCallback cb = null; 946 synchronized (mAutoFocusCallbackLock) { 947 cb = mAutoFocusCallback; 948 } 949 if (cb != null) { 950 boolean success = msg.arg1 == 0 ? false : true; 951 cb.onAutoFocus(success, mCamera); 952 } 953 return; 954 955 case CAMERA_MSG_ZOOM: 956 if (mZoomListener != null) { 957 mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera); 958 } 959 return; 960 961 case CAMERA_MSG_PREVIEW_METADATA: 962 if (mFaceListener != null) { 963 mFaceListener.onFaceDetection((Face[])msg.obj, mCamera); 964 } 965 return; 966 967 case CAMERA_MSG_ERROR : 968 Log.e(TAG, "Error " + msg.arg1); 969 if (mErrorCallback != null) { 970 mErrorCallback.onError(msg.arg1, mCamera); 971 } 972 return; 973 974 case CAMERA_MSG_FOCUS_MOVE: 975 if (mAutoFocusMoveCallback != null) { 976 mAutoFocusMoveCallback.onAutoFocusMoving(msg.arg1 == 0 ? false : true, mCamera); 977 } 978 return; 979 980 default: 981 Log.e(TAG, "Unknown message type " + msg.what); 982 return; 983 } 984 } 985 } 986 987 private static void postEventFromNative(Object camera_ref, 988 int what, int arg1, int arg2, Object obj) 989 { 990 Camera c = (Camera)((WeakReference)camera_ref).get(); 991 if (c == null) 992 return; 993 994 if (c.mEventHandler != null) { 995 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj); 996 c.mEventHandler.sendMessage(m); 997 } 998 } 999 1000 /** 1001 * Callback interface used to notify on completion of camera auto focus. 1002 * 1003 * <p>Devices that do not support auto-focus will receive a "fake" 1004 * callback to this interface. If your application needs auto-focus and 1005 * should not be installed on devices <em>without</em> auto-focus, you must 1006 * declare that your app uses the 1007 * {@code android.hardware.camera.autofocus} feature, in the 1008 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> 1009 * manifest element.</p> 1010 * 1011 * @see #autoFocus(AutoFocusCallback) 1012 */ 1013 public interface AutoFocusCallback 1014 { 1015 /** 1016 * Called when the camera auto focus completes. If the camera 1017 * does not support auto-focus and autoFocus is called, 1018 * onAutoFocus will be called immediately with a fake value of 1019 * <code>success</code> set to <code>true</code>. 1020 * 1021 * The auto-focus routine does not lock auto-exposure and auto-white 1022 * balance after it completes. 1023 * 1024 * @param success true if focus was successful, false if otherwise 1025 * @param camera the Camera service object 1026 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean) 1027 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean) 1028 */ 1029 void onAutoFocus(boolean success, Camera camera); 1030 } 1031 1032 /** 1033 * Starts camera auto-focus and registers a callback function to run when 1034 * the camera is focused. This method is only valid when preview is active 1035 * (between {@link #startPreview()} and before {@link #stopPreview()}). 1036 * 1037 * <p>Callers should check 1038 * {@link android.hardware.Camera.Parameters#getFocusMode()} to determine if 1039 * this method should be called. If the camera does not support auto-focus, 1040 * it is a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)} 1041 * callback will be called immediately. 1042 * 1043 * <p>If your application should not be installed 1044 * on devices without auto-focus, you must declare that your application 1045 * uses auto-focus with the 1046 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> 1047 * manifest element.</p> 1048 * 1049 * <p>If the current flash mode is not 1050 * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be 1051 * fired during auto-focus, depending on the driver and camera hardware.<p> 1052 * 1053 * <p>Auto-exposure lock {@link android.hardware.Camera.Parameters#getAutoExposureLock()} 1054 * and auto-white balance locks {@link android.hardware.Camera.Parameters#getAutoWhiteBalanceLock()} 1055 * do not change during and after autofocus. But auto-focus routine may stop 1056 * auto-exposure and auto-white balance transiently during focusing. 1057 * 1058 * <p>Stopping preview with {@link #stopPreview()}, or triggering still 1059 * image capture with {@link #takePicture(Camera.ShutterCallback, 1060 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the 1061 * the focus position. Applications must call cancelAutoFocus to reset the 1062 * focus.</p> 1063 * 1064 * <p>If autofocus is successful, consider using 1065 * {@link android.media.MediaActionSound} to properly play back an autofocus 1066 * success sound to the user.</p> 1067 * 1068 * @param cb the callback to run 1069 * @see #cancelAutoFocus() 1070 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean) 1071 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean) 1072 * @see android.media.MediaActionSound 1073 */ 1074 public final void autoFocus(AutoFocusCallback cb) 1075 { 1076 synchronized (mAutoFocusCallbackLock) { 1077 mAutoFocusCallback = cb; 1078 } 1079 native_autoFocus(); 1080 } 1081 private native final void native_autoFocus(); 1082 1083 /** 1084 * Cancels any auto-focus function in progress. 1085 * Whether or not auto-focus is currently in progress, 1086 * this function will return the focus position to the default. 1087 * If the camera does not support auto-focus, this is a no-op. 1088 * 1089 * @see #autoFocus(Camera.AutoFocusCallback) 1090 */ 1091 public final void cancelAutoFocus() 1092 { 1093 synchronized (mAutoFocusCallbackLock) { 1094 mAutoFocusCallback = null; 1095 } 1096 native_cancelAutoFocus(); 1097 // CAMERA_MSG_FOCUS should be removed here because the following 1098 // scenario can happen: 1099 // - An application uses the same thread for autoFocus, cancelAutoFocus 1100 // and looper thread. 1101 // - The application calls autoFocus. 1102 // - HAL sends CAMERA_MSG_FOCUS, which enters the looper message queue. 1103 // Before event handler's handleMessage() is invoked, the application 1104 // calls cancelAutoFocus and autoFocus. 1105 // - The application gets the old CAMERA_MSG_FOCUS and thinks autofocus 1106 // has been completed. But in fact it is not. 1107 // 1108 // As documented in the beginning of the file, apps should not use 1109 // multiple threads to call autoFocus and cancelAutoFocus at the same 1110 // time. It is HAL's responsibility not to send a CAMERA_MSG_FOCUS 1111 // message after native_cancelAutoFocus is called. 1112 mEventHandler.removeMessages(CAMERA_MSG_FOCUS); 1113 } 1114 private native final void native_cancelAutoFocus(); 1115 1116 /** 1117 * Callback interface used to notify on auto focus start and stop. 1118 * 1119 * <p>This is only supported in continuous autofocus modes -- {@link 1120 * Parameters#FOCUS_MODE_CONTINUOUS_VIDEO} and {@link 1121 * Parameters#FOCUS_MODE_CONTINUOUS_PICTURE}. Applications can show 1122 * autofocus animation based on this.</p> 1123 */ 1124 public interface AutoFocusMoveCallback 1125 { 1126 /** 1127 * Called when the camera auto focus starts or stops. 1128 * 1129 * @param start true if focus starts to move, false if focus stops to move 1130 * @param camera the Camera service object 1131 */ 1132 void onAutoFocusMoving(boolean start, Camera camera); 1133 } 1134 1135 /** 1136 * Sets camera auto-focus move callback. 1137 * 1138 * @param cb the callback to run 1139 */ 1140 public void setAutoFocusMoveCallback(AutoFocusMoveCallback cb) { 1141 mAutoFocusMoveCallback = cb; 1142 enableFocusMoveCallback((mAutoFocusMoveCallback != null) ? 1 : 0); 1143 } 1144 1145 private native void enableFocusMoveCallback(int enable); 1146 1147 /** 1148 * Callback interface used to signal the moment of actual image capture. 1149 * 1150 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback) 1151 */ 1152 public interface ShutterCallback 1153 { 1154 /** 1155 * Called as near as possible to the moment when a photo is captured 1156 * from the sensor. This is a good opportunity to play a shutter sound 1157 * or give other feedback of camera operation. This may be some time 1158 * after the photo was triggered, but some time before the actual data 1159 * is available. 1160 */ 1161 void onShutter(); 1162 } 1163 1164 /** 1165 * Callback interface used to supply image data from a photo capture. 1166 * 1167 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback) 1168 */ 1169 public interface PictureCallback { 1170 /** 1171 * Called when image data is available after a picture is taken. 1172 * The format of the data depends on the context of the callback 1173 * and {@link Camera.Parameters} settings. 1174 * 1175 * @param data a byte array of the picture data 1176 * @param camera the Camera service object 1177 */ 1178 void onPictureTaken(byte[] data, Camera camera); 1179 }; 1180 1181 /** 1182 * Equivalent to takePicture(shutter, raw, null, jpeg). 1183 * 1184 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback) 1185 */ 1186 public final void takePicture(ShutterCallback shutter, PictureCallback raw, 1187 PictureCallback jpeg) { 1188 takePicture(shutter, raw, null, jpeg); 1189 } 1190 private native final void native_takePicture(int msgType); 1191 1192 /** 1193 * Triggers an asynchronous image capture. The camera service will initiate 1194 * a series of callbacks to the application as the image capture progresses. 1195 * The shutter callback occurs after the image is captured. This can be used 1196 * to trigger a sound to let the user know that image has been captured. The 1197 * raw callback occurs when the raw image data is available (NOTE: the data 1198 * will be null if there is no raw image callback buffer available or the 1199 * raw image callback buffer is not large enough to hold the raw image). 1200 * The postview callback occurs when a scaled, fully processed postview 1201 * image is available (NOTE: not all hardware supports this). The jpeg 1202 * callback occurs when the compressed image is available. If the 1203 * application does not need a particular callback, a null can be passed 1204 * instead of a callback method. 1205 * 1206 * <p>This method is only valid when preview is active (after 1207 * {@link #startPreview()}). Preview will be stopped after the image is 1208 * taken; callers must call {@link #startPreview()} again if they want to 1209 * re-start preview or take more pictures. This should not be called between 1210 * {@link android.media.MediaRecorder#start()} and 1211 * {@link android.media.MediaRecorder#stop()}. 1212 * 1213 * <p>After calling this method, you must not call {@link #startPreview()} 1214 * or take another picture until the JPEG callback has returned. 1215 * 1216 * @param shutter the callback for image capture moment, or null 1217 * @param raw the callback for raw (uncompressed) image data, or null 1218 * @param postview callback with postview image data, may be null 1219 * @param jpeg the callback for JPEG image data, or null 1220 */ 1221 public final void takePicture(ShutterCallback shutter, PictureCallback raw, 1222 PictureCallback postview, PictureCallback jpeg) { 1223 mShutterCallback = shutter; 1224 mRawImageCallback = raw; 1225 mPostviewCallback = postview; 1226 mJpegCallback = jpeg; 1227 1228 // If callback is not set, do not send me callbacks. 1229 int msgType = 0; 1230 if (mShutterCallback != null) { 1231 msgType |= CAMERA_MSG_SHUTTER; 1232 } 1233 if (mRawImageCallback != null) { 1234 msgType |= CAMERA_MSG_RAW_IMAGE; 1235 } 1236 if (mPostviewCallback != null) { 1237 msgType |= CAMERA_MSG_POSTVIEW_FRAME; 1238 } 1239 if (mJpegCallback != null) { 1240 msgType |= CAMERA_MSG_COMPRESSED_IMAGE; 1241 } 1242 1243 native_takePicture(msgType); 1244 mFaceDetectionRunning = false; 1245 } 1246 1247 /** 1248 * Zooms to the requested value smoothly. The driver will notify {@link 1249 * OnZoomChangeListener} of the zoom value and whether zoom is stopped at 1250 * the time. For example, suppose the current zoom is 0 and startSmoothZoom 1251 * is called with value 3. The 1252 * {@link Camera.OnZoomChangeListener#onZoomChange(int, boolean, Camera)} 1253 * method will be called three times with zoom values 1, 2, and 3. 1254 * Applications can call {@link #stopSmoothZoom} to stop the zoom earlier. 1255 * Applications should not call startSmoothZoom again or change the zoom 1256 * value before zoom stops. If the supplied zoom value equals to the current 1257 * zoom value, no zoom callback will be generated. This method is supported 1258 * if {@link android.hardware.Camera.Parameters#isSmoothZoomSupported} 1259 * returns true. 1260 * 1261 * @param value zoom value. The valid range is 0 to {@link 1262 * android.hardware.Camera.Parameters#getMaxZoom}. 1263 * @throws IllegalArgumentException if the zoom value is invalid. 1264 * @throws RuntimeException if the method fails. 1265 * @see #setZoomChangeListener(OnZoomChangeListener) 1266 */ 1267 public native final void startSmoothZoom(int value); 1268 1269 /** 1270 * Stops the smooth zoom. Applications should wait for the {@link 1271 * OnZoomChangeListener} to know when the zoom is actually stopped. This 1272 * method is supported if {@link 1273 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true. 1274 * 1275 * @throws RuntimeException if the method fails. 1276 */ 1277 public native final void stopSmoothZoom(); 1278 1279 /** 1280 * Set the clockwise rotation of preview display in degrees. This affects 1281 * the preview frames and the picture displayed after snapshot. This method 1282 * is useful for portrait mode applications. Note that preview display of 1283 * front-facing cameras is flipped horizontally before the rotation, that 1284 * is, the image is reflected along the central vertical axis of the camera 1285 * sensor. So the users can see themselves as looking into a mirror. 1286 * 1287 * <p>This does not affect the order of byte array passed in {@link 1288 * PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This 1289 * method is not allowed to be called during preview. 1290 * 1291 * <p>If you want to make the camera image show in the same orientation as 1292 * the display, you can use the following code. 1293 * <pre> 1294 * public static void setCameraDisplayOrientation(Activity activity, 1295 * int cameraId, android.hardware.Camera camera) { 1296 * android.hardware.Camera.CameraInfo info = 1297 * new android.hardware.Camera.CameraInfo(); 1298 * android.hardware.Camera.getCameraInfo(cameraId, info); 1299 * int rotation = activity.getWindowManager().getDefaultDisplay() 1300 * .getRotation(); 1301 * int degrees = 0; 1302 * switch (rotation) { 1303 * case Surface.ROTATION_0: degrees = 0; break; 1304 * case Surface.ROTATION_90: degrees = 90; break; 1305 * case Surface.ROTATION_180: degrees = 180; break; 1306 * case Surface.ROTATION_270: degrees = 270; break; 1307 * } 1308 * 1309 * int result; 1310 * if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 1311 * result = (info.orientation + degrees) % 360; 1312 * result = (360 - result) % 360; // compensate the mirror 1313 * } else { // back-facing 1314 * result = (info.orientation - degrees + 360) % 360; 1315 * } 1316 * camera.setDisplayOrientation(result); 1317 * } 1318 * </pre> 1319 * 1320 * <p>Starting from API level 14, this method can be called when preview is 1321 * active. 1322 * 1323 * @param degrees the angle that the picture will be rotated clockwise. 1324 * Valid values are 0, 90, 180, and 270. The starting 1325 * position is 0 (landscape). 1326 * @see #setPreviewDisplay(SurfaceHolder) 1327 */ 1328 public native final void setDisplayOrientation(int degrees); 1329 1330 /** 1331 * <p>Enable or disable the default shutter sound when taking a picture.</p> 1332 * 1333 * <p>By default, the camera plays the system-defined camera shutter sound 1334 * when {@link #takePicture} is called. Using this method, the shutter sound 1335 * can be disabled. It is strongly recommended that an alternative shutter 1336 * sound is played in the {@link ShutterCallback} when the system shutter 1337 * sound is disabled.</p> 1338 * 1339 * <p>Note that devices may not always allow disabling the camera shutter 1340 * sound. If the shutter sound state cannot be set to the desired value, 1341 * this method will return false. {@link CameraInfo#canDisableShutterSound} 1342 * can be used to determine whether the device will allow the shutter sound 1343 * to be disabled.</p> 1344 * 1345 * @param enabled whether the camera should play the system shutter sound 1346 * when {@link #takePicture takePicture} is called. 1347 * @return {@code true} if the shutter sound state was successfully 1348 * changed. {@code false} if the shutter sound state could not be 1349 * changed. {@code true} is also returned if shutter sound playback 1350 * is already set to the requested state. 1351 * @see #takePicture 1352 * @see CameraInfo#canDisableShutterSound 1353 * @see ShutterCallback 1354 */ 1355 public final boolean enableShutterSound(boolean enabled) { 1356 if (!enabled) { 1357 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE); 1358 IAudioService audioService = IAudioService.Stub.asInterface(b); 1359 try { 1360 if (audioService.isCameraSoundForced()) return false; 1361 } catch (RemoteException e) { 1362 Log.e(TAG, "Audio service is unavailable for queries"); 1363 } 1364 } 1365 return _enableShutterSound(enabled); 1366 } 1367 1368 private native final boolean _enableShutterSound(boolean enabled); 1369 1370 /** 1371 * Callback interface for zoom changes during a smooth zoom operation. 1372 * 1373 * @see #setZoomChangeListener(OnZoomChangeListener) 1374 * @see #startSmoothZoom(int) 1375 */ 1376 public interface OnZoomChangeListener 1377 { 1378 /** 1379 * Called when the zoom value has changed during a smooth zoom. 1380 * 1381 * @param zoomValue the current zoom value. In smooth zoom mode, camera 1382 * calls this for every new zoom value. 1383 * @param stopped whether smooth zoom is stopped. If the value is true, 1384 * this is the last zoom update for the application. 1385 * @param camera the Camera service object 1386 */ 1387 void onZoomChange(int zoomValue, boolean stopped, Camera camera); 1388 }; 1389 1390 /** 1391 * Registers a listener to be notified when the zoom value is updated by the 1392 * camera driver during smooth zoom. 1393 * 1394 * @param listener the listener to notify 1395 * @see #startSmoothZoom(int) 1396 */ 1397 public final void setZoomChangeListener(OnZoomChangeListener listener) 1398 { 1399 mZoomListener = listener; 1400 } 1401 1402 /** 1403 * Callback interface for face detected in the preview frame. 1404 * 1405 */ 1406 public interface FaceDetectionListener 1407 { 1408 /** 1409 * Notify the listener of the detected faces in the preview frame. 1410 * 1411 * @param faces The detected faces in a list 1412 * @param camera The {@link Camera} service object 1413 */ 1414 void onFaceDetection(Face[] faces, Camera camera); 1415 } 1416 1417 /** 1418 * Registers a listener to be notified about the faces detected in the 1419 * preview frame. 1420 * 1421 * @param listener the listener to notify 1422 * @see #startFaceDetection() 1423 */ 1424 public final void setFaceDetectionListener(FaceDetectionListener listener) 1425 { 1426 mFaceListener = listener; 1427 } 1428 1429 /** 1430 * Starts the face detection. This should be called after preview is started. 1431 * The camera will notify {@link FaceDetectionListener} of the detected 1432 * faces in the preview frame. The detected faces may be the same as the 1433 * previous ones. Applications should call {@link #stopFaceDetection} to 1434 * stop the face detection. This method is supported if {@link 1435 * Parameters#getMaxNumDetectedFaces()} returns a number larger than 0. 1436 * If the face detection has started, apps should not call this again. 1437 * 1438 * <p>When the face detection is running, {@link Parameters#setWhiteBalance(String)}, 1439 * {@link Parameters#setFocusAreas(List)}, and {@link Parameters#setMeteringAreas(List)} 1440 * have no effect. The camera uses the detected faces to do auto-white balance, 1441 * auto exposure, and autofocus. 1442 * 1443 * <p>If the apps call {@link #autoFocus(AutoFocusCallback)}, the camera 1444 * will stop sending face callbacks. The last face callback indicates the 1445 * areas used to do autofocus. After focus completes, face detection will 1446 * resume sending face callbacks. If the apps call {@link 1447 * #cancelAutoFocus()}, the face callbacks will also resume.</p> 1448 * 1449 * <p>After calling {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback, 1450 * Camera.PictureCallback)} or {@link #stopPreview()}, and then resuming 1451 * preview with {@link #startPreview()}, the apps should call this method 1452 * again to resume face detection.</p> 1453 * 1454 * @throws IllegalArgumentException if the face detection is unsupported. 1455 * @throws RuntimeException if the method fails or the face detection is 1456 * already running. 1457 * @see FaceDetectionListener 1458 * @see #stopFaceDetection() 1459 * @see Parameters#getMaxNumDetectedFaces() 1460 */ 1461 public final void startFaceDetection() { 1462 if (mFaceDetectionRunning) { 1463 throw new RuntimeException("Face detection is already running"); 1464 } 1465 _startFaceDetection(CAMERA_FACE_DETECTION_HW); 1466 mFaceDetectionRunning = true; 1467 } 1468 1469 /** 1470 * Stops the face detection. 1471 * 1472 * @see #startFaceDetection() 1473 */ 1474 public final void stopFaceDetection() { 1475 _stopFaceDetection(); 1476 mFaceDetectionRunning = false; 1477 } 1478 1479 private native final void _startFaceDetection(int type); 1480 private native final void _stopFaceDetection(); 1481 1482 /** 1483 * Information about a face identified through camera face detection. 1484 * 1485 * <p>When face detection is used with a camera, the {@link FaceDetectionListener} returns a 1486 * list of face objects for use in focusing and metering.</p> 1487 * 1488 * @see FaceDetectionListener 1489 */ 1490 public static class Face { 1491 /** 1492 * Create an empty face. 1493 */ 1494 public Face() { 1495 } 1496 1497 /** 1498 * Bounds of the face. (-1000, -1000) represents the top-left of the 1499 * camera field of view, and (1000, 1000) represents the bottom-right of 1500 * the field of view. For example, suppose the size of the viewfinder UI 1501 * is 800x480. The rect passed from the driver is (-1000, -1000, 0, 0). 1502 * The corresponding viewfinder rect should be (0, 0, 400, 240). It is 1503 * guaranteed left < right and top < bottom. The coordinates can be 1504 * smaller than -1000 or bigger than 1000. But at least one vertex will 1505 * be within (-1000, -1000) and (1000, 1000). 1506 * 1507 * <p>The direction is relative to the sensor orientation, that is, what 1508 * the sensor sees. The direction is not affected by the rotation or 1509 * mirroring of {@link #setDisplayOrientation(int)}. The face bounding 1510 * rectangle does not provide any information about face orientation.</p> 1511 * 1512 * <p>Here is the matrix to convert driver coordinates to View coordinates 1513 * in pixels.</p> 1514 * <pre> 1515 * Matrix matrix = new Matrix(); 1516 * CameraInfo info = CameraHolder.instance().getCameraInfo()[cameraId]; 1517 * // Need mirror for front camera. 1518 * boolean mirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT); 1519 * matrix.setScale(mirror ? -1 : 1, 1); 1520 * // This is the value for android.hardware.Camera.setDisplayOrientation. 1521 * matrix.postRotate(displayOrientation); 1522 * // Camera driver coordinates range from (-1000, -1000) to (1000, 1000). 1523 * // UI coordinates range from (0, 0) to (width, height). 1524 * matrix.postScale(view.getWidth() / 2000f, view.getHeight() / 2000f); 1525 * matrix.postTranslate(view.getWidth() / 2f, view.getHeight() / 2f); 1526 * </pre> 1527 * 1528 * @see #startFaceDetection() 1529 */ 1530 public Rect rect; 1531 1532 /** 1533 * <p>The confidence level for the detection of the face. The range is 1 to 1534 * 100. 100 is the highest confidence.</p> 1535 * 1536 * <p>Depending on the device, even very low-confidence faces may be 1537 * listed, so applications should filter out faces with low confidence, 1538 * depending on the use case. For a typical point-and-shoot camera 1539 * application that wishes to display rectangles around detected faces, 1540 * filtering out faces with confidence less than 50 is recommended.</p> 1541 * 1542 * @see #startFaceDetection() 1543 */ 1544 public int score; 1545 1546 /** 1547 * An unique id per face while the face is visible to the tracker. If 1548 * the face leaves the field-of-view and comes back, it will get a new 1549 * id. This is an optional field, may not be supported on all devices. 1550 * If not supported, id will always be set to -1. The optional fields 1551 * are supported as a set. Either they are all valid, or none of them 1552 * are. 1553 */ 1554 public int id = -1; 1555 1556 /** 1557 * The coordinates of the center of the left eye. The coordinates are in 1558 * the same space as the ones for {@link #rect}. This is an optional 1559 * field, may not be supported on all devices. If not supported, the 1560 * value will always be set to null. The optional fields are supported 1561 * as a set. Either they are all valid, or none of them are. 1562 */ 1563 public Point leftEye = null; 1564 1565 /** 1566 * The coordinates of the center of the right eye. The coordinates are 1567 * in the same space as the ones for {@link #rect}.This is an optional 1568 * field, may not be supported on all devices. If not supported, the 1569 * value will always be set to null. The optional fields are supported 1570 * as a set. Either they are all valid, or none of them are. 1571 */ 1572 public Point rightEye = null; 1573 1574 /** 1575 * The coordinates of the center of the mouth. The coordinates are in 1576 * the same space as the ones for {@link #rect}. This is an optional 1577 * field, may not be supported on all devices. If not supported, the 1578 * value will always be set to null. The optional fields are supported 1579 * as a set. Either they are all valid, or none of them are. 1580 */ 1581 public Point mouth = null; 1582 } 1583 1584 // Error codes match the enum in include/ui/Camera.h 1585 1586 /** 1587 * Unspecified camera error. 1588 * @see Camera.ErrorCallback 1589 */ 1590 public static final int CAMERA_ERROR_UNKNOWN = 1; 1591 1592 /** 1593 * Media server died. In this case, the application must release the 1594 * Camera object and instantiate a new one. 1595 * @see Camera.ErrorCallback 1596 */ 1597 public static final int CAMERA_ERROR_SERVER_DIED = 100; 1598 1599 /** 1600 * Callback interface for camera error notification. 1601 * 1602 * @see #setErrorCallback(ErrorCallback) 1603 */ 1604 public interface ErrorCallback 1605 { 1606 /** 1607 * Callback for camera errors. 1608 * @param error error code: 1609 * <ul> 1610 * <li>{@link #CAMERA_ERROR_UNKNOWN} 1611 * <li>{@link #CAMERA_ERROR_SERVER_DIED} 1612 * </ul> 1613 * @param camera the Camera service object 1614 */ 1615 void onError(int error, Camera camera); 1616 }; 1617 1618 /** 1619 * Registers a callback to be invoked when an error occurs. 1620 * @param cb The callback to run 1621 */ 1622 public final void setErrorCallback(ErrorCallback cb) 1623 { 1624 mErrorCallback = cb; 1625 } 1626 1627 private native final void native_setParameters(String params); 1628 private native final String native_getParameters(); 1629 1630 /** 1631 * Changes the settings for this Camera service. 1632 * 1633 * @param params the Parameters to use for this Camera service 1634 * @throws RuntimeException if any parameter is invalid or not supported. 1635 * @see #getParameters() 1636 */ 1637 public void setParameters(Parameters params) { 1638 // If using preview allocations, don't allow preview size changes 1639 if (mUsingPreviewAllocation) { 1640 Size newPreviewSize = params.getPreviewSize(); 1641 Size currentPreviewSize = getParameters().getPreviewSize(); 1642 if (newPreviewSize.width != currentPreviewSize.width || 1643 newPreviewSize.height != currentPreviewSize.height) { 1644 throw new IllegalStateException("Cannot change preview size" + 1645 " while a preview allocation is configured."); 1646 } 1647 } 1648 1649 native_setParameters(params.flatten()); 1650 } 1651 1652 /** 1653 * Returns the current settings for this Camera service. 1654 * If modifications are made to the returned Parameters, they must be passed 1655 * to {@link #setParameters(Camera.Parameters)} to take effect. 1656 * 1657 * @see #setParameters(Camera.Parameters) 1658 */ 1659 public Parameters getParameters() { 1660 Parameters p = new Parameters(); 1661 String s = native_getParameters(); 1662 p.unflatten(s); 1663 return p; 1664 } 1665 1666 /** 1667 * Returns an empty {@link Parameters} for testing purpose. 1668 * 1669 * @return a Parameter object. 1670 * 1671 * @hide 1672 */ 1673 public static Parameters getEmptyParameters() { 1674 Camera camera = new Camera(); 1675 return camera.new Parameters(); 1676 } 1677 1678 /** 1679 * Image size (width and height dimensions). 1680 */ 1681 public class Size { 1682 /** 1683 * Sets the dimensions for pictures. 1684 * 1685 * @param w the photo width (pixels) 1686 * @param h the photo height (pixels) 1687 */ 1688 public Size(int w, int h) { 1689 width = w; 1690 height = h; 1691 } 1692 /** 1693 * Compares {@code obj} to this size. 1694 * 1695 * @param obj the object to compare this size with. 1696 * @return {@code true} if the width and height of {@code obj} is the 1697 * same as those of this size. {@code false} otherwise. 1698 */ 1699 @Override 1700 public boolean equals(Object obj) { 1701 if (!(obj instanceof Size)) { 1702 return false; 1703 } 1704 Size s = (Size) obj; 1705 return width == s.width && height == s.height; 1706 } 1707 @Override 1708 public int hashCode() { 1709 return width * 32713 + height; 1710 } 1711 /** width of the picture */ 1712 public int width; 1713 /** height of the picture */ 1714 public int height; 1715 }; 1716 1717 /** 1718 * <p>The Area class is used for choosing specific metering and focus areas for 1719 * the camera to use when calculating auto-exposure, auto-white balance, and 1720 * auto-focus.</p> 1721 * 1722 * <p>To find out how many simultaneous areas a given camera supports, use 1723 * {@link Parameters#getMaxNumMeteringAreas()} and 1724 * {@link Parameters#getMaxNumFocusAreas()}. If metering or focusing area 1725 * selection is unsupported, these methods will return 0.</p> 1726 * 1727 * <p>Each Area consists of a rectangle specifying its bounds, and a weight 1728 * that determines its importance. The bounds are relative to the camera's 1729 * current field of view. The coordinates are mapped so that (-1000, -1000) 1730 * is always the top-left corner of the current field of view, and (1000, 1731 * 1000) is always the bottom-right corner of the current field of 1732 * view. Setting Areas with bounds outside that range is not allowed. Areas 1733 * with zero or negative width or height are not allowed.</p> 1734 * 1735 * <p>The weight must range from 1 to 1000, and represents a weight for 1736 * every pixel in the area. This means that a large metering area with 1737 * the same weight as a smaller area will have more effect in the 1738 * metering result. Metering areas can overlap and the driver 1739 * will add the weights in the overlap region.</p> 1740 * 1741 * @see Parameters#setFocusAreas(List) 1742 * @see Parameters#getFocusAreas() 1743 * @see Parameters#getMaxNumFocusAreas() 1744 * @see Parameters#setMeteringAreas(List) 1745 * @see Parameters#getMeteringAreas() 1746 * @see Parameters#getMaxNumMeteringAreas() 1747 */ 1748 public static class Area { 1749 /** 1750 * Create an area with specified rectangle and weight. 1751 * 1752 * @param rect the bounds of the area. 1753 * @param weight the weight of the area. 1754 */ 1755 public Area(Rect rect, int weight) { 1756 this.rect = rect; 1757 this.weight = weight; 1758 } 1759 /** 1760 * Compares {@code obj} to this area. 1761 * 1762 * @param obj the object to compare this area with. 1763 * @return {@code true} if the rectangle and weight of {@code obj} is 1764 * the same as those of this area. {@code false} otherwise. 1765 */ 1766 @Override 1767 public boolean equals(Object obj) { 1768 if (!(obj instanceof Area)) { 1769 return false; 1770 } 1771 Area a = (Area) obj; 1772 if (rect == null) { 1773 if (a.rect != null) return false; 1774 } else { 1775 if (!rect.equals(a.rect)) return false; 1776 } 1777 return weight == a.weight; 1778 } 1779 1780 /** 1781 * Bounds of the area. (-1000, -1000) represents the top-left of the 1782 * camera field of view, and (1000, 1000) represents the bottom-right of 1783 * the field of view. Setting bounds outside that range is not 1784 * allowed. Bounds with zero or negative width or height are not 1785 * allowed. 1786 * 1787 * @see Parameters#getFocusAreas() 1788 * @see Parameters#getMeteringAreas() 1789 */ 1790 public Rect rect; 1791 1792 /** 1793 * Weight of the area. The weight must range from 1 to 1000, and 1794 * represents a weight for every pixel in the area. This means that a 1795 * large metering area with the same weight as a smaller area will have 1796 * more effect in the metering result. Metering areas can overlap and 1797 * the driver will add the weights in the overlap region. 1798 * 1799 * @see Parameters#getFocusAreas() 1800 * @see Parameters#getMeteringAreas() 1801 */ 1802 public int weight; 1803 } 1804 1805 /** 1806 * Camera service settings. 1807 * 1808 * <p>To make camera parameters take effect, applications have to call 1809 * {@link Camera#setParameters(Camera.Parameters)}. For example, after 1810 * {@link Camera.Parameters#setWhiteBalance} is called, white balance is not 1811 * actually changed until {@link Camera#setParameters(Camera.Parameters)} 1812 * is called with the changed parameters object. 1813 * 1814 * <p>Different devices may have different camera capabilities, such as 1815 * picture size or flash modes. The application should query the camera 1816 * capabilities before setting parameters. For example, the application 1817 * should call {@link Camera.Parameters#getSupportedColorEffects()} before 1818 * calling {@link Camera.Parameters#setColorEffect(String)}. If the 1819 * camera does not support color effects, 1820 * {@link Camera.Parameters#getSupportedColorEffects()} will return null. 1821 */ 1822 public class Parameters { 1823 // Parameter keys to communicate with the camera driver. 1824 private static final String KEY_PREVIEW_SIZE = "preview-size"; 1825 private static final String KEY_PREVIEW_FORMAT = "preview-format"; 1826 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate"; 1827 private static final String KEY_PREVIEW_FPS_RANGE = "preview-fps-range"; 1828 private static final String KEY_PICTURE_SIZE = "picture-size"; 1829 private static final String KEY_PICTURE_FORMAT = "picture-format"; 1830 private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size"; 1831 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width"; 1832 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height"; 1833 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality"; 1834 private static final String KEY_JPEG_QUALITY = "jpeg-quality"; 1835 private static final String KEY_ROTATION = "rotation"; 1836 private static final String KEY_GPS_LATITUDE = "gps-latitude"; 1837 private static final String KEY_GPS_LONGITUDE = "gps-longitude"; 1838 private static final String KEY_GPS_ALTITUDE = "gps-altitude"; 1839 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp"; 1840 private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method"; 1841 private static final String KEY_WHITE_BALANCE = "whitebalance"; 1842 private static final String KEY_EFFECT = "effect"; 1843 private static final String KEY_ANTIBANDING = "antibanding"; 1844 private static final String KEY_SCENE_MODE = "scene-mode"; 1845 private static final String KEY_FLASH_MODE = "flash-mode"; 1846 private static final String KEY_FOCUS_MODE = "focus-mode"; 1847 private static final String KEY_FOCUS_AREAS = "focus-areas"; 1848 private static final String KEY_MAX_NUM_FOCUS_AREAS = "max-num-focus-areas"; 1849 private static final String KEY_FOCAL_LENGTH = "focal-length"; 1850 private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle"; 1851 private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle"; 1852 private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation"; 1853 private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation"; 1854 private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation"; 1855 private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step"; 1856 private static final String KEY_AUTO_EXPOSURE_LOCK = "auto-exposure-lock"; 1857 private static final String KEY_AUTO_EXPOSURE_LOCK_SUPPORTED = "auto-exposure-lock-supported"; 1858 private static final String KEY_AUTO_WHITEBALANCE_LOCK = "auto-whitebalance-lock"; 1859 private static final String KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED = "auto-whitebalance-lock-supported"; 1860 private static final String KEY_METERING_AREAS = "metering-areas"; 1861 private static final String KEY_MAX_NUM_METERING_AREAS = "max-num-metering-areas"; 1862 private static final String KEY_ZOOM = "zoom"; 1863 private static final String KEY_MAX_ZOOM = "max-zoom"; 1864 private static final String KEY_ZOOM_RATIOS = "zoom-ratios"; 1865 private static final String KEY_ZOOM_SUPPORTED = "zoom-supported"; 1866 private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported"; 1867 private static final String KEY_FOCUS_DISTANCES = "focus-distances"; 1868 private static final String KEY_VIDEO_SIZE = "video-size"; 1869 private static final String KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO = 1870 "preferred-preview-size-for-video"; 1871 private static final String KEY_MAX_NUM_DETECTED_FACES_HW = "max-num-detected-faces-hw"; 1872 private static final String KEY_MAX_NUM_DETECTED_FACES_SW = "max-num-detected-faces-sw"; 1873 private static final String KEY_RECORDING_HINT = "recording-hint"; 1874 private static final String KEY_VIDEO_SNAPSHOT_SUPPORTED = "video-snapshot-supported"; 1875 private static final String KEY_VIDEO_STABILIZATION = "video-stabilization"; 1876 private static final String KEY_VIDEO_STABILIZATION_SUPPORTED = "video-stabilization-supported"; 1877 1878 // Parameter key suffix for supported values. 1879 private static final String SUPPORTED_VALUES_SUFFIX = "-values"; 1880 1881 private static final String TRUE = "true"; 1882 private static final String FALSE = "false"; 1883 1884 // Values for white balance settings. 1885 public static final String WHITE_BALANCE_AUTO = "auto"; 1886 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent"; 1887 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent"; 1888 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent"; 1889 public static final String WHITE_BALANCE_DAYLIGHT = "daylight"; 1890 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight"; 1891 public static final String WHITE_BALANCE_TWILIGHT = "twilight"; 1892 public static final String WHITE_BALANCE_SHADE = "shade"; 1893 1894 // Values for color effect settings. 1895 public static final String EFFECT_NONE = "none"; 1896 public static final String EFFECT_MONO = "mono"; 1897 public static final String EFFECT_NEGATIVE = "negative"; 1898 public static final String EFFECT_SOLARIZE = "solarize"; 1899 public static final String EFFECT_SEPIA = "sepia"; 1900 public static final String EFFECT_POSTERIZE = "posterize"; 1901 public static final String EFFECT_WHITEBOARD = "whiteboard"; 1902 public static final String EFFECT_BLACKBOARD = "blackboard"; 1903 public static final String EFFECT_AQUA = "aqua"; 1904 1905 // Values for antibanding settings. 1906 public static final String ANTIBANDING_AUTO = "auto"; 1907 public static final String ANTIBANDING_50HZ = "50hz"; 1908 public static final String ANTIBANDING_60HZ = "60hz"; 1909 public static final String ANTIBANDING_OFF = "off"; 1910 1911 // Values for flash mode settings. 1912 /** 1913 * Flash will not be fired. 1914 */ 1915 public static final String FLASH_MODE_OFF = "off"; 1916 1917 /** 1918 * Flash will be fired automatically when required. The flash may be fired 1919 * during preview, auto-focus, or snapshot depending on the driver. 1920 */ 1921 public static final String FLASH_MODE_AUTO = "auto"; 1922 1923 /** 1924 * Flash will always be fired during snapshot. The flash may also be 1925 * fired during preview or auto-focus depending on the driver. 1926 */ 1927 public static final String FLASH_MODE_ON = "on"; 1928 1929 /** 1930 * Flash will be fired in red-eye reduction mode. 1931 */ 1932 public static final String FLASH_MODE_RED_EYE = "red-eye"; 1933 1934 /** 1935 * Constant emission of light during preview, auto-focus and snapshot. 1936 * This can also be used for video recording. 1937 */ 1938 public static final String FLASH_MODE_TORCH = "torch"; 1939 1940 /** 1941 * Scene mode is off. 1942 */ 1943 public static final String SCENE_MODE_AUTO = "auto"; 1944 1945 /** 1946 * Take photos of fast moving objects. Same as {@link 1947 * #SCENE_MODE_SPORTS}. 1948 */ 1949 public static final String SCENE_MODE_ACTION = "action"; 1950 1951 /** 1952 * Take people pictures. 1953 */ 1954 public static final String SCENE_MODE_PORTRAIT = "portrait"; 1955 1956 /** 1957 * Take pictures on distant objects. 1958 */ 1959 public static final String SCENE_MODE_LANDSCAPE = "landscape"; 1960 1961 /** 1962 * Take photos at night. 1963 */ 1964 public static final String SCENE_MODE_NIGHT = "night"; 1965 1966 /** 1967 * Take people pictures at night. 1968 */ 1969 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait"; 1970 1971 /** 1972 * Take photos in a theater. Flash light is off. 1973 */ 1974 public static final String SCENE_MODE_THEATRE = "theatre"; 1975 1976 /** 1977 * Take pictures on the beach. 1978 */ 1979 public static final String SCENE_MODE_BEACH = "beach"; 1980 1981 /** 1982 * Take pictures on the snow. 1983 */ 1984 public static final String SCENE_MODE_SNOW = "snow"; 1985 1986 /** 1987 * Take sunset photos. 1988 */ 1989 public static final String SCENE_MODE_SUNSET = "sunset"; 1990 1991 /** 1992 * Avoid blurry pictures (for example, due to hand shake). 1993 */ 1994 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto"; 1995 1996 /** 1997 * For shooting firework displays. 1998 */ 1999 public static final String SCENE_MODE_FIREWORKS = "fireworks"; 2000 2001 /** 2002 * Take photos of fast moving objects. Same as {@link 2003 * #SCENE_MODE_ACTION}. 2004 */ 2005 public static final String SCENE_MODE_SPORTS = "sports"; 2006 2007 /** 2008 * Take indoor low-light shot. 2009 */ 2010 public static final String SCENE_MODE_PARTY = "party"; 2011 2012 /** 2013 * Capture the naturally warm color of scenes lit by candles. 2014 */ 2015 public static final String SCENE_MODE_CANDLELIGHT = "candlelight"; 2016 2017 /** 2018 * Applications are looking for a barcode. Camera driver will be 2019 * optimized for barcode reading. 2020 */ 2021 public static final String SCENE_MODE_BARCODE = "barcode"; 2022 2023 /** 2024 * Capture a scene using high dynamic range imaging techniques. The 2025 * camera will return an image that has an extended dynamic range 2026 * compared to a regular capture. Capturing such an image may take 2027 * longer than a regular capture. 2028 */ 2029 public static final String SCENE_MODE_HDR = "hdr"; 2030 2031 /** 2032 * Auto-focus mode. Applications should call {@link 2033 * #autoFocus(AutoFocusCallback)} to start the focus in this mode. 2034 */ 2035 public static final String FOCUS_MODE_AUTO = "auto"; 2036 2037 /** 2038 * Focus is set at infinity. Applications should not call 2039 * {@link #autoFocus(AutoFocusCallback)} in this mode. 2040 */ 2041 public static final String FOCUS_MODE_INFINITY = "infinity"; 2042 2043 /** 2044 * Macro (close-up) focus mode. Applications should call 2045 * {@link #autoFocus(AutoFocusCallback)} to start the focus in this 2046 * mode. 2047 */ 2048 public static final String FOCUS_MODE_MACRO = "macro"; 2049 2050 /** 2051 * Focus is fixed. The camera is always in this mode if the focus is not 2052 * adjustable. If the camera has auto-focus, this mode can fix the 2053 * focus, which is usually at hyperfocal distance. Applications should 2054 * not call {@link #autoFocus(AutoFocusCallback)} in this mode. 2055 */ 2056 public static final String FOCUS_MODE_FIXED = "fixed"; 2057 2058 /** 2059 * Extended depth of field (EDOF). Focusing is done digitally and 2060 * continuously. Applications should not call {@link 2061 * #autoFocus(AutoFocusCallback)} in this mode. 2062 */ 2063 public static final String FOCUS_MODE_EDOF = "edof"; 2064 2065 /** 2066 * Continuous auto focus mode intended for video recording. The camera 2067 * continuously tries to focus. This is the best choice for video 2068 * recording because the focus changes smoothly . Applications still can 2069 * call {@link #takePicture(Camera.ShutterCallback, 2070 * Camera.PictureCallback, Camera.PictureCallback)} in this mode but the 2071 * subject may not be in focus. Auto focus starts when the parameter is 2072 * set. 2073 * 2074 * <p>Since API level 14, applications can call {@link 2075 * #autoFocus(AutoFocusCallback)} in this mode. The focus callback will 2076 * immediately return with a boolean that indicates whether the focus is 2077 * sharp or not. The focus position is locked after autoFocus call. If 2078 * applications want to resume the continuous focus, cancelAutoFocus 2079 * must be called. Restarting the preview will not resume the continuous 2080 * autofocus. To stop continuous focus, applications should change the 2081 * focus mode to other modes. 2082 * 2083 * @see #FOCUS_MODE_CONTINUOUS_PICTURE 2084 */ 2085 public static final String FOCUS_MODE_CONTINUOUS_VIDEO = "continuous-video"; 2086 2087 /** 2088 * Continuous auto focus mode intended for taking pictures. The camera 2089 * continuously tries to focus. The speed of focus change is more 2090 * aggressive than {@link #FOCUS_MODE_CONTINUOUS_VIDEO}. Auto focus 2091 * starts when the parameter is set. 2092 * 2093 * <p>Applications can call {@link #autoFocus(AutoFocusCallback)} in 2094 * this mode. If the autofocus is in the middle of scanning, the focus 2095 * callback will return when it completes. If the autofocus is not 2096 * scanning, the focus callback will immediately return with a boolean 2097 * that indicates whether the focus is sharp or not. The apps can then 2098 * decide if they want to take a picture immediately or to change the 2099 * focus mode to auto, and run a full autofocus cycle. The focus 2100 * position is locked after autoFocus call. If applications want to 2101 * resume the continuous focus, cancelAutoFocus must be called. 2102 * Restarting the preview will not resume the continuous autofocus. To 2103 * stop continuous focus, applications should change the focus mode to 2104 * other modes. 2105 * 2106 * @see #FOCUS_MODE_CONTINUOUS_VIDEO 2107 */ 2108 public static final String FOCUS_MODE_CONTINUOUS_PICTURE = "continuous-picture"; 2109 2110 // Indices for focus distance array. 2111 /** 2112 * The array index of near focus distance for use with 2113 * {@link #getFocusDistances(float[])}. 2114 */ 2115 public static final int FOCUS_DISTANCE_NEAR_INDEX = 0; 2116 2117 /** 2118 * The array index of optimal focus distance for use with 2119 * {@link #getFocusDistances(float[])}. 2120 */ 2121 public static final int FOCUS_DISTANCE_OPTIMAL_INDEX = 1; 2122 2123 /** 2124 * The array index of far focus distance for use with 2125 * {@link #getFocusDistances(float[])}. 2126 */ 2127 public static final int FOCUS_DISTANCE_FAR_INDEX = 2; 2128 2129 /** 2130 * The array index of minimum preview fps for use with {@link 2131 * #getPreviewFpsRange(int[])} or {@link 2132 * #getSupportedPreviewFpsRange()}. 2133 */ 2134 public static final int PREVIEW_FPS_MIN_INDEX = 0; 2135 2136 /** 2137 * The array index of maximum preview fps for use with {@link 2138 * #getPreviewFpsRange(int[])} or {@link 2139 * #getSupportedPreviewFpsRange()}. 2140 */ 2141 public static final int PREVIEW_FPS_MAX_INDEX = 1; 2142 2143 // Formats for setPreviewFormat and setPictureFormat. 2144 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp"; 2145 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp"; 2146 private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv"; 2147 private static final String PIXEL_FORMAT_YUV420P = "yuv420p"; 2148 private static final String PIXEL_FORMAT_RGB565 = "rgb565"; 2149 private static final String PIXEL_FORMAT_JPEG = "jpeg"; 2150 private static final String PIXEL_FORMAT_BAYER_RGGB = "bayer-rggb"; 2151 2152 private HashMap<String, String> mMap; 2153 2154 private Parameters() { 2155 mMap = new HashMap<String, String>(64); 2156 } 2157 2158 /** 2159 * Writes the current Parameters to the log. 2160 * @hide 2161 * @deprecated 2162 */ 2163 public void dump() { 2164 Log.e(TAG, "dump: size=" + mMap.size()); 2165 for (String k : mMap.keySet()) { 2166 Log.e(TAG, "dump: " + k + "=" + mMap.get(k)); 2167 } 2168 } 2169 2170 /** 2171 * Creates a single string with all the parameters set in 2172 * this Parameters object. 2173 * <p>The {@link #unflatten(String)} method does the reverse.</p> 2174 * 2175 * @return a String with all values from this Parameters object, in 2176 * semi-colon delimited key-value pairs 2177 */ 2178 public String flatten() { 2179 StringBuilder flattened = new StringBuilder(128); 2180 for (String k : mMap.keySet()) { 2181 flattened.append(k); 2182 flattened.append("="); 2183 flattened.append(mMap.get(k)); 2184 flattened.append(";"); 2185 } 2186 // chop off the extra semicolon at the end 2187 flattened.deleteCharAt(flattened.length()-1); 2188 return flattened.toString(); 2189 } 2190 2191 /** 2192 * Takes a flattened string of parameters and adds each one to 2193 * this Parameters object. 2194 * <p>The {@link #flatten()} method does the reverse.</p> 2195 * 2196 * @param flattened a String of parameters (key-value paired) that 2197 * are semi-colon delimited 2198 */ 2199 public void unflatten(String flattened) { 2200 mMap.clear(); 2201 2202 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(';'); 2203 splitter.setString(flattened); 2204 for (String kv : splitter) { 2205 int pos = kv.indexOf('='); 2206 if (pos == -1) { 2207 continue; 2208 } 2209 String k = kv.substring(0, pos); 2210 String v = kv.substring(pos + 1); 2211 mMap.put(k, v); 2212 } 2213 } 2214 2215 public void remove(String key) { 2216 mMap.remove(key); 2217 } 2218 2219 /** 2220 * Sets a String parameter. 2221 * 2222 * @param key the key name for the parameter 2223 * @param value the String value of the parameter 2224 */ 2225 public void set(String key, String value) { 2226 if (key.indexOf('=') != -1 || key.indexOf(';') != -1 || key.indexOf(0) != -1) { 2227 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ; or \\0)"); 2228 return; 2229 } 2230 if (value.indexOf('=') != -1 || value.indexOf(';') != -1 || value.indexOf(0) != -1) { 2231 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ; or \\0)"); 2232 return; 2233 } 2234 2235 mMap.put(key, value); 2236 } 2237 2238 /** 2239 * Sets an integer parameter. 2240 * 2241 * @param key the key name for the parameter 2242 * @param value the int value of the parameter 2243 */ 2244 public void set(String key, int value) { 2245 mMap.put(key, Integer.toString(value)); 2246 } 2247 2248 private void set(String key, List<Area> areas) { 2249 if (areas == null) { 2250 set(key, "(0,0,0,0,0)"); 2251 } else { 2252 StringBuilder buffer = new StringBuilder(); 2253 for (int i = 0; i < areas.size(); i++) { 2254 Area area = areas.get(i); 2255 Rect rect = area.rect; 2256 buffer.append('('); 2257 buffer.append(rect.left); 2258 buffer.append(','); 2259 buffer.append(rect.top); 2260 buffer.append(','); 2261 buffer.append(rect.right); 2262 buffer.append(','); 2263 buffer.append(rect.bottom); 2264 buffer.append(','); 2265 buffer.append(area.weight); 2266 buffer.append(')'); 2267 if (i != areas.size() - 1) buffer.append(','); 2268 } 2269 set(key, buffer.toString()); 2270 } 2271 } 2272 2273 /** 2274 * Returns the value of a String parameter. 2275 * 2276 * @param key the key name for the parameter 2277 * @return the String value of the parameter 2278 */ 2279 public String get(String key) { 2280 return mMap.get(key); 2281 } 2282 2283 /** 2284 * Returns the value of an integer parameter. 2285 * 2286 * @param key the key name for the parameter 2287 * @return the int value of the parameter 2288 */ 2289 public int getInt(String key) { 2290 return Integer.parseInt(mMap.get(key)); 2291 } 2292 2293 /** 2294 * Sets the dimensions for preview pictures. If the preview has already 2295 * started, applications should stop the preview first before changing 2296 * preview size. 2297 * 2298 * The sides of width and height are based on camera orientation. That 2299 * is, the preview size is the size before it is rotated by display 2300 * orientation. So applications need to consider the display orientation 2301 * while setting preview size. For example, suppose the camera supports 2302 * both 480x320 and 320x480 preview sizes. The application wants a 3:2 2303 * preview ratio. If the display orientation is set to 0 or 180, preview 2304 * size should be set to 480x320. If the display orientation is set to 2305 * 90 or 270, preview size should be set to 320x480. The display 2306 * orientation should also be considered while setting picture size and 2307 * thumbnail size. 2308 * 2309 * @param width the width of the pictures, in pixels 2310 * @param height the height of the pictures, in pixels 2311 * @see #setDisplayOrientation(int) 2312 * @see #getCameraInfo(int, CameraInfo) 2313 * @see #setPictureSize(int, int) 2314 * @see #setJpegThumbnailSize(int, int) 2315 */ 2316 public void setPreviewSize(int width, int height) { 2317 String v = Integer.toString(width) + "x" + Integer.toString(height); 2318 set(KEY_PREVIEW_SIZE, v); 2319 } 2320 2321 /** 2322 * Returns the dimensions setting for preview pictures. 2323 * 2324 * @return a Size object with the width and height setting 2325 * for the preview picture 2326 */ 2327 public Size getPreviewSize() { 2328 String pair = get(KEY_PREVIEW_SIZE); 2329 return strToSize(pair); 2330 } 2331 2332 /** 2333 * Gets the supported preview sizes. 2334 * 2335 * @return a list of Size object. This method will always return a list 2336 * with at least one element. 2337 */ 2338 public List<Size> getSupportedPreviewSizes() { 2339 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX); 2340 return splitSize(str); 2341 } 2342 2343 /** 2344 * <p>Gets the supported video frame sizes that can be used by 2345 * MediaRecorder.</p> 2346 * 2347 * <p>If the returned list is not null, the returned list will contain at 2348 * least one Size and one of the sizes in the returned list must be 2349 * passed to MediaRecorder.setVideoSize() for camcorder application if 2350 * camera is used as the video source. In this case, the size of the 2351 * preview can be different from the resolution of the recorded video 2352 * during video recording.</p> 2353 * 2354 * @return a list of Size object if camera has separate preview and 2355 * video output; otherwise, null is returned. 2356 * @see #getPreferredPreviewSizeForVideo() 2357 */ 2358 public List<Size> getSupportedVideoSizes() { 2359 String str = get(KEY_VIDEO_SIZE + SUPPORTED_VALUES_SUFFIX); 2360 return splitSize(str); 2361 } 2362 2363 /** 2364 * Returns the preferred or recommended preview size (width and height) 2365 * in pixels for video recording. Camcorder applications should 2366 * set the preview size to a value that is not larger than the 2367 * preferred preview size. In other words, the product of the width 2368 * and height of the preview size should not be larger than that of 2369 * the preferred preview size. In addition, we recommend to choose a 2370 * preview size that has the same aspect ratio as the resolution of 2371 * video to be recorded. 2372 * 2373 * @return the preferred preview size (width and height) in pixels for 2374 * video recording if getSupportedVideoSizes() does not return 2375 * null; otherwise, null is returned. 2376 * @see #getSupportedVideoSizes() 2377 */ 2378 public Size getPreferredPreviewSizeForVideo() { 2379 String pair = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO); 2380 return strToSize(pair); 2381 } 2382 2383 /** 2384 * <p>Sets the dimensions for EXIF thumbnail in Jpeg picture. If 2385 * applications set both width and height to 0, EXIF will not contain 2386 * thumbnail.</p> 2387 * 2388 * <p>Applications need to consider the display orientation. See {@link 2389 * #setPreviewSize(int,int)} for reference.</p> 2390 * 2391 * @param width the width of the thumbnail, in pixels 2392 * @param height the height of the thumbnail, in pixels 2393 * @see #setPreviewSize(int,int) 2394 */ 2395 public void setJpegThumbnailSize(int width, int height) { 2396 set(KEY_JPEG_THUMBNAIL_WIDTH, width); 2397 set(KEY_JPEG_THUMBNAIL_HEIGHT, height); 2398 } 2399 2400 /** 2401 * Returns the dimensions for EXIF thumbnail in Jpeg picture. 2402 * 2403 * @return a Size object with the height and width setting for the EXIF 2404 * thumbnails 2405 */ 2406 public Size getJpegThumbnailSize() { 2407 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH), 2408 getInt(KEY_JPEG_THUMBNAIL_HEIGHT)); 2409 } 2410 2411 /** 2412 * Gets the supported jpeg thumbnail sizes. 2413 * 2414 * @return a list of Size object. This method will always return a list 2415 * with at least two elements. Size 0,0 (no thumbnail) is always 2416 * supported. 2417 */ 2418 public List<Size> getSupportedJpegThumbnailSizes() { 2419 String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX); 2420 return splitSize(str); 2421 } 2422 2423 /** 2424 * Sets the quality of the EXIF thumbnail in Jpeg picture. 2425 * 2426 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1 2427 * to 100, with 100 being the best. 2428 */ 2429 public void setJpegThumbnailQuality(int quality) { 2430 set(KEY_JPEG_THUMBNAIL_QUALITY, quality); 2431 } 2432 2433 /** 2434 * Returns the quality setting for the EXIF thumbnail in Jpeg picture. 2435 * 2436 * @return the JPEG quality setting of the EXIF thumbnail. 2437 */ 2438 public int getJpegThumbnailQuality() { 2439 return getInt(KEY_JPEG_THUMBNAIL_QUALITY); 2440 } 2441 2442 /** 2443 * Sets Jpeg quality of captured picture. 2444 * 2445 * @param quality the JPEG quality of captured picture. The range is 1 2446 * to 100, with 100 being the best. 2447 */ 2448 public void setJpegQuality(int quality) { 2449 set(KEY_JPEG_QUALITY, quality); 2450 } 2451 2452 /** 2453 * Returns the quality setting for the JPEG picture. 2454 * 2455 * @return the JPEG picture quality setting. 2456 */ 2457 public int getJpegQuality() { 2458 return getInt(KEY_JPEG_QUALITY); 2459 } 2460 2461 /** 2462 * Sets the rate at which preview frames are received. This is the 2463 * target frame rate. The actual frame rate depends on the driver. 2464 * 2465 * @param fps the frame rate (frames per second) 2466 * @deprecated replaced by {@link #setPreviewFpsRange(int,int)} 2467 */ 2468 @Deprecated 2469 public void setPreviewFrameRate(int fps) { 2470 set(KEY_PREVIEW_FRAME_RATE, fps); 2471 } 2472 2473 /** 2474 * Returns the setting for the rate at which preview frames are 2475 * received. This is the target frame rate. The actual frame rate 2476 * depends on the driver. 2477 * 2478 * @return the frame rate setting (frames per second) 2479 * @deprecated replaced by {@link #getPreviewFpsRange(int[])} 2480 */ 2481 @Deprecated 2482 public int getPreviewFrameRate() { 2483 return getInt(KEY_PREVIEW_FRAME_RATE); 2484 } 2485 2486 /** 2487 * Gets the supported preview frame rates. 2488 * 2489 * @return a list of supported preview frame rates. null if preview 2490 * frame rate setting is not supported. 2491 * @deprecated replaced by {@link #getSupportedPreviewFpsRange()} 2492 */ 2493 @Deprecated 2494 public List<Integer> getSupportedPreviewFrameRates() { 2495 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX); 2496 return splitInt(str); 2497 } 2498 2499 /** 2500 * Sets the minimum and maximum preview fps. This controls the rate of 2501 * preview frames received in {@link PreviewCallback}. The minimum and 2502 * maximum preview fps must be one of the elements from {@link 2503 * #getSupportedPreviewFpsRange}. 2504 * 2505 * @param min the minimum preview fps (scaled by 1000). 2506 * @param max the maximum preview fps (scaled by 1000). 2507 * @throws RuntimeException if fps range is invalid. 2508 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback) 2509 * @see #getSupportedPreviewFpsRange() 2510 */ 2511 public void setPreviewFpsRange(int min, int max) { 2512 set(KEY_PREVIEW_FPS_RANGE, "" + min + "," + max); 2513 } 2514 2515 /** 2516 * Returns the current minimum and maximum preview fps. The values are 2517 * one of the elements returned by {@link #getSupportedPreviewFpsRange}. 2518 * 2519 * @return range the minimum and maximum preview fps (scaled by 1000). 2520 * @see #PREVIEW_FPS_MIN_INDEX 2521 * @see #PREVIEW_FPS_MAX_INDEX 2522 * @see #getSupportedPreviewFpsRange() 2523 */ 2524 public void getPreviewFpsRange(int[] range) { 2525 if (range == null || range.length != 2) { 2526 throw new IllegalArgumentException( 2527 "range must be an array with two elements."); 2528 } 2529 splitInt(get(KEY_PREVIEW_FPS_RANGE), range); 2530 } 2531 2532 /** 2533 * Gets the supported preview fps (frame-per-second) ranges. Each range 2534 * contains a minimum fps and maximum fps. If minimum fps equals to 2535 * maximum fps, the camera outputs frames in fixed frame rate. If not, 2536 * the camera outputs frames in auto frame rate. The actual frame rate 2537 * fluctuates between the minimum and the maximum. The values are 2538 * multiplied by 1000 and represented in integers. For example, if frame 2539 * rate is 26.623 frames per second, the value is 26623. 2540 * 2541 * @return a list of supported preview fps ranges. This method returns a 2542 * list with at least one element. Every element is an int array 2543 * of two values - minimum fps and maximum fps. The list is 2544 * sorted from small to large (first by maximum fps and then 2545 * minimum fps). 2546 * @see #PREVIEW_FPS_MIN_INDEX 2547 * @see #PREVIEW_FPS_MAX_INDEX 2548 */ 2549 public List<int[]> getSupportedPreviewFpsRange() { 2550 String str = get(KEY_PREVIEW_FPS_RANGE + SUPPORTED_VALUES_SUFFIX); 2551 return splitRange(str); 2552 } 2553 2554 /** 2555 * Sets the image format for preview pictures. 2556 * <p>If this is never called, the default format will be 2557 * {@link android.graphics.ImageFormat#NV21}, which 2558 * uses the NV21 encoding format.</p> 2559 * 2560 * <p>Use {@link Parameters#getSupportedPreviewFormats} to get a list of 2561 * the available preview formats. 2562 * 2563 * <p>It is strongly recommended that either 2564 * {@link android.graphics.ImageFormat#NV21} or 2565 * {@link android.graphics.ImageFormat#YV12} is used, since 2566 * they are supported by all camera devices.</p> 2567 * 2568 * <p>For YV12, the image buffer that is received is not necessarily 2569 * tightly packed, as there may be padding at the end of each row of 2570 * pixel data, as described in 2571 * {@link android.graphics.ImageFormat#YV12}. For camera callback data, 2572 * it can be assumed that the stride of the Y and UV data is the 2573 * smallest possible that meets the alignment requirements. That is, if 2574 * the preview size is <var>width x height</var>, then the following 2575 * equations describe the buffer index for the beginning of row 2576 * <var>y</var> for the Y plane and row <var>c</var> for the U and V 2577 * planes: 2578 * 2579 * {@code 2580 * <pre> 2581 * yStride = (int) ceil(width / 16.0) * 16; 2582 * uvStride = (int) ceil( (yStride / 2) / 16.0) * 16; 2583 * ySize = yStride * height; 2584 * uvSize = uvStride * height / 2; 2585 * yRowIndex = yStride * y; 2586 * uRowIndex = ySize + uvSize + uvStride * c; 2587 * vRowIndex = ySize + uvStride * c; 2588 * size = ySize + uvSize * 2;</pre> 2589 * } 2590 * 2591 * @param pixel_format the desired preview picture format, defined by 2592 * one of the {@link android.graphics.ImageFormat} constants. (E.g., 2593 * <var>ImageFormat.NV21</var> (default), or 2594 * <var>ImageFormat.YV12</var>) 2595 * 2596 * @see android.graphics.ImageFormat 2597 * @see android.hardware.Camera.Parameters#getSupportedPreviewFormats 2598 */ 2599 public void setPreviewFormat(int pixel_format) { 2600 String s = cameraFormatForPixelFormat(pixel_format); 2601 if (s == null) { 2602 throw new IllegalArgumentException( 2603 "Invalid pixel_format=" + pixel_format); 2604 } 2605 2606 set(KEY_PREVIEW_FORMAT, s); 2607 } 2608 2609 /** 2610 * Returns the image format for preview frames got from 2611 * {@link PreviewCallback}. 2612 * 2613 * @return the preview format. 2614 * @see android.graphics.ImageFormat 2615 * @see #setPreviewFormat 2616 */ 2617 public int getPreviewFormat() { 2618 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT)); 2619 } 2620 2621 /** 2622 * Gets the supported preview formats. {@link android.graphics.ImageFormat#NV21} 2623 * is always supported. {@link android.graphics.ImageFormat#YV12} 2624 * is always supported since API level 12. 2625 * 2626 * @return a list of supported preview formats. This method will always 2627 * return a list with at least one element. 2628 * @see android.graphics.ImageFormat 2629 * @see #setPreviewFormat 2630 */ 2631 public List<Integer> getSupportedPreviewFormats() { 2632 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX); 2633 ArrayList<Integer> formats = new ArrayList<Integer>(); 2634 for (String s : split(str)) { 2635 int f = pixelFormatForCameraFormat(s); 2636 if (f == ImageFormat.UNKNOWN) continue; 2637 formats.add(f); 2638 } 2639 return formats; 2640 } 2641 2642 /** 2643 * <p>Sets the dimensions for pictures.</p> 2644 * 2645 * <p>Applications need to consider the display orientation. See {@link 2646 * #setPreviewSize(int,int)} for reference.</p> 2647 * 2648 * @param width the width for pictures, in pixels 2649 * @param height the height for pictures, in pixels 2650 * @see #setPreviewSize(int,int) 2651 * 2652 */ 2653 public void setPictureSize(int width, int height) { 2654 String v = Integer.toString(width) + "x" + Integer.toString(height); 2655 set(KEY_PICTURE_SIZE, v); 2656 } 2657 2658 /** 2659 * Returns the dimension setting for pictures. 2660 * 2661 * @return a Size object with the height and width setting 2662 * for pictures 2663 */ 2664 public Size getPictureSize() { 2665 String pair = get(KEY_PICTURE_SIZE); 2666 return strToSize(pair); 2667 } 2668 2669 /** 2670 * Gets the supported picture sizes. 2671 * 2672 * @return a list of supported picture sizes. This method will always 2673 * return a list with at least one element. 2674 */ 2675 public List<Size> getSupportedPictureSizes() { 2676 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX); 2677 return splitSize(str); 2678 } 2679 2680 /** 2681 * Sets the image format for pictures. 2682 * 2683 * @param pixel_format the desired picture format 2684 * (<var>ImageFormat.NV21</var>, 2685 * <var>ImageFormat.RGB_565</var>, or 2686 * <var>ImageFormat.JPEG</var>) 2687 * @see android.graphics.ImageFormat 2688 */ 2689 public void setPictureFormat(int pixel_format) { 2690 String s = cameraFormatForPixelFormat(pixel_format); 2691 if (s == null) { 2692 throw new IllegalArgumentException( 2693 "Invalid pixel_format=" + pixel_format); 2694 } 2695 2696 set(KEY_PICTURE_FORMAT, s); 2697 } 2698 2699 /** 2700 * Returns the image format for pictures. 2701 * 2702 * @return the picture format 2703 * @see android.graphics.ImageFormat 2704 */ 2705 public int getPictureFormat() { 2706 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT)); 2707 } 2708 2709 /** 2710 * Gets the supported picture formats. 2711 * 2712 * @return supported picture formats. This method will always return a 2713 * list with at least one element. 2714 * @see android.graphics.ImageFormat 2715 */ 2716 public List<Integer> getSupportedPictureFormats() { 2717 String str = get(KEY_PICTURE_FORMAT + SUPPORTED_VALUES_SUFFIX); 2718 ArrayList<Integer> formats = new ArrayList<Integer>(); 2719 for (String s : split(str)) { 2720 int f = pixelFormatForCameraFormat(s); 2721 if (f == ImageFormat.UNKNOWN) continue; 2722 formats.add(f); 2723 } 2724 return formats; 2725 } 2726 2727 private String cameraFormatForPixelFormat(int pixel_format) { 2728 switch(pixel_format) { 2729 case ImageFormat.NV16: return PIXEL_FORMAT_YUV422SP; 2730 case ImageFormat.NV21: return PIXEL_FORMAT_YUV420SP; 2731 case ImageFormat.YUY2: return PIXEL_FORMAT_YUV422I; 2732 case ImageFormat.YV12: return PIXEL_FORMAT_YUV420P; 2733 case ImageFormat.RGB_565: return PIXEL_FORMAT_RGB565; 2734 case ImageFormat.JPEG: return PIXEL_FORMAT_JPEG; 2735 case ImageFormat.BAYER_RGGB: return PIXEL_FORMAT_BAYER_RGGB; 2736 default: return null; 2737 } 2738 } 2739 2740 private int pixelFormatForCameraFormat(String format) { 2741 if (format == null) 2742 return ImageFormat.UNKNOWN; 2743 2744 if (format.equals(PIXEL_FORMAT_YUV422SP)) 2745 return ImageFormat.NV16; 2746 2747 if (format.equals(PIXEL_FORMAT_YUV420SP)) 2748 return ImageFormat.NV21; 2749 2750 if (format.equals(PIXEL_FORMAT_YUV422I)) 2751 return ImageFormat.YUY2; 2752 2753 if (format.equals(PIXEL_FORMAT_YUV420P)) 2754 return ImageFormat.YV12; 2755 2756 if (format.equals(PIXEL_FORMAT_RGB565)) 2757 return ImageFormat.RGB_565; 2758 2759 if (format.equals(PIXEL_FORMAT_JPEG)) 2760 return ImageFormat.JPEG; 2761 2762 return ImageFormat.UNKNOWN; 2763 } 2764 2765 /** 2766 * Sets the clockwise rotation angle in degrees relative to the 2767 * orientation of the camera. This affects the pictures returned from 2768 * JPEG {@link PictureCallback}. The camera driver may set orientation 2769 * in the EXIF header without rotating the picture. Or the driver may 2770 * rotate the picture and the EXIF thumbnail. If the Jpeg picture is 2771 * rotated, the orientation in the EXIF header will be missing or 1 (row 2772 * #0 is top and column #0 is left side). 2773 * 2774 * <p> 2775 * If applications want to rotate the picture to match the orientation 2776 * of what users see, apps should use 2777 * {@link android.view.OrientationEventListener} and 2778 * {@link android.hardware.Camera.CameraInfo}. The value from 2779 * OrientationEventListener is relative to the natural orientation of 2780 * the device. CameraInfo.orientation is the angle between camera 2781 * orientation and natural device orientation. The sum of the two is the 2782 * rotation angle for back-facing camera. The difference of the two is 2783 * the rotation angle for front-facing camera. Note that the JPEG 2784 * pictures of front-facing cameras are not mirrored as in preview 2785 * display. 2786 * 2787 * <p> 2788 * For example, suppose the natural orientation of the device is 2789 * portrait. The device is rotated 270 degrees clockwise, so the device 2790 * orientation is 270. Suppose a back-facing camera sensor is mounted in 2791 * landscape and the top side of the camera sensor is aligned with the 2792 * right edge of the display in natural orientation. So the camera 2793 * orientation is 90. The rotation should be set to 0 (270 + 90). 2794 * 2795 * <p>The reference code is as follows. 2796 * 2797 * <pre> 2798 * public void onOrientationChanged(int orientation) { 2799 * if (orientation == ORIENTATION_UNKNOWN) return; 2800 * android.hardware.Camera.CameraInfo info = 2801 * new android.hardware.Camera.CameraInfo(); 2802 * android.hardware.Camera.getCameraInfo(cameraId, info); 2803 * orientation = (orientation + 45) / 90 * 90; 2804 * int rotation = 0; 2805 * if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 2806 * rotation = (info.orientation - orientation + 360) % 360; 2807 * } else { // back-facing camera 2808 * rotation = (info.orientation + orientation) % 360; 2809 * } 2810 * mParameters.setRotation(rotation); 2811 * } 2812 * </pre> 2813 * 2814 * @param rotation The rotation angle in degrees relative to the 2815 * orientation of the camera. Rotation can only be 0, 2816 * 90, 180 or 270. 2817 * @throws IllegalArgumentException if rotation value is invalid. 2818 * @see android.view.OrientationEventListener 2819 * @see #getCameraInfo(int, CameraInfo) 2820 */ 2821 public void setRotation(int rotation) { 2822 if (rotation == 0 || rotation == 90 || rotation == 180 2823 || rotation == 270) { 2824 set(KEY_ROTATION, Integer.toString(rotation)); 2825 } else { 2826 throw new IllegalArgumentException( 2827 "Invalid rotation=" + rotation); 2828 } 2829 } 2830 2831 /** 2832 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF 2833 * header. 2834 * 2835 * @param latitude GPS latitude coordinate. 2836 */ 2837 public void setGpsLatitude(double latitude) { 2838 set(KEY_GPS_LATITUDE, Double.toString(latitude)); 2839 } 2840 2841 /** 2842 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF 2843 * header. 2844 * 2845 * @param longitude GPS longitude coordinate. 2846 */ 2847 public void setGpsLongitude(double longitude) { 2848 set(KEY_GPS_LONGITUDE, Double.toString(longitude)); 2849 } 2850 2851 /** 2852 * Sets GPS altitude. This will be stored in JPEG EXIF header. 2853 * 2854 * @param altitude GPS altitude in meters. 2855 */ 2856 public void setGpsAltitude(double altitude) { 2857 set(KEY_GPS_ALTITUDE, Double.toString(altitude)); 2858 } 2859 2860 /** 2861 * Sets GPS timestamp. This will be stored in JPEG EXIF header. 2862 * 2863 * @param timestamp GPS timestamp (UTC in seconds since January 1, 2864 * 1970). 2865 */ 2866 public void setGpsTimestamp(long timestamp) { 2867 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp)); 2868 } 2869 2870 /** 2871 * Sets GPS processing method. It will store up to 32 characters 2872 * in JPEG EXIF header. 2873 * 2874 * @param processing_method The processing method to get this location. 2875 */ 2876 public void setGpsProcessingMethod(String processing_method) { 2877 set(KEY_GPS_PROCESSING_METHOD, processing_method); 2878 } 2879 2880 /** 2881 * Removes GPS latitude, longitude, altitude, and timestamp from the 2882 * parameters. 2883 */ 2884 public void removeGpsData() { 2885 remove(KEY_GPS_LATITUDE); 2886 remove(KEY_GPS_LONGITUDE); 2887 remove(KEY_GPS_ALTITUDE); 2888 remove(KEY_GPS_TIMESTAMP); 2889 remove(KEY_GPS_PROCESSING_METHOD); 2890 } 2891 2892 /** 2893 * Gets the current white balance setting. 2894 * 2895 * @return current white balance. null if white balance setting is not 2896 * supported. 2897 * @see #WHITE_BALANCE_AUTO 2898 * @see #WHITE_BALANCE_INCANDESCENT 2899 * @see #WHITE_BALANCE_FLUORESCENT 2900 * @see #WHITE_BALANCE_WARM_FLUORESCENT 2901 * @see #WHITE_BALANCE_DAYLIGHT 2902 * @see #WHITE_BALANCE_CLOUDY_DAYLIGHT 2903 * @see #WHITE_BALANCE_TWILIGHT 2904 * @see #WHITE_BALANCE_SHADE 2905 * 2906 */ 2907 public String getWhiteBalance() { 2908 return get(KEY_WHITE_BALANCE); 2909 } 2910 2911 /** 2912 * Sets the white balance. Changing the setting will release the 2913 * auto-white balance lock. It is recommended not to change white 2914 * balance and AWB lock at the same time. 2915 * 2916 * @param value new white balance. 2917 * @see #getWhiteBalance() 2918 * @see #setAutoWhiteBalanceLock(boolean) 2919 */ 2920 public void setWhiteBalance(String value) { 2921 String oldValue = get(KEY_WHITE_BALANCE); 2922 if (same(value, oldValue)) return; 2923 set(KEY_WHITE_BALANCE, value); 2924 set(KEY_AUTO_WHITEBALANCE_LOCK, FALSE); 2925 } 2926 2927 /** 2928 * Gets the supported white balance. 2929 * 2930 * @return a list of supported white balance. null if white balance 2931 * setting is not supported. 2932 * @see #getWhiteBalance() 2933 */ 2934 public List<String> getSupportedWhiteBalance() { 2935 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX); 2936 return split(str); 2937 } 2938 2939 /** 2940 * Gets the current color effect setting. 2941 * 2942 * @return current color effect. null if color effect 2943 * setting is not supported. 2944 * @see #EFFECT_NONE 2945 * @see #EFFECT_MONO 2946 * @see #EFFECT_NEGATIVE 2947 * @see #EFFECT_SOLARIZE 2948 * @see #EFFECT_SEPIA 2949 * @see #EFFECT_POSTERIZE 2950 * @see #EFFECT_WHITEBOARD 2951 * @see #EFFECT_BLACKBOARD 2952 * @see #EFFECT_AQUA 2953 */ 2954 public String getColorEffect() { 2955 return get(KEY_EFFECT); 2956 } 2957 2958 /** 2959 * Sets the current color effect setting. 2960 * 2961 * @param value new color effect. 2962 * @see #getColorEffect() 2963 */ 2964 public void setColorEffect(String value) { 2965 set(KEY_EFFECT, value); 2966 } 2967 2968 /** 2969 * Gets the supported color effects. 2970 * 2971 * @return a list of supported color effects. null if color effect 2972 * setting is not supported. 2973 * @see #getColorEffect() 2974 */ 2975 public List<String> getSupportedColorEffects() { 2976 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX); 2977 return split(str); 2978 } 2979 2980 2981 /** 2982 * Gets the current antibanding setting. 2983 * 2984 * @return current antibanding. null if antibanding setting is not 2985 * supported. 2986 * @see #ANTIBANDING_AUTO 2987 * @see #ANTIBANDING_50HZ 2988 * @see #ANTIBANDING_60HZ 2989 * @see #ANTIBANDING_OFF 2990 */ 2991 public String getAntibanding() { 2992 return get(KEY_ANTIBANDING); 2993 } 2994 2995 /** 2996 * Sets the antibanding. 2997 * 2998 * @param antibanding new antibanding value. 2999 * @see #getAntibanding() 3000 */ 3001 public void setAntibanding(String antibanding) { 3002 set(KEY_ANTIBANDING, antibanding); 3003 } 3004 3005 /** 3006 * Gets the supported antibanding values. 3007 * 3008 * @return a list of supported antibanding values. null if antibanding 3009 * setting is not supported. 3010 * @see #getAntibanding() 3011 */ 3012 public List<String> getSupportedAntibanding() { 3013 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX); 3014 return split(str); 3015 } 3016 3017 /** 3018 * Gets the current scene mode setting. 3019 * 3020 * @return one of SCENE_MODE_XXX string constant. null if scene mode 3021 * setting is not supported. 3022 * @see #SCENE_MODE_AUTO 3023 * @see #SCENE_MODE_ACTION 3024 * @see #SCENE_MODE_PORTRAIT 3025 * @see #SCENE_MODE_LANDSCAPE 3026 * @see #SCENE_MODE_NIGHT 3027 * @see #SCENE_MODE_NIGHT_PORTRAIT 3028 * @see #SCENE_MODE_THEATRE 3029 * @see #SCENE_MODE_BEACH 3030 * @see #SCENE_MODE_SNOW 3031 * @see #SCENE_MODE_SUNSET 3032 * @see #SCENE_MODE_STEADYPHOTO 3033 * @see #SCENE_MODE_FIREWORKS 3034 * @see #SCENE_MODE_SPORTS 3035 * @see #SCENE_MODE_PARTY 3036 * @see #SCENE_MODE_CANDLELIGHT 3037 * @see #SCENE_MODE_BARCODE 3038 */ 3039 public String getSceneMode() { 3040 return get(KEY_SCENE_MODE); 3041 } 3042 3043 /** 3044 * Sets the scene mode. Changing scene mode may override other 3045 * parameters (such as flash mode, focus mode, white balance). For 3046 * example, suppose originally flash mode is on and supported flash 3047 * modes are on/off. In night scene mode, both flash mode and supported 3048 * flash mode may be changed to off. After setting scene mode, 3049 * applications should call getParameters to know if some parameters are 3050 * changed. 3051 * 3052 * @param value scene mode. 3053 * @see #getSceneMode() 3054 */ 3055 public void setSceneMode(String value) { 3056 set(KEY_SCENE_MODE, value); 3057 } 3058 3059 /** 3060 * Gets the supported scene modes. 3061 * 3062 * @return a list of supported scene modes. null if scene mode setting 3063 * is not supported. 3064 * @see #getSceneMode() 3065 */ 3066 public List<String> getSupportedSceneModes() { 3067 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX); 3068 return split(str); 3069 } 3070 3071 /** 3072 * Gets the current flash mode setting. 3073 * 3074 * @return current flash mode. null if flash mode setting is not 3075 * supported. 3076 * @see #FLASH_MODE_OFF 3077 * @see #FLASH_MODE_AUTO 3078 * @see #FLASH_MODE_ON 3079 * @see #FLASH_MODE_RED_EYE 3080 * @see #FLASH_MODE_TORCH 3081 */ 3082 public String getFlashMode() { 3083 return get(KEY_FLASH_MODE); 3084 } 3085 3086 /** 3087 * Sets the flash mode. 3088 * 3089 * @param value flash mode. 3090 * @see #getFlashMode() 3091 */ 3092 public void setFlashMode(String value) { 3093 set(KEY_FLASH_MODE, value); 3094 } 3095 3096 /** 3097 * Gets the supported flash modes. 3098 * 3099 * @return a list of supported flash modes. null if flash mode setting 3100 * is not supported. 3101 * @see #getFlashMode() 3102 */ 3103 public List<String> getSupportedFlashModes() { 3104 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX); 3105 return split(str); 3106 } 3107 3108 /** 3109 * Gets the current focus mode setting. 3110 * 3111 * @return current focus mode. This method will always return a non-null 3112 * value. Applications should call {@link 3113 * #autoFocus(AutoFocusCallback)} to start the focus if focus 3114 * mode is FOCUS_MODE_AUTO or FOCUS_MODE_MACRO. 3115 * @see #FOCUS_MODE_AUTO 3116 * @see #FOCUS_MODE_INFINITY 3117 * @see #FOCUS_MODE_MACRO 3118 * @see #FOCUS_MODE_FIXED 3119 * @see #FOCUS_MODE_EDOF 3120 * @see #FOCUS_MODE_CONTINUOUS_VIDEO 3121 */ 3122 public String getFocusMode() { 3123 return get(KEY_FOCUS_MODE); 3124 } 3125 3126 /** 3127 * Sets the focus mode. 3128 * 3129 * @param value focus mode. 3130 * @see #getFocusMode() 3131 */ 3132 public void setFocusMode(String value) { 3133 set(KEY_FOCUS_MODE, value); 3134 } 3135 3136 /** 3137 * Gets the supported focus modes. 3138 * 3139 * @return a list of supported focus modes. This method will always 3140 * return a list with at least one element. 3141 * @see #getFocusMode() 3142 */ 3143 public List<String> getSupportedFocusModes() { 3144 String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX); 3145 return split(str); 3146 } 3147 3148 /** 3149 * Gets the focal length (in millimeter) of the camera. 3150 * 3151 * @return the focal length. This method will always return a valid 3152 * value. 3153 */ 3154 public float getFocalLength() { 3155 return Float.parseFloat(get(KEY_FOCAL_LENGTH)); 3156 } 3157 3158 /** 3159 * Gets the horizontal angle of view in degrees. 3160 * 3161 * @return horizontal angle of view. This method will always return a 3162 * valid value. 3163 */ 3164 public float getHorizontalViewAngle() { 3165 return Float.parseFloat(get(KEY_HORIZONTAL_VIEW_ANGLE)); 3166 } 3167 3168 /** 3169 * Gets the vertical angle of view in degrees. 3170 * 3171 * @return vertical angle of view. This method will always return a 3172 * valid value. 3173 */ 3174 public float getVerticalViewAngle() { 3175 return Float.parseFloat(get(KEY_VERTICAL_VIEW_ANGLE)); 3176 } 3177 3178 /** 3179 * Gets the current exposure compensation index. 3180 * 3181 * @return current exposure compensation index. The range is {@link 3182 * #getMinExposureCompensation} to {@link 3183 * #getMaxExposureCompensation}. 0 means exposure is not 3184 * adjusted. 3185 */ 3186 public int getExposureCompensation() { 3187 return getInt(KEY_EXPOSURE_COMPENSATION, 0); 3188 } 3189 3190 /** 3191 * Sets the exposure compensation index. 3192 * 3193 * @param value exposure compensation index. The valid value range is 3194 * from {@link #getMinExposureCompensation} (inclusive) to {@link 3195 * #getMaxExposureCompensation} (inclusive). 0 means exposure is 3196 * not adjusted. Application should call 3197 * getMinExposureCompensation and getMaxExposureCompensation to 3198 * know if exposure compensation is supported. 3199 */ 3200 public void setExposureCompensation(int value) { 3201 set(KEY_EXPOSURE_COMPENSATION, value); 3202 } 3203 3204 /** 3205 * Gets the maximum exposure compensation index. 3206 * 3207 * @return maximum exposure compensation index (>=0). If both this 3208 * method and {@link #getMinExposureCompensation} return 0, 3209 * exposure compensation is not supported. 3210 */ 3211 public int getMaxExposureCompensation() { 3212 return getInt(KEY_MAX_EXPOSURE_COMPENSATION, 0); 3213 } 3214 3215 /** 3216 * Gets the minimum exposure compensation index. 3217 * 3218 * @return minimum exposure compensation index (<=0). If both this 3219 * method and {@link #getMaxExposureCompensation} return 0, 3220 * exposure compensation is not supported. 3221 */ 3222 public int getMinExposureCompensation() { 3223 return getInt(KEY_MIN_EXPOSURE_COMPENSATION, 0); 3224 } 3225 3226 /** 3227 * Gets the exposure compensation step. 3228 * 3229 * @return exposure compensation step. Applications can get EV by 3230 * multiplying the exposure compensation index and step. Ex: if 3231 * exposure compensation index is -6 and step is 0.333333333, EV 3232 * is -2. 3233 */ 3234 public float getExposureCompensationStep() { 3235 return getFloat(KEY_EXPOSURE_COMPENSATION_STEP, 0); 3236 } 3237 3238 /** 3239 * <p>Sets the auto-exposure lock state. Applications should check 3240 * {@link #isAutoExposureLockSupported} before using this method.</p> 3241 * 3242 * <p>If set to true, the camera auto-exposure routine will immediately 3243 * pause until the lock is set to false. Exposure compensation settings 3244 * changes will still take effect while auto-exposure is locked.</p> 3245 * 3246 * <p>If auto-exposure is already locked, setting this to true again has 3247 * no effect (the driver will not recalculate exposure values).</p> 3248 * 3249 * <p>Stopping preview with {@link #stopPreview()}, or triggering still 3250 * image capture with {@link #takePicture(Camera.ShutterCallback, 3251 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the 3252 * lock.</p> 3253 * 3254 * <p>Exposure compensation, auto-exposure lock, and auto-white balance 3255 * lock can be used to capture an exposure-bracketed burst of images, 3256 * for example.</p> 3257 * 3258 * <p>Auto-exposure state, including the lock state, will not be 3259 * maintained after camera {@link #release()} is called. Locking 3260 * auto-exposure after {@link #open()} but before the first call to 3261 * {@link #startPreview()} will not allow the auto-exposure routine to 3262 * run at all, and may result in severely over- or under-exposed 3263 * images.</p> 3264 * 3265 * @param toggle new state of the auto-exposure lock. True means that 3266 * auto-exposure is locked, false means that the auto-exposure 3267 * routine is free to run normally. 3268 * 3269 * @see #getAutoExposureLock() 3270 */ 3271 public void setAutoExposureLock(boolean toggle) { 3272 set(KEY_AUTO_EXPOSURE_LOCK, toggle ? TRUE : FALSE); 3273 } 3274 3275 /** 3276 * Gets the state of the auto-exposure lock. Applications should check 3277 * {@link #isAutoExposureLockSupported} before using this method. See 3278 * {@link #setAutoExposureLock} for details about the lock. 3279 * 3280 * @return State of the auto-exposure lock. Returns true if 3281 * auto-exposure is currently locked, and false otherwise. 3282 * 3283 * @see #setAutoExposureLock(boolean) 3284 * 3285 */ 3286 public boolean getAutoExposureLock() { 3287 String str = get(KEY_AUTO_EXPOSURE_LOCK); 3288 return TRUE.equals(str); 3289 } 3290 3291 /** 3292 * Returns true if auto-exposure locking is supported. Applications 3293 * should call this before trying to lock auto-exposure. See 3294 * {@link #setAutoExposureLock} for details about the lock. 3295 * 3296 * @return true if auto-exposure lock is supported. 3297 * @see #setAutoExposureLock(boolean) 3298 * 3299 */ 3300 public boolean isAutoExposureLockSupported() { 3301 String str = get(KEY_AUTO_EXPOSURE_LOCK_SUPPORTED); 3302 return TRUE.equals(str); 3303 } 3304 3305 /** 3306 * <p>Sets the auto-white balance lock state. Applications should check 3307 * {@link #isAutoWhiteBalanceLockSupported} before using this 3308 * method.</p> 3309 * 3310 * <p>If set to true, the camera auto-white balance routine will 3311 * immediately pause until the lock is set to false.</p> 3312 * 3313 * <p>If auto-white balance is already locked, setting this to true 3314 * again has no effect (the driver will not recalculate white balance 3315 * values).</p> 3316 * 3317 * <p>Stopping preview with {@link #stopPreview()}, or triggering still 3318 * image capture with {@link #takePicture(Camera.ShutterCallback, 3319 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the 3320 * the lock.</p> 3321 * 3322 * <p> Changing the white balance mode with {@link #setWhiteBalance} 3323 * will release the auto-white balance lock if it is set.</p> 3324 * 3325 * <p>Exposure compensation, AE lock, and AWB lock can be used to 3326 * capture an exposure-bracketed burst of images, for example. 3327 * Auto-white balance state, including the lock state, will not be 3328 * maintained after camera {@link #release()} is called. Locking 3329 * auto-white balance after {@link #open()} but before the first call to 3330 * {@link #startPreview()} will not allow the auto-white balance routine 3331 * to run at all, and may result in severely incorrect color in captured 3332 * images.</p> 3333 * 3334 * @param toggle new state of the auto-white balance lock. True means 3335 * that auto-white balance is locked, false means that the 3336 * auto-white balance routine is free to run normally. 3337 * 3338 * @see #getAutoWhiteBalanceLock() 3339 * @see #setWhiteBalance(String) 3340 */ 3341 public void setAutoWhiteBalanceLock(boolean toggle) { 3342 set(KEY_AUTO_WHITEBALANCE_LOCK, toggle ? TRUE : FALSE); 3343 } 3344 3345 /** 3346 * Gets the state of the auto-white balance lock. Applications should 3347 * check {@link #isAutoWhiteBalanceLockSupported} before using this 3348 * method. See {@link #setAutoWhiteBalanceLock} for details about the 3349 * lock. 3350 * 3351 * @return State of the auto-white balance lock. Returns true if 3352 * auto-white balance is currently locked, and false 3353 * otherwise. 3354 * 3355 * @see #setAutoWhiteBalanceLock(boolean) 3356 * 3357 */ 3358 public boolean getAutoWhiteBalanceLock() { 3359 String str = get(KEY_AUTO_WHITEBALANCE_LOCK); 3360 return TRUE.equals(str); 3361 } 3362 3363 /** 3364 * Returns true if auto-white balance locking is supported. Applications 3365 * should call this before trying to lock auto-white balance. See 3366 * {@link #setAutoWhiteBalanceLock} for details about the lock. 3367 * 3368 * @return true if auto-white balance lock is supported. 3369 * @see #setAutoWhiteBalanceLock(boolean) 3370 * 3371 */ 3372 public boolean isAutoWhiteBalanceLockSupported() { 3373 String str = get(KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED); 3374 return TRUE.equals(str); 3375 } 3376 3377 /** 3378 * Gets current zoom value. This also works when smooth zoom is in 3379 * progress. Applications should check {@link #isZoomSupported} before 3380 * using this method. 3381 * 3382 * @return the current zoom value. The range is 0 to {@link 3383 * #getMaxZoom}. 0 means the camera is not zoomed. 3384 */ 3385 public int getZoom() { 3386 return getInt(KEY_ZOOM, 0); 3387 } 3388 3389 /** 3390 * Sets current zoom value. If the camera is zoomed (value > 0), the 3391 * actual picture size may be smaller than picture size setting. 3392 * Applications can check the actual picture size after picture is 3393 * returned from {@link PictureCallback}. The preview size remains the 3394 * same in zoom. Applications should check {@link #isZoomSupported} 3395 * before using this method. 3396 * 3397 * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}. 3398 */ 3399 public void setZoom(int value) { 3400 set(KEY_ZOOM, value); 3401 } 3402 3403 /** 3404 * Returns true if zoom is supported. Applications should call this 3405 * before using other zoom methods. 3406 * 3407 * @return true if zoom is supported. 3408 */ 3409 public boolean isZoomSupported() { 3410 String str = get(KEY_ZOOM_SUPPORTED); 3411 return TRUE.equals(str); 3412 } 3413 3414 /** 3415 * Gets the maximum zoom value allowed for snapshot. This is the maximum 3416 * value that applications can set to {@link #setZoom(int)}. 3417 * Applications should call {@link #isZoomSupported} before using this 3418 * method. This value may change in different preview size. Applications 3419 * should call this again after setting preview size. 3420 * 3421 * @return the maximum zoom value supported by the camera. 3422 */ 3423 public int getMaxZoom() { 3424 return getInt(KEY_MAX_ZOOM, 0); 3425 } 3426 3427 /** 3428 * Gets the zoom ratios of all zoom values. Applications should check 3429 * {@link #isZoomSupported} before using this method. 3430 * 3431 * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is 3432 * returned as 320. The number of elements is {@link 3433 * #getMaxZoom} + 1. The list is sorted from small to large. The 3434 * first element is always 100. The last element is the zoom 3435 * ratio of the maximum zoom value. 3436 */ 3437 public List<Integer> getZoomRatios() { 3438 return splitInt(get(KEY_ZOOM_RATIOS)); 3439 } 3440 3441 /** 3442 * Returns true if smooth zoom is supported. Applications should call 3443 * this before using other smooth zoom methods. 3444 * 3445 * @return true if smooth zoom is supported. 3446 */ 3447 public boolean isSmoothZoomSupported() { 3448 String str = get(KEY_SMOOTH_ZOOM_SUPPORTED); 3449 return TRUE.equals(str); 3450 } 3451 3452 /** 3453 * <p>Gets the distances from the camera to where an object appears to be 3454 * in focus. The object is sharpest at the optimal focus distance. The 3455 * depth of field is the far focus distance minus near focus distance.</p> 3456 * 3457 * <p>Focus distances may change after calling {@link 3458 * #autoFocus(AutoFocusCallback)}, {@link #cancelAutoFocus}, or {@link 3459 * #startPreview()}. Applications can call {@link #getParameters()} 3460 * and this method anytime to get the latest focus distances. If the 3461 * focus mode is FOCUS_MODE_CONTINUOUS_VIDEO, focus distances may change 3462 * from time to time.</p> 3463 * 3464 * <p>This method is intended to estimate the distance between the camera 3465 * and the subject. After autofocus, the subject distance may be within 3466 * near and far focus distance. However, the precision depends on the 3467 * camera hardware, autofocus algorithm, the focus area, and the scene. 3468 * The error can be large and it should be only used as a reference.</p> 3469 * 3470 * <p>Far focus distance >= optimal focus distance >= near focus distance. 3471 * If the focus distance is infinity, the value will be 3472 * {@code Float.POSITIVE_INFINITY}.</p> 3473 * 3474 * @param output focus distances in meters. output must be a float 3475 * array with three elements. Near focus distance, optimal focus 3476 * distance, and far focus distance will be filled in the array. 3477 * @see #FOCUS_DISTANCE_NEAR_INDEX 3478 * @see #FOCUS_DISTANCE_OPTIMAL_INDEX 3479 * @see #FOCUS_DISTANCE_FAR_INDEX 3480 */ 3481 public void getFocusDistances(float[] output) { 3482 if (output == null || output.length != 3) { 3483 throw new IllegalArgumentException( 3484 "output must be a float array with three elements."); 3485 } 3486 splitFloat(get(KEY_FOCUS_DISTANCES), output); 3487 } 3488 3489 /** 3490 * Gets the maximum number of focus areas supported. This is the maximum 3491 * length of the list in {@link #setFocusAreas(List)} and 3492 * {@link #getFocusAreas()}. 3493 * 3494 * @return the maximum number of focus areas supported by the camera. 3495 * @see #getFocusAreas() 3496 */ 3497 public int getMaxNumFocusAreas() { 3498 return getInt(KEY_MAX_NUM_FOCUS_AREAS, 0); 3499 } 3500 3501 /** 3502 * <p>Gets the current focus areas. Camera driver uses the areas to decide 3503 * focus.</p> 3504 * 3505 * <p>Before using this API or {@link #setFocusAreas(List)}, apps should 3506 * call {@link #getMaxNumFocusAreas()} to know the maximum number of 3507 * focus areas first. If the value is 0, focus area is not supported.</p> 3508 * 3509 * <p>Each focus area is a rectangle with specified weight. The direction 3510 * is relative to the sensor orientation, that is, what the sensor sees. 3511 * The direction is not affected by the rotation or mirroring of 3512 * {@link #setDisplayOrientation(int)}. Coordinates of the rectangle 3513 * range from -1000 to 1000. (-1000, -1000) is the upper left point. 3514 * (1000, 1000) is the lower right point. The width and height of focus 3515 * areas cannot be 0 or negative.</p> 3516 * 3517 * <p>The weight must range from 1 to 1000. The weight should be 3518 * interpreted as a per-pixel weight - all pixels in the area have the 3519 * specified weight. This means a small area with the same weight as a 3520 * larger area will have less influence on the focusing than the larger 3521 * area. Focus areas can partially overlap and the driver will add the 3522 * weights in the overlap region.</p> 3523 * 3524 * <p>A special case of a {@code null} focus area list means the driver is 3525 * free to select focus targets as it wants. For example, the driver may 3526 * use more signals to select focus areas and change them 3527 * dynamically. Apps can set the focus area list to {@code null} if they 3528 * want the driver to completely control focusing.</p> 3529 * 3530 * <p>Focus areas are relative to the current field of view 3531 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000) 3532 * represents the top of the currently visible camera frame. The focus 3533 * area cannot be set to be outside the current field of view, even 3534 * when using zoom.</p> 3535 * 3536 * <p>Focus area only has effect if the current focus mode is 3537 * {@link #FOCUS_MODE_AUTO}, {@link #FOCUS_MODE_MACRO}, 3538 * {@link #FOCUS_MODE_CONTINUOUS_VIDEO}, or 3539 * {@link #FOCUS_MODE_CONTINUOUS_PICTURE}.</p> 3540 * 3541 * @return a list of current focus areas 3542 */ 3543 public List<Area> getFocusAreas() { 3544 return splitArea(get(KEY_FOCUS_AREAS)); 3545 } 3546 3547 /** 3548 * Sets focus areas. See {@link #getFocusAreas()} for documentation. 3549 * 3550 * @param focusAreas the focus areas 3551 * @see #getFocusAreas() 3552 */ 3553 public void setFocusAreas(List<Area> focusAreas) { 3554 set(KEY_FOCUS_AREAS, focusAreas); 3555 } 3556 3557 /** 3558 * Gets the maximum number of metering areas supported. This is the 3559 * maximum length of the list in {@link #setMeteringAreas(List)} and 3560 * {@link #getMeteringAreas()}. 3561 * 3562 * @return the maximum number of metering areas supported by the camera. 3563 * @see #getMeteringAreas() 3564 */ 3565 public int getMaxNumMeteringAreas() { 3566 return getInt(KEY_MAX_NUM_METERING_AREAS, 0); 3567 } 3568 3569 /** 3570 * <p>Gets the current metering areas. Camera driver uses these areas to 3571 * decide exposure.</p> 3572 * 3573 * <p>Before using this API or {@link #setMeteringAreas(List)}, apps should 3574 * call {@link #getMaxNumMeteringAreas()} to know the maximum number of 3575 * metering areas first. If the value is 0, metering area is not 3576 * supported.</p> 3577 * 3578 * <p>Each metering area is a rectangle with specified weight. The 3579 * direction is relative to the sensor orientation, that is, what the 3580 * sensor sees. The direction is not affected by the rotation or 3581 * mirroring of {@link #setDisplayOrientation(int)}. Coordinates of the 3582 * rectangle range from -1000 to 1000. (-1000, -1000) is the upper left 3583 * point. (1000, 1000) is the lower right point. The width and height of 3584 * metering areas cannot be 0 or negative.</p> 3585 * 3586 * <p>The weight must range from 1 to 1000, and represents a weight for 3587 * every pixel in the area. This means that a large metering area with 3588 * the same weight as a smaller area will have more effect in the 3589 * metering result. Metering areas can partially overlap and the driver 3590 * will add the weights in the overlap region.</p> 3591 * 3592 * <p>A special case of a {@code null} metering area list means the driver 3593 * is free to meter as it chooses. For example, the driver may use more 3594 * signals to select metering areas and change them dynamically. Apps 3595 * can set the metering area list to {@code null} if they want the 3596 * driver to completely control metering.</p> 3597 * 3598 * <p>Metering areas are relative to the current field of view 3599 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000) 3600 * represents the top of the currently visible camera frame. The 3601 * metering area cannot be set to be outside the current field of view, 3602 * even when using zoom.</p> 3603 * 3604 * <p>No matter what metering areas are, the final exposure are compensated 3605 * by {@link #setExposureCompensation(int)}.</p> 3606 * 3607 * @return a list of current metering areas 3608 */ 3609 public List<Area> getMeteringAreas() { 3610 return splitArea(get(KEY_METERING_AREAS)); 3611 } 3612 3613 /** 3614 * Sets metering areas. See {@link #getMeteringAreas()} for 3615 * documentation. 3616 * 3617 * @param meteringAreas the metering areas 3618 * @see #getMeteringAreas() 3619 */ 3620 public void setMeteringAreas(List<Area> meteringAreas) { 3621 set(KEY_METERING_AREAS, meteringAreas); 3622 } 3623 3624 /** 3625 * Gets the maximum number of detected faces supported. This is the 3626 * maximum length of the list returned from {@link FaceDetectionListener}. 3627 * If the return value is 0, face detection of the specified type is not 3628 * supported. 3629 * 3630 * @return the maximum number of detected face supported by the camera. 3631 * @see #startFaceDetection() 3632 */ 3633 public int getMaxNumDetectedFaces() { 3634 return getInt(KEY_MAX_NUM_DETECTED_FACES_HW, 0); 3635 } 3636 3637 /** 3638 * Sets recording mode hint. This tells the camera that the intent of 3639 * the application is to record videos {@link 3640 * android.media.MediaRecorder#start()}, not to take still pictures 3641 * {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback, 3642 * Camera.PictureCallback, Camera.PictureCallback)}. Using this hint can 3643 * allow MediaRecorder.start() to start faster or with fewer glitches on 3644 * output. This should be called before starting preview for the best 3645 * result, but can be changed while the preview is active. The default 3646 * value is false. 3647 * 3648 * The app can still call takePicture() when the hint is true or call 3649 * MediaRecorder.start() when the hint is false. But the performance may 3650 * be worse. 3651 * 3652 * @param hint true if the apps intend to record videos using 3653 * {@link android.media.MediaRecorder}. 3654 */ 3655 public void setRecordingHint(boolean hint) { 3656 set(KEY_RECORDING_HINT, hint ? TRUE : FALSE); 3657 } 3658 3659 /** 3660 * <p>Returns true if video snapshot is supported. That is, applications 3661 * can call {@link #takePicture(Camera.ShutterCallback, 3662 * Camera.PictureCallback, Camera.PictureCallback, 3663 * Camera.PictureCallback)} during recording. Applications do not need 3664 * to call {@link #startPreview()} after taking a picture. The preview 3665 * will be still active. Other than that, taking a picture during 3666 * recording is identical to taking a picture normally. All settings and 3667 * methods related to takePicture work identically. Ex: 3668 * {@link #getPictureSize()}, {@link #getSupportedPictureSizes()}, 3669 * {@link #setJpegQuality(int)}, {@link #setRotation(int)}, and etc. The 3670 * picture will have an EXIF header. {@link #FLASH_MODE_AUTO} and 3671 * {@link #FLASH_MODE_ON} also still work, but the video will record the 3672 * flash.</p> 3673 * 3674 * <p>Applications can set shutter callback as null to avoid the shutter 3675 * sound. It is also recommended to set raw picture and post view 3676 * callbacks to null to avoid the interrupt of preview display.</p> 3677 * 3678 * <p>Field-of-view of the recorded video may be different from that of the 3679 * captured pictures. The maximum size of a video snapshot may be 3680 * smaller than that for regular still captures. If the current picture 3681 * size is set higher than can be supported by video snapshot, the 3682 * picture will be captured at the maximum supported size instead.</p> 3683 * 3684 * @return true if video snapshot is supported. 3685 */ 3686 public boolean isVideoSnapshotSupported() { 3687 String str = get(KEY_VIDEO_SNAPSHOT_SUPPORTED); 3688 return TRUE.equals(str); 3689 } 3690 3691 /** 3692 * <p>Enables and disables video stabilization. Use 3693 * {@link #isVideoStabilizationSupported} to determine if calling this 3694 * method is valid.</p> 3695 * 3696 * <p>Video stabilization reduces the shaking due to the motion of the 3697 * camera in both the preview stream and in recorded videos, including 3698 * data received from the preview callback. It does not reduce motion 3699 * blur in images captured with 3700 * {@link Camera#takePicture takePicture}.</p> 3701 * 3702 * <p>Video stabilization can be enabled and disabled while preview or 3703 * recording is active, but toggling it may cause a jump in the video 3704 * stream that may be undesirable in a recorded video.</p> 3705 * 3706 * @param toggle Set to true to enable video stabilization, and false to 3707 * disable video stabilization. 3708 * @see #isVideoStabilizationSupported() 3709 * @see #getVideoStabilization() 3710 */ 3711 public void setVideoStabilization(boolean toggle) { 3712 set(KEY_VIDEO_STABILIZATION, toggle ? TRUE : FALSE); 3713 } 3714 3715 /** 3716 * Get the current state of video stabilization. See 3717 * {@link #setVideoStabilization} for details of video stabilization. 3718 * 3719 * @return true if video stabilization is enabled 3720 * @see #isVideoStabilizationSupported() 3721 * @see #setVideoStabilization(boolean) 3722 */ 3723 public boolean getVideoStabilization() { 3724 String str = get(KEY_VIDEO_STABILIZATION); 3725 return TRUE.equals(str); 3726 } 3727 3728 /** 3729 * Returns true if video stabilization is supported. See 3730 * {@link #setVideoStabilization} for details of video stabilization. 3731 * 3732 * @return true if video stabilization is supported 3733 * @see #setVideoStabilization(boolean) 3734 * @see #getVideoStabilization() 3735 */ 3736 public boolean isVideoStabilizationSupported() { 3737 String str = get(KEY_VIDEO_STABILIZATION_SUPPORTED); 3738 return TRUE.equals(str); 3739 } 3740 3741 // Splits a comma delimited string to an ArrayList of String. 3742 // Return null if the passing string is null or the size is 0. 3743 private ArrayList<String> split(String str) { 3744 if (str == null) return null; 3745 3746 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(','); 3747 splitter.setString(str); 3748 ArrayList<String> substrings = new ArrayList<String>(); 3749 for (String s : splitter) { 3750 substrings.add(s); 3751 } 3752 return substrings; 3753 } 3754 3755 // Splits a comma delimited string to an ArrayList of Integer. 3756 // Return null if the passing string is null or the size is 0. 3757 private ArrayList<Integer> splitInt(String str) { 3758 if (str == null) return null; 3759 3760 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(','); 3761 splitter.setString(str); 3762 ArrayList<Integer> substrings = new ArrayList<Integer>(); 3763 for (String s : splitter) { 3764 substrings.add(Integer.parseInt(s)); 3765 } 3766 if (substrings.size() == 0) return null; 3767 return substrings; 3768 } 3769 3770 private void splitInt(String str, int[] output) { 3771 if (str == null) return; 3772 3773 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(','); 3774 splitter.setString(str); 3775 int index = 0; 3776 for (String s : splitter) { 3777 output[index++] = Integer.parseInt(s); 3778 } 3779 } 3780 3781 // Splits a comma delimited string to an ArrayList of Float. 3782 private void splitFloat(String str, float[] output) { 3783 if (str == null) return; 3784 3785 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(','); 3786 splitter.setString(str); 3787 int index = 0; 3788 for (String s : splitter) { 3789 output[index++] = Float.parseFloat(s); 3790 } 3791 } 3792 3793 // Returns the value of a float parameter. 3794 private float getFloat(String key, float defaultValue) { 3795 try { 3796 return Float.parseFloat(mMap.get(key)); 3797 } catch (NumberFormatException ex) { 3798 return defaultValue; 3799 } 3800 } 3801 3802 // Returns the value of a integer parameter. 3803 private int getInt(String key, int defaultValue) { 3804 try { 3805 return Integer.parseInt(mMap.get(key)); 3806 } catch (NumberFormatException ex) { 3807 return defaultValue; 3808 } 3809 } 3810 3811 // Splits a comma delimited string to an ArrayList of Size. 3812 // Return null if the passing string is null or the size is 0. 3813 private ArrayList<Size> splitSize(String str) { 3814 if (str == null) return null; 3815 3816 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(','); 3817 splitter.setString(str); 3818 ArrayList<Size> sizeList = new ArrayList<Size>(); 3819 for (String s : splitter) { 3820 Size size = strToSize(s); 3821 if (size != null) sizeList.add(size); 3822 } 3823 if (sizeList.size() == 0) return null; 3824 return sizeList; 3825 } 3826 3827 // Parses a string (ex: "480x320") to Size object. 3828 // Return null if the passing string is null. 3829 private Size strToSize(String str) { 3830 if (str == null) return null; 3831 3832 int pos = str.indexOf('x'); 3833 if (pos != -1) { 3834 String width = str.substring(0, pos); 3835 String height = str.substring(pos + 1); 3836 return new Size(Integer.parseInt(width), 3837 Integer.parseInt(height)); 3838 } 3839 Log.e(TAG, "Invalid size parameter string=" + str); 3840 return null; 3841 } 3842 3843 // Splits a comma delimited string to an ArrayList of int array. 3844 // Example string: "(10000,26623),(10000,30000)". Return null if the 3845 // passing string is null or the size is 0. 3846 private ArrayList<int[]> splitRange(String str) { 3847 if (str == null || str.charAt(0) != '(' 3848 || str.charAt(str.length() - 1) != ')') { 3849 Log.e(TAG, "Invalid range list string=" + str); 3850 return null; 3851 } 3852 3853 ArrayList<int[]> rangeList = new ArrayList<int[]>(); 3854 int endIndex, fromIndex = 1; 3855 do { 3856 int[] range = new int[2]; 3857 endIndex = str.indexOf("),(", fromIndex); 3858 if (endIndex == -1) endIndex = str.length() - 1; 3859 splitInt(str.substring(fromIndex, endIndex), range); 3860 rangeList.add(range); 3861 fromIndex = endIndex + 3; 3862 } while (endIndex != str.length() - 1); 3863 3864 if (rangeList.size() == 0) return null; 3865 return rangeList; 3866 } 3867 3868 // Splits a comma delimited string to an ArrayList of Area objects. 3869 // Example string: "(-10,-10,0,0,300),(0,0,10,10,700)". Return null if 3870 // the passing string is null or the size is 0 or (0,0,0,0,0). 3871 private ArrayList<Area> splitArea(String str) { 3872 if (str == null || str.charAt(0) != '(' 3873 || str.charAt(str.length() - 1) != ')') { 3874 Log.e(TAG, "Invalid area string=" + str); 3875 return null; 3876 } 3877 3878 ArrayList<Area> result = new ArrayList<Area>(); 3879 int endIndex, fromIndex = 1; 3880 int[] array = new int[5]; 3881 do { 3882 endIndex = str.indexOf("),(", fromIndex); 3883 if (endIndex == -1) endIndex = str.length() - 1; 3884 splitInt(str.substring(fromIndex, endIndex), array); 3885 Rect rect = new Rect(array[0], array[1], array[2], array[3]); 3886 result.add(new Area(rect, array[4])); 3887 fromIndex = endIndex + 3; 3888 } while (endIndex != str.length() - 1); 3889 3890 if (result.size() == 0) return null; 3891 3892 if (result.size() == 1) { 3893 Area area = result.get(0); 3894 Rect rect = area.rect; 3895 if (rect.left == 0 && rect.top == 0 && rect.right == 0 3896 && rect.bottom == 0 && area.weight == 0) { 3897 return null; 3898 } 3899 } 3900 3901 return result; 3902 } 3903 3904 private boolean same(String s1, String s2) { 3905 if (s1 == null && s2 == null) return true; 3906 if (s1 != null && s1.equals(s2)) return true; 3907 return false; 3908 } 3909 }; 3910} 3911