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