blob: 6cf3b2b96181a754747e02291a6b5f2d414b5dfc [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
Wu-cheng Li10e09c62011-07-18 09:09:41 +080019import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
Mathias Agopiana696f5d2010-02-17 17:53:09 -080021import android.graphics.ImageFormat;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +080022import android.graphics.Point;
Wu-cheng Li30771b72011-04-02 06:19:46 +080023import android.graphics.Rect;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -080024import android.graphics.SurfaceTexture;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.os.Handler;
26import android.os.Looper;
27import android.os.Message;
Wu-cheng Libde61a52011-06-07 18:23:14 +080028import android.util.Log;
29import android.view.Surface;
30import android.view.SurfaceHolder;
31
32import java.io.IOException;
33import java.lang.ref.WeakReference;
34import java.util.ArrayList;
35import java.util.HashMap;
36import java.util.List;
37import java.util.StringTokenizer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39/**
Dan Egnorbfcbeff2010-07-12 15:12:54 -070040 * The Camera class is used to set image capture settings, start/stop preview,
41 * snap pictures, and retrieve frames for encoding for video. This class is a
42 * client for the Camera service, which manages the actual camera hardware.
Scott Maindf4578e2009-09-10 12:22:07 -070043 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070044 * <p>To access the device camera, you must declare the
Wu-cheng Li7478ea62009-09-16 18:52:55 +080045 * {@link android.Manifest.permission#CAMERA} permission in your Android
Scott Maindf4578e2009-09-10 12:22:07 -070046 * Manifest. Also be sure to include the
47 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
Dan Egnorbfcbeff2010-07-12 15:12:54 -070048 * manifest element to declare camera features used by your application.
Wu-cheng Li7478ea62009-09-16 18:52:55 +080049 * For example, if you use the camera and auto-focus feature, your Manifest
Scott Maindf4578e2009-09-10 12:22:07 -070050 * should include the following:</p>
51 * <pre> &lt;uses-permission android:name="android.permission.CAMERA" />
52 * &lt;uses-feature android:name="android.hardware.camera" />
53 * &lt;uses-feature android:name="android.hardware.camera.autofocus" /></pre>
54 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070055 * <p>To take pictures with this class, use the following steps:</p>
56 *
57 * <ol>
Dan Egnor341ff132010-07-20 11:30:17 -070058 * <li>Obtain an instance of Camera from {@link #open(int)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -070059 *
60 * <li>Get existing (default) settings with {@link #getParameters()}.
61 *
62 * <li>If necessary, modify the returned {@link Camera.Parameters} object and call
63 * {@link #setParameters(Camera.Parameters)}.
64 *
65 * <li>If desired, call {@link #setDisplayOrientation(int)}.
66 *
67 * <li><b>Important</b>: Pass a fully initialized {@link SurfaceHolder} to
68 * {@link #setPreviewDisplay(SurfaceHolder)}. Without a surface, the camera
69 * will be unable to start the preview.
70 *
71 * <li><b>Important</b>: Call {@link #startPreview()} to start updating the
72 * preview surface. Preview must be started before you can take a picture.
73 *
74 * <li>When you want, call {@link #takePicture(Camera.ShutterCallback,
75 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)} to
76 * capture a photo. Wait for the callbacks to provide the actual image data.
77 *
78 * <li>After taking a picture, preview display will have stopped. To take more
79 * photos, call {@link #startPreview()} again first.
80 *
81 * <li>Call {@link #stopPreview()} to stop updating the preview surface.
82 *
83 * <li><b>Important:</b> Call {@link #release()} to release the camera for
84 * use by other applications. Applications should release the camera
85 * immediately in {@link android.app.Activity#onPause()} (and re-{@link #open()}
86 * it in {@link android.app.Activity#onResume()}).
87 * </ol>
88 *
89 * <p>To quickly switch to video recording mode, use these steps:</p>
90 *
91 * <ol>
92 * <li>Obtain and initialize a Camera and start preview as described above.
93 *
94 * <li>Call {@link #unlock()} to allow the media process to access the camera.
95 *
96 * <li>Pass the camera to {@link android.media.MediaRecorder#setCamera(Camera)}.
97 * See {@link android.media.MediaRecorder} information about video recording.
98 *
99 * <li>When finished recording, call {@link #reconnect()} to re-acquire
100 * and re-lock the camera.
101 *
102 * <li>If desired, restart preview and take more photos or videos.
103 *
104 * <li>Call {@link #stopPreview()} and {@link #release()} as described above.
105 * </ol>
106 *
107 * <p>This class is not thread-safe, and is meant for use from one event thread.
108 * Most long-running operations (preview, focus, photo capture, etc) happen
109 * asynchronously and invoke callbacks as necessary. Callbacks will be invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700110 * on the event thread {@link #open(int)} was called from. This class's methods
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700111 * must never be called from multiple threads at once.</p>
112 *
Scott Maindf4578e2009-09-10 12:22:07 -0700113 * <p class="caution"><strong>Caution:</strong> Different Android-powered devices
114 * may have different hardware specifications, such as megapixel ratings and
115 * auto-focus capabilities. In order for your application to be compatible with
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800116 * more devices, you should not make assumptions about the device camera
Scott Maindf4578e2009-09-10 12:22:07 -0700117 * specifications.</p>
Joe Fernandez6c5c3c32011-10-18 14:57:05 -0700118 *
119 * <div class="special reference">
120 * <h3>Developer Guides</h3>
121 * <p>For more information about using cameras, read the
122 * <a href="{@docRoot}guide/topics/media/camera.html">Camera</a> developer guide.</p>
123 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 */
125public class Camera {
126 private static final String TAG = "Camera";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800127
Wu-cheng Lid2c29292010-05-28 17:32:41 +0800128 // These match the enums in frameworks/base/include/camera/Camera.h
Benny Wongda83f462009-08-12 12:01:27 -0500129 private static final int CAMERA_MSG_ERROR = 0x001;
130 private static final int CAMERA_MSG_SHUTTER = 0x002;
131 private static final int CAMERA_MSG_FOCUS = 0x004;
132 private static final int CAMERA_MSG_ZOOM = 0x008;
133 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010;
134 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020;
135 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040;
136 private static final int CAMERA_MSG_RAW_IMAGE = 0x080;
137 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800138 private static final int CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x200;
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800139 private static final int CAMERA_MSG_PREVIEW_METADATA = 0x400;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800140 private static final int CAMERA_MSG_FOCUS_MOVE = 0x800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
142 private int mNativeContext; // accessed by native methods
143 private EventHandler mEventHandler;
144 private ShutterCallback mShutterCallback;
145 private PictureCallback mRawImageCallback;
146 private PictureCallback mJpegCallback;
147 private PreviewCallback mPreviewCallback;
Dave Sparkse8b26e12009-07-14 10:35:40 -0700148 private PictureCallback mPostviewCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 private AutoFocusCallback mAutoFocusCallback;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800150 private AutoFocusMoveCallback mAutoFocusMoveCallback;
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800151 private OnZoomChangeListener mZoomListener;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800152 private FaceDetectionListener mFaceListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 private ErrorCallback mErrorCallback;
154 private boolean mOneShot;
Andrew Harp94927df2009-10-20 01:47:05 -0400155 private boolean mWithBuffer;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800156 private boolean mFaceDetectionRunning = false;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 /**
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800159 * Broadcast Action: A new picture is taken by the camera, and the entry of
160 * the picture has been added to the media store.
161 * {@link android.content.Intent#getData} is URI of the picture.
162 */
163 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
164 public static final String ACTION_NEW_PICTURE = "android.hardware.action.NEW_PICTURE";
165
166 /**
167 * Broadcast Action: A new video is recorded by the camera, and the entry
168 * of the video has been added to the media store.
169 * {@link android.content.Intent#getData} is URI of the video.
170 */
171 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
172 public static final String ACTION_NEW_VIDEO = "android.hardware.action.NEW_VIDEO";
173
174 /**
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800175 * Hardware face detection. It does not use much CPU.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800176 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800177 private static final int CAMERA_FACE_DETECTION_HW = 0;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800178
179 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800180 * Software face detection. It uses some CPU.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800181 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800182 private static final int CAMERA_FACE_DETECTION_SW = 1;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800183
184 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700185 * Returns the number of physical cameras available on this device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 */
Chih-Chung Change25cc652010-05-06 16:36:58 +0800187 public native static int getNumberOfCameras();
188
189 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700190 * Returns the information about a particular camera.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800191 * If {@link #getNumberOfCameras()} returns N, the valid id is 0 to N-1.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800192 */
193 public native static void getCameraInfo(int cameraId, CameraInfo cameraInfo);
194
195 /**
196 * Information about a camera
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800197 */
198 public static class CameraInfo {
Wu-cheng Li78366602010-09-15 14:08:15 -0700199 /**
200 * The facing of the camera is opposite to that of the screen.
201 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800202 public static final int CAMERA_FACING_BACK = 0;
Wu-cheng Li78366602010-09-15 14:08:15 -0700203
204 /**
205 * The facing of the camera is the same as that of the screen.
206 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800207 public static final int CAMERA_FACING_FRONT = 1;
208
209 /**
Joe Fernandez464cb212011-10-04 16:56:47 -0700210 * The direction that the camera faces. It should be
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800211 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
212 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700213 public int facing;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800214
215 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700216 * <p>The orientation of the camera image. The value is the angle that the
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800217 * camera image needs to be rotated clockwise so it shows correctly on
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700218 * the display in its natural orientation. It should be 0, 90, 180, or 270.</p>
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800219 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700220 * <p>For example, suppose a device has a naturally tall screen. The
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +0800221 * back-facing camera sensor is mounted in landscape. You are looking at
222 * the screen. If the top side of the camera sensor is aligned with the
223 * right edge of the screen in natural orientation, the value should be
224 * 90. If the top side of a front-facing camera sensor is aligned with
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700225 * the right of the screen, the value should be 270.</p>
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800226 *
227 * @see #setDisplayOrientation(int)
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800228 * @see Parameters#setRotation(int)
229 * @see Parameters#setPreviewSize(int, int)
230 * @see Parameters#setPictureSize(int, int)
231 * @see Parameters#setJpegThumbnailSize(int, int)
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800232 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700233 public int orientation;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800234 };
235
236 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700237 * Creates a new Camera object to access a particular hardware camera.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700238 *
239 * <p>You must call {@link #release()} when you are done using the camera,
240 * otherwise it will remain locked and be unavailable to other applications.
241 *
Dan Egnor341ff132010-07-20 11:30:17 -0700242 * <p>Your application should only have one Camera object active at a time
243 * for a particular hardware camera.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700244 *
245 * <p>Callbacks from other methods are delivered to the event loop of the
246 * thread which called open(). If this thread has no event loop, then
247 * callbacks are delivered to the main application event loop. If there
248 * is no main application event loop, callbacks are not delivered.
249 *
250 * <p class="caution"><b>Caution:</b> On some devices, this method may
251 * take a long time to complete. It is best to call this method from a
252 * worker thread (possibly using {@link android.os.AsyncTask}) to avoid
253 * blocking the main application UI thread.
254 *
Dan Egnor341ff132010-07-20 11:30:17 -0700255 * @param cameraId the hardware camera to access, between 0 and
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800256 * {@link #getNumberOfCameras()}-1.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700257 * @return a new Camera object, connected, locked and ready for use.
258 * @throws RuntimeException if connection to the camera service fails (for
Wu-cheng Lifacc8ce2011-06-17 13:09:40 +0800259 * example, if the camera is in use by another process or device policy
260 * manager has disabled the camera).
261 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800262 */
263 public static Camera open(int cameraId) {
264 return new Camera(cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
266
Chih-Chung Change25cc652010-05-06 16:36:58 +0800267 /**
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800268 * Creates a new Camera object to access the first back-facing camera on the
269 * device. If the device does not have a back-facing camera, this returns
270 * null.
Wu-cheng Li78366602010-09-15 14:08:15 -0700271 * @see #open(int)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800272 */
273 public static Camera open() {
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800274 int numberOfCameras = getNumberOfCameras();
275 CameraInfo cameraInfo = new CameraInfo();
276 for (int i = 0; i < numberOfCameras; i++) {
277 getCameraInfo(i, cameraInfo);
278 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
279 return new Camera(i);
280 }
281 }
282 return null;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800283 }
284
285 Camera(int cameraId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 mShutterCallback = null;
287 mRawImageCallback = null;
288 mJpegCallback = null;
289 mPreviewCallback = null;
Dave Sparkse8b26e12009-07-14 10:35:40 -0700290 mPostviewCallback = null;
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800291 mZoomListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
293 Looper looper;
294 if ((looper = Looper.myLooper()) != null) {
295 mEventHandler = new EventHandler(this, looper);
296 } else if ((looper = Looper.getMainLooper()) != null) {
297 mEventHandler = new EventHandler(this, looper);
298 } else {
299 mEventHandler = null;
300 }
301
Chih-Chung Change25cc652010-05-06 16:36:58 +0800302 native_setup(new WeakReference<Camera>(this), cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800304
Wu-cheng Li1c04a332011-11-22 18:21:18 +0800305 /**
306 * An empty Camera for testing purpose.
307 */
308 Camera() {
309 }
310
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800311 protected void finalize() {
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -0800312 release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800314
Chih-Chung Change25cc652010-05-06 16:36:58 +0800315 private native final void native_setup(Object camera_this, int cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 private native final void native_release();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
319 /**
320 * Disconnects and releases the Camera object resources.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700321 *
322 * <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 -0800323 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800324 public final void release() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 native_release();
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800326 mFaceDetectionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 }
328
329 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700330 * Unlocks the camera to allow another process to access it.
331 * Normally, the camera is locked to the process with an active Camera
332 * object until {@link #release()} is called. To allow rapid handoff
333 * between processes, you can call this method to release the camera
334 * temporarily for another process to use; once the other process is done
335 * you can call {@link #reconnect()} to reclaim the camera.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700337 * <p>This must be done before calling
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800338 * {@link android.media.MediaRecorder#setCamera(Camera)}. This cannot be
339 * called after recording starts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700341 * <p>If you are not recording video, you probably do not need this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700343 * @throws RuntimeException if the camera cannot be unlocked.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 */
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800345 public native final void unlock();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700348 * Re-locks the camera to prevent other processes from accessing it.
349 * Camera objects are locked by default unless {@link #unlock()} is
350 * called. Normally {@link #reconnect()} is used instead.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800351 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800352 * <p>Since API level 14, camera is automatically locked for applications in
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800353 * {@link android.media.MediaRecorder#start()}. Applications can use the
354 * camera (ex: zoom) after recording starts. There is no need to call this
355 * after recording starts or stops.
356 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700357 * <p>If you are not recording video, you probably do not need this method.
358 *
359 * @throws RuntimeException if the camera cannot be re-locked (for
360 * example, if the camera is still in use by another process).
361 */
362 public native final void lock();
363
364 /**
365 * Reconnects to the camera service after another process used it.
366 * After {@link #unlock()} is called, another process may use the
367 * camera; when the process is done, you must reconnect to the camera,
368 * which will re-acquire the lock and allow you to continue using the
369 * camera.
370 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800371 * <p>Since API level 14, camera is automatically locked for applications in
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800372 * {@link android.media.MediaRecorder#start()}. Applications can use the
373 * camera (ex: zoom) after recording starts. There is no need to call this
374 * after recording starts or stops.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700375 *
376 * <p>If you are not recording video, you probably do not need this method.
377 *
378 * @throws IOException if a connection cannot be re-established (for
379 * example, if the camera is still in use by another process).
380 */
381 public native final void reconnect() throws IOException;
382
383 /**
384 * Sets the {@link Surface} to be used for live preview.
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800385 * Either a surface or surface texture is necessary for preview, and
386 * preview is necessary to take pictures. The same surface can be re-set
387 * without harm. Setting a preview surface will un-set any preview surface
388 * texture that was set via {@link #setPreviewTexture}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700389 *
390 * <p>The {@link SurfaceHolder} must already contain a surface when this
391 * method is called. If you are using {@link android.view.SurfaceView},
392 * you will need to register a {@link SurfaceHolder.Callback} with
393 * {@link SurfaceHolder#addCallback(SurfaceHolder.Callback)} and wait for
394 * {@link SurfaceHolder.Callback#surfaceCreated(SurfaceHolder)} before
395 * calling setPreviewDisplay() or starting preview.
396 *
397 * <p>This method must be called before {@link #startPreview()}. The
398 * one exception is that if the preview surface is not set (or set to null)
399 * before startPreview() is called, then this method may be called once
400 * with a non-null parameter to set the preview surface. (This allows
401 * camera setup and surface creation to happen in parallel, saving time.)
402 * The preview surface may not otherwise change while preview is running.
403 *
404 * @param holder containing the Surface on which to place the preview,
405 * or null to remove the preview surface
406 * @throws IOException if the method fails (for example, if the surface
407 * is unavailable or unsuitable).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 */
409 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException {
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800410 if (holder != null) {
411 setPreviewDisplay(holder.getSurface());
412 } else {
413 setPreviewDisplay((Surface)null);
414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
416
Glenn Kastendbc289d2011-02-09 10:15:44 -0800417 private native final void setPreviewDisplay(Surface surface) throws IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418
419 /**
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800420 * Sets the {@link SurfaceTexture} to be used for live preview.
421 * Either a surface or surface texture is necessary for preview, and
422 * preview is necessary to take pictures. The same surface texture can be
423 * re-set without harm. Setting a preview surface texture will un-set any
424 * preview surface that was set via {@link #setPreviewDisplay}.
425 *
426 * <p>This method must be called before {@link #startPreview()}. The
427 * one exception is that if the preview surface texture is not set (or set
428 * to null) before startPreview() is called, then this method may be called
429 * once with a non-null parameter to set the preview surface. (This allows
430 * camera setup and surface creation to happen in parallel, saving time.)
431 * The preview surface texture may not otherwise change while preview is
432 * running.
433 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700434 * <p>The timestamps provided by {@link SurfaceTexture#getTimestamp()} for a
Eino-Ville Talvalae309a0f2011-03-21 11:04:34 -0700435 * SurfaceTexture set as the preview texture have an unspecified zero point,
436 * and cannot be directly compared between different cameras or different
437 * instances of the same camera, or across multiple runs of the same
438 * program.
439 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800440 * <p>If you are using the preview data to create video or still images,
441 * strongly consider using {@link android.media.MediaActionSound} to
442 * properly indicate image capture or recording start/stop to the user.</p>
443 *
444 * @see android.media.MediaActionSound
445 * @see android.graphics.SurfaceTexture
446 * @see android.view.TextureView
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800447 * @param surfaceTexture the {@link SurfaceTexture} to which the preview
448 * images are to be sent or null to remove the current preview surface
449 * texture
450 * @throws IOException if the method fails (for example, if the surface
451 * texture is unavailable or unsuitable).
452 */
Glenn Kastendbc289d2011-02-09 10:15:44 -0800453 public native final void setPreviewTexture(SurfaceTexture surfaceTexture) throws IOException;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800454
455 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700456 * Callback interface used to deliver copies of preview frames as
457 * they are displayed.
458 *
459 * @see #setPreviewCallback(Camera.PreviewCallback)
460 * @see #setOneShotPreviewCallback(Camera.PreviewCallback)
461 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
462 * @see #startPreview()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 */
464 public interface PreviewCallback
465 {
466 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700467 * Called as preview frames are displayed. This callback is invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700468 * on the event thread {@link #open(int)} was called from.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700470 * @param data the contents of the preview frame in the format defined
Mathias Agopiana696f5d2010-02-17 17:53:09 -0800471 * by {@link android.graphics.ImageFormat}, which can be queried
Scott Maindf4578e2009-09-10 12:22:07 -0700472 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
Scott Mainda0a56d2009-09-10 18:08:37 -0700473 * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800474 * is never called, the default will be the YCbCr_420_SP
475 * (NV21) format.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700476 * @param camera the Camera service object.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 */
478 void onPreviewFrame(byte[] data, Camera camera);
479 };
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700482 * Starts capturing and drawing preview frames to the screen.
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800483 * Preview will not actually start until a surface is supplied
484 * with {@link #setPreviewDisplay(SurfaceHolder)} or
485 * {@link #setPreviewTexture(SurfaceTexture)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700486 *
487 * <p>If {@link #setPreviewCallback(Camera.PreviewCallback)},
488 * {@link #setOneShotPreviewCallback(Camera.PreviewCallback)}, or
489 * {@link #setPreviewCallbackWithBuffer(Camera.PreviewCallback)} were
490 * called, {@link Camera.PreviewCallback#onPreviewFrame(byte[], Camera)}
491 * will be called when preview data becomes available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 */
493 public native final void startPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700496 * Stops capturing and drawing preview frames to the surface, and
497 * resets the camera for a future call to {@link #startPreview()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 */
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800499 public final void stopPreview() {
500 _stopPreview();
501 mFaceDetectionRunning = false;
Chih-yu Huang664d72e2011-09-23 18:59:38 +0800502
503 mShutterCallback = null;
504 mRawImageCallback = null;
505 mPostviewCallback = null;
506 mJpegCallback = null;
507 mAutoFocusCallback = null;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800508 mAutoFocusMoveCallback = null;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800509 }
510
511 private native final void _stopPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 /**
514 * Return current preview state.
515 *
516 * FIXME: Unhide before release
517 * @hide
518 */
519 public native final boolean previewEnabled();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800522 * <p>Installs a callback to be invoked for every preview frame in addition
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700523 * to displaying them on the screen. The callback will be repeatedly called
524 * for as long as preview is active. This method can be called at any time,
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800525 * even while preview is live. Any other preview callbacks are
526 * overridden.</p>
527 *
528 * <p>If you are using the preview data to create video or still images,
529 * strongly consider using {@link android.media.MediaActionSound} to
530 * properly indicate image capture or recording start/stop to the user.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700532 * @param cb a callback object that receives a copy of each preview frame,
533 * or null to stop receiving callbacks.
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800534 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 */
536 public final void setPreviewCallback(PreviewCallback cb) {
537 mPreviewCallback = cb;
538 mOneShot = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400539 mWithBuffer = false;
Dave Sparksa6118c62009-10-13 02:28:54 -0700540 // Always use one-shot mode. We fake camera preview mode by
541 // doing one-shot preview continuously.
Andrew Harp94927df2009-10-20 01:47:05 -0400542 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544
545 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800546 * <p>Installs a callback to be invoked for the next preview frame in
547 * addition to displaying it on the screen. After one invocation, the
548 * callback is cleared. This method can be called any time, even when
549 * preview is live. Any other preview callbacks are overridden.</p>
550 *
551 * <p>If you are using the preview data to create video or still images,
552 * strongly consider using {@link android.media.MediaActionSound} to
553 * properly indicate image capture or recording start/stop to the user.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700555 * @param cb a callback object that receives a copy of the next preview frame,
556 * or null to stop receiving callbacks.
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800557 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 */
559 public final void setOneShotPreviewCallback(PreviewCallback cb) {
Andrew Harp94927df2009-10-20 01:47:05 -0400560 mPreviewCallback = cb;
561 mOneShot = true;
562 mWithBuffer = false;
563 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
Andrew Harp94927df2009-10-20 01:47:05 -0400566 private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer);
567
568 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800569 * <p>Installs a callback to be invoked for every preview frame, using
570 * buffers supplied with {@link #addCallbackBuffer(byte[])}, in addition to
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700571 * displaying them on the screen. The callback will be repeatedly called
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800572 * for as long as preview is active and buffers are available. Any other
573 * preview callbacks are overridden.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400574 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700575 * <p>The purpose of this method is to improve preview efficiency and frame
576 * rate by allowing preview frame memory reuse. You must call
577 * {@link #addCallbackBuffer(byte[])} at some point -- before or after
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800578 * calling this method -- or no callbacks will received.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400579 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800580 * <p>The buffer queue will be cleared if this method is called with a null
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700581 * callback, {@link #setPreviewCallback(Camera.PreviewCallback)} is called,
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800582 * or {@link #setOneShotPreviewCallback(Camera.PreviewCallback)} is
583 * called.</p>
584 *
585 * <p>If you are using the preview data to create video or still images,
586 * strongly consider using {@link android.media.MediaActionSound} to
587 * properly indicate image capture or recording start/stop to the user.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400588 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700589 * @param cb a callback object that receives a copy of the preview frame,
590 * or null to stop receiving callbacks and clear the buffer queue.
591 * @see #addCallbackBuffer(byte[])
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800592 * @see android.media.MediaActionSound
Andrew Harp94927df2009-10-20 01:47:05 -0400593 */
594 public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
595 mPreviewCallback = cb;
596 mOneShot = false;
597 mWithBuffer = true;
598 setHasPreviewCallback(cb != null, true);
599 }
600
601 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800602 * Adds a pre-allocated buffer to the preview callback buffer queue.
603 * Applications can add one or more buffers to the queue. When a preview
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700604 * frame arrives and there is still at least one available buffer, the
605 * buffer will be used and removed from the queue. Then preview callback is
606 * invoked with the buffer. If a frame arrives and there is no buffer left,
607 * the frame is discarded. Applications should add buffers back when they
608 * finish processing the data in them.
Wu-cheng Lic10275a2010-03-09 13:49:21 -0800609 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700610 * <p>The size of the buffer is determined by multiplying the preview
James Donge00cab72011-02-17 16:38:06 -0800611 * image width, height, and bytes per pixel. The width and height can be
612 * read from {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700613 * can be computed from
614 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
615 * using the image format from {@link Camera.Parameters#getPreviewFormat()}.
Andrew Harp94927df2009-10-20 01:47:05 -0400616 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700617 * <p>This method is only necessary when
James Donge00cab72011-02-17 16:38:06 -0800618 * {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700619 * {@link #setPreviewCallback(PreviewCallback)} or
620 * {@link #setOneShotPreviewCallback(PreviewCallback)} are used, buffers
James Donge00cab72011-02-17 16:38:06 -0800621 * are automatically allocated. When a supplied buffer is too small to
622 * hold the preview frame data, preview callback will return null and
623 * the buffer will be removed from the buffer queue.
Andrew Harp94927df2009-10-20 01:47:05 -0400624 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700625 * @param callbackBuffer the buffer to add to the queue.
626 * The size should be width * height * bits_per_pixel / 8.
Wu-cheng Li5b9bcda2010-03-07 14:59:28 -0800627 * @see #setPreviewCallbackWithBuffer(PreviewCallback)
Andrew Harp94927df2009-10-20 01:47:05 -0400628 */
James Donge00cab72011-02-17 16:38:06 -0800629 public final void addCallbackBuffer(byte[] callbackBuffer)
630 {
631 _addCallbackBuffer(callbackBuffer, CAMERA_MSG_PREVIEW_FRAME);
632 }
633
634 /**
635 * Adds a pre-allocated buffer to the raw image callback buffer queue.
636 * Applications can add one or more buffers to the queue. When a raw image
637 * frame arrives and there is still at least one available buffer, the
638 * buffer will be used to hold the raw image data and removed from the
639 * queue. Then raw image callback is invoked with the buffer. If a raw
640 * image frame arrives but there is no buffer left, the frame is
641 * discarded. Applications should add buffers back when they finish
642 * processing the data in them by calling this method again in order
643 * to avoid running out of raw image callback buffers.
644 *
645 * <p>The size of the buffer is determined by multiplying the raw image
646 * width, height, and bytes per pixel. The width and height can be
647 * read from {@link Camera.Parameters#getPictureSize()}. Bytes per pixel
648 * can be computed from
649 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
650 * using the image format from {@link Camera.Parameters#getPreviewFormat()}.
651 *
652 * <p>This method is only necessary when the PictureCallbck for raw image
653 * is used while calling {@link #takePicture(Camera.ShutterCallback,
654 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
655 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700656 * <p>Please note that by calling this method, the mode for
657 * application-managed callback buffers is triggered. If this method has
658 * never been called, null will be returned by the raw image callback since
659 * there is no image callback buffer available. Furthermore, When a supplied
660 * buffer is too small to hold the raw image data, raw image callback will
661 * return null and the buffer will be removed from the buffer queue.
James Donge00cab72011-02-17 16:38:06 -0800662 *
663 * @param callbackBuffer the buffer to add to the raw image callback buffer
664 * queue. The size should be width * height * (bits per pixel) / 8. An
665 * null callbackBuffer will be ignored and won't be added to the queue.
666 *
667 * @see #takePicture(Camera.ShutterCallback,
668 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
669 *
670 * {@hide}
671 */
672 public final void addRawImageCallbackBuffer(byte[] callbackBuffer)
673 {
674 addCallbackBuffer(callbackBuffer, CAMERA_MSG_RAW_IMAGE);
675 }
676
677 private final void addCallbackBuffer(byte[] callbackBuffer, int msgType)
678 {
679 // CAMERA_MSG_VIDEO_FRAME may be allowed in the future.
680 if (msgType != CAMERA_MSG_PREVIEW_FRAME &&
681 msgType != CAMERA_MSG_RAW_IMAGE) {
682 throw new IllegalArgumentException(
683 "Unsupported message type: " + msgType);
684 }
685
686 _addCallbackBuffer(callbackBuffer, msgType);
687 }
688
689 private native final void _addCallbackBuffer(
690 byte[] callbackBuffer, int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
692 private class EventHandler extends Handler
693 {
694 private Camera mCamera;
695
696 public EventHandler(Camera c, Looper looper) {
697 super(looper);
698 mCamera = c;
699 }
700
701 @Override
702 public void handleMessage(Message msg) {
703 switch(msg.what) {
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700704 case CAMERA_MSG_SHUTTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 if (mShutterCallback != null) {
706 mShutterCallback.onShutter();
707 }
708 return;
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700709
710 case CAMERA_MSG_RAW_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700711 if (mRawImageCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 return;
715
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700716 case CAMERA_MSG_COMPRESSED_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700717 if (mJpegCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800721
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700722 case CAMERA_MSG_PREVIEW_FRAME:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 if (mPreviewCallback != null) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700724 PreviewCallback cb = mPreviewCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 if (mOneShot) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700726 // Clear the callback variable before the callback
727 // in case the app calls setPreviewCallback from
728 // the callback function
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 mPreviewCallback = null;
Andrew Harp94927df2009-10-20 01:47:05 -0400730 } else if (!mWithBuffer) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700731 // We're faking the camera preview mode to prevent
732 // the app from being flooded with preview frames.
733 // Set to oneshot mode again.
Andrew Harp94927df2009-10-20 01:47:05 -0400734 setHasPreviewCallback(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
Dave Sparksa6118c62009-10-13 02:28:54 -0700736 cb.onPreviewFrame((byte[])msg.obj, mCamera);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
738 return;
739
Dave Sparkse8b26e12009-07-14 10:35:40 -0700740 case CAMERA_MSG_POSTVIEW_FRAME:
741 if (mPostviewCallback != null) {
742 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera);
743 }
744 return;
745
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700746 case CAMERA_MSG_FOCUS:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700747 if (mAutoFocusCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700749 }
750 return;
751
752 case CAMERA_MSG_ZOOM:
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800753 if (mZoomListener != null) {
754 mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 return;
757
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800758 case CAMERA_MSG_PREVIEW_METADATA:
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800759 if (mFaceListener != null) {
Wu-cheng Lif0d6a482011-07-28 05:30:59 +0800760 mFaceListener.onFaceDetection((Face[])msg.obj, mCamera);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800761 }
762 return;
763
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700764 case CAMERA_MSG_ERROR :
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 Log.e(TAG, "Error " + msg.arg1);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700766 if (mErrorCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 mErrorCallback.onError(msg.arg1, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 return;
770
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800771 case CAMERA_MSG_FOCUS_MOVE:
772 if (mAutoFocusMoveCallback != null) {
773 mAutoFocusMoveCallback.onAutoFocusMoving(msg.arg1 == 0 ? false : true, mCamera);
774 }
775 return;
776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 default:
778 Log.e(TAG, "Unknown message type " + msg.what);
779 return;
780 }
781 }
782 }
783
784 private static void postEventFromNative(Object camera_ref,
785 int what, int arg1, int arg2, Object obj)
786 {
787 Camera c = (Camera)((WeakReference)camera_ref).get();
788 if (c == null)
789 return;
790
791 if (c.mEventHandler != null) {
792 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj);
793 c.mEventHandler.sendMessage(m);
794 }
795 }
796
797 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700798 * Callback interface used to notify on completion of camera auto focus.
799 *
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800800 * <p>Devices that do not support auto-focus will receive a "fake"
801 * callback to this interface. If your application needs auto-focus and
Scott Maindf4578e2009-09-10 12:22:07 -0700802 * should not be installed on devices <em>without</em> auto-focus, you must
803 * declare that your app uses the
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800804 * {@code android.hardware.camera.autofocus} feature, in the
Scott Maindf4578e2009-09-10 12:22:07 -0700805 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
806 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700807 *
808 * @see #autoFocus(AutoFocusCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 */
810 public interface AutoFocusCallback
811 {
812 /**
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800813 * Called when the camera auto focus completes. If the camera
814 * does not support auto-focus and autoFocus is called,
815 * onAutoFocus will be called immediately with a fake value of
816 * <code>success</code> set to <code>true</code>.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800817 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +0800818 * The auto-focus routine does not lock auto-exposure and auto-white
819 * balance after it completes.
Eino-Ville Talvala83d33522011-05-13 10:25:00 -0700820 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 * @param success true if focus was successful, false if otherwise
822 * @param camera the Camera service object
Eino-Ville Talvala83d33522011-05-13 10:25:00 -0700823 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
824 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 */
826 void onAutoFocus(boolean success, Camera camera);
Wu-cheng Li53b30912011-10-12 19:43:51 +0800827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
829 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700830 * Starts camera auto-focus and registers a callback function to run when
831 * the camera is focused. This method is only valid when preview is active
832 * (between {@link #startPreview()} and before {@link #stopPreview()}).
833 *
834 * <p>Callers should check
835 * {@link android.hardware.Camera.Parameters#getFocusMode()} to determine if
836 * this method should be called. If the camera does not support auto-focus,
837 * it is a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)}
Wu-cheng Li36322db2009-09-18 18:59:21 +0800838 * callback will be called immediately.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700839 *
Scott Mainda0a56d2009-09-10 18:08:37 -0700840 * <p>If your application should not be installed
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800841 * on devices without auto-focus, you must declare that your application
842 * uses auto-focus with the
Scott Maindf4578e2009-09-10 12:22:07 -0700843 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
844 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700845 *
Wu-cheng Li068ef422009-09-27 13:19:36 -0700846 * <p>If the current flash mode is not
847 * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700848 * fired during auto-focus, depending on the driver and camera hardware.<p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800849 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800850 * <p>Auto-exposure lock {@link android.hardware.Camera.Parameters#getAutoExposureLock()}
Wu-cheng Lib4f95be2011-09-22 11:43:28 +0800851 * and auto-white balance locks {@link android.hardware.Camera.Parameters#getAutoWhiteBalanceLock()}
852 * do not change during and after autofocus. But auto-focus routine may stop
853 * auto-exposure and auto-white balance transiently during focusing.
Eino-Ville Talvala83d33522011-05-13 10:25:00 -0700854 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800855 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
856 * image capture with {@link #takePicture(Camera.ShutterCallback,
857 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
858 * the focus position. Applications must call cancelAutoFocus to reset the
859 * focus.</p>
860 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800861 * <p>If autofocus is successful, consider using
862 * {@link android.media.MediaActionSound} to properly play back an autofocus
863 * success sound to the user.</p>
864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 * @param cb the callback to run
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700866 * @see #cancelAutoFocus()
Eino-Ville Talvala83d33522011-05-13 10:25:00 -0700867 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
868 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800869 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 */
871 public final void autoFocus(AutoFocusCallback cb)
872 {
873 mAutoFocusCallback = cb;
874 native_autoFocus();
875 }
876 private native final void native_autoFocus();
877
878 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700879 * Cancels any auto-focus function in progress.
880 * Whether or not auto-focus is currently in progress,
881 * this function will return the focus position to the default.
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800882 * If the camera does not support auto-focus, this is a no-op.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700883 *
884 * @see #autoFocus(Camera.AutoFocusCallback)
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800885 */
886 public final void cancelAutoFocus()
887 {
888 mAutoFocusCallback = null;
889 native_cancelAutoFocus();
890 }
891 private native final void native_cancelAutoFocus();
892
893 /**
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800894 * Callback interface used to notify on auto focus start and stop.
895 *
896 * <p>This is useful for continuous autofocus -- {@link Parameters#FOCUS_MODE_CONTINUOUS_VIDEO}
897 * and {@link Parameters#FOCUS_MODE_CONTINUOUS_PICTURE}. Applications can
898 * show autofocus animation.</p>
899 *
900 * @hide
901 */
902 public interface AutoFocusMoveCallback
903 {
904 /**
905 * Called when the camera auto focus starts or stops.
906 *
907 * @param start true if focus starts to move, false if focus stops to move
908 * @param camera the Camera service object
909 */
910 void onAutoFocusMoving(boolean start, Camera camera);
911 }
912
913 /**
914 * Sets camera auto-focus move callback.
915 *
916 * @param cb the callback to run
917 * @hide
918 */
919 public void setAutoFocusMoveCallback(AutoFocusMoveCallback cb) {
920 mAutoFocusMoveCallback = cb;
921 enableFocusMoveCallback((mAutoFocusMoveCallback != null) ? 1 : 0);
922 }
923
924 private native void enableFocusMoveCallback(int enable);
925
926 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700927 * Callback interface used to signal the moment of actual image capture.
928 *
929 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 */
931 public interface ShutterCallback
932 {
933 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700934 * Called as near as possible to the moment when a photo is captured
935 * from the sensor. This is a good opportunity to play a shutter sound
936 * or give other feedback of camera operation. This may be some time
937 * after the photo was triggered, but some time before the actual data
938 * is available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 */
940 void onShutter();
941 }
942
943 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700944 * Callback interface used to supply image data from a photo capture.
945 *
946 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 */
948 public interface PictureCallback {
949 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700950 * Called when image data is available after a picture is taken.
951 * The format of the data depends on the context of the callback
952 * and {@link Camera.Parameters} settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800953 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 * @param data a byte array of the picture data
955 * @param camera the Camera service object
956 */
957 void onPictureTaken(byte[] data, Camera camera);
958 };
959
960 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700961 * Equivalent to takePicture(shutter, raw, null, jpeg).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800962 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700963 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 */
965 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
966 PictureCallback jpeg) {
Dave Sparkse8b26e12009-07-14 10:35:40 -0700967 takePicture(shutter, raw, null, jpeg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 }
James Donge00cab72011-02-17 16:38:06 -0800969 private native final void native_takePicture(int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970
Dave Sparkse8b26e12009-07-14 10:35:40 -0700971 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800972 * Triggers an asynchronous image capture. The camera service will initiate
973 * a series of callbacks to the application as the image capture progresses.
974 * The shutter callback occurs after the image is captured. This can be used
975 * to trigger a sound to let the user know that image has been captured. The
976 * raw callback occurs when the raw image data is available (NOTE: the data
James Donge00cab72011-02-17 16:38:06 -0800977 * will be null if there is no raw image callback buffer available or the
978 * raw image callback buffer is not large enough to hold the raw image).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800979 * The postview callback occurs when a scaled, fully processed postview
980 * image is available (NOTE: not all hardware supports this). The jpeg
981 * callback occurs when the compressed image is available. If the
982 * application does not need a particular callback, a null can be passed
983 * instead of a callback method.
Dave Sparkse8b26e12009-07-14 10:35:40 -0700984 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700985 * <p>This method is only valid when preview is active (after
986 * {@link #startPreview()}). Preview will be stopped after the image is
987 * taken; callers must call {@link #startPreview()} again if they want to
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800988 * re-start preview or take more pictures. This should not be called between
989 * {@link android.media.MediaRecorder#start()} and
990 * {@link android.media.MediaRecorder#stop()}.
Wu-cheng Li40057ce2009-12-02 18:57:29 +0800991 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700992 * <p>After calling this method, you must not call {@link #startPreview()}
993 * or take another picture until the JPEG callback has returned.
994 *
995 * @param shutter the callback for image capture moment, or null
996 * @param raw the callback for raw (uncompressed) image data, or null
Dave Sparkse8b26e12009-07-14 10:35:40 -0700997 * @param postview callback with postview image data, may be null
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700998 * @param jpeg the callback for JPEG image data, or null
Dave Sparkse8b26e12009-07-14 10:35:40 -0700999 */
1000 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
1001 PictureCallback postview, PictureCallback jpeg) {
1002 mShutterCallback = shutter;
1003 mRawImageCallback = raw;
1004 mPostviewCallback = postview;
1005 mJpegCallback = jpeg;
James Donge00cab72011-02-17 16:38:06 -08001006
1007 // If callback is not set, do not send me callbacks.
1008 int msgType = 0;
1009 if (mShutterCallback != null) {
1010 msgType |= CAMERA_MSG_SHUTTER;
1011 }
1012 if (mRawImageCallback != null) {
1013 msgType |= CAMERA_MSG_RAW_IMAGE;
1014 }
1015 if (mPostviewCallback != null) {
1016 msgType |= CAMERA_MSG_POSTVIEW_FRAME;
1017 }
1018 if (mJpegCallback != null) {
1019 msgType |= CAMERA_MSG_COMPRESSED_IMAGE;
1020 }
1021
1022 native_takePicture(msgType);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001023 }
1024
1025 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001026 * Zooms to the requested value smoothly. The driver will notify {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001027 * OnZoomChangeListener} of the zoom value and whether zoom is stopped at
1028 * the time. For example, suppose the current zoom is 0 and startSmoothZoom
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001029 * is called with value 3. The
1030 * {@link Camera.OnZoomChangeListener#onZoomChange(int, boolean, Camera)}
1031 * method will be called three times with zoom values 1, 2, and 3.
1032 * Applications can call {@link #stopSmoothZoom} to stop the zoom earlier.
1033 * Applications should not call startSmoothZoom again or change the zoom
1034 * value before zoom stops. If the supplied zoom value equals to the current
1035 * zoom value, no zoom callback will be generated. This method is supported
1036 * if {@link android.hardware.Camera.Parameters#isSmoothZoomSupported}
1037 * returns true.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001038 *
1039 * @param value zoom value. The valid range is 0 to {@link
1040 * android.hardware.Camera.Parameters#getMaxZoom}.
Wu-cheng Li0ca25192010-03-29 16:21:12 +08001041 * @throws IllegalArgumentException if the zoom value is invalid.
1042 * @throws RuntimeException if the method fails.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001043 * @see #setZoomChangeListener(OnZoomChangeListener)
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001044 */
1045 public native final void startSmoothZoom(int value);
1046
1047 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001048 * Stops the smooth zoom. Applications should wait for the {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001049 * OnZoomChangeListener} to know when the zoom is actually stopped. This
1050 * method is supported if {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001051 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true.
Wu-cheng Li0ca25192010-03-29 16:21:12 +08001052 *
1053 * @throws RuntimeException if the method fails.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001054 */
1055 public native final void stopSmoothZoom();
1056
1057 /**
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001058 * Set the clockwise rotation of preview display in degrees. This affects
1059 * the preview frames and the picture displayed after snapshot. This method
1060 * is useful for portrait mode applications. Note that preview display of
Wu-cheng Lib982fb42010-10-19 17:19:09 +08001061 * front-facing cameras is flipped horizontally before the rotation, that
1062 * is, the image is reflected along the central vertical axis of the camera
1063 * sensor. So the users can see themselves as looking into a mirror.
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001064 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001065 * <p>This does not affect the order of byte array passed in {@link
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001066 * PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This
1067 * method is not allowed to be called during preview.
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001068 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001069 * <p>If you want to make the camera image show in the same orientation as
1070 * the display, you can use the following code.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001071 * <pre>
Chih-Chung Chang724c5222010-06-14 18:57:15 +08001072 * public static void setCameraDisplayOrientation(Activity activity,
1073 * int cameraId, android.hardware.Camera camera) {
1074 * android.hardware.Camera.CameraInfo info =
1075 * new android.hardware.Camera.CameraInfo();
1076 * android.hardware.Camera.getCameraInfo(cameraId, info);
1077 * int rotation = activity.getWindowManager().getDefaultDisplay()
1078 * .getRotation();
1079 * int degrees = 0;
1080 * switch (rotation) {
1081 * case Surface.ROTATION_0: degrees = 0; break;
1082 * case Surface.ROTATION_90: degrees = 90; break;
1083 * case Surface.ROTATION_180: degrees = 180; break;
1084 * case Surface.ROTATION_270: degrees = 270; break;
1085 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001086 *
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001087 * int result;
1088 * if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
1089 * result = (info.orientation + degrees) % 360;
1090 * result = (360 - result) % 360; // compensate the mirror
1091 * } else { // back-facing
1092 * result = (info.orientation - degrees + 360) % 360;
1093 * }
Chih-Chung Chang724c5222010-06-14 18:57:15 +08001094 * camera.setDisplayOrientation(result);
1095 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001096 * </pre>
Wu-cheng Lid3033622011-10-07 13:13:54 +08001097 *
1098 * <p>Starting from API level 14, this method can be called when preview is
1099 * active.
1100 *
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001101 * @param degrees the angle that the picture will be rotated clockwise.
Chih-Chung Change7bd22a2010-01-27 10:24:42 -08001102 * Valid values are 0, 90, 180, and 270. The starting
1103 * position is 0 (landscape).
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001104 * @see #setPreviewDisplay(SurfaceHolder)
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001105 */
1106 public native final void setDisplayOrientation(int degrees);
1107
1108 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001109 * Callback interface for zoom changes during a smooth zoom operation.
1110 *
1111 * @see #setZoomChangeListener(OnZoomChangeListener)
1112 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001113 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001114 public interface OnZoomChangeListener
Dave Sparkse8b26e12009-07-14 10:35:40 -07001115 {
1116 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001117 * Called when the zoom value has changed during a smooth zoom.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001118 *
1119 * @param zoomValue the current zoom value. In smooth zoom mode, camera
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001120 * calls this for every new zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001121 * @param stopped whether smooth zoom is stopped. If the value is true,
1122 * this is the last zoom update for the application.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001123 * @param camera the Camera service object
1124 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001125 void onZoomChange(int zoomValue, boolean stopped, Camera camera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001126 };
1127
1128 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001129 * Registers a listener to be notified when the zoom value is updated by the
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001130 * camera driver during smooth zoom.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001131 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001132 * @param listener the listener to notify
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001133 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001134 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001135 public final void setZoomChangeListener(OnZoomChangeListener listener)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001136 {
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001137 mZoomListener = listener;
Dave Sparkse8b26e12009-07-14 10:35:40 -07001138 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001139
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001140 /**
1141 * Callback interface for face detected in the preview frame.
1142 *
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001143 */
1144 public interface FaceDetectionListener
1145 {
1146 /**
1147 * Notify the listener of the detected faces in the preview frame.
1148 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001149 * @param faces The detected faces in a list
Joe Fernandez464cb212011-10-04 16:56:47 -07001150 * @param camera The {@link Camera} service object
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001151 */
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001152 void onFaceDetection(Face[] faces, Camera camera);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001153 }
1154
1155 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001156 * Registers a listener to be notified about the faces detected in the
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001157 * preview frame.
1158 *
1159 * @param listener the listener to notify
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001160 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001161 */
1162 public final void setFaceDetectionListener(FaceDetectionListener listener)
1163 {
1164 mFaceListener = listener;
1165 }
1166
1167 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001168 * Starts the face detection. This should be called after preview is started.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001169 * The camera will notify {@link FaceDetectionListener} of the detected
1170 * faces in the preview frame. The detected faces may be the same as the
1171 * previous ones. Applications should call {@link #stopFaceDetection} to
1172 * stop the face detection. This method is supported if {@link
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001173 * Parameters#getMaxNumDetectedFaces()} returns a number larger than 0.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001174 * If the face detection has started, apps should not call this again.
1175 *
Wu-cheng Li8c136702011-08-12 20:25:00 +08001176 * <p>When the face detection is running, {@link Parameters#setWhiteBalance(String)},
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001177 * {@link Parameters#setFocusAreas(List)}, and {@link Parameters#setMeteringAreas(List)}
Wu-cheng Li8c136702011-08-12 20:25:00 +08001178 * have no effect. The camera uses the detected faces to do auto-white balance,
1179 * auto exposure, and autofocus.
1180 *
1181 * <p>If the apps call {@link #autoFocus(AutoFocusCallback)}, the camera
1182 * will stop sending face callbacks. The last face callback indicates the
1183 * areas used to do autofocus. After focus completes, face detection will
1184 * resume sending face callbacks. If the apps call {@link
1185 * #cancelAutoFocus()}, the face callbacks will also resume.</p>
1186 *
1187 * <p>After calling {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
1188 * Camera.PictureCallback)} or {@link #stopPreview()}, and then resuming
1189 * preview with {@link #startPreview()}, the apps should call this method
1190 * again to resume face detection.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001191 *
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001192 * @throws IllegalArgumentException if the face detection is unsupported.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001193 * @throws RuntimeException if the method fails or the face detection is
1194 * already running.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001195 * @see FaceDetectionListener
1196 * @see #stopFaceDetection()
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001197 * @see Parameters#getMaxNumDetectedFaces()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001198 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001199 public final void startFaceDetection() {
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001200 if (mFaceDetectionRunning) {
1201 throw new RuntimeException("Face detection is already running");
1202 }
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001203 _startFaceDetection(CAMERA_FACE_DETECTION_HW);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001204 mFaceDetectionRunning = true;
1205 }
1206
1207 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001208 * Stops the face detection.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001209 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001210 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001211 */
1212 public final void stopFaceDetection() {
1213 _stopFaceDetection();
1214 mFaceDetectionRunning = false;
1215 }
1216
1217 private native final void _startFaceDetection(int type);
1218 private native final void _stopFaceDetection();
1219
1220 /**
Joe Fernandez464cb212011-10-04 16:56:47 -07001221 * Information about a face identified through camera face detection.
Wu-cheng Li53b30912011-10-12 19:43:51 +08001222 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001223 * <p>When face detection is used with a camera, the {@link FaceDetectionListener} returns a
1224 * list of face objects for use in focusing and metering.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001225 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001226 * @see FaceDetectionListener
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001227 */
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001228 public static class Face {
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001229 /**
1230 * Create an empty face.
1231 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001232 public Face() {
1233 }
1234
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001235 /**
1236 * Bounds of the face. (-1000, -1000) represents the top-left of the
1237 * camera field of view, and (1000, 1000) represents the bottom-right of
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001238 * the field of view. For example, suppose the size of the viewfinder UI
1239 * is 800x480. The rect passed from the driver is (-1000, -1000, 0, 0).
Wu-cheng Li8c136702011-08-12 20:25:00 +08001240 * The corresponding viewfinder rect should be (0, 0, 400, 240). It is
1241 * guaranteed left < right and top < bottom. The coordinates can be
1242 * smaller than -1000 or bigger than 1000. But at least one vertex will
1243 * be within (-1000, -1000) and (1000, 1000).
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001244 *
1245 * <p>The direction is relative to the sensor orientation, that is, what
1246 * the sensor sees. The direction is not affected by the rotation or
Wu-cheng Li8c136702011-08-12 20:25:00 +08001247 * mirroring of {@link #setDisplayOrientation(int)}. The face bounding
1248 * rectangle does not provide any information about face orientation.</p>
1249 *
1250 * <p>Here is the matrix to convert driver coordinates to View coordinates
1251 * in pixels.</p>
1252 * <pre>
1253 * Matrix matrix = new Matrix();
1254 * CameraInfo info = CameraHolder.instance().getCameraInfo()[cameraId];
1255 * // Need mirror for front camera.
1256 * boolean mirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
1257 * matrix.setScale(mirror ? -1 : 1, 1);
1258 * // This is the value for android.hardware.Camera.setDisplayOrientation.
1259 * matrix.postRotate(displayOrientation);
1260 * // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
1261 * // UI coordinates range from (0, 0) to (width, height).
1262 * matrix.postScale(view.getWidth() / 2000f, view.getHeight() / 2000f);
1263 * matrix.postTranslate(view.getWidth() / 2f, view.getHeight() / 2f);
1264 * </pre>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001265 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001266 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001267 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001268 public Rect rect;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001269
1270 /**
Joe Fernandez464cb212011-10-04 16:56:47 -07001271 * The confidence level for the detection of the face. The range is 1 to 100. 100 is the
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001272 * highest confidence.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001273 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001274 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001275 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001276 public int score;
Wei Huad52b3082011-08-19 13:01:51 -07001277
1278 /**
1279 * An unique id per face while the face is visible to the tracker. If
1280 * the face leaves the field-of-view and comes back, it will get a new
1281 * id. This is an optional field, may not be supported on all devices.
1282 * If not supported, id will always be set to -1. The optional fields
1283 * are supported as a set. Either they are all valid, or none of them
1284 * are.
1285 */
1286 public int id = -1;
1287
1288 /**
1289 * The coordinates of the center of the left eye. The coordinates are in
1290 * the same space as the ones for {@link #rect}. This is an optional
1291 * field, may not be supported on all devices. If not supported, the
1292 * value will always be set to null. The optional fields are supported
1293 * as a set. Either they are all valid, or none of them are.
1294 */
1295 public Point leftEye = null;
1296
1297 /**
1298 * The coordinates of the center of the right eye. The coordinates are
1299 * in the same space as the ones for {@link #rect}.This is an optional
1300 * field, may not be supported on all devices. If not supported, the
1301 * value will always be set to null. The optional fields are supported
1302 * as a set. Either they are all valid, or none of them are.
1303 */
1304 public Point rightEye = null;
1305
1306 /**
1307 * The coordinates of the center of the mouth. The coordinates are in
1308 * the same space as the ones for {@link #rect}. This is an optional
1309 * field, may not be supported on all devices. If not supported, the
1310 * value will always be set to null. The optional fields are supported
1311 * as a set. Either they are all valid, or none of them are.
1312 */
1313 public Point mouth = null;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001314 }
1315
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001316 // Error codes match the enum in include/ui/Camera.h
1317
1318 /**
1319 * Unspecified camera error.
1320 * @see Camera.ErrorCallback
1321 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 public static final int CAMERA_ERROR_UNKNOWN = 1;
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001323
1324 /**
1325 * Media server died. In this case, the application must release the
1326 * Camera object and instantiate a new one.
1327 * @see Camera.ErrorCallback
1328 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 public static final int CAMERA_ERROR_SERVER_DIED = 100;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001332 * Callback interface for camera error notification.
1333 *
1334 * @see #setErrorCallback(ErrorCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 */
1336 public interface ErrorCallback
1337 {
1338 /**
1339 * Callback for camera errors.
1340 * @param error error code:
1341 * <ul>
1342 * <li>{@link #CAMERA_ERROR_UNKNOWN}
1343 * <li>{@link #CAMERA_ERROR_SERVER_DIED}
1344 * </ul>
1345 * @param camera the Camera service object
1346 */
1347 void onError(int error, Camera camera);
1348 };
1349
1350 /**
1351 * Registers a callback to be invoked when an error occurs.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001352 * @param cb The callback to run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 */
1354 public final void setErrorCallback(ErrorCallback cb)
1355 {
1356 mErrorCallback = cb;
1357 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 private native final void native_setParameters(String params);
1360 private native final String native_getParameters();
1361
1362 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001363 * Changes the settings for this Camera service.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001364 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 * @param params the Parameters to use for this Camera service
Wu-cheng Li99a3f3e2010-11-19 15:56:16 +08001366 * @throws RuntimeException if any parameter is invalid or not supported.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001367 * @see #getParameters()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 */
1369 public void setParameters(Parameters params) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 native_setParameters(params.flatten());
1371 }
1372
1373 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001374 * Returns the current settings for this Camera service.
1375 * If modifications are made to the returned Parameters, they must be passed
1376 * to {@link #setParameters(Camera.Parameters)} to take effect.
1377 *
1378 * @see #setParameters(Camera.Parameters)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 */
1380 public Parameters getParameters() {
1381 Parameters p = new Parameters();
1382 String s = native_getParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 p.unflatten(s);
1384 return p;
1385 }
1386
1387 /**
Wu-cheng Li1c04a332011-11-22 18:21:18 +08001388 * Returns an empty {@link Parameters} for testing purpose.
1389 *
Ken Wakasaf76a50c2012-03-09 19:56:35 +09001390 * @return a Parameter object.
Wu-cheng Li1c04a332011-11-22 18:21:18 +08001391 *
1392 * @hide
1393 */
1394 public static Parameters getEmptyParameters() {
1395 Camera camera = new Camera();
1396 return camera.new Parameters();
1397 }
1398
1399 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001400 * Image size (width and height dimensions).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 */
1402 public class Size {
1403 /**
1404 * Sets the dimensions for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001405 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 * @param w the photo width (pixels)
1407 * @param h the photo height (pixels)
1408 */
1409 public Size(int w, int h) {
1410 width = w;
1411 height = h;
1412 }
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08001413 /**
1414 * Compares {@code obj} to this size.
1415 *
1416 * @param obj the object to compare this size with.
1417 * @return {@code true} if the width and height of {@code obj} is the
1418 * same as those of this size. {@code false} otherwise.
1419 */
1420 @Override
1421 public boolean equals(Object obj) {
1422 if (!(obj instanceof Size)) {
1423 return false;
1424 }
1425 Size s = (Size) obj;
1426 return width == s.width && height == s.height;
1427 }
1428 @Override
1429 public int hashCode() {
1430 return width * 32713 + height;
1431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 /** width of the picture */
1433 public int width;
1434 /** height of the picture */
1435 public int height;
1436 };
1437
1438 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07001439 * <p>The Area class is used for choosing specific metering and focus areas for
1440 * the camera to use when calculating auto-exposure, auto-white balance, and
1441 * auto-focus.</p>
1442 *
1443 * <p>To find out how many simultaneous areas a given camera supports, use
1444 * {@link Parameters#getMaxNumMeteringAreas()} and
1445 * {@link Parameters#getMaxNumFocusAreas()}. If metering or focusing area
1446 * selection is unsupported, these methods will return 0.</p>
1447 *
1448 * <p>Each Area consists of a rectangle specifying its bounds, and a weight
1449 * that determines its importance. The bounds are relative to the camera's
1450 * current field of view. The coordinates are mapped so that (-1000, -1000)
1451 * is always the top-left corner of the current field of view, and (1000,
1452 * 1000) is always the bottom-right corner of the current field of
1453 * view. Setting Areas with bounds outside that range is not allowed. Areas
1454 * with zero or negative width or height are not allowed.</p>
1455 *
1456 * <p>The weight must range from 1 to 1000, and represents a weight for
1457 * every pixel in the area. This means that a large metering area with
1458 * the same weight as a smaller area will have more effect in the
1459 * metering result. Metering areas can overlap and the driver
1460 * will add the weights in the overlap region.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08001461 *
Wu-cheng Libde61a52011-06-07 18:23:14 +08001462 * @see Parameters#setFocusAreas(List)
1463 * @see Parameters#getFocusAreas()
1464 * @see Parameters#getMaxNumFocusAreas()
1465 * @see Parameters#setMeteringAreas(List)
1466 * @see Parameters#getMeteringAreas()
1467 * @see Parameters#getMaxNumMeteringAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08001468 */
1469 public static class Area {
1470 /**
1471 * Create an area with specified rectangle and weight.
1472 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07001473 * @param rect the bounds of the area.
1474 * @param weight the weight of the area.
Wu-cheng Li30771b72011-04-02 06:19:46 +08001475 */
1476 public Area(Rect rect, int weight) {
1477 this.rect = rect;
1478 this.weight = weight;
1479 }
1480 /**
1481 * Compares {@code obj} to this area.
1482 *
1483 * @param obj the object to compare this area with.
1484 * @return {@code true} if the rectangle and weight of {@code obj} is
1485 * the same as those of this area. {@code false} otherwise.
1486 */
1487 @Override
1488 public boolean equals(Object obj) {
1489 if (!(obj instanceof Area)) {
1490 return false;
1491 }
1492 Area a = (Area) obj;
1493 if (rect == null) {
1494 if (a.rect != null) return false;
1495 } else {
1496 if (!rect.equals(a.rect)) return false;
1497 }
1498 return weight == a.weight;
1499 }
1500
Wu-cheng Libde61a52011-06-07 18:23:14 +08001501 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07001502 * Bounds of the area. (-1000, -1000) represents the top-left of the
1503 * camera field of view, and (1000, 1000) represents the bottom-right of
1504 * the field of view. Setting bounds outside that range is not
1505 * allowed. Bounds with zero or negative width or height are not
1506 * allowed.
Wu-cheng Libde61a52011-06-07 18:23:14 +08001507 *
1508 * @see Parameters#getFocusAreas()
1509 * @see Parameters#getMeteringAreas()
1510 */
Wu-cheng Li30771b72011-04-02 06:19:46 +08001511 public Rect rect;
1512
Wu-cheng Libde61a52011-06-07 18:23:14 +08001513 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07001514 * Weight of the area. The weight must range from 1 to 1000, and
1515 * represents a weight for every pixel in the area. This means that a
1516 * large metering area with the same weight as a smaller area will have
1517 * more effect in the metering result. Metering areas can overlap and
1518 * the driver will add the weights in the overlap region.
Wu-cheng Libde61a52011-06-07 18:23:14 +08001519 *
1520 * @see Parameters#getFocusAreas()
1521 * @see Parameters#getMeteringAreas()
1522 */
Wu-cheng Li30771b72011-04-02 06:19:46 +08001523 public int weight;
Wu-cheng Libde61a52011-06-07 18:23:14 +08001524 }
Wu-cheng Li30771b72011-04-02 06:19:46 +08001525
1526 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001527 * Camera service settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001528 *
1529 * <p>To make camera parameters take effect, applications have to call
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001530 * {@link Camera#setParameters(Camera.Parameters)}. For example, after
1531 * {@link Camera.Parameters#setWhiteBalance} is called, white balance is not
1532 * actually changed until {@link Camera#setParameters(Camera.Parameters)}
1533 * is called with the changed parameters object.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001534 *
1535 * <p>Different devices may have different camera capabilities, such as
1536 * picture size or flash modes. The application should query the camera
1537 * capabilities before setting parameters. For example, the application
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001538 * should call {@link Camera.Parameters#getSupportedColorEffects()} before
1539 * calling {@link Camera.Parameters#setColorEffect(String)}. If the
1540 * camera does not support color effects,
1541 * {@link Camera.Parameters#getSupportedColorEffects()} will return null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 */
1543 public class Parameters {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001544 // Parameter keys to communicate with the camera driver.
1545 private static final String KEY_PREVIEW_SIZE = "preview-size";
1546 private static final String KEY_PREVIEW_FORMAT = "preview-format";
1547 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate";
Wu-cheng Li454630f2010-08-11 16:48:05 -07001548 private static final String KEY_PREVIEW_FPS_RANGE = "preview-fps-range";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001549 private static final String KEY_PICTURE_SIZE = "picture-size";
1550 private static final String KEY_PICTURE_FORMAT = "picture-format";
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08001551 private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001552 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width";
1553 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height";
1554 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality";
1555 private static final String KEY_JPEG_QUALITY = "jpeg-quality";
1556 private static final String KEY_ROTATION = "rotation";
1557 private static final String KEY_GPS_LATITUDE = "gps-latitude";
1558 private static final String KEY_GPS_LONGITUDE = "gps-longitude";
1559 private static final String KEY_GPS_ALTITUDE = "gps-altitude";
1560 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp";
Ray Chen055c9862010-02-23 10:45:42 +08001561 private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001562 private static final String KEY_WHITE_BALANCE = "whitebalance";
1563 private static final String KEY_EFFECT = "effect";
1564 private static final String KEY_ANTIBANDING = "antibanding";
1565 private static final String KEY_SCENE_MODE = "scene-mode";
1566 private static final String KEY_FLASH_MODE = "flash-mode";
Wu-cheng Li36322db2009-09-18 18:59:21 +08001567 private static final String KEY_FOCUS_MODE = "focus-mode";
Wu-cheng Li30771b72011-04-02 06:19:46 +08001568 private static final String KEY_FOCUS_AREAS = "focus-areas";
1569 private static final String KEY_MAX_NUM_FOCUS_AREAS = "max-num-focus-areas";
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08001570 private static final String KEY_FOCAL_LENGTH = "focal-length";
1571 private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle";
1572 private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle";
Wu-cheng Liff723b62010-02-09 13:38:19 +08001573 private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation";
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001574 private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation";
1575 private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation";
1576 private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step";
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07001577 private static final String KEY_AUTO_EXPOSURE_LOCK = "auto-exposure-lock";
1578 private static final String KEY_AUTO_EXPOSURE_LOCK_SUPPORTED = "auto-exposure-lock-supported";
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07001579 private static final String KEY_AUTO_WHITEBALANCE_LOCK = "auto-whitebalance-lock";
1580 private static final String KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED = "auto-whitebalance-lock-supported";
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08001581 private static final String KEY_METERING_AREAS = "metering-areas";
1582 private static final String KEY_MAX_NUM_METERING_AREAS = "max-num-metering-areas";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001583 private static final String KEY_ZOOM = "zoom";
1584 private static final String KEY_MAX_ZOOM = "max-zoom";
1585 private static final String KEY_ZOOM_RATIOS = "zoom-ratios";
1586 private static final String KEY_ZOOM_SUPPORTED = "zoom-supported";
1587 private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08001588 private static final String KEY_FOCUS_DISTANCES = "focus-distances";
James Dongdd0b16c2010-09-21 16:23:48 -07001589 private static final String KEY_VIDEO_SIZE = "video-size";
1590 private static final String KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO =
1591 "preferred-preview-size-for-video";
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001592 private static final String KEY_MAX_NUM_DETECTED_FACES_HW = "max-num-detected-faces-hw";
1593 private static final String KEY_MAX_NUM_DETECTED_FACES_SW = "max-num-detected-faces-sw";
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08001594 private static final String KEY_RECORDING_HINT = "recording-hint";
Wu-cheng Li98bb2512011-08-30 21:33:10 +08001595 private static final String KEY_VIDEO_SNAPSHOT_SUPPORTED = "video-snapshot-supported";
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07001596 private static final String KEY_VIDEO_STABILIZATION = "video-stabilization";
1597 private static final String KEY_VIDEO_STABILIZATION_SUPPORTED = "video-stabilization-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08001598
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001599 // Parameter key suffix for supported values.
1600 private static final String SUPPORTED_VALUES_SUFFIX = "-values";
1601
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001602 private static final String TRUE = "true";
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07001603 private static final String FALSE = "false";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001604
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001605 // Values for white balance settings.
1606 public static final String WHITE_BALANCE_AUTO = "auto";
1607 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent";
1608 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent";
1609 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent";
1610 public static final String WHITE_BALANCE_DAYLIGHT = "daylight";
1611 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight";
1612 public static final String WHITE_BALANCE_TWILIGHT = "twilight";
1613 public static final String WHITE_BALANCE_SHADE = "shade";
1614
1615 // Values for color effect settings.
1616 public static final String EFFECT_NONE = "none";
1617 public static final String EFFECT_MONO = "mono";
1618 public static final String EFFECT_NEGATIVE = "negative";
1619 public static final String EFFECT_SOLARIZE = "solarize";
1620 public static final String EFFECT_SEPIA = "sepia";
1621 public static final String EFFECT_POSTERIZE = "posterize";
1622 public static final String EFFECT_WHITEBOARD = "whiteboard";
1623 public static final String EFFECT_BLACKBOARD = "blackboard";
1624 public static final String EFFECT_AQUA = "aqua";
1625
1626 // Values for antibanding settings.
1627 public static final String ANTIBANDING_AUTO = "auto";
1628 public static final String ANTIBANDING_50HZ = "50hz";
1629 public static final String ANTIBANDING_60HZ = "60hz";
1630 public static final String ANTIBANDING_OFF = "off";
1631
1632 // Values for flash mode settings.
1633 /**
1634 * Flash will not be fired.
1635 */
1636 public static final String FLASH_MODE_OFF = "off";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001637
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001638 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07001639 * Flash will be fired automatically when required. The flash may be fired
1640 * during preview, auto-focus, or snapshot depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001641 */
1642 public static final String FLASH_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001643
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001644 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07001645 * Flash will always be fired during snapshot. The flash may also be
1646 * fired during preview or auto-focus depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001647 */
1648 public static final String FLASH_MODE_ON = "on";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001649
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001650 /**
1651 * Flash will be fired in red-eye reduction mode.
1652 */
1653 public static final String FLASH_MODE_RED_EYE = "red-eye";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001654
Wu-cheng Li36322db2009-09-18 18:59:21 +08001655 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07001656 * Constant emission of light during preview, auto-focus and snapshot.
1657 * This can also be used for video recording.
Wu-cheng Li36322db2009-09-18 18:59:21 +08001658 */
Wu-cheng Li068ef422009-09-27 13:19:36 -07001659 public static final String FLASH_MODE_TORCH = "torch";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001660
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001661 /**
1662 * Scene mode is off.
1663 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001664 public static final String SCENE_MODE_AUTO = "auto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001665
1666 /**
1667 * Take photos of fast moving objects. Same as {@link
1668 * #SCENE_MODE_SPORTS}.
1669 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001670 public static final String SCENE_MODE_ACTION = "action";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001671
1672 /**
1673 * Take people pictures.
1674 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001675 public static final String SCENE_MODE_PORTRAIT = "portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001676
1677 /**
1678 * Take pictures on distant objects.
1679 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001680 public static final String SCENE_MODE_LANDSCAPE = "landscape";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001681
1682 /**
1683 * Take photos at night.
1684 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001685 public static final String SCENE_MODE_NIGHT = "night";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001686
1687 /**
1688 * Take people pictures at night.
1689 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001690 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001691
1692 /**
1693 * Take photos in a theater. Flash light is off.
1694 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001695 public static final String SCENE_MODE_THEATRE = "theatre";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001696
1697 /**
1698 * Take pictures on the beach.
1699 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001700 public static final String SCENE_MODE_BEACH = "beach";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001701
1702 /**
1703 * Take pictures on the snow.
1704 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001705 public static final String SCENE_MODE_SNOW = "snow";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001706
1707 /**
1708 * Take sunset photos.
1709 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001710 public static final String SCENE_MODE_SUNSET = "sunset";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001711
1712 /**
1713 * Avoid blurry pictures (for example, due to hand shake).
1714 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001715 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001716
1717 /**
1718 * For shooting firework displays.
1719 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001720 public static final String SCENE_MODE_FIREWORKS = "fireworks";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001721
1722 /**
1723 * Take photos of fast moving objects. Same as {@link
1724 * #SCENE_MODE_ACTION}.
1725 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001726 public static final String SCENE_MODE_SPORTS = "sports";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001727
1728 /**
1729 * Take indoor low-light shot.
1730 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001731 public static final String SCENE_MODE_PARTY = "party";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001732
1733 /**
1734 * Capture the naturally warm color of scenes lit by candles.
1735 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001736 public static final String SCENE_MODE_CANDLELIGHT = "candlelight";
1737
Wu-cheng Lic58b4232010-03-29 17:21:28 +08001738 /**
1739 * Applications are looking for a barcode. Camera driver will be
1740 * optimized for barcode reading.
1741 */
1742 public static final String SCENE_MODE_BARCODE = "barcode";
1743
Wu-cheng Li36322db2009-09-18 18:59:21 +08001744 /**
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07001745 * Auto-focus mode. Applications should call {@link
1746 * #autoFocus(AutoFocusCallback)} to start the focus in this mode.
Wu-cheng Li36322db2009-09-18 18:59:21 +08001747 */
1748 public static final String FOCUS_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001749
Wu-cheng Li36322db2009-09-18 18:59:21 +08001750 /**
1751 * Focus is set at infinity. Applications should not call
1752 * {@link #autoFocus(AutoFocusCallback)} in this mode.
1753 */
1754 public static final String FOCUS_MODE_INFINITY = "infinity";
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07001755
1756 /**
1757 * Macro (close-up) focus mode. Applications should call
1758 * {@link #autoFocus(AutoFocusCallback)} to start the focus in this
1759 * mode.
1760 */
Wu-cheng Li36322db2009-09-18 18:59:21 +08001761 public static final String FOCUS_MODE_MACRO = "macro";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001762
Wu-cheng Li36322db2009-09-18 18:59:21 +08001763 /**
1764 * Focus is fixed. The camera is always in this mode if the focus is not
1765 * adjustable. If the camera has auto-focus, this mode can fix the
1766 * focus, which is usually at hyperfocal distance. Applications should
1767 * not call {@link #autoFocus(AutoFocusCallback)} in this mode.
1768 */
1769 public static final String FOCUS_MODE_FIXED = "fixed";
1770
Wu-cheng Lic58b4232010-03-29 17:21:28 +08001771 /**
1772 * Extended depth of field (EDOF). Focusing is done digitally and
1773 * continuously. Applications should not call {@link
1774 * #autoFocus(AutoFocusCallback)} in this mode.
1775 */
1776 public static final String FOCUS_MODE_EDOF = "edof";
1777
Wu-cheng Li699fe932010-08-05 11:50:25 -07001778 /**
Wu-cheng Lid45cb722010-09-20 16:15:32 -07001779 * Continuous auto focus mode intended for video recording. The camera
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08001780 * continuously tries to focus. This is the best choice for video
1781 * recording because the focus changes smoothly . Applications still can
1782 * call {@link #takePicture(Camera.ShutterCallback,
1783 * Camera.PictureCallback, Camera.PictureCallback)} in this mode but the
1784 * subject may not be in focus. Auto focus starts when the parameter is
Wu-cheng Li53b30912011-10-12 19:43:51 +08001785 * set.
1786 *
1787 * <p>Since API level 14, applications can call {@link
1788 * #autoFocus(AutoFocusCallback)} in this mode. The focus callback will
1789 * immediately return with a boolean that indicates whether the focus is
1790 * sharp or not. The focus position is locked after autoFocus call. If
1791 * applications want to resume the continuous focus, cancelAutoFocus
1792 * must be called. Restarting the preview will not resume the continuous
1793 * autofocus. To stop continuous focus, applications should change the
1794 * focus mode to other modes.
1795 *
1796 * @see #FOCUS_MODE_CONTINUOUS_PICTURE
Wu-cheng Li699fe932010-08-05 11:50:25 -07001797 */
Wu-cheng Lid45cb722010-09-20 16:15:32 -07001798 public static final String FOCUS_MODE_CONTINUOUS_VIDEO = "continuous-video";
Wu-cheng Li699fe932010-08-05 11:50:25 -07001799
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08001800 /**
1801 * Continuous auto focus mode intended for taking pictures. The camera
1802 * continuously tries to focus. The speed of focus change is more
1803 * aggressive than {@link #FOCUS_MODE_CONTINUOUS_VIDEO}. Auto focus
Wu-cheng Li53b30912011-10-12 19:43:51 +08001804 * starts when the parameter is set.
1805 *
Wu-cheng Li0f4f97b2011-10-27 18:07:01 +08001806 * <p>Applications can call {@link #autoFocus(AutoFocusCallback)} in
1807 * this mode. If the autofocus is in the middle of scanning, the focus
1808 * callback will return when it completes. If the autofocus is not
1809 * scanning, the focus callback will immediately return with a boolean
1810 * that indicates whether the focus is sharp or not. The apps can then
1811 * decide if they want to take a picture immediately or to change the
1812 * focus mode to auto, and run a full autofocus cycle. The focus
1813 * position is locked after autoFocus call. If applications want to
1814 * resume the continuous focus, cancelAutoFocus must be called.
1815 * Restarting the preview will not resume the continuous autofocus. To
1816 * stop continuous focus, applications should change the focus mode to
1817 * other modes.
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08001818 *
1819 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08001820 */
1821 public static final String FOCUS_MODE_CONTINUOUS_PICTURE = "continuous-picture";
1822
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08001823 // Indices for focus distance array.
1824 /**
1825 * The array index of near focus distance for use with
1826 * {@link #getFocusDistances(float[])}.
1827 */
1828 public static final int FOCUS_DISTANCE_NEAR_INDEX = 0;
1829
1830 /**
1831 * The array index of optimal focus distance for use with
1832 * {@link #getFocusDistances(float[])}.
1833 */
1834 public static final int FOCUS_DISTANCE_OPTIMAL_INDEX = 1;
1835
1836 /**
1837 * The array index of far focus distance for use with
1838 * {@link #getFocusDistances(float[])}.
1839 */
1840 public static final int FOCUS_DISTANCE_FAR_INDEX = 2;
1841
Wu-cheng Lica099612010-05-06 16:47:30 +08001842 /**
Wu-cheng Li454630f2010-08-11 16:48:05 -07001843 * The array index of minimum preview fps for use with {@link
1844 * #getPreviewFpsRange(int[])} or {@link
1845 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07001846 */
1847 public static final int PREVIEW_FPS_MIN_INDEX = 0;
1848
1849 /**
1850 * The array index of maximum preview fps for use with {@link
1851 * #getPreviewFpsRange(int[])} or {@link
1852 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07001853 */
1854 public static final int PREVIEW_FPS_MAX_INDEX = 1;
1855
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001856 // Formats for setPreviewFormat and setPictureFormat.
1857 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp";
1858 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp";
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001859 private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv";
Wu-cheng Li10a1b302011-02-22 15:49:25 +08001860 private static final String PIXEL_FORMAT_YUV420P = "yuv420p";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001861 private static final String PIXEL_FORMAT_RGB565 = "rgb565";
1862 private static final String PIXEL_FORMAT_JPEG = "jpeg";
Wu-cheng Li70fb9082011-08-02 17:49:50 +08001863 private static final String PIXEL_FORMAT_BAYER_RGGB = "bayer-rggb";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 private HashMap<String, String> mMap;
1866
1867 private Parameters() {
1868 mMap = new HashMap<String, String>();
1869 }
1870
1871 /**
1872 * Writes the current Parameters to the log.
1873 * @hide
1874 * @deprecated
1875 */
1876 public void dump() {
1877 Log.e(TAG, "dump: size=" + mMap.size());
1878 for (String k : mMap.keySet()) {
1879 Log.e(TAG, "dump: " + k + "=" + mMap.get(k));
1880 }
1881 }
1882
1883 /**
1884 * Creates a single string with all the parameters set in
1885 * this Parameters object.
1886 * <p>The {@link #unflatten(String)} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001887 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 * @return a String with all values from this Parameters object, in
1889 * semi-colon delimited key-value pairs
1890 */
1891 public String flatten() {
1892 StringBuilder flattened = new StringBuilder();
1893 for (String k : mMap.keySet()) {
1894 flattened.append(k);
1895 flattened.append("=");
1896 flattened.append(mMap.get(k));
1897 flattened.append(";");
1898 }
1899 // chop off the extra semicolon at the end
1900 flattened.deleteCharAt(flattened.length()-1);
1901 return flattened.toString();
1902 }
1903
1904 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001905 * Takes a flattened string of parameters and adds each one to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 * this Parameters object.
1907 * <p>The {@link #flatten()} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001908 *
1909 * @param flattened a String of parameters (key-value paired) that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 * are semi-colon delimited
1911 */
1912 public void unflatten(String flattened) {
1913 mMap.clear();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 StringTokenizer tokenizer = new StringTokenizer(flattened, ";");
1916 while (tokenizer.hasMoreElements()) {
1917 String kv = tokenizer.nextToken();
1918 int pos = kv.indexOf('=');
1919 if (pos == -1) {
1920 continue;
1921 }
1922 String k = kv.substring(0, pos);
1923 String v = kv.substring(pos + 1);
1924 mMap.put(k, v);
1925 }
1926 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 public void remove(String key) {
1929 mMap.remove(key);
1930 }
1931
1932 /**
1933 * Sets a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001934 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 * @param key the key name for the parameter
1936 * @param value the String value of the parameter
1937 */
1938 public void set(String key, String value) {
1939 if (key.indexOf('=') != -1 || key.indexOf(';') != -1) {
1940 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ;)");
1941 return;
1942 }
1943 if (value.indexOf('=') != -1 || value.indexOf(';') != -1) {
1944 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ;)");
1945 return;
1946 }
1947
1948 mMap.put(key, value);
1949 }
1950
1951 /**
1952 * Sets an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001953 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 * @param key the key name for the parameter
1955 * @param value the int value of the parameter
1956 */
1957 public void set(String key, int value) {
1958 mMap.put(key, Integer.toString(value));
1959 }
1960
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08001961 private void set(String key, List<Area> areas) {
Wu-cheng Lif715bf92011-04-14 14:04:18 +08001962 if (areas == null) {
1963 set(key, "(0,0,0,0,0)");
1964 } else {
1965 StringBuilder buffer = new StringBuilder();
1966 for (int i = 0; i < areas.size(); i++) {
1967 Area area = areas.get(i);
1968 Rect rect = area.rect;
1969 buffer.append('(');
1970 buffer.append(rect.left);
1971 buffer.append(',');
1972 buffer.append(rect.top);
1973 buffer.append(',');
1974 buffer.append(rect.right);
1975 buffer.append(',');
1976 buffer.append(rect.bottom);
1977 buffer.append(',');
1978 buffer.append(area.weight);
1979 buffer.append(')');
1980 if (i != areas.size() - 1) buffer.append(',');
1981 }
1982 set(key, buffer.toString());
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08001983 }
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08001984 }
1985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 /**
1987 * Returns the value of a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001988 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 * @param key the key name for the parameter
1990 * @return the String value of the parameter
1991 */
1992 public String get(String key) {
1993 return mMap.get(key);
1994 }
1995
1996 /**
1997 * Returns the value of an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001998 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 * @param key the key name for the parameter
2000 * @return the int value of the parameter
2001 */
2002 public int getInt(String key) {
2003 return Integer.parseInt(mMap.get(key));
2004 }
2005
2006 /**
Wu-cheng Li26274fa2011-05-05 14:36:28 +08002007 * Sets the dimensions for preview pictures. If the preview has already
2008 * started, applications should stop the preview first before changing
2009 * preview size.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002010 *
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002011 * The sides of width and height are based on camera orientation. That
2012 * is, the preview size is the size before it is rotated by display
2013 * orientation. So applications need to consider the display orientation
2014 * while setting preview size. For example, suppose the camera supports
2015 * both 480x320 and 320x480 preview sizes. The application wants a 3:2
2016 * preview ratio. If the display orientation is set to 0 or 180, preview
2017 * size should be set to 480x320. If the display orientation is set to
2018 * 90 or 270, preview size should be set to 320x480. The display
2019 * orientation should also be considered while setting picture size and
2020 * thumbnail size.
2021 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 * @param width the width of the pictures, in pixels
2023 * @param height the height of the pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002024 * @see #setDisplayOrientation(int)
2025 * @see #getCameraInfo(int, CameraInfo)
2026 * @see #setPictureSize(int, int)
2027 * @see #setJpegThumbnailSize(int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 */
2029 public void setPreviewSize(int width, int height) {
2030 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002031 set(KEY_PREVIEW_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 }
2033
2034 /**
2035 * Returns the dimensions setting for preview pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002036 *
James Dongdd0b16c2010-09-21 16:23:48 -07002037 * @return a Size object with the width and height setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 * for the preview picture
2039 */
2040 public Size getPreviewSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002041 String pair = get(KEY_PREVIEW_SIZE);
2042 return strToSize(pair);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 }
2044
2045 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002046 * Gets the supported preview sizes.
2047 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002048 * @return a list of Size object. This method will always return a list
Wu-cheng Li9c799382009-12-04 19:59:18 +08002049 * with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002050 */
2051 public List<Size> getSupportedPreviewSizes() {
2052 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX);
2053 return splitSize(str);
2054 }
2055
2056 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002057 * <p>Gets the supported video frame sizes that can be used by
2058 * MediaRecorder.</p>
James Dongdd0b16c2010-09-21 16:23:48 -07002059 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002060 * <p>If the returned list is not null, the returned list will contain at
James Dongdd0b16c2010-09-21 16:23:48 -07002061 * least one Size and one of the sizes in the returned list must be
2062 * passed to MediaRecorder.setVideoSize() for camcorder application if
2063 * camera is used as the video source. In this case, the size of the
2064 * preview can be different from the resolution of the recorded video
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002065 * during video recording.</p>
James Dongdd0b16c2010-09-21 16:23:48 -07002066 *
2067 * @return a list of Size object if camera has separate preview and
2068 * video output; otherwise, null is returned.
2069 * @see #getPreferredPreviewSizeForVideo()
2070 */
2071 public List<Size> getSupportedVideoSizes() {
2072 String str = get(KEY_VIDEO_SIZE + SUPPORTED_VALUES_SUFFIX);
2073 return splitSize(str);
2074 }
2075
2076 /**
2077 * Returns the preferred or recommended preview size (width and height)
2078 * in pixels for video recording. Camcorder applications should
2079 * set the preview size to a value that is not larger than the
2080 * preferred preview size. In other words, the product of the width
2081 * and height of the preview size should not be larger than that of
2082 * the preferred preview size. In addition, we recommend to choose a
2083 * preview size that has the same aspect ratio as the resolution of
2084 * video to be recorded.
2085 *
2086 * @return the preferred preview size (width and height) in pixels for
2087 * video recording if getSupportedVideoSizes() does not return
2088 * null; otherwise, null is returned.
2089 * @see #getSupportedVideoSizes()
2090 */
2091 public Size getPreferredPreviewSizeForVideo() {
2092 String pair = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
2093 return strToSize(pair);
2094 }
2095
2096 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002097 * <p>Sets the dimensions for EXIF thumbnail in Jpeg picture. If
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002098 * applications set both width and height to 0, EXIF will not contain
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002099 * thumbnail.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002100 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002101 * <p>Applications need to consider the display orientation. See {@link
2102 * #setPreviewSize(int,int)} for reference.</p>
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002103 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 * @param width the width of the thumbnail, in pixels
2105 * @param height the height of the thumbnail, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002106 * @see #setPreviewSize(int,int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002108 public void setJpegThumbnailSize(int width, int height) {
2109 set(KEY_JPEG_THUMBNAIL_WIDTH, width);
2110 set(KEY_JPEG_THUMBNAIL_HEIGHT, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
2112
2113 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002114 * Returns the dimensions for EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002116 * @return a Size object with the height and width setting for the EXIF
2117 * thumbnails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002119 public Size getJpegThumbnailSize() {
2120 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH),
2121 getInt(KEY_JPEG_THUMBNAIL_HEIGHT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
2123
2124 /**
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002125 * Gets the supported jpeg thumbnail sizes.
2126 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002127 * @return a list of Size object. This method will always return a list
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002128 * with at least two elements. Size 0,0 (no thumbnail) is always
2129 * supported.
2130 */
2131 public List<Size> getSupportedJpegThumbnailSizes() {
2132 String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX);
2133 return splitSize(str);
2134 }
2135
2136 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002137 * Sets the quality of the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002139 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1
2140 * to 100, with 100 being the best.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002142 public void setJpegThumbnailQuality(int quality) {
2143 set(KEY_JPEG_THUMBNAIL_QUALITY, quality);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 }
2145
2146 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002147 * Returns the quality setting for the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002149 * @return the JPEG quality setting of the EXIF thumbnail.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002151 public int getJpegThumbnailQuality() {
2152 return getInt(KEY_JPEG_THUMBNAIL_QUALITY);
2153 }
2154
2155 /**
2156 * Sets Jpeg quality of captured picture.
2157 *
2158 * @param quality the JPEG quality of captured picture. The range is 1
2159 * to 100, with 100 being the best.
2160 */
2161 public void setJpegQuality(int quality) {
2162 set(KEY_JPEG_QUALITY, quality);
2163 }
2164
2165 /**
2166 * Returns the quality setting for the JPEG picture.
2167 *
2168 * @return the JPEG picture quality setting.
2169 */
2170 public int getJpegQuality() {
2171 return getInt(KEY_JPEG_QUALITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 }
2173
2174 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08002175 * Sets the rate at which preview frames are received. This is the
2176 * target frame rate. The actual frame rate depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002177 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 * @param fps the frame rate (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002179 * @deprecated replaced by {@link #setPreviewFpsRange(int,int)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002181 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 public void setPreviewFrameRate(int fps) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002183 set(KEY_PREVIEW_FRAME_RATE, fps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 }
2185
2186 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08002187 * Returns the setting for the rate at which preview frames are
2188 * received. This is the target frame rate. The actual frame rate
2189 * depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002190 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 * @return the frame rate setting (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002192 * @deprecated replaced by {@link #getPreviewFpsRange(int[])}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002194 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 public int getPreviewFrameRate() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002196 return getInt(KEY_PREVIEW_FRAME_RATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 }
2198
2199 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002200 * Gets the supported preview frame rates.
2201 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002202 * @return a list of supported preview frame rates. null if preview
2203 * frame rate setting is not supported.
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002204 * @deprecated replaced by {@link #getSupportedPreviewFpsRange()}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002205 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002206 @Deprecated
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002207 public List<Integer> getSupportedPreviewFrameRates() {
2208 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX);
2209 return splitInt(str);
2210 }
2211
2212 /**
Wu-cheng Li454630f2010-08-11 16:48:05 -07002213 * Sets the maximum and maximum preview fps. This controls the rate of
Wu-cheng Li1620d112010-08-27 15:09:20 -07002214 * preview frames received in {@link PreviewCallback}. The minimum and
Wu-cheng Li454630f2010-08-11 16:48:05 -07002215 * maximum preview fps must be one of the elements from {@link
2216 * #getSupportedPreviewFpsRange}.
2217 *
2218 * @param min the minimum preview fps (scaled by 1000).
2219 * @param max the maximum preview fps (scaled by 1000).
2220 * @throws RuntimeException if fps range is invalid.
2221 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
2222 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07002223 */
2224 public void setPreviewFpsRange(int min, int max) {
2225 set(KEY_PREVIEW_FPS_RANGE, "" + min + "," + max);
2226 }
2227
2228 /**
2229 * Returns the current minimum and maximum preview fps. The values are
2230 * one of the elements returned by {@link #getSupportedPreviewFpsRange}.
2231 *
2232 * @return range the minimum and maximum preview fps (scaled by 1000).
2233 * @see #PREVIEW_FPS_MIN_INDEX
2234 * @see #PREVIEW_FPS_MAX_INDEX
2235 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07002236 */
2237 public void getPreviewFpsRange(int[] range) {
2238 if (range == null || range.length != 2) {
2239 throw new IllegalArgumentException(
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002240 "range must be an array with two elements.");
Wu-cheng Li454630f2010-08-11 16:48:05 -07002241 }
2242 splitInt(get(KEY_PREVIEW_FPS_RANGE), range);
2243 }
2244
2245 /**
2246 * Gets the supported preview fps (frame-per-second) ranges. Each range
2247 * contains a minimum fps and maximum fps. If minimum fps equals to
2248 * maximum fps, the camera outputs frames in fixed frame rate. If not,
2249 * the camera outputs frames in auto frame rate. The actual frame rate
2250 * fluctuates between the minimum and the maximum. The values are
2251 * multiplied by 1000 and represented in integers. For example, if frame
2252 * rate is 26.623 frames per second, the value is 26623.
2253 *
2254 * @return a list of supported preview fps ranges. This method returns a
2255 * list with at least one element. Every element is an int array
2256 * of two values - minimum fps and maximum fps. The list is
2257 * sorted from small to large (first by maximum fps and then
2258 * minimum fps).
2259 * @see #PREVIEW_FPS_MIN_INDEX
2260 * @see #PREVIEW_FPS_MAX_INDEX
Wu-cheng Li454630f2010-08-11 16:48:05 -07002261 */
2262 public List<int[]> getSupportedPreviewFpsRange() {
2263 String str = get(KEY_PREVIEW_FPS_RANGE + SUPPORTED_VALUES_SUFFIX);
2264 return splitRange(str);
2265 }
2266
2267 /**
Wu-cheng Li7478ea62009-09-16 18:52:55 +08002268 * Sets the image format for preview pictures.
Scott Mainda0a56d2009-09-10 18:08:37 -07002269 * <p>If this is never called, the default format will be
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002270 * {@link android.graphics.ImageFormat#NV21}, which
Scott Maindf4578e2009-09-10 12:22:07 -07002271 * uses the NV21 encoding format.</p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +08002272 *
Scott Maindf4578e2009-09-10 12:22:07 -07002273 * @param pixel_format the desired preview picture format, defined
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002274 * by one of the {@link android.graphics.ImageFormat} constants.
2275 * (E.g., <var>ImageFormat.NV21</var> (default),
2276 * <var>ImageFormat.RGB_565</var>, or
2277 * <var>ImageFormat.JPEG</var>)
2278 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 */
2280 public void setPreviewFormat(int pixel_format) {
2281 String s = cameraFormatForPixelFormat(pixel_format);
2282 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002283 throw new IllegalArgumentException(
2284 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 }
2286
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002287 set(KEY_PREVIEW_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
2289
2290 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002291 * Returns the image format for preview frames got from
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002292 * {@link PreviewCallback}.
Wu-cheng Li7478ea62009-09-16 18:52:55 +08002293 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002294 * @return the preview format.
2295 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 */
2297 public int getPreviewFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002298 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT));
2299 }
2300
2301 /**
Wu-cheng Lif9293e72011-02-25 13:35:10 +08002302 * Gets the supported preview formats. {@link android.graphics.ImageFormat#NV21}
2303 * is always supported. {@link android.graphics.ImageFormat#YV12}
2304 * is always supported since API level 12.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002305 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002306 * @return a list of supported preview formats. This method will always
2307 * return a list with at least one element.
2308 * @see android.graphics.ImageFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002309 */
2310 public List<Integer> getSupportedPreviewFormats() {
2311 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX);
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002312 ArrayList<Integer> formats = new ArrayList<Integer>();
2313 for (String s : split(str)) {
2314 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002315 if (f == ImageFormat.UNKNOWN) continue;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002316 formats.add(f);
2317 }
2318 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
2320
2321 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002322 * <p>Sets the dimensions for pictures.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002323 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002324 * <p>Applications need to consider the display orientation. See {@link
2325 * #setPreviewSize(int,int)} for reference.</p>
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 * @param width the width for pictures, in pixels
2328 * @param height the height for pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002329 * @see #setPreviewSize(int,int)
2330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 */
2332 public void setPictureSize(int width, int height) {
2333 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002334 set(KEY_PICTURE_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 }
2336
2337 /**
2338 * Returns the dimension setting for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002339 *
2340 * @return a Size object with the height and width setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 * for pictures
2342 */
2343 public Size getPictureSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002344 String pair = get(KEY_PICTURE_SIZE);
2345 return strToSize(pair);
2346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002348 /**
2349 * Gets the supported picture sizes.
2350 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002351 * @return a list of supported picture sizes. This method will always
2352 * return a list with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002353 */
2354 public List<Size> getSupportedPictureSizes() {
2355 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX);
2356 return splitSize(str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 }
2358
2359 /**
2360 * Sets the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002361 *
2362 * @param pixel_format the desired picture format
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002363 * (<var>ImageFormat.NV21</var>,
2364 * <var>ImageFormat.RGB_565</var>, or
2365 * <var>ImageFormat.JPEG</var>)
2366 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 */
2368 public void setPictureFormat(int pixel_format) {
2369 String s = cameraFormatForPixelFormat(pixel_format);
2370 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002371 throw new IllegalArgumentException(
2372 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 }
2374
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002375 set(KEY_PICTURE_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 }
2377
2378 /**
2379 * Returns the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002380 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002381 * @return the picture format
2382 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 */
2384 public int getPictureFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002385 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT));
2386 }
2387
2388 /**
2389 * Gets the supported picture formats.
2390 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002391 * @return supported picture formats. This method will always return a
2392 * list with at least one element.
2393 * @see android.graphics.ImageFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002394 */
2395 public List<Integer> getSupportedPictureFormats() {
Wu-cheng Li9c799382009-12-04 19:59:18 +08002396 String str = get(KEY_PICTURE_FORMAT + SUPPORTED_VALUES_SUFFIX);
2397 ArrayList<Integer> formats = new ArrayList<Integer>();
2398 for (String s : split(str)) {
2399 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002400 if (f == ImageFormat.UNKNOWN) continue;
Wu-cheng Li9c799382009-12-04 19:59:18 +08002401 formats.add(f);
2402 }
2403 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
2405
2406 private String cameraFormatForPixelFormat(int pixel_format) {
2407 switch(pixel_format) {
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002408 case ImageFormat.NV16: return PIXEL_FORMAT_YUV422SP;
2409 case ImageFormat.NV21: return PIXEL_FORMAT_YUV420SP;
2410 case ImageFormat.YUY2: return PIXEL_FORMAT_YUV422I;
Wu-cheng Li10a1b302011-02-22 15:49:25 +08002411 case ImageFormat.YV12: return PIXEL_FORMAT_YUV420P;
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002412 case ImageFormat.RGB_565: return PIXEL_FORMAT_RGB565;
2413 case ImageFormat.JPEG: return PIXEL_FORMAT_JPEG;
Wu-cheng Li70fb9082011-08-02 17:49:50 +08002414 case ImageFormat.BAYER_RGGB: return PIXEL_FORMAT_BAYER_RGGB;
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002415 default: return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 }
2417 }
2418
2419 private int pixelFormatForCameraFormat(String format) {
2420 if (format == null)
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002421 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002423 if (format.equals(PIXEL_FORMAT_YUV422SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002424 return ImageFormat.NV16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002426 if (format.equals(PIXEL_FORMAT_YUV420SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002427 return ImageFormat.NV21;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002429 if (format.equals(PIXEL_FORMAT_YUV422I))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002430 return ImageFormat.YUY2;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002431
Wu-cheng Li10a1b302011-02-22 15:49:25 +08002432 if (format.equals(PIXEL_FORMAT_YUV420P))
2433 return ImageFormat.YV12;
2434
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002435 if (format.equals(PIXEL_FORMAT_RGB565))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002436 return ImageFormat.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002438 if (format.equals(PIXEL_FORMAT_JPEG))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002439 return ImageFormat.JPEG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002441 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 }
2443
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002444 /**
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002445 * Sets the rotation angle in degrees relative to the orientation of
2446 * the camera. This affects the pictures returned from JPEG {@link
2447 * PictureCallback}. The camera driver may set orientation in the
2448 * EXIF header without rotating the picture. Or the driver may rotate
2449 * the picture and the EXIF thumbnail. If the Jpeg picture is rotated,
2450 * the orientation in the EXIF header will be missing or 1 (row #0 is
2451 * top and column #0 is left side).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002452 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08002453 * <p>If applications want to rotate the picture to match the orientation
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08002454 * of what users see, apps should use {@link
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002455 * android.view.OrientationEventListener} and {@link CameraInfo}.
2456 * The value from OrientationEventListener is relative to the natural
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08002457 * orientation of the device. CameraInfo.orientation is the angle
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08002458 * between camera orientation and natural device orientation. The sum
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08002459 * of the two is the rotation angle for back-facing camera. The
2460 * difference of the two is the rotation angle for front-facing camera.
2461 * Note that the JPEG pictures of front-facing cameras are not mirrored
2462 * as in preview display.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002463 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08002464 * <p>For example, suppose the natural orientation of the device is
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002465 * portrait. The device is rotated 270 degrees clockwise, so the device
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08002466 * orientation is 270. Suppose a back-facing camera sensor is mounted in
2467 * landscape and the top side of the camera sensor is aligned with the
2468 * right edge of the display in natural orientation. So the camera
2469 * orientation is 90. The rotation should be set to 0 (270 + 90).
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002470 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08002471 * <p>The reference code is as follows.
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002472 *
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -08002473 * <pre>
Scott Main9a10bf02011-08-24 19:09:48 -07002474 * public void onOrientationChanged(int orientation) {
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002475 * if (orientation == ORIENTATION_UNKNOWN) return;
2476 * android.hardware.Camera.CameraInfo info =
2477 * new android.hardware.Camera.CameraInfo();
2478 * android.hardware.Camera.getCameraInfo(cameraId, info);
2479 * orientation = (orientation + 45) / 90 * 90;
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08002480 * int rotation = 0;
2481 * if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
2482 * rotation = (info.orientation - orientation + 360) % 360;
2483 * } else { // back-facing camera
2484 * rotation = (info.orientation + orientation) % 360;
2485 * }
2486 * mParameters.setRotation(rotation);
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002487 * }
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -08002488 * </pre>
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002489 *
2490 * @param rotation The rotation angle in degrees relative to the
2491 * orientation of the camera. Rotation can only be 0,
2492 * 90, 180 or 270.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002493 * @throws IllegalArgumentException if rotation value is invalid.
2494 * @see android.view.OrientationEventListener
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002495 * @see #getCameraInfo(int, CameraInfo)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002496 */
2497 public void setRotation(int rotation) {
2498 if (rotation == 0 || rotation == 90 || rotation == 180
2499 || rotation == 270) {
2500 set(KEY_ROTATION, Integer.toString(rotation));
2501 } else {
2502 throw new IllegalArgumentException(
2503 "Invalid rotation=" + rotation);
2504 }
2505 }
2506
2507 /**
2508 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF
2509 * header.
2510 *
2511 * @param latitude GPS latitude coordinate.
2512 */
2513 public void setGpsLatitude(double latitude) {
2514 set(KEY_GPS_LATITUDE, Double.toString(latitude));
2515 }
2516
2517 /**
2518 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF
2519 * header.
2520 *
2521 * @param longitude GPS longitude coordinate.
2522 */
2523 public void setGpsLongitude(double longitude) {
2524 set(KEY_GPS_LONGITUDE, Double.toString(longitude));
2525 }
2526
2527 /**
2528 * Sets GPS altitude. This will be stored in JPEG EXIF header.
2529 *
2530 * @param altitude GPS altitude in meters.
2531 */
2532 public void setGpsAltitude(double altitude) {
2533 set(KEY_GPS_ALTITUDE, Double.toString(altitude));
2534 }
2535
2536 /**
2537 * Sets GPS timestamp. This will be stored in JPEG EXIF header.
2538 *
2539 * @param timestamp GPS timestamp (UTC in seconds since January 1,
2540 * 1970).
2541 */
2542 public void setGpsTimestamp(long timestamp) {
2543 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp));
2544 }
2545
2546 /**
Ray Chene2083772010-03-10 15:02:49 -08002547 * Sets GPS processing method. It will store up to 32 characters
Ray Chen055c9862010-02-23 10:45:42 +08002548 * in JPEG EXIF header.
2549 *
2550 * @param processing_method The processing method to get this location.
2551 */
2552 public void setGpsProcessingMethod(String processing_method) {
2553 set(KEY_GPS_PROCESSING_METHOD, processing_method);
2554 }
2555
2556 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002557 * Removes GPS latitude, longitude, altitude, and timestamp from the
2558 * parameters.
2559 */
2560 public void removeGpsData() {
2561 remove(KEY_GPS_LATITUDE);
2562 remove(KEY_GPS_LONGITUDE);
2563 remove(KEY_GPS_ALTITUDE);
2564 remove(KEY_GPS_TIMESTAMP);
Ray Chen055c9862010-02-23 10:45:42 +08002565 remove(KEY_GPS_PROCESSING_METHOD);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002566 }
2567
2568 /**
2569 * Gets the current white balance setting.
2570 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002571 * @return current white balance. null if white balance setting is not
2572 * supported.
2573 * @see #WHITE_BALANCE_AUTO
2574 * @see #WHITE_BALANCE_INCANDESCENT
2575 * @see #WHITE_BALANCE_FLUORESCENT
2576 * @see #WHITE_BALANCE_WARM_FLUORESCENT
2577 * @see #WHITE_BALANCE_DAYLIGHT
2578 * @see #WHITE_BALANCE_CLOUDY_DAYLIGHT
2579 * @see #WHITE_BALANCE_TWILIGHT
2580 * @see #WHITE_BALANCE_SHADE
2581 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002582 */
2583 public String getWhiteBalance() {
2584 return get(KEY_WHITE_BALANCE);
2585 }
2586
2587 /**
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07002588 * Sets the white balance. Changing the setting will release the
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08002589 * auto-white balance lock. It is recommended not to change white
2590 * balance and AWB lock at the same time.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002591 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002592 * @param value new white balance.
2593 * @see #getWhiteBalance()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07002594 * @see #setAutoWhiteBalanceLock(boolean)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002595 */
2596 public void setWhiteBalance(String value) {
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08002597 String oldValue = get(KEY_WHITE_BALANCE);
2598 if (same(value, oldValue)) return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002599 set(KEY_WHITE_BALANCE, value);
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07002600 set(KEY_AUTO_WHITEBALANCE_LOCK, FALSE);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002601 }
2602
2603 /**
2604 * Gets the supported white balance.
2605 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002606 * @return a list of supported white balance. null if white balance
2607 * setting is not supported.
2608 * @see #getWhiteBalance()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002609 */
2610 public List<String> getSupportedWhiteBalance() {
2611 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX);
2612 return split(str);
2613 }
2614
2615 /**
2616 * Gets the current color effect setting.
2617 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002618 * @return current color effect. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002619 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002620 * @see #EFFECT_NONE
2621 * @see #EFFECT_MONO
2622 * @see #EFFECT_NEGATIVE
2623 * @see #EFFECT_SOLARIZE
2624 * @see #EFFECT_SEPIA
2625 * @see #EFFECT_POSTERIZE
2626 * @see #EFFECT_WHITEBOARD
2627 * @see #EFFECT_BLACKBOARD
2628 * @see #EFFECT_AQUA
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002629 */
2630 public String getColorEffect() {
2631 return get(KEY_EFFECT);
2632 }
2633
2634 /**
2635 * Sets the current color effect setting.
2636 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002637 * @param value new color effect.
2638 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002639 */
2640 public void setColorEffect(String value) {
2641 set(KEY_EFFECT, value);
2642 }
2643
2644 /**
2645 * Gets the supported color effects.
2646 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002647 * @return a list of supported color effects. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002648 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002649 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002650 */
2651 public List<String> getSupportedColorEffects() {
2652 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX);
2653 return split(str);
2654 }
2655
2656
2657 /**
2658 * Gets the current antibanding setting.
2659 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002660 * @return current antibanding. null if antibanding setting is not
2661 * supported.
2662 * @see #ANTIBANDING_AUTO
2663 * @see #ANTIBANDING_50HZ
2664 * @see #ANTIBANDING_60HZ
2665 * @see #ANTIBANDING_OFF
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002666 */
2667 public String getAntibanding() {
2668 return get(KEY_ANTIBANDING);
2669 }
2670
2671 /**
2672 * Sets the antibanding.
2673 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002674 * @param antibanding new antibanding value.
2675 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002676 */
2677 public void setAntibanding(String antibanding) {
2678 set(KEY_ANTIBANDING, antibanding);
2679 }
2680
2681 /**
2682 * Gets the supported antibanding values.
2683 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002684 * @return a list of supported antibanding values. null if antibanding
2685 * setting is not supported.
2686 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002687 */
2688 public List<String> getSupportedAntibanding() {
2689 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX);
2690 return split(str);
2691 }
2692
2693 /**
2694 * Gets the current scene mode setting.
2695 *
2696 * @return one of SCENE_MODE_XXX string constant. null if scene mode
2697 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002698 * @see #SCENE_MODE_AUTO
2699 * @see #SCENE_MODE_ACTION
2700 * @see #SCENE_MODE_PORTRAIT
2701 * @see #SCENE_MODE_LANDSCAPE
2702 * @see #SCENE_MODE_NIGHT
2703 * @see #SCENE_MODE_NIGHT_PORTRAIT
2704 * @see #SCENE_MODE_THEATRE
2705 * @see #SCENE_MODE_BEACH
2706 * @see #SCENE_MODE_SNOW
2707 * @see #SCENE_MODE_SUNSET
2708 * @see #SCENE_MODE_STEADYPHOTO
2709 * @see #SCENE_MODE_FIREWORKS
2710 * @see #SCENE_MODE_SPORTS
2711 * @see #SCENE_MODE_PARTY
2712 * @see #SCENE_MODE_CANDLELIGHT
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002713 */
2714 public String getSceneMode() {
2715 return get(KEY_SCENE_MODE);
2716 }
2717
2718 /**
Wu-cheng Lic58b4232010-03-29 17:21:28 +08002719 * Sets the scene mode. Changing scene mode may override other
2720 * parameters (such as flash mode, focus mode, white balance). For
2721 * example, suppose originally flash mode is on and supported flash
2722 * modes are on/off. In night scene mode, both flash mode and supported
2723 * flash mode may be changed to off. After setting scene mode,
Wu-cheng Li2988ab72009-09-30 17:08:19 -07002724 * applications should call getParameters to know if some parameters are
2725 * changed.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002726 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002727 * @param value scene mode.
2728 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002729 */
2730 public void setSceneMode(String value) {
2731 set(KEY_SCENE_MODE, value);
2732 }
2733
2734 /**
2735 * Gets the supported scene modes.
2736 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002737 * @return a list of supported scene modes. null if scene mode setting
2738 * is not supported.
2739 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002740 */
2741 public List<String> getSupportedSceneModes() {
2742 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX);
2743 return split(str);
2744 }
2745
2746 /**
2747 * Gets the current flash mode setting.
2748 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002749 * @return current flash mode. null if flash mode setting is not
2750 * supported.
2751 * @see #FLASH_MODE_OFF
2752 * @see #FLASH_MODE_AUTO
2753 * @see #FLASH_MODE_ON
2754 * @see #FLASH_MODE_RED_EYE
2755 * @see #FLASH_MODE_TORCH
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002756 */
2757 public String getFlashMode() {
2758 return get(KEY_FLASH_MODE);
2759 }
2760
2761 /**
2762 * Sets the flash mode.
2763 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002764 * @param value flash mode.
2765 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002766 */
2767 public void setFlashMode(String value) {
2768 set(KEY_FLASH_MODE, value);
2769 }
2770
2771 /**
2772 * Gets the supported flash modes.
2773 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002774 * @return a list of supported flash modes. null if flash mode setting
2775 * is not supported.
2776 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002777 */
2778 public List<String> getSupportedFlashModes() {
2779 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX);
2780 return split(str);
2781 }
2782
Wu-cheng Li36322db2009-09-18 18:59:21 +08002783 /**
2784 * Gets the current focus mode setting.
2785 *
Wu-cheng Li699fe932010-08-05 11:50:25 -07002786 * @return current focus mode. This method will always return a non-null
2787 * value. Applications should call {@link
2788 * #autoFocus(AutoFocusCallback)} to start the focus if focus
2789 * mode is FOCUS_MODE_AUTO or FOCUS_MODE_MACRO.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002790 * @see #FOCUS_MODE_AUTO
2791 * @see #FOCUS_MODE_INFINITY
2792 * @see #FOCUS_MODE_MACRO
2793 * @see #FOCUS_MODE_FIXED
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07002794 * @see #FOCUS_MODE_EDOF
Wu-cheng Lid45cb722010-09-20 16:15:32 -07002795 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Li36322db2009-09-18 18:59:21 +08002796 */
2797 public String getFocusMode() {
2798 return get(KEY_FOCUS_MODE);
2799 }
2800
2801 /**
2802 * Sets the focus mode.
2803 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002804 * @param value focus mode.
2805 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08002806 */
2807 public void setFocusMode(String value) {
2808 set(KEY_FOCUS_MODE, value);
2809 }
2810
2811 /**
2812 * Gets the supported focus modes.
2813 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002814 * @return a list of supported focus modes. This method will always
2815 * return a list with at least one element.
2816 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08002817 */
2818 public List<String> getSupportedFocusModes() {
2819 String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX);
2820 return split(str);
2821 }
2822
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002823 /**
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08002824 * Gets the focal length (in millimeter) of the camera.
2825 *
2826 * @return the focal length. This method will always return a valid
2827 * value.
2828 */
2829 public float getFocalLength() {
2830 return Float.parseFloat(get(KEY_FOCAL_LENGTH));
2831 }
2832
2833 /**
2834 * Gets the horizontal angle of view in degrees.
2835 *
2836 * @return horizontal angle of view. This method will always return a
2837 * valid value.
2838 */
2839 public float getHorizontalViewAngle() {
2840 return Float.parseFloat(get(KEY_HORIZONTAL_VIEW_ANGLE));
2841 }
2842
2843 /**
2844 * Gets the vertical angle of view in degrees.
2845 *
2846 * @return vertical angle of view. This method will always return a
2847 * valid value.
2848 */
2849 public float getVerticalViewAngle() {
2850 return Float.parseFloat(get(KEY_VERTICAL_VIEW_ANGLE));
2851 }
2852
2853 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002854 * Gets the current exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002855 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002856 * @return current exposure compensation index. The range is {@link
2857 * #getMinExposureCompensation} to {@link
2858 * #getMaxExposureCompensation}. 0 means exposure is not
2859 * adjusted.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002860 */
2861 public int getExposureCompensation() {
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002862 return getInt(KEY_EXPOSURE_COMPENSATION, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08002863 }
2864
2865 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002866 * Sets the exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002867 *
Wu-cheng Li0402e7d2010-02-26 15:04:55 +08002868 * @param value exposure compensation index. The valid value range is
2869 * from {@link #getMinExposureCompensation} (inclusive) to {@link
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002870 * #getMaxExposureCompensation} (inclusive). 0 means exposure is
2871 * not adjusted. Application should call
2872 * getMinExposureCompensation and getMaxExposureCompensation to
2873 * know if exposure compensation is supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002874 */
2875 public void setExposureCompensation(int value) {
2876 set(KEY_EXPOSURE_COMPENSATION, value);
2877 }
2878
2879 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002880 * Gets the maximum exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002881 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002882 * @return maximum exposure compensation index (>=0). If both this
2883 * method and {@link #getMinExposureCompensation} return 0,
2884 * exposure compensation is not supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002885 */
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002886 public int getMaxExposureCompensation() {
2887 return getInt(KEY_MAX_EXPOSURE_COMPENSATION, 0);
2888 }
2889
2890 /**
2891 * Gets the minimum exposure compensation index.
2892 *
2893 * @return minimum exposure compensation index (<=0). If both this
2894 * method and {@link #getMaxExposureCompensation} return 0,
2895 * exposure compensation is not supported.
2896 */
2897 public int getMinExposureCompensation() {
2898 return getInt(KEY_MIN_EXPOSURE_COMPENSATION, 0);
2899 }
2900
2901 /**
2902 * Gets the exposure compensation step.
2903 *
2904 * @return exposure compensation step. Applications can get EV by
2905 * multiplying the exposure compensation index and step. Ex: if
2906 * exposure compensation index is -6 and step is 0.333333333, EV
2907 * is -2.
2908 */
2909 public float getExposureCompensationStep() {
2910 return getFloat(KEY_EXPOSURE_COMPENSATION_STEP, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08002911 }
2912
2913 /**
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002914 * <p>Sets the auto-exposure lock state. Applications should check
2915 * {@link #isAutoExposureLockSupported} before using this method.</p>
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002916 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002917 * <p>If set to true, the camera auto-exposure routine will immediately
2918 * pause until the lock is set to false. Exposure compensation settings
2919 * changes will still take effect while auto-exposure is locked.</p>
2920 *
2921 * <p>If auto-exposure is already locked, setting this to true again has
2922 * no effect (the driver will not recalculate exposure values).</p>
2923 *
2924 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
2925 * image capture with {@link #takePicture(Camera.ShutterCallback,
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08002926 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
2927 * lock.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002928 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08002929 * <p>Exposure compensation, auto-exposure lock, and auto-white balance
2930 * lock can be used to capture an exposure-bracketed burst of images,
2931 * for example.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002932 *
2933 * <p>Auto-exposure state, including the lock state, will not be
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002934 * maintained after camera {@link #release()} is called. Locking
2935 * auto-exposure after {@link #open()} but before the first call to
2936 * {@link #startPreview()} will not allow the auto-exposure routine to
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002937 * run at all, and may result in severely over- or under-exposed
2938 * images.</p>
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002939 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002940 * @param toggle new state of the auto-exposure lock. True means that
2941 * auto-exposure is locked, false means that the auto-exposure
2942 * routine is free to run normally.
2943 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002944 * @see #getAutoExposureLock()
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002945 */
2946 public void setAutoExposureLock(boolean toggle) {
2947 set(KEY_AUTO_EXPOSURE_LOCK, toggle ? TRUE : FALSE);
2948 }
2949
2950 /**
2951 * Gets the state of the auto-exposure lock. Applications should check
2952 * {@link #isAutoExposureLockSupported} before using this method. See
2953 * {@link #setAutoExposureLock} for details about the lock.
2954 *
2955 * @return State of the auto-exposure lock. Returns true if
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08002956 * auto-exposure is currently locked, and false otherwise.
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002957 *
2958 * @see #setAutoExposureLock(boolean)
2959 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002960 */
2961 public boolean getAutoExposureLock() {
2962 String str = get(KEY_AUTO_EXPOSURE_LOCK);
2963 return TRUE.equals(str);
2964 }
2965
2966 /**
2967 * Returns true if auto-exposure locking is supported. Applications
2968 * should call this before trying to lock auto-exposure. See
2969 * {@link #setAutoExposureLock} for details about the lock.
2970 *
2971 * @return true if auto-exposure lock is supported.
2972 * @see #setAutoExposureLock(boolean)
2973 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002974 */
2975 public boolean isAutoExposureLockSupported() {
2976 String str = get(KEY_AUTO_EXPOSURE_LOCK_SUPPORTED);
2977 return TRUE.equals(str);
2978 }
2979
2980 /**
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002981 * <p>Sets the auto-white balance lock state. Applications should check
2982 * {@link #isAutoWhiteBalanceLockSupported} before using this
2983 * method.</p>
2984 *
2985 * <p>If set to true, the camera auto-white balance routine will
2986 * immediately pause until the lock is set to false.</p>
2987 *
2988 * <p>If auto-white balance is already locked, setting this to true
2989 * again has no effect (the driver will not recalculate white balance
2990 * values).</p>
2991 *
2992 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
2993 * image capture with {@link #takePicture(Camera.ShutterCallback,
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08002994 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
2995 * the lock.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002996 *
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07002997 * <p> Changing the white balance mode with {@link #setWhiteBalance}
2998 * will release the auto-white balance lock if it is set.</p>
2999 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003000 * <p>Exposure compensation, AE lock, and AWB lock can be used to
3001 * capture an exposure-bracketed burst of images, for example.
3002 * Auto-white balance state, including the lock state, will not be
3003 * maintained after camera {@link #release()} is called. Locking
3004 * auto-white balance after {@link #open()} but before the first call to
3005 * {@link #startPreview()} will not allow the auto-white balance routine
3006 * to run at all, and may result in severely incorrect color in captured
3007 * images.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003008 *
3009 * @param toggle new state of the auto-white balance lock. True means
3010 * that auto-white balance is locked, false means that the
3011 * auto-white balance routine is free to run normally.
3012 *
3013 * @see #getAutoWhiteBalanceLock()
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003014 * @see #setWhiteBalance(String)
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003015 */
3016 public void setAutoWhiteBalanceLock(boolean toggle) {
3017 set(KEY_AUTO_WHITEBALANCE_LOCK, toggle ? TRUE : FALSE);
3018 }
3019
3020 /**
3021 * Gets the state of the auto-white balance lock. Applications should
3022 * check {@link #isAutoWhiteBalanceLockSupported} before using this
3023 * method. See {@link #setAutoWhiteBalanceLock} for details about the
3024 * lock.
3025 *
3026 * @return State of the auto-white balance lock. Returns true if
3027 * auto-white balance is currently locked, and false
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003028 * otherwise.
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003029 *
3030 * @see #setAutoWhiteBalanceLock(boolean)
3031 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003032 */
3033 public boolean getAutoWhiteBalanceLock() {
3034 String str = get(KEY_AUTO_WHITEBALANCE_LOCK);
3035 return TRUE.equals(str);
3036 }
3037
3038 /**
3039 * Returns true if auto-white balance locking is supported. Applications
3040 * should call this before trying to lock auto-white balance. See
3041 * {@link #setAutoWhiteBalanceLock} for details about the lock.
3042 *
3043 * @return true if auto-white balance lock is supported.
3044 * @see #setAutoWhiteBalanceLock(boolean)
3045 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003046 */
3047 public boolean isAutoWhiteBalanceLockSupported() {
3048 String str = get(KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED);
3049 return TRUE.equals(str);
3050 }
3051
3052 /**
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003053 * Gets current zoom value. This also works when smooth zoom is in
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003054 * progress. Applications should check {@link #isZoomSupported} before
3055 * using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003056 *
3057 * @return the current zoom value. The range is 0 to {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003058 * #getMaxZoom}. 0 means the camera is not zoomed.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003059 */
3060 public int getZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003061 return getInt(KEY_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003062 }
3063
3064 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003065 * Sets current zoom value. If the camera is zoomed (value > 0), the
3066 * actual picture size may be smaller than picture size setting.
3067 * Applications can check the actual picture size after picture is
3068 * returned from {@link PictureCallback}. The preview size remains the
3069 * same in zoom. Applications should check {@link #isZoomSupported}
3070 * before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003071 *
3072 * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003073 */
3074 public void setZoom(int value) {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003075 set(KEY_ZOOM, value);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003076 }
3077
3078 /**
3079 * Returns true if zoom is supported. Applications should call this
3080 * before using other zoom methods.
3081 *
3082 * @return true if zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003083 */
3084 public boolean isZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003085 String str = get(KEY_ZOOM_SUPPORTED);
3086 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003087 }
3088
3089 /**
3090 * Gets the maximum zoom value allowed for snapshot. This is the maximum
3091 * value that applications can set to {@link #setZoom(int)}.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003092 * Applications should call {@link #isZoomSupported} before using this
3093 * method. This value may change in different preview size. Applications
3094 * should call this again after setting preview size.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003095 *
3096 * @return the maximum zoom value supported by the camera.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003097 */
3098 public int getMaxZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003099 return getInt(KEY_MAX_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003100 }
3101
3102 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003103 * Gets the zoom ratios of all zoom values. Applications should check
3104 * {@link #isZoomSupported} before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003105 *
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003106 * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is
3107 * returned as 320. The number of elements is {@link
3108 * #getMaxZoom} + 1. The list is sorted from small to large. The
3109 * first element is always 100. The last element is the zoom
3110 * ratio of the maximum zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003111 */
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003112 public List<Integer> getZoomRatios() {
3113 return splitInt(get(KEY_ZOOM_RATIOS));
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003114 }
3115
3116 /**
3117 * Returns true if smooth zoom is supported. Applications should call
3118 * this before using other smooth zoom methods.
3119 *
3120 * @return true if smooth zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003121 */
3122 public boolean isSmoothZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003123 String str = get(KEY_SMOOTH_ZOOM_SUPPORTED);
3124 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003125 }
3126
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003127 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003128 * <p>Gets the distances from the camera to where an object appears to be
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003129 * in focus. The object is sharpest at the optimal focus distance. The
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003130 * depth of field is the far focus distance minus near focus distance.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003131 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003132 * <p>Focus distances may change after calling {@link
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003133 * #autoFocus(AutoFocusCallback)}, {@link #cancelAutoFocus}, or {@link
3134 * #startPreview()}. Applications can call {@link #getParameters()}
3135 * and this method anytime to get the latest focus distances. If the
Wu-cheng Lid45cb722010-09-20 16:15:32 -07003136 * focus mode is FOCUS_MODE_CONTINUOUS_VIDEO, focus distances may change
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003137 * from time to time.</p>
Wu-cheng Li699fe932010-08-05 11:50:25 -07003138 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003139 * <p>This method is intended to estimate the distance between the camera
Wu-cheng Li699fe932010-08-05 11:50:25 -07003140 * and the subject. After autofocus, the subject distance may be within
3141 * near and far focus distance. However, the precision depends on the
3142 * camera hardware, autofocus algorithm, the focus area, and the scene.
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003143 * The error can be large and it should be only used as a reference.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003144 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003145 * <p>Far focus distance >= optimal focus distance >= near focus distance.
Wu-cheng Li185cc452010-05-20 15:36:13 +08003146 * If the focus distance is infinity, the value will be
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003147 * {@code Float.POSITIVE_INFINITY}.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003148 *
3149 * @param output focus distances in meters. output must be a float
3150 * array with three elements. Near focus distance, optimal focus
3151 * distance, and far focus distance will be filled in the array.
Wu-cheng Li185cc452010-05-20 15:36:13 +08003152 * @see #FOCUS_DISTANCE_NEAR_INDEX
3153 * @see #FOCUS_DISTANCE_OPTIMAL_INDEX
3154 * @see #FOCUS_DISTANCE_FAR_INDEX
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003155 */
3156 public void getFocusDistances(float[] output) {
3157 if (output == null || output.length != 3) {
3158 throw new IllegalArgumentException(
Ken Wakasaf76a50c2012-03-09 19:56:35 +09003159 "output must be a float array with three elements.");
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003160 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07003161 splitFloat(get(KEY_FOCUS_DISTANCES), output);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003162 }
3163
Wu-cheng Li30771b72011-04-02 06:19:46 +08003164 /**
3165 * Gets the maximum number of focus areas supported. This is the maximum
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003166 * length of the list in {@link #setFocusAreas(List)} and
3167 * {@link #getFocusAreas()}.
Wu-cheng Li30771b72011-04-02 06:19:46 +08003168 *
3169 * @return the maximum number of focus areas supported by the camera.
3170 * @see #getFocusAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08003171 */
3172 public int getMaxNumFocusAreas() {
3173 return getInt(KEY_MAX_NUM_FOCUS_AREAS, 0);
3174 }
3175
3176 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003177 * <p>Gets the current focus areas. Camera driver uses the areas to decide
3178 * focus.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003179 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003180 * <p>Before using this API or {@link #setFocusAreas(List)}, apps should
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003181 * call {@link #getMaxNumFocusAreas()} to know the maximum number of
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003182 * focus areas first. If the value is 0, focus area is not supported.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003183 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003184 * <p>Each focus area is a rectangle with specified weight. The direction
Wu-cheng Li30771b72011-04-02 06:19:46 +08003185 * is relative to the sensor orientation, that is, what the sensor sees.
3186 * The direction is not affected by the rotation or mirroring of
3187 * {@link #setDisplayOrientation(int)}. Coordinates of the rectangle
3188 * range from -1000 to 1000. (-1000, -1000) is the upper left point.
Wu-cheng Libde61a52011-06-07 18:23:14 +08003189 * (1000, 1000) is the lower right point. The width and height of focus
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003190 * areas cannot be 0 or negative.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003191 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003192 * <p>The weight must range from 1 to 1000. The weight should be
Eino-Ville Talvala4e396e02011-04-21 09:23:15 -07003193 * interpreted as a per-pixel weight - all pixels in the area have the
3194 * specified weight. This means a small area with the same weight as a
3195 * larger area will have less influence on the focusing than the larger
3196 * area. Focus areas can partially overlap and the driver will add the
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003197 * weights in the overlap region.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003198 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003199 * <p>A special case of a {@code null} focus area list means the driver is
3200 * free to select focus targets as it wants. For example, the driver may
3201 * use more signals to select focus areas and change them
3202 * dynamically. Apps can set the focus area list to {@code null} if they
3203 * want the driver to completely control focusing.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003204 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003205 * <p>Focus areas are relative to the current field of view
Wu-cheng Li30771b72011-04-02 06:19:46 +08003206 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000)
3207 * represents the top of the currently visible camera frame. The focus
3208 * area cannot be set to be outside the current field of view, even
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003209 * when using zoom.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003210 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003211 * <p>Focus area only has effect if the current focus mode is
Wu-cheng Li53b30912011-10-12 19:43:51 +08003212 * {@link #FOCUS_MODE_AUTO}, {@link #FOCUS_MODE_MACRO},
3213 * {@link #FOCUS_MODE_CONTINUOUS_VIDEO}, or
3214 * {@link #FOCUS_MODE_CONTINUOUS_PICTURE}.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003215 *
3216 * @return a list of current focus areas
Wu-cheng Li30771b72011-04-02 06:19:46 +08003217 */
3218 public List<Area> getFocusAreas() {
Wu-cheng Lif715bf92011-04-14 14:04:18 +08003219 return splitArea(get(KEY_FOCUS_AREAS));
Wu-cheng Li30771b72011-04-02 06:19:46 +08003220 }
3221
3222 /**
3223 * Sets focus areas. See {@link #getFocusAreas()} for documentation.
3224 *
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003225 * @param focusAreas the focus areas
Wu-cheng Li30771b72011-04-02 06:19:46 +08003226 * @see #getFocusAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08003227 */
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003228 public void setFocusAreas(List<Area> focusAreas) {
3229 set(KEY_FOCUS_AREAS, focusAreas);
3230 }
3231
3232 /**
3233 * Gets the maximum number of metering areas supported. This is the
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003234 * maximum length of the list in {@link #setMeteringAreas(List)} and
3235 * {@link #getMeteringAreas()}.
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003236 *
3237 * @return the maximum number of metering areas supported by the camera.
3238 * @see #getMeteringAreas()
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003239 */
3240 public int getMaxNumMeteringAreas() {
3241 return getInt(KEY_MAX_NUM_METERING_AREAS, 0);
3242 }
3243
3244 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003245 * <p>Gets the current metering areas. Camera driver uses these areas to
3246 * decide exposure.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003247 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003248 * <p>Before using this API or {@link #setMeteringAreas(List)}, apps should
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003249 * call {@link #getMaxNumMeteringAreas()} to know the maximum number of
3250 * metering areas first. If the value is 0, metering area is not
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003251 * supported.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003252 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003253 * <p>Each metering area is a rectangle with specified weight. The
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003254 * direction is relative to the sensor orientation, that is, what the
3255 * sensor sees. The direction is not affected by the rotation or
3256 * mirroring of {@link #setDisplayOrientation(int)}. Coordinates of the
3257 * rectangle range from -1000 to 1000. (-1000, -1000) is the upper left
Wu-cheng Libde61a52011-06-07 18:23:14 +08003258 * point. (1000, 1000) is the lower right point. The width and height of
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003259 * metering areas cannot be 0 or negative.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003260 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003261 * <p>The weight must range from 1 to 1000, and represents a weight for
Eino-Ville Talvala4e396e02011-04-21 09:23:15 -07003262 * every pixel in the area. This means that a large metering area with
3263 * the same weight as a smaller area will have more effect in the
3264 * metering result. Metering areas can partially overlap and the driver
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003265 * will add the weights in the overlap region.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003266 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003267 * <p>A special case of a {@code null} metering area list means the driver
3268 * is free to meter as it chooses. For example, the driver may use more
3269 * signals to select metering areas and change them dynamically. Apps
3270 * can set the metering area list to {@code null} if they want the
3271 * driver to completely control metering.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003272 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003273 * <p>Metering areas are relative to the current field of view
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003274 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000)
3275 * represents the top of the currently visible camera frame. The
3276 * metering area cannot be set to be outside the current field of view,
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003277 * even when using zoom.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003278 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003279 * <p>No matter what metering areas are, the final exposure are compensated
3280 * by {@link #setExposureCompensation(int)}.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003281 *
3282 * @return a list of current metering areas
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003283 */
3284 public List<Area> getMeteringAreas() {
Wu-cheng Lid8aab932011-06-21 11:50:43 +08003285 return splitArea(get(KEY_METERING_AREAS));
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003286 }
3287
3288 /**
3289 * Sets metering areas. See {@link #getMeteringAreas()} for
3290 * documentation.
3291 *
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003292 * @param meteringAreas the metering areas
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003293 * @see #getMeteringAreas()
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003294 */
3295 public void setMeteringAreas(List<Area> meteringAreas) {
3296 set(KEY_METERING_AREAS, meteringAreas);
Wu-cheng Li30771b72011-04-02 06:19:46 +08003297 }
3298
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08003299 /**
3300 * Gets the maximum number of detected faces supported. This is the
3301 * maximum length of the list returned from {@link FaceDetectionListener}.
3302 * If the return value is 0, face detection of the specified type is not
3303 * supported.
3304 *
3305 * @return the maximum number of detected face supported by the camera.
Joe Fernandez464cb212011-10-04 16:56:47 -07003306 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08003307 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08003308 public int getMaxNumDetectedFaces() {
3309 return getInt(KEY_MAX_NUM_DETECTED_FACES_HW, 0);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08003310 }
3311
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08003312 /**
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08003313 * Sets recording mode hint. This tells the camera that the intent of
3314 * the application is to record videos {@link
3315 * android.media.MediaRecorder#start()}, not to take still pictures
3316 * {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
3317 * Camera.PictureCallback, Camera.PictureCallback)}. Using this hint can
3318 * allow MediaRecorder.start() to start faster or with fewer glitches on
3319 * output. This should be called before starting preview for the best
3320 * result, but can be changed while the preview is active. The default
3321 * value is false.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08003322 *
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08003323 * The app can still call takePicture() when the hint is true or call
3324 * MediaRecorder.start() when the hint is false. But the performance may
3325 * be worse.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08003326 *
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08003327 * @param hint true if the apps intend to record videos using
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08003328 * {@link android.media.MediaRecorder}.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08003329 */
3330 public void setRecordingHint(boolean hint) {
3331 set(KEY_RECORDING_HINT, hint ? TRUE : FALSE);
3332 }
3333
Wu-cheng Li98bb2512011-08-30 21:33:10 +08003334 /**
3335 * Returns true if video snapshot is supported. That is, applications
3336 * can call {@link #takePicture(Camera.ShutterCallback,
3337 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}
3338 * during recording. Applications do not need to call {@link
3339 * #startPreview()} after taking a picture. The preview will be still
3340 * active. Other than that, taking a picture during recording is
3341 * identical to taking a picture normally. All settings and methods
3342 * related to takePicture work identically. Ex: {@link
3343 * #getPictureSize()}, {@link #getSupportedPictureSizes()}, {@link
3344 * #setJpegQuality(int)}, {@link #setRotation(int)}, and etc. The
3345 * picture will have an EXIF header. {@link #FLASH_MODE_AUTO} and {@link
3346 * #FLASH_MODE_ON} also still work, but the video will record the flash.
3347 *
3348 * Applications can set shutter callback as null to avoid the shutter
3349 * sound. It is also recommended to set raw picture and post view
3350 * callbacks to null to avoid the interrupt of preview display.
3351 *
3352 * Field-of-view of the recorded video may be different from that of the
3353 * captured pictures.
3354 *
3355 * @return true if video snapshot is supported.
Wu-cheng Li98bb2512011-08-30 21:33:10 +08003356 */
3357 public boolean isVideoSnapshotSupported() {
3358 String str = get(KEY_VIDEO_SNAPSHOT_SUPPORTED);
3359 return TRUE.equals(str);
3360 }
3361
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003362 /**
3363 * <p>Enables and disables video stabilization. Use
3364 * {@link #isVideoStabilizationSupported} to determine if calling this
3365 * method is valid.</p>
3366 *
3367 * <p>Video stabilization reduces the shaking due to the motion of the
3368 * camera in both the preview stream and in recorded videos, including
3369 * data received from the preview callback. It does not reduce motion
3370 * blur in images captured with
3371 * {@link Camera#takePicture takePicture}.</p>
3372 *
3373 * <p>Video stabilization can be enabled and disabled while preview or
3374 * recording is active, but toggling it may cause a jump in the video
3375 * stream that may be undesirable in a recorded video.</p>
3376 *
3377 * @param toggle Set to true to enable video stabilization, and false to
3378 * disable video stabilization.
3379 * @see #isVideoStabilizationSupported()
3380 * @see #getVideoStabilization()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003381 */
3382 public void setVideoStabilization(boolean toggle) {
3383 set(KEY_VIDEO_STABILIZATION, toggle ? TRUE : FALSE);
3384 }
3385
3386 /**
3387 * Get the current state of video stabilization. See
3388 * {@link #setVideoStabilization} for details of video stabilization.
3389 *
3390 * @return true if video stabilization is enabled
3391 * @see #isVideoStabilizationSupported()
3392 * @see #setVideoStabilization(boolean)
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003393 */
3394 public boolean getVideoStabilization() {
3395 String str = get(KEY_VIDEO_STABILIZATION);
3396 return TRUE.equals(str);
3397 }
3398
3399 /**
3400 * Returns true if video stabilization is supported. See
3401 * {@link #setVideoStabilization} for details of video stabilization.
3402 *
3403 * @return true if video stabilization is supported
3404 * @see #setVideoStabilization(boolean)
3405 * @see #getVideoStabilization()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003406 */
3407 public boolean isVideoStabilizationSupported() {
3408 String str = get(KEY_VIDEO_STABILIZATION_SUPPORTED);
3409 return TRUE.equals(str);
3410 }
3411
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003412 // Splits a comma delimited string to an ArrayList of String.
3413 // Return null if the passing string is null or the size is 0.
3414 private ArrayList<String> split(String str) {
3415 if (str == null) return null;
3416
3417 // Use StringTokenizer because it is faster than split.
3418 StringTokenizer tokenizer = new StringTokenizer(str, ",");
3419 ArrayList<String> substrings = new ArrayList<String>();
3420 while (tokenizer.hasMoreElements()) {
3421 substrings.add(tokenizer.nextToken());
3422 }
3423 return substrings;
3424 }
3425
3426 // Splits a comma delimited string to an ArrayList of Integer.
3427 // Return null if the passing string is null or the size is 0.
3428 private ArrayList<Integer> splitInt(String str) {
3429 if (str == null) return null;
3430
3431 StringTokenizer tokenizer = new StringTokenizer(str, ",");
3432 ArrayList<Integer> substrings = new ArrayList<Integer>();
3433 while (tokenizer.hasMoreElements()) {
3434 String token = tokenizer.nextToken();
3435 substrings.add(Integer.parseInt(token));
3436 }
3437 if (substrings.size() == 0) return null;
3438 return substrings;
3439 }
3440
Wu-cheng Li454630f2010-08-11 16:48:05 -07003441 private void splitInt(String str, int[] output) {
3442 if (str == null) return;
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003443
3444 StringTokenizer tokenizer = new StringTokenizer(str, ",");
Wu-cheng Li454630f2010-08-11 16:48:05 -07003445 int index = 0;
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003446 while (tokenizer.hasMoreElements()) {
3447 String token = tokenizer.nextToken();
Wu-cheng Li454630f2010-08-11 16:48:05 -07003448 output[index++] = Integer.parseInt(token);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003449 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07003450 }
3451
3452 // Splits a comma delimited string to an ArrayList of Float.
3453 private void splitFloat(String str, float[] output) {
3454 if (str == null) return;
3455
3456 StringTokenizer tokenizer = new StringTokenizer(str, ",");
3457 int index = 0;
3458 while (tokenizer.hasMoreElements()) {
3459 String token = tokenizer.nextToken();
3460 output[index++] = Float.parseFloat(token);
3461 }
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003462 }
3463
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003464 // Returns the value of a float parameter.
3465 private float getFloat(String key, float defaultValue) {
3466 try {
3467 return Float.parseFloat(mMap.get(key));
3468 } catch (NumberFormatException ex) {
3469 return defaultValue;
3470 }
3471 }
3472
3473 // Returns the value of a integer parameter.
3474 private int getInt(String key, int defaultValue) {
3475 try {
3476 return Integer.parseInt(mMap.get(key));
3477 } catch (NumberFormatException ex) {
3478 return defaultValue;
3479 }
3480 }
3481
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003482 // Splits a comma delimited string to an ArrayList of Size.
3483 // Return null if the passing string is null or the size is 0.
3484 private ArrayList<Size> splitSize(String str) {
3485 if (str == null) return null;
3486
3487 StringTokenizer tokenizer = new StringTokenizer(str, ",");
3488 ArrayList<Size> sizeList = new ArrayList<Size>();
3489 while (tokenizer.hasMoreElements()) {
3490 Size size = strToSize(tokenizer.nextToken());
3491 if (size != null) sizeList.add(size);
3492 }
3493 if (sizeList.size() == 0) return null;
3494 return sizeList;
3495 }
3496
3497 // Parses a string (ex: "480x320") to Size object.
3498 // Return null if the passing string is null.
3499 private Size strToSize(String str) {
3500 if (str == null) return null;
3501
3502 int pos = str.indexOf('x');
3503 if (pos != -1) {
3504 String width = str.substring(0, pos);
3505 String height = str.substring(pos + 1);
3506 return new Size(Integer.parseInt(width),
3507 Integer.parseInt(height));
3508 }
3509 Log.e(TAG, "Invalid size parameter string=" + str);
3510 return null;
3511 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07003512
3513 // Splits a comma delimited string to an ArrayList of int array.
3514 // Example string: "(10000,26623),(10000,30000)". Return null if the
3515 // passing string is null or the size is 0.
3516 private ArrayList<int[]> splitRange(String str) {
3517 if (str == null || str.charAt(0) != '('
3518 || str.charAt(str.length() - 1) != ')') {
3519 Log.e(TAG, "Invalid range list string=" + str);
3520 return null;
3521 }
3522
3523 ArrayList<int[]> rangeList = new ArrayList<int[]>();
3524 int endIndex, fromIndex = 1;
3525 do {
3526 int[] range = new int[2];
3527 endIndex = str.indexOf("),(", fromIndex);
3528 if (endIndex == -1) endIndex = str.length() - 1;
3529 splitInt(str.substring(fromIndex, endIndex), range);
3530 rangeList.add(range);
3531 fromIndex = endIndex + 3;
3532 } while (endIndex != str.length() - 1);
3533
3534 if (rangeList.size() == 0) return null;
3535 return rangeList;
3536 }
Wu-cheng Li30771b72011-04-02 06:19:46 +08003537
3538 // Splits a comma delimited string to an ArrayList of Area objects.
3539 // Example string: "(-10,-10,0,0,300),(0,0,10,10,700)". Return null if
Wu-cheng Lif715bf92011-04-14 14:04:18 +08003540 // the passing string is null or the size is 0 or (0,0,0,0,0).
Wu-cheng Li30771b72011-04-02 06:19:46 +08003541 private ArrayList<Area> splitArea(String str) {
3542 if (str == null || str.charAt(0) != '('
3543 || str.charAt(str.length() - 1) != ')') {
3544 Log.e(TAG, "Invalid area string=" + str);
3545 return null;
3546 }
3547
3548 ArrayList<Area> result = new ArrayList<Area>();
3549 int endIndex, fromIndex = 1;
3550 int[] array = new int[5];
3551 do {
3552 endIndex = str.indexOf("),(", fromIndex);
3553 if (endIndex == -1) endIndex = str.length() - 1;
3554 splitInt(str.substring(fromIndex, endIndex), array);
3555 Rect rect = new Rect(array[0], array[1], array[2], array[3]);
3556 result.add(new Area(rect, array[4]));
3557 fromIndex = endIndex + 3;
3558 } while (endIndex != str.length() - 1);
3559
3560 if (result.size() == 0) return null;
Wu-cheng Lif715bf92011-04-14 14:04:18 +08003561
3562 if (result.size() == 1) {
Wu-cheng Libde61a52011-06-07 18:23:14 +08003563 Area area = result.get(0);
Wu-cheng Lif715bf92011-04-14 14:04:18 +08003564 Rect rect = area.rect;
3565 if (rect.left == 0 && rect.top == 0 && rect.right == 0
3566 && rect.bottom == 0 && area.weight == 0) {
3567 return null;
3568 }
3569 }
3570
Wu-cheng Li30771b72011-04-02 06:19:46 +08003571 return result;
3572 }
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08003573
3574 private boolean same(String s1, String s2) {
3575 if (s1 == null && s2 == null) return true;
3576 if (s1 != null && s1.equals(s2)) return true;
3577 return false;
3578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 };
3580}