blob: dee1d03f9d66de2ea7ca1b91343e0b1935d82d26 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +080020import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import java.util.HashMap;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +080022import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.StringTokenizer;
24import java.io.IOException;
25
26import android.util.Log;
27import android.view.Surface;
28import android.view.SurfaceHolder;
Mathias Agopiana696f5d2010-02-17 17:53:09 -080029import android.graphics.ImageFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33
34/**
Dan Egnorbfcbeff2010-07-12 15:12:54 -070035 * The Camera class is used to set image capture settings, start/stop preview,
36 * snap pictures, and retrieve frames for encoding for video. This class is a
37 * client for the Camera service, which manages the actual camera hardware.
Scott Maindf4578e2009-09-10 12:22:07 -070038 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070039 * <p>To access the device camera, you must declare the
Wu-cheng Li7478ea62009-09-16 18:52:55 +080040 * {@link android.Manifest.permission#CAMERA} permission in your Android
Scott Maindf4578e2009-09-10 12:22:07 -070041 * Manifest. Also be sure to include the
42 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
Dan Egnorbfcbeff2010-07-12 15:12:54 -070043 * manifest element to declare camera features used by your application.
Wu-cheng Li7478ea62009-09-16 18:52:55 +080044 * For example, if you use the camera and auto-focus feature, your Manifest
Scott Maindf4578e2009-09-10 12:22:07 -070045 * should include the following:</p>
46 * <pre> &lt;uses-permission android:name="android.permission.CAMERA" />
47 * &lt;uses-feature android:name="android.hardware.camera" />
48 * &lt;uses-feature android:name="android.hardware.camera.autofocus" /></pre>
49 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070050 * <p>To take pictures with this class, use the following steps:</p>
51 *
52 * <ol>
Dan Egnor341ff132010-07-20 11:30:17 -070053 * <li>Obtain an instance of Camera from {@link #open(int)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -070054 *
55 * <li>Get existing (default) settings with {@link #getParameters()}.
56 *
57 * <li>If necessary, modify the returned {@link Camera.Parameters} object and call
58 * {@link #setParameters(Camera.Parameters)}.
59 *
60 * <li>If desired, call {@link #setDisplayOrientation(int)}.
61 *
62 * <li><b>Important</b>: Pass a fully initialized {@link SurfaceHolder} to
63 * {@link #setPreviewDisplay(SurfaceHolder)}. Without a surface, the camera
64 * will be unable to start the preview.
65 *
66 * <li><b>Important</b>: Call {@link #startPreview()} to start updating the
67 * preview surface. Preview must be started before you can take a picture.
68 *
69 * <li>When you want, call {@link #takePicture(Camera.ShutterCallback,
70 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)} to
71 * capture a photo. Wait for the callbacks to provide the actual image data.
72 *
73 * <li>After taking a picture, preview display will have stopped. To take more
74 * photos, call {@link #startPreview()} again first.
75 *
76 * <li>Call {@link #stopPreview()} to stop updating the preview surface.
77 *
78 * <li><b>Important:</b> Call {@link #release()} to release the camera for
79 * use by other applications. Applications should release the camera
80 * immediately in {@link android.app.Activity#onPause()} (and re-{@link #open()}
81 * it in {@link android.app.Activity#onResume()}).
82 * </ol>
83 *
84 * <p>To quickly switch to video recording mode, use these steps:</p>
85 *
86 * <ol>
87 * <li>Obtain and initialize a Camera and start preview as described above.
88 *
89 * <li>Call {@link #unlock()} to allow the media process to access the camera.
90 *
91 * <li>Pass the camera to {@link android.media.MediaRecorder#setCamera(Camera)}.
92 * See {@link android.media.MediaRecorder} information about video recording.
93 *
94 * <li>When finished recording, call {@link #reconnect()} to re-acquire
95 * and re-lock the camera.
96 *
97 * <li>If desired, restart preview and take more photos or videos.
98 *
99 * <li>Call {@link #stopPreview()} and {@link #release()} as described above.
100 * </ol>
101 *
102 * <p>This class is not thread-safe, and is meant for use from one event thread.
103 * Most long-running operations (preview, focus, photo capture, etc) happen
104 * asynchronously and invoke callbacks as necessary. Callbacks will be invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700105 * on the event thread {@link #open(int)} was called from. This class's methods
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700106 * must never be called from multiple threads at once.</p>
107 *
Scott Maindf4578e2009-09-10 12:22:07 -0700108 * <p class="caution"><strong>Caution:</strong> Different Android-powered devices
109 * may have different hardware specifications, such as megapixel ratings and
110 * auto-focus capabilities. In order for your application to be compatible with
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800111 * more devices, you should not make assumptions about the device camera
Scott Maindf4578e2009-09-10 12:22:07 -0700112 * specifications.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
114public class Camera {
115 private static final String TAG = "Camera";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800116
Wu-cheng Lid2c29292010-05-28 17:32:41 +0800117 // These match the enums in frameworks/base/include/camera/Camera.h
Benny Wongda83f462009-08-12 12:01:27 -0500118 private static final int CAMERA_MSG_ERROR = 0x001;
119 private static final int CAMERA_MSG_SHUTTER = 0x002;
120 private static final int CAMERA_MSG_FOCUS = 0x004;
121 private static final int CAMERA_MSG_ZOOM = 0x008;
122 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010;
123 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020;
124 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040;
125 private static final int CAMERA_MSG_RAW_IMAGE = 0x080;
126 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100;
127 private static final int CAMERA_MSG_ALL_MSGS = 0x1FF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
129 private int mNativeContext; // accessed by native methods
130 private EventHandler mEventHandler;
131 private ShutterCallback mShutterCallback;
132 private PictureCallback mRawImageCallback;
133 private PictureCallback mJpegCallback;
134 private PreviewCallback mPreviewCallback;
Dave Sparkse8b26e12009-07-14 10:35:40 -0700135 private PictureCallback mPostviewCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 private AutoFocusCallback mAutoFocusCallback;
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800137 private OnZoomChangeListener mZoomListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 private ErrorCallback mErrorCallback;
139 private boolean mOneShot;
Andrew Harp94927df2009-10-20 01:47:05 -0400140 private boolean mWithBuffer;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700143 * Returns the number of physical cameras available on this device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 */
Chih-Chung Change25cc652010-05-06 16:36:58 +0800145 public native static int getNumberOfCameras();
146
147 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700148 * Returns the information about a particular camera.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800149 * If {@link #getNumberOfCameras()} returns N, the valid id is 0 to N-1.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800150 */
151 public native static void getCameraInfo(int cameraId, CameraInfo cameraInfo);
152
153 /**
154 * Information about a camera
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800155 */
156 public static class CameraInfo {
Wu-cheng Li78366602010-09-15 14:08:15 -0700157 /**
158 * The facing of the camera is opposite to that of the screen.
159 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800160 public static final int CAMERA_FACING_BACK = 0;
Wu-cheng Li78366602010-09-15 14:08:15 -0700161
162 /**
163 * The facing of the camera is the same as that of the screen.
164 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800165 public static final int CAMERA_FACING_FRONT = 1;
166
167 /**
168 * The direction that the camera faces to. It should be
169 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
170 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700171 public int facing;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800172
173 /**
174 * The orientation of the camera image. The value is the angle that the
175 * camera image needs to be rotated clockwise so it shows correctly on
176 * the display in its natural orientation. It should be 0, 90, 180, or 270.
177 *
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +0800178 * For example, suppose a device has a naturally tall screen. The
179 * back-facing camera sensor is mounted in landscape. You are looking at
180 * the screen. If the top side of the camera sensor is aligned with the
181 * right edge of the screen in natural orientation, the value should be
182 * 90. If the top side of a front-facing camera sensor is aligned with
183 * the right of the screen, the value should be 270.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800184 *
185 * @see #setDisplayOrientation(int)
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800186 * @see Parameters#setRotation(int)
187 * @see Parameters#setPreviewSize(int, int)
188 * @see Parameters#setPictureSize(int, int)
189 * @see Parameters#setJpegThumbnailSize(int, int)
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800190 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700191 public int orientation;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800192 };
193
194 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700195 * Creates a new Camera object to access a particular hardware camera.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700196 *
197 * <p>You must call {@link #release()} when you are done using the camera,
198 * otherwise it will remain locked and be unavailable to other applications.
199 *
Dan Egnor341ff132010-07-20 11:30:17 -0700200 * <p>Your application should only have one Camera object active at a time
201 * for a particular hardware camera.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700202 *
203 * <p>Callbacks from other methods are delivered to the event loop of the
204 * thread which called open(). If this thread has no event loop, then
205 * callbacks are delivered to the main application event loop. If there
206 * is no main application event loop, callbacks are not delivered.
207 *
208 * <p class="caution"><b>Caution:</b> On some devices, this method may
209 * take a long time to complete. It is best to call this method from a
210 * worker thread (possibly using {@link android.os.AsyncTask}) to avoid
211 * blocking the main application UI thread.
212 *
Dan Egnor341ff132010-07-20 11:30:17 -0700213 * @param cameraId the hardware camera to access, between 0 and
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800214 * {@link #getNumberOfCameras()}-1.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700215 * @return a new Camera object, connected, locked and ready for use.
216 * @throws RuntimeException if connection to the camera service fails (for
217 * example, if the camera is in use by another process).
Chih-Chung Change25cc652010-05-06 16:36:58 +0800218 */
219 public static Camera open(int cameraId) {
220 return new Camera(cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
222
Chih-Chung Change25cc652010-05-06 16:36:58 +0800223 /**
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800224 * Creates a new Camera object to access the first back-facing camera on the
225 * device. If the device does not have a back-facing camera, this returns
226 * null.
Wu-cheng Li78366602010-09-15 14:08:15 -0700227 * @see #open(int)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800228 */
229 public static Camera open() {
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800230 int numberOfCameras = getNumberOfCameras();
231 CameraInfo cameraInfo = new CameraInfo();
232 for (int i = 0; i < numberOfCameras; i++) {
233 getCameraInfo(i, cameraInfo);
234 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
235 return new Camera(i);
236 }
237 }
238 return null;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800239 }
240
241 Camera(int cameraId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 mShutterCallback = null;
243 mRawImageCallback = null;
244 mJpegCallback = null;
245 mPreviewCallback = null;
Dave Sparkse8b26e12009-07-14 10:35:40 -0700246 mPostviewCallback = null;
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800247 mZoomListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
249 Looper looper;
250 if ((looper = Looper.myLooper()) != null) {
251 mEventHandler = new EventHandler(this, looper);
252 } else if ((looper = Looper.getMainLooper()) != null) {
253 mEventHandler = new EventHandler(this, looper);
254 } else {
255 mEventHandler = null;
256 }
257
Chih-Chung Change25cc652010-05-06 16:36:58 +0800258 native_setup(new WeakReference<Camera>(this), cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800260
261 protected void finalize() {
262 native_release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800264
Chih-Chung Change25cc652010-05-06 16:36:58 +0800265 private native final void native_setup(Object camera_this, int cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 private native final void native_release();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268
269 /**
270 * Disconnects and releases the Camera object resources.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700271 *
272 * <p>You must call this as soon as you're done with the Camera object.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800274 public final void release() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 native_release();
276 }
277
278 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700279 * Unlocks the camera to allow another process to access it.
280 * Normally, the camera is locked to the process with an active Camera
281 * object until {@link #release()} is called. To allow rapid handoff
282 * between processes, you can call this method to release the camera
283 * temporarily for another process to use; once the other process is done
284 * you can call {@link #reconnect()} to reclaim the camera.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700286 * <p>This must be done before calling
287 * {@link android.media.MediaRecorder#setCamera(Camera)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700289 * <p>If you are not recording video, you probably do not need this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700291 * @throws RuntimeException if the camera cannot be unlocked.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 */
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800293 public native final void unlock();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700296 * Re-locks the camera to prevent other processes from accessing it.
297 * Camera objects are locked by default unless {@link #unlock()} is
298 * called. Normally {@link #reconnect()} is used instead.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800299 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700300 * <p>If you are not recording video, you probably do not need this method.
301 *
302 * @throws RuntimeException if the camera cannot be re-locked (for
303 * example, if the camera is still in use by another process).
304 */
305 public native final void lock();
306
307 /**
308 * Reconnects to the camera service after another process used it.
309 * After {@link #unlock()} is called, another process may use the
310 * camera; when the process is done, you must reconnect to the camera,
311 * which will re-acquire the lock and allow you to continue using the
312 * camera.
313 *
314 * <p>This must be done after {@link android.media.MediaRecorder} is
315 * done recording if {@link android.media.MediaRecorder#setCamera(Camera)}
316 * was used.
317 *
318 * <p>If you are not recording video, you probably do not need this method.
319 *
320 * @throws IOException if a connection cannot be re-established (for
321 * example, if the camera is still in use by another process).
322 */
323 public native final void reconnect() throws IOException;
324
325 /**
326 * Sets the {@link Surface} to be used for live preview.
327 * A surface is necessary for preview, and preview is necessary to take
328 * pictures. The same surface can be re-set without harm.
329 *
330 * <p>The {@link SurfaceHolder} must already contain a surface when this
331 * method is called. If you are using {@link android.view.SurfaceView},
332 * you will need to register a {@link SurfaceHolder.Callback} with
333 * {@link SurfaceHolder#addCallback(SurfaceHolder.Callback)} and wait for
334 * {@link SurfaceHolder.Callback#surfaceCreated(SurfaceHolder)} before
335 * calling setPreviewDisplay() or starting preview.
336 *
337 * <p>This method must be called before {@link #startPreview()}. The
338 * one exception is that if the preview surface is not set (or set to null)
339 * before startPreview() is called, then this method may be called once
340 * with a non-null parameter to set the preview surface. (This allows
341 * camera setup and surface creation to happen in parallel, saving time.)
342 * The preview surface may not otherwise change while preview is running.
343 *
344 * @param holder containing the Surface on which to place the preview,
345 * or null to remove the preview surface
346 * @throws IOException if the method fails (for example, if the surface
347 * is unavailable or unsuitable).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 */
349 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException {
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800350 if (holder != null) {
351 setPreviewDisplay(holder.getSurface());
352 } else {
353 setPreviewDisplay((Surface)null);
354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
356
357 private native final void setPreviewDisplay(Surface surface);
358
359 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700360 * Callback interface used to deliver copies of preview frames as
361 * they are displayed.
362 *
363 * @see #setPreviewCallback(Camera.PreviewCallback)
364 * @see #setOneShotPreviewCallback(Camera.PreviewCallback)
365 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
366 * @see #startPreview()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 */
368 public interface PreviewCallback
369 {
370 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700371 * Called as preview frames are displayed. This callback is invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700372 * on the event thread {@link #open(int)} was called from.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700374 * @param data the contents of the preview frame in the format defined
Mathias Agopiana696f5d2010-02-17 17:53:09 -0800375 * by {@link android.graphics.ImageFormat}, which can be queried
Scott Maindf4578e2009-09-10 12:22:07 -0700376 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
Scott Mainda0a56d2009-09-10 18:08:37 -0700377 * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800378 * is never called, the default will be the YCbCr_420_SP
379 * (NV21) format.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700380 * @param camera the Camera service object.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 */
382 void onPreviewFrame(byte[] data, Camera camera);
383 };
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700386 * Starts capturing and drawing preview frames to the screen.
387 * Preview will not actually start until a surface is supplied with
388 * {@link #setPreviewDisplay(SurfaceHolder)}.
389 *
390 * <p>If {@link #setPreviewCallback(Camera.PreviewCallback)},
391 * {@link #setOneShotPreviewCallback(Camera.PreviewCallback)}, or
392 * {@link #setPreviewCallbackWithBuffer(Camera.PreviewCallback)} were
393 * called, {@link Camera.PreviewCallback#onPreviewFrame(byte[], Camera)}
394 * will be called when preview data becomes available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 */
396 public native final void startPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700399 * Stops capturing and drawing preview frames to the surface, and
400 * resets the camera for a future call to {@link #startPreview()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 */
402 public native final void stopPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 /**
405 * Return current preview state.
406 *
407 * FIXME: Unhide before release
408 * @hide
409 */
410 public native final boolean previewEnabled();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700413 * Installs a callback to be invoked for every preview frame in addition
414 * to displaying them on the screen. The callback will be repeatedly called
415 * for as long as preview is active. This method can be called at any time,
416 * even while preview is live. Any other preview callbacks are overridden.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700418 * @param cb a callback object that receives a copy of each preview frame,
419 * or null to stop receiving callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 */
421 public final void setPreviewCallback(PreviewCallback cb) {
422 mPreviewCallback = cb;
423 mOneShot = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400424 mWithBuffer = false;
Dave Sparksa6118c62009-10-13 02:28:54 -0700425 // Always use one-shot mode. We fake camera preview mode by
426 // doing one-shot preview continuously.
Andrew Harp94927df2009-10-20 01:47:05 -0400427 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429
430 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700431 * Installs a callback to be invoked for the next preview frame in addition
432 * to displaying it on the screen. After one invocation, the callback is
433 * cleared. This method can be called any time, even when preview is live.
434 * Any other preview callbacks are overridden.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700436 * @param cb a callback object that receives a copy of the next preview frame,
437 * or null to stop receiving callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 */
439 public final void setOneShotPreviewCallback(PreviewCallback cb) {
Andrew Harp94927df2009-10-20 01:47:05 -0400440 mPreviewCallback = cb;
441 mOneShot = true;
442 mWithBuffer = false;
443 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445
Andrew Harp94927df2009-10-20 01:47:05 -0400446 private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer);
447
448 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700449 * Installs a callback to be invoked for every preview frame, using buffers
450 * supplied with {@link #addCallbackBuffer(byte[])}, in addition to
451 * displaying them on the screen. The callback will be repeatedly called
452 * for as long as preview is active and buffers are available.
453 * Any other preview callbacks are overridden.
Andrew Harp94927df2009-10-20 01:47:05 -0400454 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700455 * <p>The purpose of this method is to improve preview efficiency and frame
456 * rate by allowing preview frame memory reuse. You must call
457 * {@link #addCallbackBuffer(byte[])} at some point -- before or after
458 * calling this method -- or no callbacks will received.
Andrew Harp94927df2009-10-20 01:47:05 -0400459 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700460 * The buffer queue will be cleared if this method is called with a null
461 * callback, {@link #setPreviewCallback(Camera.PreviewCallback)} is called,
462 * or {@link #setOneShotPreviewCallback(Camera.PreviewCallback)} is called.
Andrew Harp94927df2009-10-20 01:47:05 -0400463 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700464 * @param cb a callback object that receives a copy of the preview frame,
465 * or null to stop receiving callbacks and clear the buffer queue.
466 * @see #addCallbackBuffer(byte[])
Andrew Harp94927df2009-10-20 01:47:05 -0400467 */
468 public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
469 mPreviewCallback = cb;
470 mOneShot = false;
471 mWithBuffer = true;
472 setHasPreviewCallback(cb != null, true);
473 }
474
475 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800476 * Adds a pre-allocated buffer to the preview callback buffer queue.
477 * Applications can add one or more buffers to the queue. When a preview
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700478 * frame arrives and there is still at least one available buffer, the
479 * buffer will be used and removed from the queue. Then preview callback is
480 * invoked with the buffer. If a frame arrives and there is no buffer left,
481 * the frame is discarded. Applications should add buffers back when they
482 * finish processing the data in them.
Wu-cheng Lic10275a2010-03-09 13:49:21 -0800483 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700484 * <p>The size of the buffer is determined by multiplying the preview
485 * image width, height, and bytes per pixel. The width and height can be
486 * read from {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel
487 * can be computed from
488 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
489 * using the image format from {@link Camera.Parameters#getPreviewFormat()}.
Andrew Harp94927df2009-10-20 01:47:05 -0400490 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700491 * <p>This method is only necessary when
492 * {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When
493 * {@link #setPreviewCallback(PreviewCallback)} or
494 * {@link #setOneShotPreviewCallback(PreviewCallback)} are used, buffers
495 * are automatically allocated.
Andrew Harp94927df2009-10-20 01:47:05 -0400496 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700497 * @param callbackBuffer the buffer to add to the queue.
498 * The size should be width * height * bits_per_pixel / 8.
Wu-cheng Li5b9bcda2010-03-07 14:59:28 -0800499 * @see #setPreviewCallbackWithBuffer(PreviewCallback)
Andrew Harp94927df2009-10-20 01:47:05 -0400500 */
501 public native final void addCallbackBuffer(byte[] callbackBuffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502
503 private class EventHandler extends Handler
504 {
505 private Camera mCamera;
506
507 public EventHandler(Camera c, Looper looper) {
508 super(looper);
509 mCamera = c;
510 }
511
512 @Override
513 public void handleMessage(Message msg) {
514 switch(msg.what) {
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700515 case CAMERA_MSG_SHUTTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 if (mShutterCallback != null) {
517 mShutterCallback.onShutter();
518 }
519 return;
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700520
521 case CAMERA_MSG_RAW_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700522 if (mRawImageCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 return;
526
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700527 case CAMERA_MSG_COMPRESSED_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700528 if (mJpegCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800532
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700533 case CAMERA_MSG_PREVIEW_FRAME:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 if (mPreviewCallback != null) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700535 PreviewCallback cb = mPreviewCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 if (mOneShot) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700537 // Clear the callback variable before the callback
538 // in case the app calls setPreviewCallback from
539 // the callback function
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 mPreviewCallback = null;
Andrew Harp94927df2009-10-20 01:47:05 -0400541 } else if (!mWithBuffer) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700542 // We're faking the camera preview mode to prevent
543 // the app from being flooded with preview frames.
544 // Set to oneshot mode again.
Andrew Harp94927df2009-10-20 01:47:05 -0400545 setHasPreviewCallback(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 }
Dave Sparksa6118c62009-10-13 02:28:54 -0700547 cb.onPreviewFrame((byte[])msg.obj, mCamera);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549 return;
550
Dave Sparkse8b26e12009-07-14 10:35:40 -0700551 case CAMERA_MSG_POSTVIEW_FRAME:
552 if (mPostviewCallback != null) {
553 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera);
554 }
555 return;
556
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700557 case CAMERA_MSG_FOCUS:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700558 if (mAutoFocusCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700560 }
561 return;
562
563 case CAMERA_MSG_ZOOM:
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800564 if (mZoomListener != null) {
565 mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 return;
568
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700569 case CAMERA_MSG_ERROR :
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 Log.e(TAG, "Error " + msg.arg1);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700571 if (mErrorCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 mErrorCallback.onError(msg.arg1, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 return;
575
576 default:
577 Log.e(TAG, "Unknown message type " + msg.what);
578 return;
579 }
580 }
581 }
582
583 private static void postEventFromNative(Object camera_ref,
584 int what, int arg1, int arg2, Object obj)
585 {
586 Camera c = (Camera)((WeakReference)camera_ref).get();
587 if (c == null)
588 return;
589
590 if (c.mEventHandler != null) {
591 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj);
592 c.mEventHandler.sendMessage(m);
593 }
594 }
595
596 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700597 * Callback interface used to notify on completion of camera auto focus.
598 *
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800599 * <p>Devices that do not support auto-focus will receive a "fake"
600 * callback to this interface. If your application needs auto-focus and
Scott Maindf4578e2009-09-10 12:22:07 -0700601 * should not be installed on devices <em>without</em> auto-focus, you must
602 * declare that your app uses the
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800603 * {@code android.hardware.camera.autofocus} feature, in the
Scott Maindf4578e2009-09-10 12:22:07 -0700604 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
605 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700606 *
607 * @see #autoFocus(AutoFocusCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 */
609 public interface AutoFocusCallback
610 {
611 /**
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800612 * Called when the camera auto focus completes. If the camera
613 * does not support auto-focus and autoFocus is called,
614 * onAutoFocus will be called immediately with a fake value of
615 * <code>success</code> set to <code>true</code>.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800616 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * @param success true if focus was successful, false if otherwise
618 * @param camera the Camera service object
619 */
620 void onAutoFocus(boolean success, Camera camera);
621 };
622
623 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700624 * Starts camera auto-focus and registers a callback function to run when
625 * the camera is focused. This method is only valid when preview is active
626 * (between {@link #startPreview()} and before {@link #stopPreview()}).
627 *
628 * <p>Callers should check
629 * {@link android.hardware.Camera.Parameters#getFocusMode()} to determine if
630 * this method should be called. If the camera does not support auto-focus,
631 * it is a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)}
Wu-cheng Li36322db2009-09-18 18:59:21 +0800632 * callback will be called immediately.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700633 *
Scott Mainda0a56d2009-09-10 18:08:37 -0700634 * <p>If your application should not be installed
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800635 * on devices without auto-focus, you must declare that your application
636 * uses auto-focus with the
Scott Maindf4578e2009-09-10 12:22:07 -0700637 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
638 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700639 *
Wu-cheng Li068ef422009-09-27 13:19:36 -0700640 * <p>If the current flash mode is not
641 * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700642 * fired during auto-focus, depending on the driver and camera hardware.<p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800643 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 * @param cb the callback to run
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700645 * @see #cancelAutoFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 */
647 public final void autoFocus(AutoFocusCallback cb)
648 {
649 mAutoFocusCallback = cb;
650 native_autoFocus();
651 }
652 private native final void native_autoFocus();
653
654 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700655 * Cancels any auto-focus function in progress.
656 * Whether or not auto-focus is currently in progress,
657 * this function will return the focus position to the default.
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800658 * If the camera does not support auto-focus, this is a no-op.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700659 *
660 * @see #autoFocus(Camera.AutoFocusCallback)
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800661 */
662 public final void cancelAutoFocus()
663 {
664 mAutoFocusCallback = null;
665 native_cancelAutoFocus();
666 }
667 private native final void native_cancelAutoFocus();
668
669 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700670 * Callback interface used to signal the moment of actual image capture.
671 *
672 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 */
674 public interface ShutterCallback
675 {
676 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700677 * Called as near as possible to the moment when a photo is captured
678 * from the sensor. This is a good opportunity to play a shutter sound
679 * or give other feedback of camera operation. This may be some time
680 * after the photo was triggered, but some time before the actual data
681 * is available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 */
683 void onShutter();
684 }
685
686 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700687 * Callback interface used to supply image data from a photo capture.
688 *
689 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 */
691 public interface PictureCallback {
692 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700693 * Called when image data is available after a picture is taken.
694 * The format of the data depends on the context of the callback
695 * and {@link Camera.Parameters} settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800696 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 * @param data a byte array of the picture data
698 * @param camera the Camera service object
699 */
700 void onPictureTaken(byte[] data, Camera camera);
701 };
702
703 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700704 * Equivalent to takePicture(shutter, raw, null, jpeg).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800705 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700706 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 */
708 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
709 PictureCallback jpeg) {
Dave Sparkse8b26e12009-07-14 10:35:40 -0700710 takePicture(shutter, raw, null, jpeg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712 private native final void native_takePicture();
713
Dave Sparkse8b26e12009-07-14 10:35:40 -0700714 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800715 * Triggers an asynchronous image capture. The camera service will initiate
716 * a series of callbacks to the application as the image capture progresses.
717 * The shutter callback occurs after the image is captured. This can be used
718 * to trigger a sound to let the user know that image has been captured. The
719 * raw callback occurs when the raw image data is available (NOTE: the data
720 * may be null if the hardware does not have enough memory to make a copy).
721 * The postview callback occurs when a scaled, fully processed postview
722 * image is available (NOTE: not all hardware supports this). The jpeg
723 * callback occurs when the compressed image is available. If the
724 * application does not need a particular callback, a null can be passed
725 * instead of a callback method.
Dave Sparkse8b26e12009-07-14 10:35:40 -0700726 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700727 * <p>This method is only valid when preview is active (after
728 * {@link #startPreview()}). Preview will be stopped after the image is
729 * taken; callers must call {@link #startPreview()} again if they want to
730 * re-start preview or take more pictures.
Wu-cheng Li40057ce2009-12-02 18:57:29 +0800731 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700732 * <p>After calling this method, you must not call {@link #startPreview()}
733 * or take another picture until the JPEG callback has returned.
734 *
735 * @param shutter the callback for image capture moment, or null
736 * @param raw the callback for raw (uncompressed) image data, or null
Dave Sparkse8b26e12009-07-14 10:35:40 -0700737 * @param postview callback with postview image data, may be null
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700738 * @param jpeg the callback for JPEG image data, or null
Dave Sparkse8b26e12009-07-14 10:35:40 -0700739 */
740 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
741 PictureCallback postview, PictureCallback jpeg) {
742 mShutterCallback = shutter;
743 mRawImageCallback = raw;
744 mPostviewCallback = postview;
745 mJpegCallback = jpeg;
746 native_takePicture();
747 }
748
749 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700750 * Zooms to the requested value smoothly. The driver will notify {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800751 * OnZoomChangeListener} of the zoom value and whether zoom is stopped at
752 * the time. For example, suppose the current zoom is 0 and startSmoothZoom
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700753 * is called with value 3. The
754 * {@link Camera.OnZoomChangeListener#onZoomChange(int, boolean, Camera)}
755 * method will be called three times with zoom values 1, 2, and 3.
756 * Applications can call {@link #stopSmoothZoom} to stop the zoom earlier.
757 * Applications should not call startSmoothZoom again or change the zoom
758 * value before zoom stops. If the supplied zoom value equals to the current
759 * zoom value, no zoom callback will be generated. This method is supported
760 * if {@link android.hardware.Camera.Parameters#isSmoothZoomSupported}
761 * returns true.
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700762 *
763 * @param value zoom value. The valid range is 0 to {@link
764 * android.hardware.Camera.Parameters#getMaxZoom}.
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800765 * @throws IllegalArgumentException if the zoom value is invalid.
766 * @throws RuntimeException if the method fails.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700767 * @see #setZoomChangeListener(OnZoomChangeListener)
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700768 */
769 public native final void startSmoothZoom(int value);
770
771 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700772 * Stops the smooth zoom. Applications should wait for the {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800773 * OnZoomChangeListener} to know when the zoom is actually stopped. This
774 * method is supported if {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -0800775 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true.
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800776 *
777 * @throws RuntimeException if the method fails.
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700778 */
779 public native final void stopSmoothZoom();
780
781 /**
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +0800782 * Set the clockwise rotation of preview display in degrees. This affects
783 * the preview frames and the picture displayed after snapshot. This method
784 * is useful for portrait mode applications. Note that preview display of
Wu-cheng Lib982fb42010-10-19 17:19:09 +0800785 * front-facing cameras is flipped horizontally before the rotation, that
786 * is, the image is reflected along the central vertical axis of the camera
787 * sensor. So the users can see themselves as looking into a mirror.
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800788 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800789 * <p>This does not affect the order of byte array passed in {@link
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +0800790 * PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This
791 * method is not allowed to be called during preview.
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800792 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800793 * <p>If you want to make the camera image show in the same orientation as
794 * the display, you can use the following code.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800795 * <pre>
Chih-Chung Chang724c5222010-06-14 18:57:15 +0800796 * public static void setCameraDisplayOrientation(Activity activity,
797 * int cameraId, android.hardware.Camera camera) {
798 * android.hardware.Camera.CameraInfo info =
799 * new android.hardware.Camera.CameraInfo();
800 * android.hardware.Camera.getCameraInfo(cameraId, info);
801 * int rotation = activity.getWindowManager().getDefaultDisplay()
802 * .getRotation();
803 * int degrees = 0;
804 * switch (rotation) {
805 * case Surface.ROTATION_0: degrees = 0; break;
806 * case Surface.ROTATION_90: degrees = 90; break;
807 * case Surface.ROTATION_180: degrees = 180; break;
808 * case Surface.ROTATION_270: degrees = 270; break;
809 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800810 *
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +0800811 * int result;
812 * if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
813 * result = (info.orientation + degrees) % 360;
814 * result = (360 - result) % 360; // compensate the mirror
815 * } else { // back-facing
816 * result = (info.orientation - degrees + 360) % 360;
817 * }
Chih-Chung Chang724c5222010-06-14 18:57:15 +0800818 * camera.setDisplayOrientation(result);
819 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800820 * </pre>
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800821 * @param degrees the angle that the picture will be rotated clockwise.
Chih-Chung Change7bd22a2010-01-27 10:24:42 -0800822 * Valid values are 0, 90, 180, and 270. The starting
823 * position is 0 (landscape).
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +0800824 * @see #setPreviewDisplay(SurfaceHolder)
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800825 */
826 public native final void setDisplayOrientation(int degrees);
827
828 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700829 * Callback interface for zoom changes during a smooth zoom operation.
830 *
831 * @see #setZoomChangeListener(OnZoomChangeListener)
832 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -0700833 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800834 public interface OnZoomChangeListener
Dave Sparkse8b26e12009-07-14 10:35:40 -0700835 {
836 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700837 * Called when the zoom value has changed during a smooth zoom.
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700838 *
839 * @param zoomValue the current zoom value. In smooth zoom mode, camera
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800840 * calls this for every new zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700841 * @param stopped whether smooth zoom is stopped. If the value is true,
842 * this is the last zoom update for the application.
Dave Sparkse8b26e12009-07-14 10:35:40 -0700843 * @param camera the Camera service object
844 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800845 void onZoomChange(int zoomValue, boolean stopped, Camera camera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700846 };
847
848 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800849 * Registers a listener to be notified when the zoom value is updated by the
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700850 * camera driver during smooth zoom.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -0800851 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800852 * @param listener the listener to notify
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -0800853 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -0700854 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800855 public final void setZoomChangeListener(OnZoomChangeListener listener)
Dave Sparkse8b26e12009-07-14 10:35:40 -0700856 {
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800857 mZoomListener = listener;
Dave Sparkse8b26e12009-07-14 10:35:40 -0700858 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800859
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700860 // Error codes match the enum in include/ui/Camera.h
861
862 /**
863 * Unspecified camera error.
864 * @see Camera.ErrorCallback
865 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 public static final int CAMERA_ERROR_UNKNOWN = 1;
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700867
868 /**
869 * Media server died. In this case, the application must release the
870 * Camera object and instantiate a new one.
871 * @see Camera.ErrorCallback
872 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 public static final int CAMERA_ERROR_SERVER_DIED = 100;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700876 * Callback interface for camera error notification.
877 *
878 * @see #setErrorCallback(ErrorCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 */
880 public interface ErrorCallback
881 {
882 /**
883 * Callback for camera errors.
884 * @param error error code:
885 * <ul>
886 * <li>{@link #CAMERA_ERROR_UNKNOWN}
887 * <li>{@link #CAMERA_ERROR_SERVER_DIED}
888 * </ul>
889 * @param camera the Camera service object
890 */
891 void onError(int error, Camera camera);
892 };
893
894 /**
895 * Registers a callback to be invoked when an error occurs.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700896 * @param cb The callback to run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 */
898 public final void setErrorCallback(ErrorCallback cb)
899 {
900 mErrorCallback = cb;
901 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 private native final void native_setParameters(String params);
904 private native final String native_getParameters();
905
906 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700907 * Changes the settings for this Camera service.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800908 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 * @param params the Parameters to use for this Camera service
Wu-cheng Li99a3f3e2010-11-19 15:56:16 +0800910 * @throws RuntimeException if any parameter is invalid or not supported.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700911 * @see #getParameters()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 */
913 public void setParameters(Parameters params) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 native_setParameters(params.flatten());
915 }
916
917 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700918 * Returns the current settings for this Camera service.
919 * If modifications are made to the returned Parameters, they must be passed
920 * to {@link #setParameters(Camera.Parameters)} to take effect.
921 *
922 * @see #setParameters(Camera.Parameters)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 */
924 public Parameters getParameters() {
925 Parameters p = new Parameters();
926 String s = native_getParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 p.unflatten(s);
928 return p;
929 }
930
931 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700932 * Image size (width and height dimensions).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 */
934 public class Size {
935 /**
936 * Sets the dimensions for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800937 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 * @param w the photo width (pixels)
939 * @param h the photo height (pixels)
940 */
941 public Size(int w, int h) {
942 width = w;
943 height = h;
944 }
Wu-cheng Li4c4300c2010-01-23 15:45:39 +0800945 /**
946 * Compares {@code obj} to this size.
947 *
948 * @param obj the object to compare this size with.
949 * @return {@code true} if the width and height of {@code obj} is the
950 * same as those of this size. {@code false} otherwise.
951 */
952 @Override
953 public boolean equals(Object obj) {
954 if (!(obj instanceof Size)) {
955 return false;
956 }
957 Size s = (Size) obj;
958 return width == s.width && height == s.height;
959 }
960 @Override
961 public int hashCode() {
962 return width * 32713 + height;
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 /** width of the picture */
965 public int width;
966 /** height of the picture */
967 public int height;
968 };
969
970 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700971 * Camera service settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800972 *
973 * <p>To make camera parameters take effect, applications have to call
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700974 * {@link Camera#setParameters(Camera.Parameters)}. For example, after
975 * {@link Camera.Parameters#setWhiteBalance} is called, white balance is not
976 * actually changed until {@link Camera#setParameters(Camera.Parameters)}
977 * is called with the changed parameters object.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800978 *
979 * <p>Different devices may have different camera capabilities, such as
980 * picture size or flash modes. The application should query the camera
981 * capabilities before setting parameters. For example, the application
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700982 * should call {@link Camera.Parameters#getSupportedColorEffects()} before
983 * calling {@link Camera.Parameters#setColorEffect(String)}. If the
984 * camera does not support color effects,
985 * {@link Camera.Parameters#getSupportedColorEffects()} will return null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 */
987 public class Parameters {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800988 // Parameter keys to communicate with the camera driver.
989 private static final String KEY_PREVIEW_SIZE = "preview-size";
990 private static final String KEY_PREVIEW_FORMAT = "preview-format";
991 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate";
Wu-cheng Li454630f2010-08-11 16:48:05 -0700992 private static final String KEY_PREVIEW_FPS_RANGE = "preview-fps-range";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800993 private static final String KEY_PICTURE_SIZE = "picture-size";
994 private static final String KEY_PICTURE_FORMAT = "picture-format";
Wu-cheng Li4c4300c2010-01-23 15:45:39 +0800995 private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800996 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width";
997 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height";
998 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality";
999 private static final String KEY_JPEG_QUALITY = "jpeg-quality";
1000 private static final String KEY_ROTATION = "rotation";
1001 private static final String KEY_GPS_LATITUDE = "gps-latitude";
1002 private static final String KEY_GPS_LONGITUDE = "gps-longitude";
1003 private static final String KEY_GPS_ALTITUDE = "gps-altitude";
1004 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp";
Ray Chen055c9862010-02-23 10:45:42 +08001005 private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001006 private static final String KEY_WHITE_BALANCE = "whitebalance";
1007 private static final String KEY_EFFECT = "effect";
1008 private static final String KEY_ANTIBANDING = "antibanding";
1009 private static final String KEY_SCENE_MODE = "scene-mode";
1010 private static final String KEY_FLASH_MODE = "flash-mode";
Wu-cheng Li36322db2009-09-18 18:59:21 +08001011 private static final String KEY_FOCUS_MODE = "focus-mode";
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08001012 private static final String KEY_FOCAL_LENGTH = "focal-length";
1013 private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle";
1014 private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle";
Wu-cheng Liff723b62010-02-09 13:38:19 +08001015 private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation";
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001016 private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation";
1017 private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation";
1018 private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001019 private static final String KEY_ZOOM = "zoom";
1020 private static final String KEY_MAX_ZOOM = "max-zoom";
1021 private static final String KEY_ZOOM_RATIOS = "zoom-ratios";
1022 private static final String KEY_ZOOM_SUPPORTED = "zoom-supported";
1023 private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08001024 private static final String KEY_FOCUS_DISTANCES = "focus-distances";
1025
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001026 // Parameter key suffix for supported values.
1027 private static final String SUPPORTED_VALUES_SUFFIX = "-values";
1028
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001029 private static final String TRUE = "true";
1030
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001031 // Values for white balance settings.
1032 public static final String WHITE_BALANCE_AUTO = "auto";
1033 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent";
1034 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent";
1035 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent";
1036 public static final String WHITE_BALANCE_DAYLIGHT = "daylight";
1037 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight";
1038 public static final String WHITE_BALANCE_TWILIGHT = "twilight";
1039 public static final String WHITE_BALANCE_SHADE = "shade";
1040
1041 // Values for color effect settings.
1042 public static final String EFFECT_NONE = "none";
1043 public static final String EFFECT_MONO = "mono";
1044 public static final String EFFECT_NEGATIVE = "negative";
1045 public static final String EFFECT_SOLARIZE = "solarize";
1046 public static final String EFFECT_SEPIA = "sepia";
1047 public static final String EFFECT_POSTERIZE = "posterize";
1048 public static final String EFFECT_WHITEBOARD = "whiteboard";
1049 public static final String EFFECT_BLACKBOARD = "blackboard";
1050 public static final String EFFECT_AQUA = "aqua";
1051
1052 // Values for antibanding settings.
1053 public static final String ANTIBANDING_AUTO = "auto";
1054 public static final String ANTIBANDING_50HZ = "50hz";
1055 public static final String ANTIBANDING_60HZ = "60hz";
1056 public static final String ANTIBANDING_OFF = "off";
1057
1058 // Values for flash mode settings.
1059 /**
1060 * Flash will not be fired.
1061 */
1062 public static final String FLASH_MODE_OFF = "off";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001063
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001064 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07001065 * Flash will be fired automatically when required. The flash may be fired
1066 * during preview, auto-focus, or snapshot depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001067 */
1068 public static final String FLASH_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001069
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001070 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07001071 * Flash will always be fired during snapshot. The flash may also be
1072 * fired during preview or auto-focus depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001073 */
1074 public static final String FLASH_MODE_ON = "on";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001075
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001076 /**
1077 * Flash will be fired in red-eye reduction mode.
1078 */
1079 public static final String FLASH_MODE_RED_EYE = "red-eye";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001080
Wu-cheng Li36322db2009-09-18 18:59:21 +08001081 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07001082 * Constant emission of light during preview, auto-focus and snapshot.
1083 * This can also be used for video recording.
Wu-cheng Li36322db2009-09-18 18:59:21 +08001084 */
Wu-cheng Li068ef422009-09-27 13:19:36 -07001085 public static final String FLASH_MODE_TORCH = "torch";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001086
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001087 /**
1088 * Scene mode is off.
1089 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001090 public static final String SCENE_MODE_AUTO = "auto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001091
1092 /**
1093 * Take photos of fast moving objects. Same as {@link
1094 * #SCENE_MODE_SPORTS}.
1095 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001096 public static final String SCENE_MODE_ACTION = "action";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001097
1098 /**
1099 * Take people pictures.
1100 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001101 public static final String SCENE_MODE_PORTRAIT = "portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001102
1103 /**
1104 * Take pictures on distant objects.
1105 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001106 public static final String SCENE_MODE_LANDSCAPE = "landscape";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001107
1108 /**
1109 * Take photos at night.
1110 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001111 public static final String SCENE_MODE_NIGHT = "night";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001112
1113 /**
1114 * Take people pictures at night.
1115 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001116 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001117
1118 /**
1119 * Take photos in a theater. Flash light is off.
1120 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001121 public static final String SCENE_MODE_THEATRE = "theatre";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001122
1123 /**
1124 * Take pictures on the beach.
1125 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001126 public static final String SCENE_MODE_BEACH = "beach";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001127
1128 /**
1129 * Take pictures on the snow.
1130 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001131 public static final String SCENE_MODE_SNOW = "snow";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001132
1133 /**
1134 * Take sunset photos.
1135 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001136 public static final String SCENE_MODE_SUNSET = "sunset";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001137
1138 /**
1139 * Avoid blurry pictures (for example, due to hand shake).
1140 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001141 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001142
1143 /**
1144 * For shooting firework displays.
1145 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001146 public static final String SCENE_MODE_FIREWORKS = "fireworks";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001147
1148 /**
1149 * Take photos of fast moving objects. Same as {@link
1150 * #SCENE_MODE_ACTION}.
1151 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001152 public static final String SCENE_MODE_SPORTS = "sports";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001153
1154 /**
1155 * Take indoor low-light shot.
1156 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001157 public static final String SCENE_MODE_PARTY = "party";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001158
1159 /**
1160 * Capture the naturally warm color of scenes lit by candles.
1161 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001162 public static final String SCENE_MODE_CANDLELIGHT = "candlelight";
1163
Wu-cheng Lic58b4232010-03-29 17:21:28 +08001164 /**
1165 * Applications are looking for a barcode. Camera driver will be
1166 * optimized for barcode reading.
1167 */
1168 public static final String SCENE_MODE_BARCODE = "barcode";
1169
Wu-cheng Li36322db2009-09-18 18:59:21 +08001170 /**
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07001171 * Auto-focus mode. Applications should call {@link
1172 * #autoFocus(AutoFocusCallback)} to start the focus in this mode.
Wu-cheng Li36322db2009-09-18 18:59:21 +08001173 */
1174 public static final String FOCUS_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001175
Wu-cheng Li36322db2009-09-18 18:59:21 +08001176 /**
1177 * Focus is set at infinity. Applications should not call
1178 * {@link #autoFocus(AutoFocusCallback)} in this mode.
1179 */
1180 public static final String FOCUS_MODE_INFINITY = "infinity";
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07001181
1182 /**
1183 * Macro (close-up) focus mode. Applications should call
1184 * {@link #autoFocus(AutoFocusCallback)} to start the focus in this
1185 * mode.
1186 */
Wu-cheng Li36322db2009-09-18 18:59:21 +08001187 public static final String FOCUS_MODE_MACRO = "macro";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001188
Wu-cheng Li36322db2009-09-18 18:59:21 +08001189 /**
1190 * Focus is fixed. The camera is always in this mode if the focus is not
1191 * adjustable. If the camera has auto-focus, this mode can fix the
1192 * focus, which is usually at hyperfocal distance. Applications should
1193 * not call {@link #autoFocus(AutoFocusCallback)} in this mode.
1194 */
1195 public static final String FOCUS_MODE_FIXED = "fixed";
1196
Wu-cheng Lic58b4232010-03-29 17:21:28 +08001197 /**
1198 * Extended depth of field (EDOF). Focusing is done digitally and
1199 * continuously. Applications should not call {@link
1200 * #autoFocus(AutoFocusCallback)} in this mode.
1201 */
1202 public static final String FOCUS_MODE_EDOF = "edof";
1203
Wu-cheng Li699fe932010-08-05 11:50:25 -07001204 /**
Wu-cheng Lid45cb722010-09-20 16:15:32 -07001205 * Continuous auto focus mode intended for video recording. The camera
1206 * continuously tries to focus. This is ideal for shooting video.
1207 * Applications still can call {@link
1208 * #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
1209 * Camera.PictureCallback)} in this mode but the subject may not be in
1210 * focus. Auto focus starts when the parameter is set. Applications
1211 * should not call {@link #autoFocus(AutoFocusCallback)} in this mode.
1212 * To stop continuous focus, applications should change the focus mode
1213 * to other modes.
Wu-cheng Li699fe932010-08-05 11:50:25 -07001214 */
Wu-cheng Lid45cb722010-09-20 16:15:32 -07001215 public static final String FOCUS_MODE_CONTINUOUS_VIDEO = "continuous-video";
Wu-cheng Li699fe932010-08-05 11:50:25 -07001216
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08001217 // Indices for focus distance array.
1218 /**
1219 * The array index of near focus distance for use with
1220 * {@link #getFocusDistances(float[])}.
1221 */
1222 public static final int FOCUS_DISTANCE_NEAR_INDEX = 0;
1223
1224 /**
1225 * The array index of optimal focus distance for use with
1226 * {@link #getFocusDistances(float[])}.
1227 */
1228 public static final int FOCUS_DISTANCE_OPTIMAL_INDEX = 1;
1229
1230 /**
1231 * The array index of far focus distance for use with
1232 * {@link #getFocusDistances(float[])}.
1233 */
1234 public static final int FOCUS_DISTANCE_FAR_INDEX = 2;
1235
Wu-cheng Lica099612010-05-06 16:47:30 +08001236 /**
Wu-cheng Li454630f2010-08-11 16:48:05 -07001237 * The array index of minimum preview fps for use with {@link
1238 * #getPreviewFpsRange(int[])} or {@link
1239 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07001240 */
1241 public static final int PREVIEW_FPS_MIN_INDEX = 0;
1242
1243 /**
1244 * The array index of maximum preview fps for use with {@link
1245 * #getPreviewFpsRange(int[])} or {@link
1246 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07001247 */
1248 public static final int PREVIEW_FPS_MAX_INDEX = 1;
1249
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001250 // Formats for setPreviewFormat and setPictureFormat.
1251 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp";
1252 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp";
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001253 private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001254 private static final String PIXEL_FORMAT_RGB565 = "rgb565";
1255 private static final String PIXEL_FORMAT_JPEG = "jpeg";
1256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 private HashMap<String, String> mMap;
1258
1259 private Parameters() {
1260 mMap = new HashMap<String, String>();
1261 }
1262
1263 /**
1264 * Writes the current Parameters to the log.
1265 * @hide
1266 * @deprecated
1267 */
1268 public void dump() {
1269 Log.e(TAG, "dump: size=" + mMap.size());
1270 for (String k : mMap.keySet()) {
1271 Log.e(TAG, "dump: " + k + "=" + mMap.get(k));
1272 }
1273 }
1274
1275 /**
1276 * Creates a single string with all the parameters set in
1277 * this Parameters object.
1278 * <p>The {@link #unflatten(String)} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001279 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 * @return a String with all values from this Parameters object, in
1281 * semi-colon delimited key-value pairs
1282 */
1283 public String flatten() {
1284 StringBuilder flattened = new StringBuilder();
1285 for (String k : mMap.keySet()) {
1286 flattened.append(k);
1287 flattened.append("=");
1288 flattened.append(mMap.get(k));
1289 flattened.append(";");
1290 }
1291 // chop off the extra semicolon at the end
1292 flattened.deleteCharAt(flattened.length()-1);
1293 return flattened.toString();
1294 }
1295
1296 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001297 * Takes a flattened string of parameters and adds each one to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 * this Parameters object.
1299 * <p>The {@link #flatten()} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001300 *
1301 * @param flattened a String of parameters (key-value paired) that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 * are semi-colon delimited
1303 */
1304 public void unflatten(String flattened) {
1305 mMap.clear();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 StringTokenizer tokenizer = new StringTokenizer(flattened, ";");
1308 while (tokenizer.hasMoreElements()) {
1309 String kv = tokenizer.nextToken();
1310 int pos = kv.indexOf('=');
1311 if (pos == -1) {
1312 continue;
1313 }
1314 String k = kv.substring(0, pos);
1315 String v = kv.substring(pos + 1);
1316 mMap.put(k, v);
1317 }
1318 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 public void remove(String key) {
1321 mMap.remove(key);
1322 }
1323
1324 /**
1325 * Sets a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 * @param key the key name for the parameter
1328 * @param value the String value of the parameter
1329 */
1330 public void set(String key, String value) {
1331 if (key.indexOf('=') != -1 || key.indexOf(';') != -1) {
1332 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ;)");
1333 return;
1334 }
1335 if (value.indexOf('=') != -1 || value.indexOf(';') != -1) {
1336 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ;)");
1337 return;
1338 }
1339
1340 mMap.put(key, value);
1341 }
1342
1343 /**
1344 * Sets an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001345 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 * @param key the key name for the parameter
1347 * @param value the int value of the parameter
1348 */
1349 public void set(String key, int value) {
1350 mMap.put(key, Integer.toString(value));
1351 }
1352
1353 /**
1354 * Returns the value of a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001355 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 * @param key the key name for the parameter
1357 * @return the String value of the parameter
1358 */
1359 public String get(String key) {
1360 return mMap.get(key);
1361 }
1362
1363 /**
1364 * Returns the value of an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 * @param key the key name for the parameter
1367 * @return the int value of the parameter
1368 */
1369 public int getInt(String key) {
1370 return Integer.parseInt(mMap.get(key));
1371 }
1372
1373 /**
1374 * Sets the dimensions for preview pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001375 *
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08001376 * The sides of width and height are based on camera orientation. That
1377 * is, the preview size is the size before it is rotated by display
1378 * orientation. So applications need to consider the display orientation
1379 * while setting preview size. For example, suppose the camera supports
1380 * both 480x320 and 320x480 preview sizes. The application wants a 3:2
1381 * preview ratio. If the display orientation is set to 0 or 180, preview
1382 * size should be set to 480x320. If the display orientation is set to
1383 * 90 or 270, preview size should be set to 320x480. The display
1384 * orientation should also be considered while setting picture size and
1385 * thumbnail size.
1386 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 * @param width the width of the pictures, in pixels
1388 * @param height the height of the pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08001389 * @see #setDisplayOrientation(int)
1390 * @see #getCameraInfo(int, CameraInfo)
1391 * @see #setPictureSize(int, int)
1392 * @see #setJpegThumbnailSize(int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 */
1394 public void setPreviewSize(int width, int height) {
1395 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001396 set(KEY_PREVIEW_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 }
1398
1399 /**
1400 * Returns the dimensions setting for preview pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001401 *
1402 * @return a Size object with the height and width setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 * for the preview picture
1404 */
1405 public Size getPreviewSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001406 String pair = get(KEY_PREVIEW_SIZE);
1407 return strToSize(pair);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409
1410 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001411 * Gets the supported preview sizes.
1412 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001413 * @return a list of Size object. This method will always return a list
Wu-cheng Li9c799382009-12-04 19:59:18 +08001414 * with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001415 */
1416 public List<Size> getSupportedPreviewSizes() {
1417 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX);
1418 return splitSize(str);
1419 }
1420
1421 /**
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08001422 * Sets the dimensions for EXIF thumbnail in Jpeg picture. If
1423 * applications set both width and height to 0, EXIF will not contain
1424 * thumbnail.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001425 *
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08001426 * Applications need to consider the display orientation. See {@link
1427 * #setPreviewSize(int,int)} for reference.
1428 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 * @param width the width of the thumbnail, in pixels
1430 * @param height the height of the thumbnail, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08001431 * @see #setPreviewSize(int,int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001433 public void setJpegThumbnailSize(int width, int height) {
1434 set(KEY_JPEG_THUMBNAIL_WIDTH, width);
1435 set(KEY_JPEG_THUMBNAIL_HEIGHT, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 }
1437
1438 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001439 * Returns the dimensions for EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001441 * @return a Size object with the height and width setting for the EXIF
1442 * thumbnails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001444 public Size getJpegThumbnailSize() {
1445 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH),
1446 getInt(KEY_JPEG_THUMBNAIL_HEIGHT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 }
1448
1449 /**
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08001450 * Gets the supported jpeg thumbnail sizes.
1451 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001452 * @return a list of Size object. This method will always return a list
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08001453 * with at least two elements. Size 0,0 (no thumbnail) is always
1454 * supported.
1455 */
1456 public List<Size> getSupportedJpegThumbnailSizes() {
1457 String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX);
1458 return splitSize(str);
1459 }
1460
1461 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001462 * Sets the quality of the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001464 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1
1465 * to 100, with 100 being the best.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001467 public void setJpegThumbnailQuality(int quality) {
1468 set(KEY_JPEG_THUMBNAIL_QUALITY, quality);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 }
1470
1471 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001472 * Returns the quality setting for the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001474 * @return the JPEG quality setting of the EXIF thumbnail.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001476 public int getJpegThumbnailQuality() {
1477 return getInt(KEY_JPEG_THUMBNAIL_QUALITY);
1478 }
1479
1480 /**
1481 * Sets Jpeg quality of captured picture.
1482 *
1483 * @param quality the JPEG quality of captured picture. The range is 1
1484 * to 100, with 100 being the best.
1485 */
1486 public void setJpegQuality(int quality) {
1487 set(KEY_JPEG_QUALITY, quality);
1488 }
1489
1490 /**
1491 * Returns the quality setting for the JPEG picture.
1492 *
1493 * @return the JPEG picture quality setting.
1494 */
1495 public int getJpegQuality() {
1496 return getInt(KEY_JPEG_QUALITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 }
1498
1499 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08001500 * Sets the rate at which preview frames are received. This is the
1501 * target frame rate. The actual frame rate depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001502 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 * @param fps the frame rate (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07001504 * @deprecated replaced by {@link #setPreviewFpsRange(int,int)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07001506 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 public void setPreviewFrameRate(int fps) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001508 set(KEY_PREVIEW_FRAME_RATE, fps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 }
1510
1511 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08001512 * Returns the setting for the rate at which preview frames are
1513 * received. This is the target frame rate. The actual frame rate
1514 * depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001515 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 * @return the frame rate setting (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07001517 * @deprecated replaced by {@link #getPreviewFpsRange(int[])}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07001519 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 public int getPreviewFrameRate() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001521 return getInt(KEY_PREVIEW_FRAME_RATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523
1524 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001525 * Gets the supported preview frame rates.
1526 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001527 * @return a list of supported preview frame rates. null if preview
1528 * frame rate setting is not supported.
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07001529 * @deprecated replaced by {@link #getSupportedPreviewFpsRange()}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001530 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07001531 @Deprecated
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001532 public List<Integer> getSupportedPreviewFrameRates() {
1533 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX);
1534 return splitInt(str);
1535 }
1536
1537 /**
Wu-cheng Li454630f2010-08-11 16:48:05 -07001538 * Sets the maximum and maximum preview fps. This controls the rate of
Wu-cheng Li1620d112010-08-27 15:09:20 -07001539 * preview frames received in {@link PreviewCallback}. The minimum and
Wu-cheng Li454630f2010-08-11 16:48:05 -07001540 * maximum preview fps must be one of the elements from {@link
1541 * #getSupportedPreviewFpsRange}.
1542 *
1543 * @param min the minimum preview fps (scaled by 1000).
1544 * @param max the maximum preview fps (scaled by 1000).
1545 * @throws RuntimeException if fps range is invalid.
1546 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
1547 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07001548 */
1549 public void setPreviewFpsRange(int min, int max) {
1550 set(KEY_PREVIEW_FPS_RANGE, "" + min + "," + max);
1551 }
1552
1553 /**
1554 * Returns the current minimum and maximum preview fps. The values are
1555 * one of the elements returned by {@link #getSupportedPreviewFpsRange}.
1556 *
1557 * @return range the minimum and maximum preview fps (scaled by 1000).
1558 * @see #PREVIEW_FPS_MIN_INDEX
1559 * @see #PREVIEW_FPS_MAX_INDEX
1560 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07001561 */
1562 public void getPreviewFpsRange(int[] range) {
1563 if (range == null || range.length != 2) {
1564 throw new IllegalArgumentException(
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07001565 "range must be an array with two elements.");
Wu-cheng Li454630f2010-08-11 16:48:05 -07001566 }
1567 splitInt(get(KEY_PREVIEW_FPS_RANGE), range);
1568 }
1569
1570 /**
1571 * Gets the supported preview fps (frame-per-second) ranges. Each range
1572 * contains a minimum fps and maximum fps. If minimum fps equals to
1573 * maximum fps, the camera outputs frames in fixed frame rate. If not,
1574 * the camera outputs frames in auto frame rate. The actual frame rate
1575 * fluctuates between the minimum and the maximum. The values are
1576 * multiplied by 1000 and represented in integers. For example, if frame
1577 * rate is 26.623 frames per second, the value is 26623.
1578 *
1579 * @return a list of supported preview fps ranges. This method returns a
1580 * list with at least one element. Every element is an int array
1581 * of two values - minimum fps and maximum fps. The list is
1582 * sorted from small to large (first by maximum fps and then
1583 * minimum fps).
1584 * @see #PREVIEW_FPS_MIN_INDEX
1585 * @see #PREVIEW_FPS_MAX_INDEX
Wu-cheng Li454630f2010-08-11 16:48:05 -07001586 */
1587 public List<int[]> getSupportedPreviewFpsRange() {
1588 String str = get(KEY_PREVIEW_FPS_RANGE + SUPPORTED_VALUES_SUFFIX);
1589 return splitRange(str);
1590 }
1591
1592 /**
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001593 * Sets the image format for preview pictures.
Scott Mainda0a56d2009-09-10 18:08:37 -07001594 * <p>If this is never called, the default format will be
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001595 * {@link android.graphics.ImageFormat#NV21}, which
Scott Maindf4578e2009-09-10 12:22:07 -07001596 * uses the NV21 encoding format.</p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001597 *
Scott Maindf4578e2009-09-10 12:22:07 -07001598 * @param pixel_format the desired preview picture format, defined
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001599 * by one of the {@link android.graphics.ImageFormat} constants.
1600 * (E.g., <var>ImageFormat.NV21</var> (default),
1601 * <var>ImageFormat.RGB_565</var>, or
1602 * <var>ImageFormat.JPEG</var>)
1603 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 */
1605 public void setPreviewFormat(int pixel_format) {
1606 String s = cameraFormatForPixelFormat(pixel_format);
1607 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001608 throw new IllegalArgumentException(
1609 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 }
1611
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001612 set(KEY_PREVIEW_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 }
1614
1615 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001616 * Returns the image format for preview frames got from
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001617 * {@link PreviewCallback}.
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001618 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001619 * @return the preview format.
1620 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 */
1622 public int getPreviewFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001623 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT));
1624 }
1625
1626 /**
1627 * Gets the supported preview formats.
1628 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001629 * @return a list of supported preview formats. This method will always
1630 * return a list with at least one element.
1631 * @see android.graphics.ImageFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001632 */
1633 public List<Integer> getSupportedPreviewFormats() {
1634 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX);
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001635 ArrayList<Integer> formats = new ArrayList<Integer>();
1636 for (String s : split(str)) {
1637 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001638 if (f == ImageFormat.UNKNOWN) continue;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001639 formats.add(f);
1640 }
1641 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 }
1643
1644 /**
1645 * Sets the dimensions for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001646 *
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08001647 * Applications need to consider the display orientation. See {@link
1648 * #setPreviewSize(int,int)} for reference.
1649 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 * @param width the width for pictures, in pixels
1651 * @param height the height for pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08001652 * @see #setPreviewSize(int,int)
1653 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 */
1655 public void setPictureSize(int width, int height) {
1656 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001657 set(KEY_PICTURE_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
1659
1660 /**
1661 * Returns the dimension setting for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001662 *
1663 * @return a Size object with the height and width setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 * for pictures
1665 */
1666 public Size getPictureSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001667 String pair = get(KEY_PICTURE_SIZE);
1668 return strToSize(pair);
1669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001671 /**
1672 * Gets the supported picture sizes.
1673 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001674 * @return a list of supported picture sizes. This method will always
1675 * return a list with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001676 */
1677 public List<Size> getSupportedPictureSizes() {
1678 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX);
1679 return splitSize(str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
1681
1682 /**
1683 * Sets the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001684 *
1685 * @param pixel_format the desired picture format
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001686 * (<var>ImageFormat.NV21</var>,
1687 * <var>ImageFormat.RGB_565</var>, or
1688 * <var>ImageFormat.JPEG</var>)
1689 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 */
1691 public void setPictureFormat(int pixel_format) {
1692 String s = cameraFormatForPixelFormat(pixel_format);
1693 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001694 throw new IllegalArgumentException(
1695 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
1697
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001698 set(KEY_PICTURE_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 }
1700
1701 /**
1702 * Returns the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001703 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001704 * @return the picture format
1705 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 */
1707 public int getPictureFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001708 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT));
1709 }
1710
1711 /**
1712 * Gets the supported picture formats.
1713 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001714 * @return supported picture formats. This method will always return a
1715 * list with at least one element.
1716 * @see android.graphics.ImageFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001717 */
1718 public List<Integer> getSupportedPictureFormats() {
Wu-cheng Li9c799382009-12-04 19:59:18 +08001719 String str = get(KEY_PICTURE_FORMAT + SUPPORTED_VALUES_SUFFIX);
1720 ArrayList<Integer> formats = new ArrayList<Integer>();
1721 for (String s : split(str)) {
1722 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001723 if (f == ImageFormat.UNKNOWN) continue;
Wu-cheng Li9c799382009-12-04 19:59:18 +08001724 formats.add(f);
1725 }
1726 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 }
1728
1729 private String cameraFormatForPixelFormat(int pixel_format) {
1730 switch(pixel_format) {
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001731 case ImageFormat.NV16: return PIXEL_FORMAT_YUV422SP;
1732 case ImageFormat.NV21: return PIXEL_FORMAT_YUV420SP;
1733 case ImageFormat.YUY2: return PIXEL_FORMAT_YUV422I;
1734 case ImageFormat.RGB_565: return PIXEL_FORMAT_RGB565;
1735 case ImageFormat.JPEG: return PIXEL_FORMAT_JPEG;
1736 default: return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 }
1738 }
1739
1740 private int pixelFormatForCameraFormat(String format) {
1741 if (format == null)
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001742 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001744 if (format.equals(PIXEL_FORMAT_YUV422SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001745 return ImageFormat.NV16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001747 if (format.equals(PIXEL_FORMAT_YUV420SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001748 return ImageFormat.NV21;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001750 if (format.equals(PIXEL_FORMAT_YUV422I))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001751 return ImageFormat.YUY2;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001752
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001753 if (format.equals(PIXEL_FORMAT_RGB565))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001754 return ImageFormat.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001756 if (format.equals(PIXEL_FORMAT_JPEG))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001757 return ImageFormat.JPEG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001759 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 }
1761
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001762 /**
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07001763 * Sets the rotation angle in degrees relative to the orientation of
1764 * the camera. This affects the pictures returned from JPEG {@link
1765 * PictureCallback}. The camera driver may set orientation in the
1766 * EXIF header without rotating the picture. Or the driver may rotate
1767 * the picture and the EXIF thumbnail. If the Jpeg picture is rotated,
1768 * the orientation in the EXIF header will be missing or 1 (row #0 is
1769 * top and column #0 is left side).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001770 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001771 * <p>If applications want to rotate the picture to match the orientation
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001772 * of what users see, apps should use {@link
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07001773 * android.view.OrientationEventListener} and {@link CameraInfo}.
1774 * The value from OrientationEventListener is relative to the natural
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001775 * orientation of the device. CameraInfo.orientation is the angle
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001776 * between camera orientation and natural device orientation. The sum
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001777 * of the two is the rotation angle for back-facing camera. The
1778 * difference of the two is the rotation angle for front-facing camera.
1779 * Note that the JPEG pictures of front-facing cameras are not mirrored
1780 * as in preview display.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001781 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001782 * <p>For example, suppose the natural orientation of the device is
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07001783 * portrait. The device is rotated 270 degrees clockwise, so the device
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001784 * orientation is 270. Suppose a back-facing camera sensor is mounted in
1785 * landscape and the top side of the camera sensor is aligned with the
1786 * right edge of the display in natural orientation. So the camera
1787 * orientation is 90. The rotation should be set to 0 (270 + 90).
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07001788 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001789 * <p>The reference code is as follows.
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07001790 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001791 * <pre>
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07001792 * public void public void onOrientationChanged(int orientation) {
1793 * if (orientation == ORIENTATION_UNKNOWN) return;
1794 * android.hardware.Camera.CameraInfo info =
1795 * new android.hardware.Camera.CameraInfo();
1796 * android.hardware.Camera.getCameraInfo(cameraId, info);
1797 * orientation = (orientation + 45) / 90 * 90;
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001798 * int rotation = 0;
1799 * if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
1800 * rotation = (info.orientation - orientation + 360) % 360;
1801 * } else { // back-facing camera
1802 * rotation = (info.orientation + orientation) % 360;
1803 * }
1804 * mParameters.setRotation(rotation);
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07001805 * }
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001806 * </pre>
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07001807 *
1808 * @param rotation The rotation angle in degrees relative to the
1809 * orientation of the camera. Rotation can only be 0,
1810 * 90, 180 or 270.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001811 * @throws IllegalArgumentException if rotation value is invalid.
1812 * @see android.view.OrientationEventListener
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07001813 * @see #getCameraInfo(int, CameraInfo)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001814 */
1815 public void setRotation(int rotation) {
1816 if (rotation == 0 || rotation == 90 || rotation == 180
1817 || rotation == 270) {
1818 set(KEY_ROTATION, Integer.toString(rotation));
1819 } else {
1820 throw new IllegalArgumentException(
1821 "Invalid rotation=" + rotation);
1822 }
1823 }
1824
1825 /**
1826 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF
1827 * header.
1828 *
1829 * @param latitude GPS latitude coordinate.
1830 */
1831 public void setGpsLatitude(double latitude) {
1832 set(KEY_GPS_LATITUDE, Double.toString(latitude));
1833 }
1834
1835 /**
1836 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF
1837 * header.
1838 *
1839 * @param longitude GPS longitude coordinate.
1840 */
1841 public void setGpsLongitude(double longitude) {
1842 set(KEY_GPS_LONGITUDE, Double.toString(longitude));
1843 }
1844
1845 /**
1846 * Sets GPS altitude. This will be stored in JPEG EXIF header.
1847 *
1848 * @param altitude GPS altitude in meters.
1849 */
1850 public void setGpsAltitude(double altitude) {
1851 set(KEY_GPS_ALTITUDE, Double.toString(altitude));
1852 }
1853
1854 /**
1855 * Sets GPS timestamp. This will be stored in JPEG EXIF header.
1856 *
1857 * @param timestamp GPS timestamp (UTC in seconds since January 1,
1858 * 1970).
1859 */
1860 public void setGpsTimestamp(long timestamp) {
1861 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp));
1862 }
1863
1864 /**
Ray Chene2083772010-03-10 15:02:49 -08001865 * Sets GPS processing method. It will store up to 32 characters
Ray Chen055c9862010-02-23 10:45:42 +08001866 * in JPEG EXIF header.
1867 *
1868 * @param processing_method The processing method to get this location.
1869 */
1870 public void setGpsProcessingMethod(String processing_method) {
1871 set(KEY_GPS_PROCESSING_METHOD, processing_method);
1872 }
1873
1874 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001875 * Removes GPS latitude, longitude, altitude, and timestamp from the
1876 * parameters.
1877 */
1878 public void removeGpsData() {
1879 remove(KEY_GPS_LATITUDE);
1880 remove(KEY_GPS_LONGITUDE);
1881 remove(KEY_GPS_ALTITUDE);
1882 remove(KEY_GPS_TIMESTAMP);
Ray Chen055c9862010-02-23 10:45:42 +08001883 remove(KEY_GPS_PROCESSING_METHOD);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001884 }
1885
1886 /**
1887 * Gets the current white balance setting.
1888 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001889 * @return current white balance. null if white balance setting is not
1890 * supported.
1891 * @see #WHITE_BALANCE_AUTO
1892 * @see #WHITE_BALANCE_INCANDESCENT
1893 * @see #WHITE_BALANCE_FLUORESCENT
1894 * @see #WHITE_BALANCE_WARM_FLUORESCENT
1895 * @see #WHITE_BALANCE_DAYLIGHT
1896 * @see #WHITE_BALANCE_CLOUDY_DAYLIGHT
1897 * @see #WHITE_BALANCE_TWILIGHT
1898 * @see #WHITE_BALANCE_SHADE
1899 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001900 */
1901 public String getWhiteBalance() {
1902 return get(KEY_WHITE_BALANCE);
1903 }
1904
1905 /**
1906 * Sets the white balance.
1907 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001908 * @param value new white balance.
1909 * @see #getWhiteBalance()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001910 */
1911 public void setWhiteBalance(String value) {
1912 set(KEY_WHITE_BALANCE, value);
1913 }
1914
1915 /**
1916 * Gets the supported white balance.
1917 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001918 * @return a list of supported white balance. null if white balance
1919 * setting is not supported.
1920 * @see #getWhiteBalance()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001921 */
1922 public List<String> getSupportedWhiteBalance() {
1923 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX);
1924 return split(str);
1925 }
1926
1927 /**
1928 * Gets the current color effect setting.
1929 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001930 * @return current color effect. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001931 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001932 * @see #EFFECT_NONE
1933 * @see #EFFECT_MONO
1934 * @see #EFFECT_NEGATIVE
1935 * @see #EFFECT_SOLARIZE
1936 * @see #EFFECT_SEPIA
1937 * @see #EFFECT_POSTERIZE
1938 * @see #EFFECT_WHITEBOARD
1939 * @see #EFFECT_BLACKBOARD
1940 * @see #EFFECT_AQUA
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001941 */
1942 public String getColorEffect() {
1943 return get(KEY_EFFECT);
1944 }
1945
1946 /**
1947 * Sets the current color effect setting.
1948 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001949 * @param value new color effect.
1950 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001951 */
1952 public void setColorEffect(String value) {
1953 set(KEY_EFFECT, value);
1954 }
1955
1956 /**
1957 * Gets the supported color effects.
1958 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001959 * @return a list of supported color effects. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001960 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001961 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001962 */
1963 public List<String> getSupportedColorEffects() {
1964 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX);
1965 return split(str);
1966 }
1967
1968
1969 /**
1970 * Gets the current antibanding setting.
1971 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001972 * @return current antibanding. null if antibanding setting is not
1973 * supported.
1974 * @see #ANTIBANDING_AUTO
1975 * @see #ANTIBANDING_50HZ
1976 * @see #ANTIBANDING_60HZ
1977 * @see #ANTIBANDING_OFF
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001978 */
1979 public String getAntibanding() {
1980 return get(KEY_ANTIBANDING);
1981 }
1982
1983 /**
1984 * Sets the antibanding.
1985 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001986 * @param antibanding new antibanding value.
1987 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001988 */
1989 public void setAntibanding(String antibanding) {
1990 set(KEY_ANTIBANDING, antibanding);
1991 }
1992
1993 /**
1994 * Gets the supported antibanding values.
1995 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001996 * @return a list of supported antibanding values. null if antibanding
1997 * setting is not supported.
1998 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001999 */
2000 public List<String> getSupportedAntibanding() {
2001 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX);
2002 return split(str);
2003 }
2004
2005 /**
2006 * Gets the current scene mode setting.
2007 *
2008 * @return one of SCENE_MODE_XXX string constant. null if scene mode
2009 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002010 * @see #SCENE_MODE_AUTO
2011 * @see #SCENE_MODE_ACTION
2012 * @see #SCENE_MODE_PORTRAIT
2013 * @see #SCENE_MODE_LANDSCAPE
2014 * @see #SCENE_MODE_NIGHT
2015 * @see #SCENE_MODE_NIGHT_PORTRAIT
2016 * @see #SCENE_MODE_THEATRE
2017 * @see #SCENE_MODE_BEACH
2018 * @see #SCENE_MODE_SNOW
2019 * @see #SCENE_MODE_SUNSET
2020 * @see #SCENE_MODE_STEADYPHOTO
2021 * @see #SCENE_MODE_FIREWORKS
2022 * @see #SCENE_MODE_SPORTS
2023 * @see #SCENE_MODE_PARTY
2024 * @see #SCENE_MODE_CANDLELIGHT
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002025 */
2026 public String getSceneMode() {
2027 return get(KEY_SCENE_MODE);
2028 }
2029
2030 /**
Wu-cheng Lic58b4232010-03-29 17:21:28 +08002031 * Sets the scene mode. Changing scene mode may override other
2032 * parameters (such as flash mode, focus mode, white balance). For
2033 * example, suppose originally flash mode is on and supported flash
2034 * modes are on/off. In night scene mode, both flash mode and supported
2035 * flash mode may be changed to off. After setting scene mode,
Wu-cheng Li2988ab72009-09-30 17:08:19 -07002036 * applications should call getParameters to know if some parameters are
2037 * changed.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002038 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002039 * @param value scene mode.
2040 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002041 */
2042 public void setSceneMode(String value) {
2043 set(KEY_SCENE_MODE, value);
2044 }
2045
2046 /**
2047 * Gets the supported scene modes.
2048 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002049 * @return a list of supported scene modes. null if scene mode setting
2050 * is not supported.
2051 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002052 */
2053 public List<String> getSupportedSceneModes() {
2054 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX);
2055 return split(str);
2056 }
2057
2058 /**
2059 * Gets the current flash mode setting.
2060 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002061 * @return current flash mode. null if flash mode setting is not
2062 * supported.
2063 * @see #FLASH_MODE_OFF
2064 * @see #FLASH_MODE_AUTO
2065 * @see #FLASH_MODE_ON
2066 * @see #FLASH_MODE_RED_EYE
2067 * @see #FLASH_MODE_TORCH
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002068 */
2069 public String getFlashMode() {
2070 return get(KEY_FLASH_MODE);
2071 }
2072
2073 /**
2074 * Sets the flash mode.
2075 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002076 * @param value flash mode.
2077 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002078 */
2079 public void setFlashMode(String value) {
2080 set(KEY_FLASH_MODE, value);
2081 }
2082
2083 /**
2084 * Gets the supported flash modes.
2085 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002086 * @return a list of supported flash modes. null if flash mode setting
2087 * is not supported.
2088 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002089 */
2090 public List<String> getSupportedFlashModes() {
2091 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX);
2092 return split(str);
2093 }
2094
Wu-cheng Li36322db2009-09-18 18:59:21 +08002095 /**
2096 * Gets the current focus mode setting.
2097 *
Wu-cheng Li699fe932010-08-05 11:50:25 -07002098 * @return current focus mode. This method will always return a non-null
2099 * value. Applications should call {@link
2100 * #autoFocus(AutoFocusCallback)} to start the focus if focus
2101 * mode is FOCUS_MODE_AUTO or FOCUS_MODE_MACRO.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002102 * @see #FOCUS_MODE_AUTO
2103 * @see #FOCUS_MODE_INFINITY
2104 * @see #FOCUS_MODE_MACRO
2105 * @see #FOCUS_MODE_FIXED
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07002106 * @see #FOCUS_MODE_EDOF
Wu-cheng Lid45cb722010-09-20 16:15:32 -07002107 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Li36322db2009-09-18 18:59:21 +08002108 */
2109 public String getFocusMode() {
2110 return get(KEY_FOCUS_MODE);
2111 }
2112
2113 /**
2114 * Sets the focus mode.
2115 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002116 * @param value focus mode.
2117 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08002118 */
2119 public void setFocusMode(String value) {
2120 set(KEY_FOCUS_MODE, value);
2121 }
2122
2123 /**
2124 * Gets the supported focus modes.
2125 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002126 * @return a list of supported focus modes. This method will always
2127 * return a list with at least one element.
2128 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08002129 */
2130 public List<String> getSupportedFocusModes() {
2131 String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX);
2132 return split(str);
2133 }
2134
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002135 /**
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08002136 * Gets the focal length (in millimeter) of the camera.
2137 *
2138 * @return the focal length. This method will always return a valid
2139 * value.
2140 */
2141 public float getFocalLength() {
2142 return Float.parseFloat(get(KEY_FOCAL_LENGTH));
2143 }
2144
2145 /**
2146 * Gets the horizontal angle of view in degrees.
2147 *
2148 * @return horizontal angle of view. This method will always return a
2149 * valid value.
2150 */
2151 public float getHorizontalViewAngle() {
2152 return Float.parseFloat(get(KEY_HORIZONTAL_VIEW_ANGLE));
2153 }
2154
2155 /**
2156 * Gets the vertical angle of view in degrees.
2157 *
2158 * @return vertical angle of view. This method will always return a
2159 * valid value.
2160 */
2161 public float getVerticalViewAngle() {
2162 return Float.parseFloat(get(KEY_VERTICAL_VIEW_ANGLE));
2163 }
2164
2165 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002166 * Gets the current exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002167 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002168 * @return current exposure compensation index. The range is {@link
2169 * #getMinExposureCompensation} to {@link
2170 * #getMaxExposureCompensation}. 0 means exposure is not
2171 * adjusted.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002172 */
2173 public int getExposureCompensation() {
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002174 return getInt(KEY_EXPOSURE_COMPENSATION, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08002175 }
2176
2177 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002178 * Sets the exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002179 *
Wu-cheng Li0402e7d2010-02-26 15:04:55 +08002180 * @param value exposure compensation index. The valid value range is
2181 * from {@link #getMinExposureCompensation} (inclusive) to {@link
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002182 * #getMaxExposureCompensation} (inclusive). 0 means exposure is
2183 * not adjusted. Application should call
2184 * getMinExposureCompensation and getMaxExposureCompensation to
2185 * know if exposure compensation is supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002186 */
2187 public void setExposureCompensation(int value) {
2188 set(KEY_EXPOSURE_COMPENSATION, value);
2189 }
2190
2191 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002192 * Gets the maximum exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002193 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002194 * @return maximum exposure compensation index (>=0). If both this
2195 * method and {@link #getMinExposureCompensation} return 0,
2196 * exposure compensation is not supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002197 */
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002198 public int getMaxExposureCompensation() {
2199 return getInt(KEY_MAX_EXPOSURE_COMPENSATION, 0);
2200 }
2201
2202 /**
2203 * Gets the minimum exposure compensation index.
2204 *
2205 * @return minimum exposure compensation index (<=0). If both this
2206 * method and {@link #getMaxExposureCompensation} return 0,
2207 * exposure compensation is not supported.
2208 */
2209 public int getMinExposureCompensation() {
2210 return getInt(KEY_MIN_EXPOSURE_COMPENSATION, 0);
2211 }
2212
2213 /**
2214 * Gets the exposure compensation step.
2215 *
2216 * @return exposure compensation step. Applications can get EV by
2217 * multiplying the exposure compensation index and step. Ex: if
2218 * exposure compensation index is -6 and step is 0.333333333, EV
2219 * is -2.
2220 */
2221 public float getExposureCompensationStep() {
2222 return getFloat(KEY_EXPOSURE_COMPENSATION_STEP, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08002223 }
2224
2225 /**
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002226 * Gets current zoom value. This also works when smooth zoom is in
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002227 * progress. Applications should check {@link #isZoomSupported} before
2228 * using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002229 *
2230 * @return the current zoom value. The range is 0 to {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002231 * #getMaxZoom}. 0 means the camera is not zoomed.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002232 */
2233 public int getZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002234 return getInt(KEY_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002235 }
2236
2237 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002238 * Sets current zoom value. If the camera is zoomed (value > 0), the
2239 * actual picture size may be smaller than picture size setting.
2240 * Applications can check the actual picture size after picture is
2241 * returned from {@link PictureCallback}. The preview size remains the
2242 * same in zoom. Applications should check {@link #isZoomSupported}
2243 * before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002244 *
2245 * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002246 */
2247 public void setZoom(int value) {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002248 set(KEY_ZOOM, value);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002249 }
2250
2251 /**
2252 * Returns true if zoom is supported. Applications should call this
2253 * before using other zoom methods.
2254 *
2255 * @return true if zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002256 */
2257 public boolean isZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002258 String str = get(KEY_ZOOM_SUPPORTED);
2259 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002260 }
2261
2262 /**
2263 * Gets the maximum zoom value allowed for snapshot. This is the maximum
2264 * value that applications can set to {@link #setZoom(int)}.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002265 * Applications should call {@link #isZoomSupported} before using this
2266 * method. This value may change in different preview size. Applications
2267 * should call this again after setting preview size.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002268 *
2269 * @return the maximum zoom value supported by the camera.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002270 */
2271 public int getMaxZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002272 return getInt(KEY_MAX_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002273 }
2274
2275 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002276 * Gets the zoom ratios of all zoom values. Applications should check
2277 * {@link #isZoomSupported} before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002278 *
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002279 * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is
2280 * returned as 320. The number of elements is {@link
2281 * #getMaxZoom} + 1. The list is sorted from small to large. The
2282 * first element is always 100. The last element is the zoom
2283 * ratio of the maximum zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002284 */
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002285 public List<Integer> getZoomRatios() {
2286 return splitInt(get(KEY_ZOOM_RATIOS));
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002287 }
2288
2289 /**
2290 * Returns true if smooth zoom is supported. Applications should call
2291 * this before using other smooth zoom methods.
2292 *
2293 * @return true if smooth zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002294 */
2295 public boolean isSmoothZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002296 String str = get(KEY_SMOOTH_ZOOM_SUPPORTED);
2297 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002298 }
2299
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002300 /**
2301 * Gets the distances from the camera to where an object appears to be
2302 * in focus. The object is sharpest at the optimal focus distance. The
2303 * depth of field is the far focus distance minus near focus distance.
2304 *
2305 * Focus distances may change after calling {@link
2306 * #autoFocus(AutoFocusCallback)}, {@link #cancelAutoFocus}, or {@link
2307 * #startPreview()}. Applications can call {@link #getParameters()}
2308 * and this method anytime to get the latest focus distances. If the
Wu-cheng Lid45cb722010-09-20 16:15:32 -07002309 * focus mode is FOCUS_MODE_CONTINUOUS_VIDEO, focus distances may change
2310 * from time to time.
Wu-cheng Li699fe932010-08-05 11:50:25 -07002311 *
2312 * This method is intended to estimate the distance between the camera
2313 * and the subject. After autofocus, the subject distance may be within
2314 * near and far focus distance. However, the precision depends on the
2315 * camera hardware, autofocus algorithm, the focus area, and the scene.
2316 * The error can be large and it should be only used as a reference.
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002317 *
Wu-cheng Li185cc452010-05-20 15:36:13 +08002318 * Far focus distance >= optimal focus distance >= near focus distance.
2319 * If the focus distance is infinity, the value will be
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002320 * Float.POSITIVE_INFINITY.
2321 *
2322 * @param output focus distances in meters. output must be a float
2323 * array with three elements. Near focus distance, optimal focus
2324 * distance, and far focus distance will be filled in the array.
Wu-cheng Li185cc452010-05-20 15:36:13 +08002325 * @see #FOCUS_DISTANCE_NEAR_INDEX
2326 * @see #FOCUS_DISTANCE_OPTIMAL_INDEX
2327 * @see #FOCUS_DISTANCE_FAR_INDEX
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002328 */
2329 public void getFocusDistances(float[] output) {
2330 if (output == null || output.length != 3) {
2331 throw new IllegalArgumentException(
2332 "output must be an float array with three elements.");
2333 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07002334 splitFloat(get(KEY_FOCUS_DISTANCES), output);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002335 }
2336
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002337 // Splits a comma delimited string to an ArrayList of String.
2338 // Return null if the passing string is null or the size is 0.
2339 private ArrayList<String> split(String str) {
2340 if (str == null) return null;
2341
2342 // Use StringTokenizer because it is faster than split.
2343 StringTokenizer tokenizer = new StringTokenizer(str, ",");
2344 ArrayList<String> substrings = new ArrayList<String>();
2345 while (tokenizer.hasMoreElements()) {
2346 substrings.add(tokenizer.nextToken());
2347 }
2348 return substrings;
2349 }
2350
2351 // Splits a comma delimited string to an ArrayList of Integer.
2352 // Return null if the passing string is null or the size is 0.
2353 private ArrayList<Integer> splitInt(String str) {
2354 if (str == null) return null;
2355
2356 StringTokenizer tokenizer = new StringTokenizer(str, ",");
2357 ArrayList<Integer> substrings = new ArrayList<Integer>();
2358 while (tokenizer.hasMoreElements()) {
2359 String token = tokenizer.nextToken();
2360 substrings.add(Integer.parseInt(token));
2361 }
2362 if (substrings.size() == 0) return null;
2363 return substrings;
2364 }
2365
Wu-cheng Li454630f2010-08-11 16:48:05 -07002366 private void splitInt(String str, int[] output) {
2367 if (str == null) return;
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002368
2369 StringTokenizer tokenizer = new StringTokenizer(str, ",");
Wu-cheng Li454630f2010-08-11 16:48:05 -07002370 int index = 0;
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002371 while (tokenizer.hasMoreElements()) {
2372 String token = tokenizer.nextToken();
Wu-cheng Li454630f2010-08-11 16:48:05 -07002373 output[index++] = Integer.parseInt(token);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002374 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07002375 }
2376
2377 // Splits a comma delimited string to an ArrayList of Float.
2378 private void splitFloat(String str, float[] output) {
2379 if (str == null) return;
2380
2381 StringTokenizer tokenizer = new StringTokenizer(str, ",");
2382 int index = 0;
2383 while (tokenizer.hasMoreElements()) {
2384 String token = tokenizer.nextToken();
2385 output[index++] = Float.parseFloat(token);
2386 }
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002387 }
2388
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002389 // Returns the value of a float parameter.
2390 private float getFloat(String key, float defaultValue) {
2391 try {
2392 return Float.parseFloat(mMap.get(key));
2393 } catch (NumberFormatException ex) {
2394 return defaultValue;
2395 }
2396 }
2397
2398 // Returns the value of a integer parameter.
2399 private int getInt(String key, int defaultValue) {
2400 try {
2401 return Integer.parseInt(mMap.get(key));
2402 } catch (NumberFormatException ex) {
2403 return defaultValue;
2404 }
2405 }
2406
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002407 // Splits a comma delimited string to an ArrayList of Size.
2408 // Return null if the passing string is null or the size is 0.
2409 private ArrayList<Size> splitSize(String str) {
2410 if (str == null) return null;
2411
2412 StringTokenizer tokenizer = new StringTokenizer(str, ",");
2413 ArrayList<Size> sizeList = new ArrayList<Size>();
2414 while (tokenizer.hasMoreElements()) {
2415 Size size = strToSize(tokenizer.nextToken());
2416 if (size != null) sizeList.add(size);
2417 }
2418 if (sizeList.size() == 0) return null;
2419 return sizeList;
2420 }
2421
2422 // Parses a string (ex: "480x320") to Size object.
2423 // Return null if the passing string is null.
2424 private Size strToSize(String str) {
2425 if (str == null) return null;
2426
2427 int pos = str.indexOf('x');
2428 if (pos != -1) {
2429 String width = str.substring(0, pos);
2430 String height = str.substring(pos + 1);
2431 return new Size(Integer.parseInt(width),
2432 Integer.parseInt(height));
2433 }
2434 Log.e(TAG, "Invalid size parameter string=" + str);
2435 return null;
2436 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07002437
2438 // Splits a comma delimited string to an ArrayList of int array.
2439 // Example string: "(10000,26623),(10000,30000)". Return null if the
2440 // passing string is null or the size is 0.
2441 private ArrayList<int[]> splitRange(String str) {
2442 if (str == null || str.charAt(0) != '('
2443 || str.charAt(str.length() - 1) != ')') {
2444 Log.e(TAG, "Invalid range list string=" + str);
2445 return null;
2446 }
2447
2448 ArrayList<int[]> rangeList = new ArrayList<int[]>();
2449 int endIndex, fromIndex = 1;
2450 do {
2451 int[] range = new int[2];
2452 endIndex = str.indexOf("),(", fromIndex);
2453 if (endIndex == -1) endIndex = str.length() - 1;
2454 splitInt(str.substring(fromIndex, endIndex), range);
2455 rangeList.add(range);
2456 fromIndex = endIndex + 3;
2457 } while (endIndex != str.length() - 1);
2458
2459 if (rangeList.size() == 0) return null;
2460 return rangeList;
2461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 };
2463}