The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.hardware; |
| 18 | |
| 19 | import java.lang.ref.WeakReference; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.StringTokenizer; |
| 22 | import java.io.IOException; |
| 23 | |
| 24 | import android.util.Log; |
| 25 | import android.view.Surface; |
| 26 | import android.view.SurfaceHolder; |
| 27 | import android.graphics.PixelFormat; |
| 28 | import android.os.Handler; |
| 29 | import android.os.Looper; |
| 30 | import android.os.Message; |
| 31 | |
| 32 | /** |
| 33 | * The Camera class is used to connect/disconnect with the camera service, |
| 34 | * set capture settings, start/stop preview, snap a picture, and retrieve |
| 35 | * frames for encoding for video. |
| 36 | * <p>There is no default constructor for this class. Use {@link #open()} to |
| 37 | * get a Camera object.</p> |
Scott Main | df4578e | 2009-09-10 12:22:07 -0700 | [diff] [blame^] | 38 | * |
| 39 | * <p>In order to use the device camera, you must declare the |
| 40 | * {@link android.Manifest.permission#CAMERA} permission in your Android |
| 41 | * Manifest. Also be sure to include the |
| 42 | * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> |
| 43 | * manifest element in order to declare camera features used by your application. |
| 44 | * For example, if you use the camera and auto-focus feature, your Manifest |
| 45 | * should include the following:</p> |
| 46 | * <pre> <uses-permission android:name="android.permission.CAMERA" /> |
| 47 | * <uses-feature android:name="android.hardware.camera" /> |
| 48 | * <uses-feature android:name="android.hardware.camera.autofocus" /></pre> |
| 49 | * |
| 50 | * <p class="caution"><strong>Caution:</strong> Different Android-powered devices |
| 51 | * may have different hardware specifications, such as megapixel ratings and |
| 52 | * auto-focus capabilities. In order for your application to be compatible with |
| 53 | * more devices, you should not make assumptions about the device camera |
| 54 | * specifications.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | */ |
| 56 | public class Camera { |
| 57 | private static final String TAG = "Camera"; |
| 58 | |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 59 | // These match the enums in frameworks/base/include/ui/Camera.h |
| 60 | private static final int CAMERA_MSG_ERROR = 0; |
| 61 | private static final int CAMERA_MSG_SHUTTER = 1; |
| 62 | private static final int CAMERA_MSG_FOCUS = 2; |
| 63 | private static final int CAMERA_MSG_ZOOM = 3; |
| 64 | private static final int CAMERA_MSG_PREVIEW_FRAME = 4; |
| 65 | private static final int CAMERA_MSG_VIDEO_FRAME = 5; |
| 66 | private static final int CAMERA_MSG_POSTVIEW_FRAME = 6; |
| 67 | private static final int CAMERA_MSG_RAW_IMAGE = 7; |
| 68 | private static final int CAMERA_MSG_COMPRESSED_IMAGE = 8; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| 70 | private int mNativeContext; // accessed by native methods |
| 71 | private EventHandler mEventHandler; |
| 72 | private ShutterCallback mShutterCallback; |
| 73 | private PictureCallback mRawImageCallback; |
| 74 | private PictureCallback mJpegCallback; |
| 75 | private PreviewCallback mPreviewCallback; |
| 76 | private AutoFocusCallback mAutoFocusCallback; |
| 77 | private ErrorCallback mErrorCallback; |
| 78 | private boolean mOneShot; |
| 79 | |
| 80 | /** |
| 81 | * Returns a new Camera object. |
| 82 | */ |
| 83 | public static Camera open() { |
| 84 | return new Camera(); |
| 85 | } |
| 86 | |
| 87 | Camera() { |
| 88 | mShutterCallback = null; |
| 89 | mRawImageCallback = null; |
| 90 | mJpegCallback = null; |
| 91 | mPreviewCallback = null; |
| 92 | |
| 93 | Looper looper; |
| 94 | if ((looper = Looper.myLooper()) != null) { |
| 95 | mEventHandler = new EventHandler(this, looper); |
| 96 | } else if ((looper = Looper.getMainLooper()) != null) { |
| 97 | mEventHandler = new EventHandler(this, looper); |
| 98 | } else { |
| 99 | mEventHandler = null; |
| 100 | } |
| 101 | |
| 102 | native_setup(new WeakReference<Camera>(this)); |
| 103 | } |
| 104 | |
| 105 | protected void finalize() { |
| 106 | native_release(); |
| 107 | } |
| 108 | |
| 109 | private native final void native_setup(Object camera_this); |
| 110 | private native final void native_release(); |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Disconnects and releases the Camera object resources. |
| 115 | * <p>It is recommended that you call this as soon as you're done with the |
| 116 | * Camera object.</p> |
| 117 | */ |
| 118 | public final void release() { |
| 119 | native_release(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Reconnect to the camera after passing it to MediaRecorder. To save |
| 124 | * setup/teardown time, a client of Camera can pass an initialized Camera |
| 125 | * object to a MediaRecorder to use for video recording. Once the |
| 126 | * MediaRecorder is done with the Camera, this method can be used to |
| 127 | * re-establish a connection with the camera hardware. NOTE: The Camera |
| 128 | * object must first be unlocked by the process that owns it before it |
| 129 | * can be connected to another proces. |
| 130 | * |
| 131 | * @throws IOException if the method fails. |
| 132 | * |
| 133 | * FIXME: Unhide after approval |
| 134 | * @hide |
| 135 | */ |
| 136 | public native final void reconnect() throws IOException; |
| 137 | |
| 138 | /** |
| 139 | * Lock the camera to prevent other processes from accessing it. To save |
| 140 | * setup/teardown time, a client of Camera can pass an initialized Camera |
| 141 | * object to another process. This method is used to re-lock the Camera |
| 142 | * object prevent other processes from accessing it. By default, the |
| 143 | * Camera object is locked. Locking it again from the same process will |
| 144 | * have no effect. Attempting to lock it from another process if it has |
| 145 | * not been unlocked will fail. |
| 146 | * Returns 0 if lock was successful. |
| 147 | * |
| 148 | * FIXME: Unhide after approval |
| 149 | * @hide |
| 150 | */ |
| 151 | public native final int lock(); |
| 152 | |
| 153 | /** |
| 154 | * Unlock the camera to allow aother process to access it. To save |
| 155 | * setup/teardown time, a client of Camera can pass an initialized Camera |
| 156 | * object to another process. This method is used to unlock the Camera |
| 157 | * object before handing off the Camera object to the other process. |
| 158 | |
| 159 | * Returns 0 if unlock was successful. |
| 160 | * |
| 161 | * FIXME: Unhide after approval |
| 162 | * @hide |
| 163 | */ |
| 164 | public native final int unlock(); |
| 165 | |
| 166 | /** |
| 167 | * Sets the SurfaceHolder to be used for a picture preview. If the surface |
| 168 | * changed since the last call, the screen will blank. Nothing happens |
| 169 | * if the same surface is re-set. |
| 170 | * |
| 171 | * @param holder the SurfaceHolder upon which to place the picture preview |
| 172 | * @throws IOException if the method fails. |
| 173 | */ |
| 174 | public final void setPreviewDisplay(SurfaceHolder holder) throws IOException { |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 175 | if (holder != null) { |
| 176 | setPreviewDisplay(holder.getSurface()); |
| 177 | } else { |
| 178 | setPreviewDisplay((Surface)null); |
| 179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private native final void setPreviewDisplay(Surface surface); |
| 183 | |
| 184 | /** |
| 185 | * Used to get a copy of each preview frame. |
| 186 | */ |
| 187 | public interface PreviewCallback |
| 188 | { |
| 189 | /** |
| 190 | * The callback that delivers the preview frames. |
| 191 | * |
Scott Main | df4578e | 2009-09-10 12:22:07 -0700 | [diff] [blame^] | 192 | * @param data The contents of the preview frame in the format defined |
| 193 | * by {@link android.graphics.PixelFormat}, which can be queried |
| 194 | * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | * @param camera The Camera service object. |
| 196 | */ |
| 197 | void onPreviewFrame(byte[] data, Camera camera); |
| 198 | }; |
| 199 | |
| 200 | /** |
| 201 | * Start drawing preview frames to the surface. |
| 202 | */ |
| 203 | public native final void startPreview(); |
| 204 | |
| 205 | /** |
| 206 | * Stop drawing preview frames to the surface. |
| 207 | */ |
| 208 | public native final void stopPreview(); |
| 209 | |
| 210 | /** |
| 211 | * Return current preview state. |
| 212 | * |
| 213 | * FIXME: Unhide before release |
| 214 | * @hide |
| 215 | */ |
| 216 | public native final boolean previewEnabled(); |
| 217 | |
| 218 | /** |
| 219 | * Can be called at any time to instruct the camera to use a callback for |
| 220 | * each preview frame in addition to displaying it. |
| 221 | * |
| 222 | * @param cb A callback object that receives a copy of each preview frame. |
| 223 | * Pass null to stop receiving callbacks at any time. |
| 224 | */ |
| 225 | public final void setPreviewCallback(PreviewCallback cb) { |
| 226 | mPreviewCallback = cb; |
| 227 | mOneShot = false; |
| 228 | setHasPreviewCallback(cb != null, false); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Installs a callback to retrieve a single preview frame, after which the |
| 233 | * callback is cleared. |
| 234 | * |
| 235 | * @param cb A callback object that receives a copy of the preview frame. |
| 236 | */ |
| 237 | public final void setOneShotPreviewCallback(PreviewCallback cb) { |
| 238 | if (cb != null) { |
| 239 | mPreviewCallback = cb; |
| 240 | mOneShot = true; |
| 241 | setHasPreviewCallback(true, true); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | private native final void setHasPreviewCallback(boolean installed, boolean oneshot); |
| 246 | |
| 247 | private class EventHandler extends Handler |
| 248 | { |
| 249 | private Camera mCamera; |
| 250 | |
| 251 | public EventHandler(Camera c, Looper looper) { |
| 252 | super(looper); |
| 253 | mCamera = c; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void handleMessage(Message msg) { |
| 258 | switch(msg.what) { |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 259 | case CAMERA_MSG_SHUTTER: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | if (mShutterCallback != null) { |
| 261 | mShutterCallback.onShutter(); |
| 262 | } |
| 263 | return; |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 264 | |
| 265 | case CAMERA_MSG_RAW_IMAGE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | if (mRawImageCallback != null) |
| 267 | mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera); |
| 268 | return; |
| 269 | |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 270 | case CAMERA_MSG_COMPRESSED_IMAGE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | if (mJpegCallback != null) |
| 272 | mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera); |
| 273 | return; |
| 274 | |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 275 | case CAMERA_MSG_PREVIEW_FRAME: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | if (mPreviewCallback != null) { |
| 277 | mPreviewCallback.onPreviewFrame((byte[])msg.obj, mCamera); |
| 278 | if (mOneShot) { |
| 279 | mPreviewCallback = null; |
| 280 | } |
| 281 | } |
| 282 | return; |
| 283 | |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 284 | case CAMERA_MSG_FOCUS: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | if (mAutoFocusCallback != null) |
| 286 | mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera); |
| 287 | return; |
| 288 | |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 289 | case CAMERA_MSG_ERROR : |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | Log.e(TAG, "Error " + msg.arg1); |
| 291 | if (mErrorCallback != null) |
| 292 | mErrorCallback.onError(msg.arg1, mCamera); |
| 293 | return; |
| 294 | |
| 295 | default: |
| 296 | Log.e(TAG, "Unknown message type " + msg.what); |
| 297 | return; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | private static void postEventFromNative(Object camera_ref, |
| 303 | int what, int arg1, int arg2, Object obj) |
| 304 | { |
| 305 | Camera c = (Camera)((WeakReference)camera_ref).get(); |
| 306 | if (c == null) |
| 307 | return; |
| 308 | |
| 309 | if (c.mEventHandler != null) { |
| 310 | Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj); |
| 311 | c.mEventHandler.sendMessage(m); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Handles the callback for the camera auto focus. |
Scott Main | df4578e | 2009-09-10 12:22:07 -0700 | [diff] [blame^] | 317 | * <p>Devices that do not support auto-focus will receive a "fake" |
| 318 | * callback to this interface. If your application needs auto-focus and |
| 319 | * should not be installed on devices <em>without</em> auto-focus, you must |
| 320 | * declare that your app uses the |
| 321 | * {@code android.hardware.camera.autofocus} feature, in the |
| 322 | * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> |
| 323 | * manifest element.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | */ |
| 325 | public interface AutoFocusCallback |
| 326 | { |
| 327 | /** |
| 328 | * Callback for the camera auto focus. |
| 329 | * |
| 330 | * @param success true if focus was successful, false if otherwise |
| 331 | * @param camera the Camera service object |
| 332 | */ |
| 333 | void onAutoFocus(boolean success, Camera camera); |
| 334 | }; |
| 335 | |
| 336 | /** |
| 337 | * Starts auto-focus function and registers a callback function to |
| 338 | * run when camera is focused. Only valid after startPreview() has |
| 339 | * been called. |
Scott Main | df4578e | 2009-09-10 12:22:07 -0700 | [diff] [blame^] | 340 | * <p>Devices that do not support auto-focus will trigger a "fake" |
| 341 | * callback to the |
| 342 | * {@link android.hardware.Camera.AutoFocusCallback}. |
| 343 | * If your application should not be installed |
| 344 | * on devices without auto-focus, you must declare that your application |
| 345 | * uses auto-focus with the |
| 346 | * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a> |
| 347 | * manifest element.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | * |
| 349 | * @param cb the callback to run |
| 350 | */ |
| 351 | public final void autoFocus(AutoFocusCallback cb) |
| 352 | { |
| 353 | mAutoFocusCallback = cb; |
| 354 | native_autoFocus(); |
| 355 | } |
| 356 | private native final void native_autoFocus(); |
| 357 | |
| 358 | /** |
| 359 | * An interface which contains a callback for the shutter closing after taking a picture. |
| 360 | */ |
| 361 | public interface ShutterCallback |
| 362 | { |
| 363 | /** |
| 364 | * Can be used to play a shutter sound as soon as the image has been captured, but before |
| 365 | * the data is available. |
| 366 | */ |
| 367 | void onShutter(); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Handles the callback for when a picture is taken. |
| 372 | */ |
| 373 | public interface PictureCallback { |
| 374 | /** |
| 375 | * Callback for when a picture is taken. |
| 376 | * |
| 377 | * @param data a byte array of the picture data |
| 378 | * @param camera the Camera service object |
| 379 | */ |
| 380 | void onPictureTaken(byte[] data, Camera camera); |
| 381 | }; |
| 382 | |
| 383 | /** |
| 384 | * Triggers an asynchronous image capture. The camera service |
| 385 | * will initiate a series of callbacks to the application as the |
| 386 | * image capture progresses. The shutter callback occurs after |
| 387 | * the image is captured. This can be used to trigger a sound |
| 388 | * to let the user know that image has been captured. The raw |
| 389 | * callback occurs when the raw image data is available. The jpeg |
| 390 | * callback occurs when the compressed image is available. If the |
| 391 | * application does not need a particular callback, a null can be |
| 392 | * passed instead of a callback method. |
| 393 | * |
| 394 | * @param shutter callback after the image is captured, may be null |
| 395 | * @param raw callback with raw image data, may be null |
| 396 | * @param jpeg callback with jpeg image data, may be null |
| 397 | */ |
| 398 | public final void takePicture(ShutterCallback shutter, PictureCallback raw, |
| 399 | PictureCallback jpeg) { |
| 400 | mShutterCallback = shutter; |
| 401 | mRawImageCallback = raw; |
| 402 | mJpegCallback = jpeg; |
| 403 | native_takePicture(); |
| 404 | } |
| 405 | private native final void native_takePicture(); |
| 406 | |
James Dong | a1b653d | 2009-07-02 10:04:20 -0700 | [diff] [blame] | 407 | // These match the enum in include/ui/Camera.h |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | /** Unspecified camerar error. @see #ErrorCallback */ |
| 409 | public static final int CAMERA_ERROR_UNKNOWN = 1; |
| 410 | /** Media server died. In this case, the application must release the |
| 411 | * Camera object and instantiate a new one. @see #ErrorCallback */ |
| 412 | public static final int CAMERA_ERROR_SERVER_DIED = 100; |
| 413 | |
| 414 | /** |
| 415 | * Handles the camera error callback. |
| 416 | */ |
| 417 | public interface ErrorCallback |
| 418 | { |
| 419 | /** |
| 420 | * Callback for camera errors. |
| 421 | * @param error error code: |
| 422 | * <ul> |
| 423 | * <li>{@link #CAMERA_ERROR_UNKNOWN} |
| 424 | * <li>{@link #CAMERA_ERROR_SERVER_DIED} |
| 425 | * </ul> |
| 426 | * @param camera the Camera service object |
| 427 | */ |
| 428 | void onError(int error, Camera camera); |
| 429 | }; |
| 430 | |
| 431 | /** |
| 432 | * Registers a callback to be invoked when an error occurs. |
| 433 | * @param cb the callback to run |
| 434 | */ |
| 435 | public final void setErrorCallback(ErrorCallback cb) |
| 436 | { |
| 437 | mErrorCallback = cb; |
| 438 | } |
| 439 | |
| 440 | private native final void native_setParameters(String params); |
| 441 | private native final String native_getParameters(); |
| 442 | |
| 443 | /** |
| 444 | * Sets the Parameters for pictures from this Camera service. |
| 445 | * |
| 446 | * @param params the Parameters to use for this Camera service |
| 447 | */ |
| 448 | public void setParameters(Parameters params) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | native_setParameters(params.flatten()); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Returns the picture Parameters for this Camera service. |
| 454 | */ |
| 455 | public Parameters getParameters() { |
| 456 | Parameters p = new Parameters(); |
| 457 | String s = native_getParameters(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | p.unflatten(s); |
| 459 | return p; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Handles the picture size (dimensions). |
| 464 | */ |
| 465 | public class Size { |
| 466 | /** |
| 467 | * Sets the dimensions for pictures. |
| 468 | * |
| 469 | * @param w the photo width (pixels) |
| 470 | * @param h the photo height (pixels) |
| 471 | */ |
| 472 | public Size(int w, int h) { |
| 473 | width = w; |
| 474 | height = h; |
| 475 | } |
| 476 | /** width of the picture */ |
| 477 | public int width; |
| 478 | /** height of the picture */ |
| 479 | public int height; |
| 480 | }; |
| 481 | |
| 482 | /** |
| 483 | * Handles the parameters for pictures created by a Camera service. |
| 484 | */ |
| 485 | public class Parameters { |
| 486 | private HashMap<String, String> mMap; |
| 487 | |
| 488 | private Parameters() { |
| 489 | mMap = new HashMap<String, String>(); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Writes the current Parameters to the log. |
| 494 | * @hide |
| 495 | * @deprecated |
| 496 | */ |
| 497 | public void dump() { |
| 498 | Log.e(TAG, "dump: size=" + mMap.size()); |
| 499 | for (String k : mMap.keySet()) { |
| 500 | Log.e(TAG, "dump: " + k + "=" + mMap.get(k)); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Creates a single string with all the parameters set in |
| 506 | * this Parameters object. |
| 507 | * <p>The {@link #unflatten(String)} method does the reverse.</p> |
| 508 | * |
| 509 | * @return a String with all values from this Parameters object, in |
| 510 | * semi-colon delimited key-value pairs |
| 511 | */ |
| 512 | public String flatten() { |
| 513 | StringBuilder flattened = new StringBuilder(); |
| 514 | for (String k : mMap.keySet()) { |
| 515 | flattened.append(k); |
| 516 | flattened.append("="); |
| 517 | flattened.append(mMap.get(k)); |
| 518 | flattened.append(";"); |
| 519 | } |
| 520 | // chop off the extra semicolon at the end |
| 521 | flattened.deleteCharAt(flattened.length()-1); |
| 522 | return flattened.toString(); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Takes a flattened string of parameters and adds each one to |
| 527 | * this Parameters object. |
| 528 | * <p>The {@link #flatten()} method does the reverse.</p> |
| 529 | * |
| 530 | * @param flattened a String of parameters (key-value paired) that |
| 531 | * are semi-colon delimited |
| 532 | */ |
| 533 | public void unflatten(String flattened) { |
| 534 | mMap.clear(); |
| 535 | |
| 536 | StringTokenizer tokenizer = new StringTokenizer(flattened, ";"); |
| 537 | while (tokenizer.hasMoreElements()) { |
| 538 | String kv = tokenizer.nextToken(); |
| 539 | int pos = kv.indexOf('='); |
| 540 | if (pos == -1) { |
| 541 | continue; |
| 542 | } |
| 543 | String k = kv.substring(0, pos); |
| 544 | String v = kv.substring(pos + 1); |
| 545 | mMap.put(k, v); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | public void remove(String key) { |
| 550 | mMap.remove(key); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Sets a String parameter. |
| 555 | * |
| 556 | * @param key the key name for the parameter |
| 557 | * @param value the String value of the parameter |
| 558 | */ |
| 559 | public void set(String key, String value) { |
| 560 | if (key.indexOf('=') != -1 || key.indexOf(';') != -1) { |
| 561 | Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ;)"); |
| 562 | return; |
| 563 | } |
| 564 | if (value.indexOf('=') != -1 || value.indexOf(';') != -1) { |
| 565 | Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ;)"); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | mMap.put(key, value); |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * Sets an integer parameter. |
| 574 | * |
| 575 | * @param key the key name for the parameter |
| 576 | * @param value the int value of the parameter |
| 577 | */ |
| 578 | public void set(String key, int value) { |
| 579 | mMap.put(key, Integer.toString(value)); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Returns the value of a String parameter. |
| 584 | * |
| 585 | * @param key the key name for the parameter |
| 586 | * @return the String value of the parameter |
| 587 | */ |
| 588 | public String get(String key) { |
| 589 | return mMap.get(key); |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Returns the value of an integer parameter. |
| 594 | * |
| 595 | * @param key the key name for the parameter |
| 596 | * @return the int value of the parameter |
| 597 | */ |
| 598 | public int getInt(String key) { |
| 599 | return Integer.parseInt(mMap.get(key)); |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Sets the dimensions for preview pictures. |
| 604 | * |
| 605 | * @param width the width of the pictures, in pixels |
| 606 | * @param height the height of the pictures, in pixels |
| 607 | */ |
| 608 | public void setPreviewSize(int width, int height) { |
| 609 | String v = Integer.toString(width) + "x" + Integer.toString(height); |
| 610 | set("preview-size", v); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Returns the dimensions setting for preview pictures. |
| 615 | * |
| 616 | * @return a Size object with the height and width setting |
| 617 | * for the preview picture |
| 618 | */ |
| 619 | public Size getPreviewSize() { |
| 620 | String pair = get("preview-size"); |
| 621 | if (pair == null) |
| 622 | return null; |
| 623 | String[] dims = pair.split("x"); |
| 624 | if (dims.length != 2) |
| 625 | return null; |
| 626 | |
| 627 | return new Size(Integer.parseInt(dims[0]), |
| 628 | Integer.parseInt(dims[1])); |
| 629 | |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Sets the dimensions for EXIF thumbnails. |
| 634 | * |
| 635 | * @param width the width of the thumbnail, in pixels |
| 636 | * @param height the height of the thumbnail, in pixels |
| 637 | * |
| 638 | * FIXME: unhide before release |
| 639 | * @hide |
| 640 | */ |
| 641 | public void setThumbnailSize(int width, int height) { |
| 642 | set("jpeg-thumbnail-width", width); |
| 643 | set("jpeg-thumbnail-height", height); |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Returns the dimensions for EXIF thumbnail |
| 648 | * |
| 649 | * @return a Size object with the height and width setting |
| 650 | * for the EXIF thumbnails |
| 651 | * |
| 652 | * FIXME: unhide before release |
| 653 | * @hide |
| 654 | */ |
| 655 | public Size getThumbnailSize() { |
| 656 | return new Size(getInt("jpeg-thumbnail-width"), |
| 657 | getInt("jpeg-thumbnail-height")); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Sets the quality of the EXIF thumbnail |
| 662 | * |
| 663 | * @param quality the JPEG quality of the EXIT thumbnail |
| 664 | * |
| 665 | * FIXME: unhide before release |
| 666 | * @hide |
| 667 | */ |
| 668 | public void setThumbnailQuality(int quality) { |
| 669 | set("jpeg-thumbnail-quality", quality); |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Returns the quality setting for the EXIF thumbnail |
| 674 | * |
| 675 | * @return the JPEG quality setting of the EXIF thumbnail |
| 676 | * |
| 677 | * FIXME: unhide before release |
| 678 | * @hide |
| 679 | */ |
| 680 | public int getThumbnailQuality() { |
| 681 | return getInt("jpeg-thumbnail-quality"); |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Sets the rate at which preview frames are received. |
| 686 | * |
| 687 | * @param fps the frame rate (frames per second) |
| 688 | */ |
| 689 | public void setPreviewFrameRate(int fps) { |
| 690 | set("preview-frame-rate", fps); |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Returns the setting for the rate at which preview frames |
| 695 | * are received. |
| 696 | * |
| 697 | * @return the frame rate setting (frames per second) |
| 698 | */ |
| 699 | public int getPreviewFrameRate() { |
| 700 | return getInt("preview-frame-rate"); |
| 701 | } |
| 702 | |
| 703 | /** |
Scott Main | df4578e | 2009-09-10 12:22:07 -0700 | [diff] [blame^] | 704 | * Sets the image format for preview pictures. |
| 705 | * <p>If the image format is not set with this method, then the |
| 706 | * preview format will default to |
| 707 | * {@link android.graphics.PixelFormat#YCbCr_420_SP}, which |
| 708 | * uses the NV21 encoding format.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | * |
Scott Main | df4578e | 2009-09-10 12:22:07 -0700 | [diff] [blame^] | 710 | * @param pixel_format the desired preview picture format, defined |
| 711 | * by one of the {@link android.graphics.PixelFormat} constants. |
| 712 | * (E.g., <var>PixelFormat.YCbCr_420_SP</var> (default), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | * <var>PixelFormat.RGB_565</var>, or |
| 714 | * <var>PixelFormat.JPEG</var>) |
| 715 | * @see android.graphics.PixelFormat |
| 716 | */ |
| 717 | public void setPreviewFormat(int pixel_format) { |
| 718 | String s = cameraFormatForPixelFormat(pixel_format); |
| 719 | if (s == null) { |
| 720 | throw new IllegalArgumentException(); |
| 721 | } |
| 722 | |
| 723 | set("preview-format", s); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Returns the image format for preview pictures. |
| 728 | * |
Scott Main | df4578e | 2009-09-10 12:22:07 -0700 | [diff] [blame^] | 729 | * @return the {@link android.graphics.PixelFormat} int representing |
| 730 | * the preview picture format. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | */ |
| 732 | public int getPreviewFormat() { |
| 733 | return pixelFormatForCameraFormat(get("preview-format")); |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Sets the dimensions for pictures. |
| 738 | * |
| 739 | * @param width the width for pictures, in pixels |
| 740 | * @param height the height for pictures, in pixels |
| 741 | */ |
| 742 | public void setPictureSize(int width, int height) { |
| 743 | String v = Integer.toString(width) + "x" + Integer.toString(height); |
| 744 | set("picture-size", v); |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Returns the dimension setting for pictures. |
| 749 | * |
| 750 | * @return a Size object with the height and width setting |
| 751 | * for pictures |
| 752 | */ |
| 753 | public Size getPictureSize() { |
| 754 | String pair = get("picture-size"); |
| 755 | if (pair == null) |
| 756 | return null; |
| 757 | String[] dims = pair.split("x"); |
| 758 | if (dims.length != 2) |
| 759 | return null; |
| 760 | |
| 761 | return new Size(Integer.parseInt(dims[0]), |
| 762 | Integer.parseInt(dims[1])); |
| 763 | |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Sets the image format for pictures. |
| 768 | * |
| 769 | * @param pixel_format the desired picture format |
| 770 | * (<var>PixelFormat.YCbCr_420_SP</var>, |
| 771 | * <var>PixelFormat.RGB_565</var>, or |
| 772 | * <var>PixelFormat.JPEG</var>) |
| 773 | * @see android.graphics.PixelFormat |
| 774 | */ |
| 775 | public void setPictureFormat(int pixel_format) { |
| 776 | String s = cameraFormatForPixelFormat(pixel_format); |
| 777 | if (s == null) { |
| 778 | throw new IllegalArgumentException(); |
| 779 | } |
| 780 | |
| 781 | set("picture-format", s); |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Returns the image format for pictures. |
| 786 | * |
| 787 | * @return the PixelFormat int representing the picture format |
| 788 | */ |
| 789 | public int getPictureFormat() { |
| 790 | return pixelFormatForCameraFormat(get("picture-format")); |
| 791 | } |
| 792 | |
| 793 | private String cameraFormatForPixelFormat(int pixel_format) { |
| 794 | switch(pixel_format) { |
| 795 | case PixelFormat.YCbCr_422_SP: return "yuv422sp"; |
| 796 | case PixelFormat.YCbCr_420_SP: return "yuv420sp"; |
| 797 | case PixelFormat.RGB_565: return "rgb565"; |
| 798 | case PixelFormat.JPEG: return "jpeg"; |
| 799 | default: return null; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | private int pixelFormatForCameraFormat(String format) { |
| 804 | if (format == null) |
| 805 | return PixelFormat.UNKNOWN; |
| 806 | |
| 807 | if (format.equals("yuv422sp")) |
| 808 | return PixelFormat.YCbCr_422_SP; |
| 809 | |
| 810 | if (format.equals("yuv420sp")) |
| 811 | return PixelFormat.YCbCr_420_SP; |
| 812 | |
| 813 | if (format.equals("rgb565")) |
| 814 | return PixelFormat.RGB_565; |
| 815 | |
| 816 | if (format.equals("jpeg")) |
| 817 | return PixelFormat.JPEG; |
| 818 | |
| 819 | return PixelFormat.UNKNOWN; |
| 820 | } |
| 821 | |
| 822 | }; |
| 823 | } |
| 824 | |
| 825 | |