Camera.java revision 1c632933d93e2c6b845b2a5578019aff68361e2d
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.PixelFormat; 30import android.os.Handler; 31import android.os.Looper; 32import android.os.Message; 33 34/** 35 * The Camera class is used to connect/disconnect with the camera service, 36 * set capture settings, start/stop preview, snap a picture, and retrieve 37 * frames for encoding for video. 38 * <p>There is no default constructor for this class. Use {@link #open()} to 39 * get a Camera object.</p> 40 */ 41public class Camera { 42 private static final String TAG = "Camera"; 43 44 // These match the enums in frameworks/base/include/ui/Camera.h 45 private static final int CAMERA_MSG_ERROR = 0x001; 46 private static final int CAMERA_MSG_SHUTTER = 0x002; 47 private static final int CAMERA_MSG_FOCUS = 0x004; 48 private static final int CAMERA_MSG_ZOOM = 0x008; 49 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010; 50 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020; 51 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040; 52 private static final int CAMERA_MSG_RAW_IMAGE = 0x080; 53 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100; 54 private static final int CAMERA_MSG_ALL_MSGS = 0x1FF; 55 56 private int mNativeContext; // accessed by native methods 57 private EventHandler mEventHandler; 58 private ShutterCallback mShutterCallback; 59 private PictureCallback mRawImageCallback; 60 private PictureCallback mJpegCallback; 61 private PreviewCallback mPreviewCallback; 62 private PictureCallback mPostviewCallback; 63 private AutoFocusCallback mAutoFocusCallback; 64 private ZoomCallback mZoomCallback; 65 private ErrorCallback mErrorCallback; 66 private boolean mOneShot; 67 68 /** 69 * Returns a new Camera object. 70 */ 71 public static Camera open() { 72 return new Camera(); 73 } 74 75 Camera() { 76 mShutterCallback = null; 77 mRawImageCallback = null; 78 mJpegCallback = null; 79 mPreviewCallback = null; 80 mPostviewCallback = null; 81 mZoomCallback = null; 82 83 Looper looper; 84 if ((looper = Looper.myLooper()) != null) { 85 mEventHandler = new EventHandler(this, looper); 86 } else if ((looper = Looper.getMainLooper()) != null) { 87 mEventHandler = new EventHandler(this, looper); 88 } else { 89 mEventHandler = null; 90 } 91 92 native_setup(new WeakReference<Camera>(this)); 93 } 94 95 protected void finalize() { 96 native_release(); 97 } 98 99 private native final void native_setup(Object camera_this); 100 private native final void native_release(); 101 102 103 /** 104 * Disconnects and releases the Camera object resources. 105 * <p>It is recommended that you call this as soon as you're done with the 106 * Camera object.</p> 107 */ 108 public final void release() { 109 native_release(); 110 } 111 112 /** 113 * Reconnect to the camera after passing it to MediaRecorder. To save 114 * setup/teardown time, a client of Camera can pass an initialized Camera 115 * object to a MediaRecorder to use for video recording. Once the 116 * MediaRecorder is done with the Camera, this method can be used to 117 * re-establish a connection with the camera hardware. NOTE: The Camera 118 * object must first be unlocked by the process that owns it before it 119 * can be connected to another process. 120 * 121 * @throws IOException if the method fails. 122 * 123 * FIXME: Unhide after approval 124 * @hide 125 */ 126 public native final void reconnect() throws IOException; 127 128 /** 129 * Lock the camera to prevent other processes from accessing it. To save 130 * setup/teardown time, a client of Camera can pass an initialized Camera 131 * object to another process. This method is used to re-lock the Camera 132 * object prevent other processes from accessing it. By default, the 133 * Camera object is locked. Locking it again from the same process will 134 * have no effect. Attempting to lock it from another process if it has 135 * not been unlocked will fail. 136 * Returns 0 if lock was successful. 137 * 138 * FIXME: Unhide after approval 139 * @hide 140 */ 141 public native final int lock(); 142 143 /** 144 * Unlock the camera to allow another process to access it. To save 145 * setup/teardown time, a client of Camera can pass an initialized Camera 146 * object to another process. This method is used to unlock the Camera 147 * object before handing off the Camera object to the other process. 148 149 * Returns 0 if unlock was successful. 150 * 151 * FIXME: Unhide after approval 152 * @hide 153 */ 154 public native final int unlock(); 155 156 /** 157 * Sets the SurfaceHolder to be used for a picture preview. If the surface 158 * changed since the last call, the screen will blank. Nothing happens 159 * if the same surface is re-set. 160 * 161 * @param holder the SurfaceHolder upon which to place the picture preview 162 * @throws IOException if the method fails. 163 */ 164 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException { 165 if (holder != null) { 166 setPreviewDisplay(holder.getSurface()); 167 } else { 168 setPreviewDisplay((Surface)null); 169 } 170 } 171 172 private native final void setPreviewDisplay(Surface surface); 173 174 /** 175 * Used to get a copy of each preview frame. 176 */ 177 public interface PreviewCallback 178 { 179 /** 180 * The callback that delivers the preview frames. 181 * 182 * @param data The contents of the preview frame in {@link 183 * android.hardware.Camera.Parameters#getPreviewFormat()} 184 * format. If {@link 185 * android.hardware.Camera.Parameters#setPreviewFormat(int)} 186 * is never called, the default will be the YCbCr_420_SP 187 * (NV21) format. 188 * @param camera The Camera service object. 189 */ 190 void onPreviewFrame(byte[] data, Camera camera); 191 }; 192 193 /** 194 * Start drawing preview frames to the surface. 195 */ 196 public native final void startPreview(); 197 198 /** 199 * Stop drawing preview frames to the surface. 200 */ 201 public native final void stopPreview(); 202 203 /** 204 * Return current preview state. 205 * 206 * FIXME: Unhide before release 207 * @hide 208 */ 209 public native final boolean previewEnabled(); 210 211 /** 212 * Can be called at any time to instruct the camera to use a callback for 213 * each preview frame in addition to displaying it. 214 * 215 * @param cb A callback object that receives a copy of each preview frame. 216 * Pass null to stop receiving callbacks at any time. 217 */ 218 public final void setPreviewCallback(PreviewCallback cb) { 219 mPreviewCallback = cb; 220 mOneShot = false; 221 setHasPreviewCallback(cb != null, false); 222 } 223 224 /** 225 * Installs a callback to retrieve a single preview frame, after which the 226 * callback is cleared. 227 * 228 * @param cb A callback object that receives a copy of the preview frame. 229 */ 230 public final void setOneShotPreviewCallback(PreviewCallback cb) { 231 if (cb != null) { 232 mPreviewCallback = cb; 233 mOneShot = true; 234 setHasPreviewCallback(true, true); 235 } 236 } 237 238 private native final void setHasPreviewCallback(boolean installed, boolean oneshot); 239 240 private class EventHandler extends Handler 241 { 242 private Camera mCamera; 243 244 public EventHandler(Camera c, Looper looper) { 245 super(looper); 246 mCamera = c; 247 } 248 249 @Override 250 public void handleMessage(Message msg) { 251 switch(msg.what) { 252 case CAMERA_MSG_SHUTTER: 253 if (mShutterCallback != null) { 254 mShutterCallback.onShutter(); 255 } 256 return; 257 258 case CAMERA_MSG_RAW_IMAGE: 259 if (mRawImageCallback != null) { 260 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera); 261 } 262 return; 263 264 case CAMERA_MSG_COMPRESSED_IMAGE: 265 if (mJpegCallback != null) { 266 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera); 267 } 268 return; 269 270 case CAMERA_MSG_PREVIEW_FRAME: 271 if (mPreviewCallback != null) { 272 mPreviewCallback.onPreviewFrame((byte[])msg.obj, mCamera); 273 if (mOneShot) { 274 mPreviewCallback = null; 275 } 276 } 277 return; 278 279 case CAMERA_MSG_POSTVIEW_FRAME: 280 if (mPostviewCallback != null) { 281 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera); 282 } 283 return; 284 285 case CAMERA_MSG_FOCUS: 286 if (mAutoFocusCallback != null) { 287 mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera); 288 } 289 return; 290 291 case CAMERA_MSG_ZOOM: 292 if (mZoomCallback != null) { 293 mZoomCallback.onZoomUpdate(msg.arg1, mCamera); 294 } 295 return; 296 297 case CAMERA_MSG_ERROR : 298 Log.e(TAG, "Error " + msg.arg1); 299 if (mErrorCallback != null) { 300 mErrorCallback.onError(msg.arg1, mCamera); 301 } 302 return; 303 304 default: 305 Log.e(TAG, "Unknown message type " + msg.what); 306 return; 307 } 308 } 309 } 310 311 private static void postEventFromNative(Object camera_ref, 312 int what, int arg1, int arg2, Object obj) 313 { 314 Camera c = (Camera)((WeakReference)camera_ref).get(); 315 if (c == null) 316 return; 317 318 if (c.mEventHandler != null) { 319 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj); 320 c.mEventHandler.sendMessage(m); 321 } 322 } 323 324 /** 325 * Handles the callback for the camera auto focus. 326 */ 327 public interface AutoFocusCallback 328 { 329 /** 330 * Callback for the camera auto focus. If the camera does not support 331 * auto-focus and autoFocus is called, onAutoFocus will be called 332 * immediately with success. 333 * 334 * @param success true if focus was successful, false if otherwise 335 * @param camera the Camera service object 336 */ 337 void onAutoFocus(boolean success, Camera camera); 338 }; 339 340 /** 341 * Starts auto-focus function and registers a callback function to run when 342 * camera is focused. Only valid after startPreview() has been called. If 343 * the camera does not support auto-focus, it is a no-op and {@link 344 * AutoFocusCallback#onAutoFocus(boolean, Camera)} callback will be called 345 * immediately. 346 * 347 * @param cb the callback to run 348 */ 349 public final void autoFocus(AutoFocusCallback cb) 350 { 351 mAutoFocusCallback = cb; 352 native_autoFocus(); 353 } 354 private native final void native_autoFocus(); 355 356 /** 357 * An interface which contains a callback for the shutter closing after taking a picture. 358 */ 359 public interface ShutterCallback 360 { 361 /** 362 * Can be used to play a shutter sound as soon as the image has been captured, but before 363 * the data is available. 364 */ 365 void onShutter(); 366 } 367 368 /** 369 * Handles the callback for when a picture is taken. 370 */ 371 public interface PictureCallback { 372 /** 373 * Callback for when a picture is taken. 374 * 375 * @param data a byte array of the picture data 376 * @param camera the Camera service object 377 */ 378 void onPictureTaken(byte[] data, Camera camera); 379 }; 380 381 /** 382 * Triggers an asynchronous image capture. The camera service will initiate 383 * a series of callbacks to the application as the image capture progresses. 384 * The shutter callback occurs after the image is captured. This can be used 385 * to trigger a sound to let the user know that image has been captured. The 386 * raw callback occurs when the raw image data is available (NOTE: the data 387 * may be null if the hardware does not have enough memory to make a copy). 388 * The jpeg callback occurs when the compressed image is available. If the 389 * application does not need a particular callback, a null can be passed 390 * instead of a callback method. 391 * 392 * @param shutter callback after the image is captured, may be null 393 * @param raw callback with raw image data, may be null 394 * @param jpeg callback with jpeg image data, may be null 395 */ 396 public final void takePicture(ShutterCallback shutter, PictureCallback raw, 397 PictureCallback jpeg) { 398 takePicture(shutter, raw, null, jpeg); 399 } 400 private native final void native_takePicture(); 401 402 /** 403 * Triggers an asynchronous image capture. The camera service will initiate 404 * a series of callbacks to the application as the image capture progresses. 405 * The shutter callback occurs after the image is captured. This can be used 406 * to trigger a sound to let the user know that image has been captured. The 407 * raw callback occurs when the raw image data is available (NOTE: the data 408 * may be null if the hardware does not have enough memory to make a copy). 409 * The postview callback occurs when a scaled, fully processed postview 410 * image is available (NOTE: not all hardware supports this). The jpeg 411 * callback occurs when the compressed image is available. If the 412 * application does not need a particular callback, a null can be passed 413 * instead of a callback method. 414 * 415 * @param shutter callback after the image is captured, may be null 416 * @param raw callback with raw image data, may be null 417 * @param postview callback with postview image data, may be null 418 * @param jpeg callback with jpeg image data, may be null 419 */ 420 public final void takePicture(ShutterCallback shutter, PictureCallback raw, 421 PictureCallback postview, PictureCallback jpeg) { 422 mShutterCallback = shutter; 423 mRawImageCallback = raw; 424 mPostviewCallback = postview; 425 mJpegCallback = jpeg; 426 native_takePicture(); 427 } 428 429 /** 430 * Handles the zoom callback. 431 */ 432 public interface ZoomCallback 433 { 434 /** 435 * Callback for zoom updates 436 * @param zoomLevel new zoom level in 1/1000 increments, 437 * e.g. a zoom of 3.2x is stored as 3200. Accuracy of the 438 * value is dependent on the hardware implementation. Not 439 * all devices will generate this callback. 440 * @param camera the Camera service object 441 */ 442 void onZoomUpdate(int zoomLevel, Camera camera); 443 }; 444 445 /** 446 * Registers a callback to be invoked when the zoom 447 * level is updated by the camera driver. 448 * @param cb the callback to run 449 */ 450 public final void setZoomCallback(ZoomCallback cb) 451 { 452 mZoomCallback = cb; 453 } 454 455 // These match the enum in include/ui/Camera.h 456 /** Unspecified camerar error. @see #ErrorCallback */ 457 public static final int CAMERA_ERROR_UNKNOWN = 1; 458 /** Media server died. In this case, the application must release the 459 * Camera object and instantiate a new one. @see #ErrorCallback */ 460 public static final int CAMERA_ERROR_SERVER_DIED = 100; 461 462 /** 463 * Handles the camera error callback. 464 */ 465 public interface ErrorCallback 466 { 467 /** 468 * Callback for camera errors. 469 * @param error error code: 470 * <ul> 471 * <li>{@link #CAMERA_ERROR_UNKNOWN} 472 * <li>{@link #CAMERA_ERROR_SERVER_DIED} 473 * </ul> 474 * @param camera the Camera service object 475 */ 476 void onError(int error, Camera camera); 477 }; 478 479 /** 480 * Registers a callback to be invoked when an error occurs. 481 * @param cb the callback to run 482 */ 483 public final void setErrorCallback(ErrorCallback cb) 484 { 485 mErrorCallback = cb; 486 } 487 488 private native final void native_setParameters(String params); 489 private native final String native_getParameters(); 490 491 /** 492 * Sets the Parameters for pictures from this Camera service. 493 * 494 * @param params the Parameters to use for this Camera service 495 */ 496 public void setParameters(Parameters params) { 497 native_setParameters(params.flatten()); 498 } 499 500 /** 501 * Returns the picture Parameters for this Camera service. 502 */ 503 public Parameters getParameters() { 504 Parameters p = new Parameters(); 505 String s = native_getParameters(); 506 p.unflatten(s); 507 return p; 508 } 509 510 /** 511 * Handles the picture size (dimensions). 512 */ 513 public class Size { 514 /** 515 * Sets the dimensions for pictures. 516 * 517 * @param w the photo width (pixels) 518 * @param h the photo height (pixels) 519 */ 520 public Size(int w, int h) { 521 width = w; 522 height = h; 523 } 524 /** width of the picture */ 525 public int width; 526 /** height of the picture */ 527 public int height; 528 }; 529 530 /** 531 * Handles the parameters for pictures created by a Camera service. 532 * 533 * <p>To make camera parameters take effect, applications have to call 534 * Camera.setParameters. For example, after setWhiteBalance is called, white 535 * balance is not changed until Camera.setParameters() is called. 536 * 537 * <p>Different devices may have different camera capabilities, such as 538 * picture size or flash modes. The application should query the camera 539 * capabilities before setting parameters. For example, the application 540 * should call getSupportedColorEffects before calling setEffect. If the 541 * camera does not support color effects, getSupportedColorEffects will 542 * return null. 543 */ 544 public class Parameters { 545 // Parameter keys to communicate with the camera driver. 546 private static final String KEY_PREVIEW_SIZE = "preview-size"; 547 private static final String KEY_PREVIEW_FORMAT = "preview-format"; 548 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate"; 549 private static final String KEY_PICTURE_SIZE = "picture-size"; 550 private static final String KEY_PICTURE_FORMAT = "picture-format"; 551 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width"; 552 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height"; 553 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality"; 554 private static final String KEY_JPEG_QUALITY = "jpeg-quality"; 555 private static final String KEY_ROTATION = "rotation"; 556 private static final String KEY_GPS_LATITUDE = "gps-latitude"; 557 private static final String KEY_GPS_LONGITUDE = "gps-longitude"; 558 private static final String KEY_GPS_ALTITUDE = "gps-altitude"; 559 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp"; 560 private static final String KEY_WHITE_BALANCE = "whitebalance"; 561 private static final String KEY_EFFECT = "effect"; 562 private static final String KEY_ANTIBANDING = "antibanding"; 563 private static final String KEY_SCENE_MODE = "scene-mode"; 564 private static final String KEY_FLASH_MODE = "flash-mode"; 565 // Parameter key suffix for supported values. 566 private static final String SUPPORTED_VALUES_SUFFIX = "-values"; 567 568 // Values for white balance settings. 569 public static final String WHITE_BALANCE_AUTO = "auto"; 570 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent"; 571 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent"; 572 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent"; 573 public static final String WHITE_BALANCE_DAYLIGHT = "daylight"; 574 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight"; 575 public static final String WHITE_BALANCE_TWILIGHT = "twilight"; 576 public static final String WHITE_BALANCE_SHADE = "shade"; 577 578 // Values for color effect settings. 579 public static final String EFFECT_NONE = "none"; 580 public static final String EFFECT_MONO = "mono"; 581 public static final String EFFECT_NEGATIVE = "negative"; 582 public static final String EFFECT_SOLARIZE = "solarize"; 583 public static final String EFFECT_SEPIA = "sepia"; 584 public static final String EFFECT_POSTERIZE = "posterize"; 585 public static final String EFFECT_WHITEBOARD = "whiteboard"; 586 public static final String EFFECT_BLACKBOARD = "blackboard"; 587 public static final String EFFECT_AQUA = "aqua"; 588 589 // Values for antibanding settings. 590 public static final String ANTIBANDING_AUTO = "auto"; 591 public static final String ANTIBANDING_50HZ = "50hz"; 592 public static final String ANTIBANDING_60HZ = "60hz"; 593 public static final String ANTIBANDING_OFF = "off"; 594 595 // Values for flash mode settings. 596 /** 597 * Flash will not be fired. 598 */ 599 public static final String FLASH_MODE_OFF = "off"; 600 /** 601 * Flash will be fired automatically when required. The timing is 602 * decided by camera driver. 603 */ 604 public static final String FLASH_MODE_AUTO = "auto"; 605 /** 606 * Flash will always be fired. The timing is decided by camera driver. 607 */ 608 public static final String FLASH_MODE_ON = "on"; 609 /** 610 * Flash will be fired in red-eye reduction mode. 611 */ 612 public static final String FLASH_MODE_RED_EYE = "red-eye"; 613 614 // Values for scene mode settings. 615 public static final String SCENE_MODE_AUTO = "auto"; 616 public static final String SCENE_MODE_ACTION = "action"; 617 public static final String SCENE_MODE_PORTRAIT = "portrait"; 618 public static final String SCENE_MODE_LANDSCAPE = "landscape"; 619 public static final String SCENE_MODE_NIGHT = "night"; 620 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait"; 621 public static final String SCENE_MODE_THEATRE = "theatre"; 622 public static final String SCENE_MODE_BEACH = "beach"; 623 public static final String SCENE_MODE_SNOW = "snow"; 624 public static final String SCENE_MODE_SUNSET = "sunset"; 625 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto"; 626 public static final String SCENE_MODE_FIREWORKS = "fireworks"; 627 public static final String SCENE_MODE_SPORTS = "sports"; 628 public static final String SCENE_MODE_PARTY = "party"; 629 public static final String SCENE_MODE_CANDLELIGHT = "candlelight"; 630 631 // Formats for setPreviewFormat and setPictureFormat. 632 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp"; 633 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp"; 634 private static final String PIXEL_FORMAT_RGB565 = "rgb565"; 635 private static final String PIXEL_FORMAT_JPEG = "jpeg"; 636 637 private HashMap<String, String> mMap; 638 639 private Parameters() { 640 mMap = new HashMap<String, String>(); 641 } 642 643 /** 644 * Writes the current Parameters to the log. 645 * @hide 646 * @deprecated 647 */ 648 public void dump() { 649 Log.e(TAG, "dump: size=" + mMap.size()); 650 for (String k : mMap.keySet()) { 651 Log.e(TAG, "dump: " + k + "=" + mMap.get(k)); 652 } 653 } 654 655 /** 656 * Creates a single string with all the parameters set in 657 * this Parameters object. 658 * <p>The {@link #unflatten(String)} method does the reverse.</p> 659 * 660 * @return a String with all values from this Parameters object, in 661 * semi-colon delimited key-value pairs 662 */ 663 public String flatten() { 664 StringBuilder flattened = new StringBuilder(); 665 for (String k : mMap.keySet()) { 666 flattened.append(k); 667 flattened.append("="); 668 flattened.append(mMap.get(k)); 669 flattened.append(";"); 670 } 671 // chop off the extra semicolon at the end 672 flattened.deleteCharAt(flattened.length()-1); 673 return flattened.toString(); 674 } 675 676 /** 677 * Takes a flattened string of parameters and adds each one to 678 * this Parameters object. 679 * <p>The {@link #flatten()} method does the reverse.</p> 680 * 681 * @param flattened a String of parameters (key-value paired) that 682 * are semi-colon delimited 683 */ 684 public void unflatten(String flattened) { 685 mMap.clear(); 686 687 StringTokenizer tokenizer = new StringTokenizer(flattened, ";"); 688 while (tokenizer.hasMoreElements()) { 689 String kv = tokenizer.nextToken(); 690 int pos = kv.indexOf('='); 691 if (pos == -1) { 692 continue; 693 } 694 String k = kv.substring(0, pos); 695 String v = kv.substring(pos + 1); 696 mMap.put(k, v); 697 } 698 } 699 700 public void remove(String key) { 701 mMap.remove(key); 702 } 703 704 /** 705 * Sets a String parameter. 706 * 707 * @param key the key name for the parameter 708 * @param value the String value of the parameter 709 */ 710 public void set(String key, String value) { 711 if (key.indexOf('=') != -1 || key.indexOf(';') != -1) { 712 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ;)"); 713 return; 714 } 715 if (value.indexOf('=') != -1 || value.indexOf(';') != -1) { 716 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ;)"); 717 return; 718 } 719 720 mMap.put(key, value); 721 } 722 723 /** 724 * Sets an integer parameter. 725 * 726 * @param key the key name for the parameter 727 * @param value the int value of the parameter 728 */ 729 public void set(String key, int value) { 730 mMap.put(key, Integer.toString(value)); 731 } 732 733 /** 734 * Returns the value of a String parameter. 735 * 736 * @param key the key name for the parameter 737 * @return the String value of the parameter 738 */ 739 public String get(String key) { 740 return mMap.get(key); 741 } 742 743 /** 744 * Returns the value of an integer parameter. 745 * 746 * @param key the key name for the parameter 747 * @return the int value of the parameter 748 */ 749 public int getInt(String key) { 750 return Integer.parseInt(mMap.get(key)); 751 } 752 753 /** 754 * Sets the dimensions for preview pictures. 755 * 756 * @param width the width of the pictures, in pixels 757 * @param height the height of the pictures, in pixels 758 */ 759 public void setPreviewSize(int width, int height) { 760 String v = Integer.toString(width) + "x" + Integer.toString(height); 761 set(KEY_PREVIEW_SIZE, v); 762 } 763 764 /** 765 * Returns the dimensions setting for preview pictures. 766 * 767 * @return a Size object with the height and width setting 768 * for the preview picture 769 */ 770 public Size getPreviewSize() { 771 String pair = get(KEY_PREVIEW_SIZE); 772 return strToSize(pair); 773 } 774 775 /** 776 * Gets the supported preview sizes. 777 * 778 * @return a List of Size object. null if preview size setting is not 779 * supported. 780 */ 781 public List<Size> getSupportedPreviewSizes() { 782 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX); 783 return splitSize(str); 784 } 785 786 /** 787 * Sets the dimensions for EXIF thumbnail in Jpeg picture. 788 * 789 * @param width the width of the thumbnail, in pixels 790 * @param height the height of the thumbnail, in pixels 791 */ 792 public void setJpegThumbnailSize(int width, int height) { 793 set(KEY_JPEG_THUMBNAIL_WIDTH, width); 794 set(KEY_JPEG_THUMBNAIL_HEIGHT, height); 795 } 796 797 /** 798 * Returns the dimensions for EXIF thumbnail in Jpeg picture. 799 * 800 * @return a Size object with the height and width setting for the EXIF 801 * thumbnails 802 */ 803 public Size getJpegThumbnailSize() { 804 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH), 805 getInt(KEY_JPEG_THUMBNAIL_HEIGHT)); 806 } 807 808 /** 809 * Sets the quality of the EXIF thumbnail in Jpeg picture. 810 * 811 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1 812 * to 100, with 100 being the best. 813 */ 814 public void setJpegThumbnailQuality(int quality) { 815 set(KEY_JPEG_THUMBNAIL_QUALITY, quality); 816 } 817 818 /** 819 * Returns the quality setting for the EXIF thumbnail in Jpeg picture. 820 * 821 * @return the JPEG quality setting of the EXIF thumbnail. 822 */ 823 public int getJpegThumbnailQuality() { 824 return getInt(KEY_JPEG_THUMBNAIL_QUALITY); 825 } 826 827 /** 828 * Sets Jpeg quality of captured picture. 829 * 830 * @param quality the JPEG quality of captured picture. The range is 1 831 * to 100, with 100 being the best. 832 */ 833 public void setJpegQuality(int quality) { 834 set(KEY_JPEG_QUALITY, quality); 835 } 836 837 /** 838 * Returns the quality setting for the JPEG picture. 839 * 840 * @return the JPEG picture quality setting. 841 */ 842 public int getJpegQuality() { 843 return getInt(KEY_JPEG_QUALITY); 844 } 845 846 /** 847 * Sets the rate at which preview frames are received. 848 * 849 * @param fps the frame rate (frames per second) 850 */ 851 public void setPreviewFrameRate(int fps) { 852 set(KEY_PREVIEW_FRAME_RATE, fps); 853 } 854 855 /** 856 * Returns the setting for the rate at which preview frames 857 * are received. 858 * 859 * @return the frame rate setting (frames per second) 860 */ 861 public int getPreviewFrameRate() { 862 return getInt(KEY_PREVIEW_FRAME_RATE); 863 } 864 865 /** 866 * Gets the supported preview frame rates. 867 * 868 * @return a List of Integer objects (preview frame rates). null if 869 * preview frame rate setting is not supported. 870 */ 871 public List<Integer> getSupportedPreviewFrameRates() { 872 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX); 873 return splitInt(str); 874 } 875 876 /** 877 * Sets the image format for preview pictures. If this is never called, 878 * the default will be the YCbCr_420_SP (NV21) format. 879 * 880 * @param pixel_format the desired preview picture format 881 * (<var>PixelFormat.YCbCr_420_SP (NV21)</var>, 882 * <var>PixelFormat.RGB_565</var>, or 883 * <var>PixelFormat.JPEG</var>) 884 * @see android.graphics.PixelFormat 885 */ 886 public void setPreviewFormat(int pixel_format) { 887 String s = cameraFormatForPixelFormat(pixel_format); 888 if (s == null) { 889 throw new IllegalArgumentException( 890 "Invalid pixel_format=" + pixel_format); 891 } 892 893 set(KEY_PREVIEW_FORMAT, s); 894 } 895 896 /** 897 * Returns the image format for preview pictures got from 898 * {@link PreviewCallback}. 899 * 900 * @return the PixelFormat int representing the preview picture format 901 * @see android.graphics.PixelFormat 902 */ 903 public int getPreviewFormat() { 904 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT)); 905 } 906 907 /** 908 * Gets the supported preview formats. 909 * 910 * @return a List of Integer objects. null if preview format setting is 911 * not supported. 912 */ 913 public List<Integer> getSupportedPreviewFormats() { 914 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX); 915 return splitInt(str); 916 } 917 918 /** 919 * Sets the dimensions for pictures. 920 * 921 * @param width the width for pictures, in pixels 922 * @param height the height for pictures, in pixels 923 */ 924 public void setPictureSize(int width, int height) { 925 String v = Integer.toString(width) + "x" + Integer.toString(height); 926 set(KEY_PICTURE_SIZE, v); 927 } 928 929 /** 930 * Returns the dimension setting for pictures. 931 * 932 * @return a Size object with the height and width setting 933 * for pictures 934 */ 935 public Size getPictureSize() { 936 String pair = get(KEY_PICTURE_SIZE); 937 return strToSize(pair); 938 } 939 940 /** 941 * Gets the supported picture sizes. 942 * 943 * @return a List of Size objects. null if picture size setting is not 944 * supported. 945 */ 946 public List<Size> getSupportedPictureSizes() { 947 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX); 948 return splitSize(str); 949 } 950 951 /** 952 * Sets the image format for pictures. 953 * 954 * @param pixel_format the desired picture format 955 * (<var>PixelFormat.YCbCr_420_SP (NV21)</var>, 956 * <var>PixelFormat.RGB_565</var>, or 957 * <var>PixelFormat.JPEG</var>) 958 * @see android.graphics.PixelFormat 959 */ 960 public void setPictureFormat(int pixel_format) { 961 String s = cameraFormatForPixelFormat(pixel_format); 962 if (s == null) { 963 throw new IllegalArgumentException( 964 "Invalid pixel_format=" + pixel_format); 965 } 966 967 set(KEY_PICTURE_FORMAT, s); 968 } 969 970 /** 971 * Returns the image format for pictures. 972 * 973 * @return the PixelFormat int representing the picture format 974 */ 975 public int getPictureFormat() { 976 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT)); 977 } 978 979 /** 980 * Gets the supported picture formats. 981 * 982 * @return a List of Integer objects (values are PixelFormat.XXX). null 983 * if picture setting is not supported. 984 */ 985 public List<Integer> getSupportedPictureFormats() { 986 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX); 987 return splitInt(str); 988 } 989 990 private String cameraFormatForPixelFormat(int pixel_format) { 991 switch(pixel_format) { 992 case PixelFormat.YCbCr_422_SP: return PIXEL_FORMAT_YUV422SP; 993 case PixelFormat.YCbCr_420_SP: return PIXEL_FORMAT_YUV420SP; 994 case PixelFormat.RGB_565: return PIXEL_FORMAT_RGB565; 995 case PixelFormat.JPEG: return PIXEL_FORMAT_JPEG; 996 default: return null; 997 } 998 } 999 1000 private int pixelFormatForCameraFormat(String format) { 1001 if (format == null) 1002 return PixelFormat.UNKNOWN; 1003 1004 if (format.equals(PIXEL_FORMAT_YUV422SP)) 1005 return PixelFormat.YCbCr_422_SP; 1006 1007 if (format.equals(PIXEL_FORMAT_YUV420SP)) 1008 return PixelFormat.YCbCr_420_SP; 1009 1010 if (format.equals(PIXEL_FORMAT_RGB565)) 1011 return PixelFormat.RGB_565; 1012 1013 if (format.equals(PIXEL_FORMAT_JPEG)) 1014 return PixelFormat.JPEG; 1015 1016 return PixelFormat.UNKNOWN; 1017 } 1018 1019 /** 1020 * Sets the orientation of the device in degrees, which instructs the 1021 * camera driver to rotate the picture and thumbnail, in order to match 1022 * what the user sees from the viewfinder. For example, suppose the 1023 * natural position of the device is landscape. If the user takes a 1024 * picture in landscape mode in 2048x1536 resolution, the rotation 1025 * should be set to 0. If the user rotates the phone 90 degrees 1026 * clockwise, the rotation should be set to 90. Applications can use 1027 * {@link android.view.OrientationEventListener} to set this parameter. 1028 * 1029 * Since the picture is rotated, the orientation in the EXIF header is 1030 * missing or always 1 (row #0 is top and column #0 is left side). 1031 * 1032 * @param rotation The orientation of the device in degrees. Rotation 1033 * can only be 0, 90, 180 or 270. 1034 * @throws IllegalArgumentException if rotation value is invalid. 1035 * @see android.view.OrientationEventListener 1036 */ 1037 public void setRotation(int rotation) { 1038 if (rotation == 0 || rotation == 90 || rotation == 180 1039 || rotation == 270) { 1040 set(KEY_ROTATION, Integer.toString(rotation)); 1041 } else { 1042 throw new IllegalArgumentException( 1043 "Invalid rotation=" + rotation); 1044 } 1045 } 1046 1047 /** 1048 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF 1049 * header. 1050 * 1051 * @param latitude GPS latitude coordinate. 1052 */ 1053 public void setGpsLatitude(double latitude) { 1054 set(KEY_GPS_LATITUDE, Double.toString(latitude)); 1055 } 1056 1057 /** 1058 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF 1059 * header. 1060 * 1061 * @param longitude GPS longitude coordinate. 1062 */ 1063 public void setGpsLongitude(double longitude) { 1064 set(KEY_GPS_LONGITUDE, Double.toString(longitude)); 1065 } 1066 1067 /** 1068 * Sets GPS altitude. This will be stored in JPEG EXIF header. 1069 * 1070 * @param altitude GPS altitude in meters. 1071 */ 1072 public void setGpsAltitude(double altitude) { 1073 set(KEY_GPS_ALTITUDE, Double.toString(altitude)); 1074 } 1075 1076 /** 1077 * Sets GPS timestamp. This will be stored in JPEG EXIF header. 1078 * 1079 * @param timestamp GPS timestamp (UTC in seconds since January 1, 1080 * 1970). 1081 */ 1082 public void setGpsTimestamp(long timestamp) { 1083 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp)); 1084 } 1085 1086 /** 1087 * Removes GPS latitude, longitude, altitude, and timestamp from the 1088 * parameters. 1089 */ 1090 public void removeGpsData() { 1091 remove(KEY_GPS_LATITUDE); 1092 remove(KEY_GPS_LONGITUDE); 1093 remove(KEY_GPS_ALTITUDE); 1094 remove(KEY_GPS_TIMESTAMP); 1095 } 1096 1097 /** 1098 * Gets the current white balance setting. 1099 * 1100 * @return one of WHITE_BALANCE_XXX string constant. null if white 1101 * balance setting is not supported. 1102 */ 1103 public String getWhiteBalance() { 1104 return get(KEY_WHITE_BALANCE); 1105 } 1106 1107 /** 1108 * Sets the white balance. 1109 * 1110 * @param value WHITE_BALANCE_XXX string constant. 1111 */ 1112 public void setWhiteBalance(String value) { 1113 set(KEY_WHITE_BALANCE, value); 1114 } 1115 1116 /** 1117 * Gets the supported white balance. 1118 * 1119 * @return a List of WHITE_BALANCE_XXX string constants. null if white 1120 * balance setting is not supported. 1121 */ 1122 public List<String> getSupportedWhiteBalance() { 1123 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX); 1124 return split(str); 1125 } 1126 1127 /** 1128 * Gets the current color effect setting. 1129 * 1130 * @return one of EFFECT_XXX string constant. null if color effect 1131 * setting is not supported. 1132 */ 1133 public String getColorEffect() { 1134 return get(KEY_EFFECT); 1135 } 1136 1137 /** 1138 * Sets the current color effect setting. 1139 * 1140 * @param value EFFECT_XXX string constants. 1141 */ 1142 public void setColorEffect(String value) { 1143 set(KEY_EFFECT, value); 1144 } 1145 1146 /** 1147 * Gets the supported color effects. 1148 * 1149 * @return a List of EFFECT_XXX string constants. null if color effect 1150 * setting is not supported. 1151 */ 1152 public List<String> getSupportedColorEffects() { 1153 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX); 1154 return split(str); 1155 } 1156 1157 1158 /** 1159 * Gets the current antibanding setting. 1160 * 1161 * @return one of ANTIBANDING_XXX string constant. null if antibanding 1162 * setting is not supported. 1163 */ 1164 public String getAntibanding() { 1165 return get(KEY_ANTIBANDING); 1166 } 1167 1168 /** 1169 * Sets the antibanding. 1170 * 1171 * @param antibanding ANTIBANDING_XXX string constant. 1172 */ 1173 public void setAntibanding(String antibanding) { 1174 set(KEY_ANTIBANDING, antibanding); 1175 } 1176 1177 /** 1178 * Gets the supported antibanding values. 1179 * 1180 * @return a List of ANTIBANDING_XXX string constants. null if 1181 * antibanding setting is not supported. 1182 */ 1183 public List<String> getSupportedAntibanding() { 1184 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX); 1185 return split(str); 1186 } 1187 1188 /** 1189 * Gets the current scene mode setting. 1190 * 1191 * @return one of SCENE_MODE_XXX string constant. null if scene mode 1192 * setting is not supported. 1193 */ 1194 public String getSceneMode() { 1195 return get(KEY_SCENE_MODE); 1196 } 1197 1198 /** 1199 * Sets the scene mode. 1200 * 1201 * @param value SCENE_MODE_XXX string constants. 1202 */ 1203 public void setSceneMode(String value) { 1204 set(KEY_SCENE_MODE, value); 1205 } 1206 1207 /** 1208 * Gets the supported scene modes. 1209 * 1210 * @return a List of SCENE_MODE_XXX string constant. null if scene mode 1211 * setting is not supported. 1212 */ 1213 public List<String> getSupportedSceneModes() { 1214 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX); 1215 return split(str); 1216 } 1217 1218 /** 1219 * Gets the current flash mode setting. 1220 * 1221 * @return one of FLASH_MODE_XXX string constant. null if flash mode 1222 * setting is not supported. 1223 */ 1224 public String getFlashMode() { 1225 return get(KEY_FLASH_MODE); 1226 } 1227 1228 /** 1229 * Sets the flash mode. 1230 * 1231 * @param value FLASH_MODE_XXX string constants. 1232 */ 1233 public void setFlashMode(String value) { 1234 set(KEY_FLASH_MODE, value); 1235 } 1236 1237 /** 1238 * Gets the supported flash modes. 1239 * 1240 * @return a List of FLASH_MODE_XXX string constants. null if flash mode 1241 * setting is not supported. 1242 */ 1243 public List<String> getSupportedFlashModes() { 1244 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX); 1245 return split(str); 1246 } 1247 1248 // Splits a comma delimited string to an ArrayList of String. 1249 // Return null if the passing string is null or the size is 0. 1250 private ArrayList<String> split(String str) { 1251 if (str == null) return null; 1252 1253 // Use StringTokenizer because it is faster than split. 1254 StringTokenizer tokenizer = new StringTokenizer(str, ","); 1255 ArrayList<String> substrings = new ArrayList<String>(); 1256 while (tokenizer.hasMoreElements()) { 1257 substrings.add(tokenizer.nextToken()); 1258 } 1259 return substrings; 1260 } 1261 1262 // Splits a comma delimited string to an ArrayList of Integer. 1263 // Return null if the passing string is null or the size is 0. 1264 private ArrayList<Integer> splitInt(String str) { 1265 if (str == null) return null; 1266 1267 StringTokenizer tokenizer = new StringTokenizer(str, ","); 1268 ArrayList<Integer> substrings = new ArrayList<Integer>(); 1269 while (tokenizer.hasMoreElements()) { 1270 String token = tokenizer.nextToken(); 1271 substrings.add(Integer.parseInt(token)); 1272 } 1273 if (substrings.size() == 0) return null; 1274 return substrings; 1275 } 1276 1277 // Splits a comma delimited string to an ArrayList of Size. 1278 // Return null if the passing string is null or the size is 0. 1279 private ArrayList<Size> splitSize(String str) { 1280 if (str == null) return null; 1281 1282 StringTokenizer tokenizer = new StringTokenizer(str, ","); 1283 ArrayList<Size> sizeList = new ArrayList<Size>(); 1284 while (tokenizer.hasMoreElements()) { 1285 Size size = strToSize(tokenizer.nextToken()); 1286 if (size != null) sizeList.add(size); 1287 } 1288 if (sizeList.size() == 0) return null; 1289 return sizeList; 1290 } 1291 1292 // Parses a string (ex: "480x320") to Size object. 1293 // Return null if the passing string is null. 1294 private Size strToSize(String str) { 1295 if (str == null) return null; 1296 1297 int pos = str.indexOf('x'); 1298 if (pos != -1) { 1299 String width = str.substring(0, pos); 1300 String height = str.substring(pos + 1); 1301 return new Size(Integer.parseInt(width), 1302 Integer.parseInt(height)); 1303 } 1304 Log.e(TAG, "Invalid size parameter string=" + str); 1305 return null; 1306 } 1307 }; 1308} 1309 1310 1311