blob: 1e8671b6a1ddc3f02fc288b1c7abad2551e99ac0 [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;
Ali Utku Selen0a120182011-02-09 14:11:22 +010029import android.text.TextUtils;
Wu-cheng Libde61a52011-06-07 18:23:14 +080030import android.view.Surface;
31import android.view.SurfaceHolder;
32
33import java.io.IOException;
34import java.lang.ref.WeakReference;
35import java.util.ArrayList;
36import java.util.HashMap;
37import java.util.List;
James Dong248ba232012-04-28 21:30:46 -070038import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40/**
Dan Egnorbfcbeff2010-07-12 15:12:54 -070041 * The Camera class is used to set image capture settings, start/stop preview,
42 * snap pictures, and retrieve frames for encoding for video. This class is a
43 * client for the Camera service, which manages the actual camera hardware.
Scott Maindf4578e2009-09-10 12:22:07 -070044 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070045 * <p>To access the device camera, you must declare the
Wu-cheng Li7478ea62009-09-16 18:52:55 +080046 * {@link android.Manifest.permission#CAMERA} permission in your Android
Scott Maindf4578e2009-09-10 12:22:07 -070047 * Manifest. Also be sure to include the
48 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
Dan Egnorbfcbeff2010-07-12 15:12:54 -070049 * manifest element to declare camera features used by your application.
Wu-cheng Li7478ea62009-09-16 18:52:55 +080050 * For example, if you use the camera and auto-focus feature, your Manifest
Scott Maindf4578e2009-09-10 12:22:07 -070051 * should include the following:</p>
52 * <pre> &lt;uses-permission android:name="android.permission.CAMERA" />
53 * &lt;uses-feature android:name="android.hardware.camera" />
54 * &lt;uses-feature android:name="android.hardware.camera.autofocus" /></pre>
55 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070056 * <p>To take pictures with this class, use the following steps:</p>
57 *
58 * <ol>
Dan Egnor341ff132010-07-20 11:30:17 -070059 * <li>Obtain an instance of Camera from {@link #open(int)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -070060 *
61 * <li>Get existing (default) settings with {@link #getParameters()}.
62 *
63 * <li>If necessary, modify the returned {@link Camera.Parameters} object and call
64 * {@link #setParameters(Camera.Parameters)}.
65 *
66 * <li>If desired, call {@link #setDisplayOrientation(int)}.
67 *
68 * <li><b>Important</b>: Pass a fully initialized {@link SurfaceHolder} to
69 * {@link #setPreviewDisplay(SurfaceHolder)}. Without a surface, the camera
70 * will be unable to start the preview.
71 *
72 * <li><b>Important</b>: Call {@link #startPreview()} to start updating the
73 * preview surface. Preview must be started before you can take a picture.
74 *
75 * <li>When you want, call {@link #takePicture(Camera.ShutterCallback,
76 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)} to
77 * capture a photo. Wait for the callbacks to provide the actual image data.
78 *
79 * <li>After taking a picture, preview display will have stopped. To take more
80 * photos, call {@link #startPreview()} again first.
81 *
82 * <li>Call {@link #stopPreview()} to stop updating the preview surface.
83 *
84 * <li><b>Important:</b> Call {@link #release()} to release the camera for
85 * use by other applications. Applications should release the camera
86 * immediately in {@link android.app.Activity#onPause()} (and re-{@link #open()}
87 * it in {@link android.app.Activity#onResume()}).
88 * </ol>
89 *
90 * <p>To quickly switch to video recording mode, use these steps:</p>
91 *
92 * <ol>
93 * <li>Obtain and initialize a Camera and start preview as described above.
94 *
95 * <li>Call {@link #unlock()} to allow the media process to access the camera.
96 *
97 * <li>Pass the camera to {@link android.media.MediaRecorder#setCamera(Camera)}.
98 * See {@link android.media.MediaRecorder} information about video recording.
99 *
100 * <li>When finished recording, call {@link #reconnect()} to re-acquire
101 * and re-lock the camera.
102 *
103 * <li>If desired, restart preview and take more photos or videos.
104 *
105 * <li>Call {@link #stopPreview()} and {@link #release()} as described above.
106 * </ol>
107 *
108 * <p>This class is not thread-safe, and is meant for use from one event thread.
109 * Most long-running operations (preview, focus, photo capture, etc) happen
110 * asynchronously and invoke callbacks as necessary. Callbacks will be invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700111 * on the event thread {@link #open(int)} was called from. This class's methods
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700112 * must never be called from multiple threads at once.</p>
113 *
Scott Maindf4578e2009-09-10 12:22:07 -0700114 * <p class="caution"><strong>Caution:</strong> Different Android-powered devices
115 * may have different hardware specifications, such as megapixel ratings and
116 * auto-focus capabilities. In order for your application to be compatible with
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800117 * more devices, you should not make assumptions about the device camera
Scott Maindf4578e2009-09-10 12:22:07 -0700118 * specifications.</p>
Joe Fernandez6c5c3c32011-10-18 14:57:05 -0700119 *
120 * <div class="special reference">
121 * <h3>Developer Guides</h3>
122 * <p>For more information about using cameras, read the
123 * <a href="{@docRoot}guide/topics/media/camera.html">Camera</a> developer guide.</p>
124 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 */
126public class Camera {
127 private static final String TAG = "Camera";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800128
Wu-cheng Lid2c29292010-05-28 17:32:41 +0800129 // These match the enums in frameworks/base/include/camera/Camera.h
Benny Wongda83f462009-08-12 12:01:27 -0500130 private static final int CAMERA_MSG_ERROR = 0x001;
131 private static final int CAMERA_MSG_SHUTTER = 0x002;
132 private static final int CAMERA_MSG_FOCUS = 0x004;
133 private static final int CAMERA_MSG_ZOOM = 0x008;
134 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010;
135 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020;
136 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040;
137 private static final int CAMERA_MSG_RAW_IMAGE = 0x080;
138 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800139 private static final int CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x200;
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800140 private static final int CAMERA_MSG_PREVIEW_METADATA = 0x400;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800141 private static final int CAMERA_MSG_FOCUS_MOVE = 0x800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
143 private int mNativeContext; // accessed by native methods
144 private EventHandler mEventHandler;
145 private ShutterCallback mShutterCallback;
146 private PictureCallback mRawImageCallback;
147 private PictureCallback mJpegCallback;
148 private PreviewCallback mPreviewCallback;
Dave Sparkse8b26e12009-07-14 10:35:40 -0700149 private PictureCallback mPostviewCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 private AutoFocusCallback mAutoFocusCallback;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800151 private AutoFocusMoveCallback mAutoFocusMoveCallback;
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800152 private OnZoomChangeListener mZoomListener;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800153 private FaceDetectionListener mFaceListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 private ErrorCallback mErrorCallback;
155 private boolean mOneShot;
Andrew Harp94927df2009-10-20 01:47:05 -0400156 private boolean mWithBuffer;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800157 private boolean mFaceDetectionRunning = false;
Wu-cheng Lif05c1d62012-05-02 23:29:47 +0800158 private Object mAutoFocusCallbackLock = new Object();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 /**
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800161 * Broadcast Action: A new picture is taken by the camera, and the entry of
162 * the picture has been added to the media store.
163 * {@link android.content.Intent#getData} is URI of the picture.
164 */
165 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
166 public static final String ACTION_NEW_PICTURE = "android.hardware.action.NEW_PICTURE";
167
168 /**
169 * Broadcast Action: A new video is recorded by the camera, and the entry
170 * of the video has been added to the media store.
171 * {@link android.content.Intent#getData} is URI of the video.
172 */
173 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
174 public static final String ACTION_NEW_VIDEO = "android.hardware.action.NEW_VIDEO";
175
176 /**
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800177 * Hardware face detection. It does not use much CPU.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800178 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800179 private static final int CAMERA_FACE_DETECTION_HW = 0;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800180
181 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800182 * Software face detection. It uses some CPU.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800183 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800184 private static final int CAMERA_FACE_DETECTION_SW = 1;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800185
186 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700187 * Returns the number of physical cameras available on this device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 */
Chih-Chung Change25cc652010-05-06 16:36:58 +0800189 public native static int getNumberOfCameras();
190
191 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700192 * Returns the information about a particular camera.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800193 * If {@link #getNumberOfCameras()} returns N, the valid id is 0 to N-1.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800194 */
195 public native static void getCameraInfo(int cameraId, CameraInfo cameraInfo);
196
197 /**
198 * Information about a camera
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800199 */
200 public static class CameraInfo {
Wu-cheng Li78366602010-09-15 14:08:15 -0700201 /**
202 * The facing of the camera is opposite to that of the screen.
203 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800204 public static final int CAMERA_FACING_BACK = 0;
Wu-cheng Li78366602010-09-15 14:08:15 -0700205
206 /**
207 * The facing of the camera is the same as that of the screen.
208 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800209 public static final int CAMERA_FACING_FRONT = 1;
210
211 /**
Joe Fernandez464cb212011-10-04 16:56:47 -0700212 * The direction that the camera faces. It should be
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800213 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
214 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700215 public int facing;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800216
217 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700218 * <p>The orientation of the camera image. The value is the angle that the
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800219 * camera image needs to be rotated clockwise so it shows correctly on
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700220 * the display in its natural orientation. It should be 0, 90, 180, or 270.</p>
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800221 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700222 * <p>For example, suppose a device has a naturally tall screen. The
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +0800223 * back-facing camera sensor is mounted in landscape. You are looking at
224 * the screen. If the top side of the camera sensor is aligned with the
225 * right edge of the screen in natural orientation, the value should be
226 * 90. If the top side of a front-facing camera sensor is aligned with
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700227 * the right of the screen, the value should be 270.</p>
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800228 *
229 * @see #setDisplayOrientation(int)
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800230 * @see Parameters#setRotation(int)
231 * @see Parameters#setPreviewSize(int, int)
232 * @see Parameters#setPictureSize(int, int)
233 * @see Parameters#setJpegThumbnailSize(int, int)
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800234 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700235 public int orientation;
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -0700236
237 /**
238 * <p>Whether the shutter sound can be disabled.</p>
239 *
240 * <p>On some devices, the camera shutter sound cannot be turned off
241 * through {@link #enableShutterSound enableShutterSound}. This field
242 * can be used to determine whether a call to disable the shutter sound
243 * will succeed.</p>
244 *
245 * <p>If this field is set to true, then a call of
246 * {@code enableShutterSound(false)} will be successful. If set to
247 * false, then that call will fail, and the shutter sound will be played
248 * when {@link Camera#takePicture takePicture} is called.</p>
249 */
250 public boolean canDisableShutterSound;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800251 };
252
253 /**
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800254 * Creates a new Camera object to access a particular hardware camera. If
255 * the same camera is opened by other applications, this will throw a
256 * RuntimeException.
257 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700258 * <p>You must call {@link #release()} when you are done using the camera,
259 * otherwise it will remain locked and be unavailable to other applications.
260 *
Dan Egnor341ff132010-07-20 11:30:17 -0700261 * <p>Your application should only have one Camera object active at a time
262 * for a particular hardware camera.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700263 *
264 * <p>Callbacks from other methods are delivered to the event loop of the
265 * thread which called open(). If this thread has no event loop, then
266 * callbacks are delivered to the main application event loop. If there
267 * is no main application event loop, callbacks are not delivered.
268 *
269 * <p class="caution"><b>Caution:</b> On some devices, this method may
270 * take a long time to complete. It is best to call this method from a
271 * worker thread (possibly using {@link android.os.AsyncTask}) to avoid
272 * blocking the main application UI thread.
273 *
Dan Egnor341ff132010-07-20 11:30:17 -0700274 * @param cameraId the hardware camera to access, between 0 and
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800275 * {@link #getNumberOfCameras()}-1.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700276 * @return a new Camera object, connected, locked and ready for use.
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800277 * @throws RuntimeException if opening the camera fails (for example, if the
278 * camera is in use by another process or device policy manager has
279 * disabled the camera).
Wu-cheng Lifacc8ce2011-06-17 13:09:40 +0800280 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800281 */
282 public static Camera open(int cameraId) {
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800283 return new Camera(cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
285
Chih-Chung Change25cc652010-05-06 16:36:58 +0800286 /**
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800287 * Creates a new Camera object to access the first back-facing camera on the
288 * device. If the device does not have a back-facing camera, this returns
289 * null.
Wu-cheng Li78366602010-09-15 14:08:15 -0700290 * @see #open(int)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800291 */
292 public static Camera open() {
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800293 int numberOfCameras = getNumberOfCameras();
294 CameraInfo cameraInfo = new CameraInfo();
295 for (int i = 0; i < numberOfCameras; i++) {
296 getCameraInfo(i, cameraInfo);
297 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800298 return new Camera(i);
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800299 }
300 }
301 return null;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800302 }
303
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800304 Camera(int cameraId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 mShutterCallback = null;
306 mRawImageCallback = null;
307 mJpegCallback = null;
308 mPreviewCallback = null;
Dave Sparkse8b26e12009-07-14 10:35:40 -0700309 mPostviewCallback = null;
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800310 mZoomListener = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311
312 Looper looper;
313 if ((looper = Looper.myLooper()) != null) {
314 mEventHandler = new EventHandler(this, looper);
315 } else if ((looper = Looper.getMainLooper()) != null) {
316 mEventHandler = new EventHandler(this, looper);
317 } else {
318 mEventHandler = null;
319 }
320
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800321 native_setup(new WeakReference<Camera>(this), cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800323
Wu-cheng Li1c04a332011-11-22 18:21:18 +0800324 /**
325 * An empty Camera for testing purpose.
326 */
327 Camera() {
328 }
329
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800330 protected void finalize() {
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -0800331 release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800333
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800334 private native final void native_setup(Object camera_this, int cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 private native final void native_release();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
338 /**
339 * Disconnects and releases the Camera object resources.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700340 *
341 * <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 -0800342 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800343 public final void release() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 native_release();
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800345 mFaceDetectionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347
348 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700349 * Unlocks the camera to allow another process to access it.
350 * Normally, the camera is locked to the process with an active Camera
351 * object until {@link #release()} is called. To allow rapid handoff
352 * between processes, you can call this method to release the camera
353 * temporarily for another process to use; once the other process is done
354 * you can call {@link #reconnect()} to reclaim the camera.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700356 * <p>This must be done before calling
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800357 * {@link android.media.MediaRecorder#setCamera(Camera)}. This cannot be
358 * called after recording starts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700360 * <p>If you are not recording video, you probably do not need this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700362 * @throws RuntimeException if the camera cannot be unlocked.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 */
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800364 public native final void unlock();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700367 * Re-locks the camera to prevent other processes from accessing it.
368 * Camera objects are locked by default unless {@link #unlock()} is
369 * called. Normally {@link #reconnect()} is used instead.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800370 *
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.
375 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700376 * <p>If you are not recording video, you probably do not need this method.
377 *
378 * @throws RuntimeException if the camera cannot be re-locked (for
379 * example, if the camera is still in use by another process).
380 */
381 public native final void lock();
382
383 /**
384 * Reconnects to the camera service after another process used it.
385 * After {@link #unlock()} is called, another process may use the
386 * camera; when the process is done, you must reconnect to the camera,
387 * which will re-acquire the lock and allow you to continue using the
388 * camera.
389 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800390 * <p>Since API level 14, camera is automatically locked for applications in
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800391 * {@link android.media.MediaRecorder#start()}. Applications can use the
392 * camera (ex: zoom) after recording starts. There is no need to call this
393 * after recording starts or stops.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700394 *
395 * <p>If you are not recording video, you probably do not need this method.
396 *
397 * @throws IOException if a connection cannot be re-established (for
398 * example, if the camera is still in use by another process).
399 */
400 public native final void reconnect() throws IOException;
401
402 /**
403 * Sets the {@link Surface} to be used for live preview.
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800404 * Either a surface or surface texture is necessary for preview, and
405 * preview is necessary to take pictures. The same surface can be re-set
406 * without harm. Setting a preview surface will un-set any preview surface
407 * texture that was set via {@link #setPreviewTexture}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700408 *
409 * <p>The {@link SurfaceHolder} must already contain a surface when this
410 * method is called. If you are using {@link android.view.SurfaceView},
411 * you will need to register a {@link SurfaceHolder.Callback} with
412 * {@link SurfaceHolder#addCallback(SurfaceHolder.Callback)} and wait for
413 * {@link SurfaceHolder.Callback#surfaceCreated(SurfaceHolder)} before
414 * calling setPreviewDisplay() or starting preview.
415 *
416 * <p>This method must be called before {@link #startPreview()}. The
417 * one exception is that if the preview surface is not set (or set to null)
418 * before startPreview() is called, then this method may be called once
419 * with a non-null parameter to set the preview surface. (This allows
420 * camera setup and surface creation to happen in parallel, saving time.)
421 * The preview surface may not otherwise change while preview is running.
422 *
423 * @param holder containing the Surface on which to place the preview,
424 * or null to remove the preview surface
425 * @throws IOException if the method fails (for example, if the surface
426 * is unavailable or unsuitable).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 */
428 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException {
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800429 if (holder != null) {
430 setPreviewDisplay(holder.getSurface());
431 } else {
432 setPreviewDisplay((Surface)null);
433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 }
435
Glenn Kastendbc289d2011-02-09 10:15:44 -0800436 private native final void setPreviewDisplay(Surface surface) throws IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437
438 /**
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800439 * Sets the {@link SurfaceTexture} to be used for live preview.
440 * Either a surface or surface texture is necessary for preview, and
441 * preview is necessary to take pictures. The same surface texture can be
442 * re-set without harm. Setting a preview surface texture will un-set any
443 * preview surface that was set via {@link #setPreviewDisplay}.
444 *
445 * <p>This method must be called before {@link #startPreview()}. The
446 * one exception is that if the preview surface texture is not set (or set
447 * to null) before startPreview() is called, then this method may be called
448 * once with a non-null parameter to set the preview surface. (This allows
449 * camera setup and surface creation to happen in parallel, saving time.)
450 * The preview surface texture may not otherwise change while preview is
451 * running.
452 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700453 * <p>The timestamps provided by {@link SurfaceTexture#getTimestamp()} for a
Eino-Ville Talvalae309a0f2011-03-21 11:04:34 -0700454 * SurfaceTexture set as the preview texture have an unspecified zero point,
455 * and cannot be directly compared between different cameras or different
456 * instances of the same camera, or across multiple runs of the same
457 * program.
458 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800459 * <p>If you are using the preview data to create video or still images,
460 * strongly consider using {@link android.media.MediaActionSound} to
461 * properly indicate image capture or recording start/stop to the user.</p>
462 *
463 * @see android.media.MediaActionSound
464 * @see android.graphics.SurfaceTexture
465 * @see android.view.TextureView
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800466 * @param surfaceTexture the {@link SurfaceTexture} to which the preview
467 * images are to be sent or null to remove the current preview surface
468 * texture
469 * @throws IOException if the method fails (for example, if the surface
470 * texture is unavailable or unsuitable).
471 */
Glenn Kastendbc289d2011-02-09 10:15:44 -0800472 public native final void setPreviewTexture(SurfaceTexture surfaceTexture) throws IOException;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800473
474 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700475 * Callback interface used to deliver copies of preview frames as
476 * they are displayed.
477 *
478 * @see #setPreviewCallback(Camera.PreviewCallback)
479 * @see #setOneShotPreviewCallback(Camera.PreviewCallback)
480 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
481 * @see #startPreview()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 */
483 public interface PreviewCallback
484 {
485 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700486 * Called as preview frames are displayed. This callback is invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700487 * on the event thread {@link #open(int)} was called from.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700489 * <p>If using the {@link android.graphics.ImageFormat#YV12} format,
490 * refer to the equations in {@link Camera.Parameters#setPreviewFormat}
491 * for the arrangement of the pixel data in the preview callback
492 * buffers.
493 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700494 * @param data the contents of the preview frame in the format defined
Mathias Agopiana696f5d2010-02-17 17:53:09 -0800495 * by {@link android.graphics.ImageFormat}, which can be queried
Scott Maindf4578e2009-09-10 12:22:07 -0700496 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
Scott Mainda0a56d2009-09-10 18:08:37 -0700497 * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800498 * is never called, the default will be the YCbCr_420_SP
499 * (NV21) format.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700500 * @param camera the Camera service object.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 */
502 void onPreviewFrame(byte[] data, Camera camera);
503 };
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700506 * Starts capturing and drawing preview frames to the screen.
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800507 * Preview will not actually start until a surface is supplied
508 * with {@link #setPreviewDisplay(SurfaceHolder)} or
509 * {@link #setPreviewTexture(SurfaceTexture)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700510 *
511 * <p>If {@link #setPreviewCallback(Camera.PreviewCallback)},
512 * {@link #setOneShotPreviewCallback(Camera.PreviewCallback)}, or
513 * {@link #setPreviewCallbackWithBuffer(Camera.PreviewCallback)} were
514 * called, {@link Camera.PreviewCallback#onPreviewFrame(byte[], Camera)}
515 * will be called when preview data becomes available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 */
517 public native final void startPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700520 * Stops capturing and drawing preview frames to the surface, and
521 * resets the camera for a future call to {@link #startPreview()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 */
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800523 public final void stopPreview() {
524 _stopPreview();
525 mFaceDetectionRunning = false;
Chih-yu Huang664d72e2011-09-23 18:59:38 +0800526
527 mShutterCallback = null;
528 mRawImageCallback = null;
529 mPostviewCallback = null;
530 mJpegCallback = null;
Wu-cheng Lif05c1d62012-05-02 23:29:47 +0800531 synchronized (mAutoFocusCallbackLock) {
532 mAutoFocusCallback = null;
533 }
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800534 mAutoFocusMoveCallback = null;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800535 }
536
537 private native final void _stopPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 /**
540 * Return current preview state.
541 *
542 * FIXME: Unhide before release
543 * @hide
544 */
545 public native final boolean previewEnabled();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800548 * <p>Installs a callback to be invoked for every preview frame in addition
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700549 * to displaying them on the screen. The callback will be repeatedly called
550 * for as long as preview is active. This method can be called at any time,
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800551 * even while preview is live. Any other preview callbacks are
552 * overridden.</p>
553 *
554 * <p>If you are using the preview data to create video or still images,
555 * strongly consider using {@link android.media.MediaActionSound} to
556 * properly indicate image capture or recording start/stop to the user.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700558 * @param cb a callback object that receives a copy of each preview frame,
559 * or null to stop receiving callbacks.
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800560 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 */
562 public final void setPreviewCallback(PreviewCallback cb) {
563 mPreviewCallback = cb;
564 mOneShot = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400565 mWithBuffer = false;
Dave Sparksa6118c62009-10-13 02:28:54 -0700566 // Always use one-shot mode. We fake camera preview mode by
567 // doing one-shot preview continuously.
Andrew Harp94927df2009-10-20 01:47:05 -0400568 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570
571 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800572 * <p>Installs a callback to be invoked for the next preview frame in
573 * addition to displaying it on the screen. After one invocation, the
574 * callback is cleared. This method can be called any time, even when
575 * preview is live. Any other preview callbacks are overridden.</p>
576 *
577 * <p>If you are using the preview data to create video or still images,
578 * strongly consider using {@link android.media.MediaActionSound} to
579 * properly indicate image capture or recording start/stop to the user.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700581 * @param cb a callback object that receives a copy of the next preview frame,
582 * or null to stop receiving callbacks.
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800583 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 */
585 public final void setOneShotPreviewCallback(PreviewCallback cb) {
Andrew Harp94927df2009-10-20 01:47:05 -0400586 mPreviewCallback = cb;
587 mOneShot = true;
588 mWithBuffer = false;
589 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 }
591
Andrew Harp94927df2009-10-20 01:47:05 -0400592 private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer);
593
594 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800595 * <p>Installs a callback to be invoked for every preview frame, using
596 * buffers supplied with {@link #addCallbackBuffer(byte[])}, in addition to
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700597 * displaying them on the screen. The callback will be repeatedly called
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800598 * for as long as preview is active and buffers are available. Any other
599 * preview callbacks are overridden.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400600 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700601 * <p>The purpose of this method is to improve preview efficiency and frame
602 * rate by allowing preview frame memory reuse. You must call
603 * {@link #addCallbackBuffer(byte[])} at some point -- before or after
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800604 * calling this method -- or no callbacks will received.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400605 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800606 * <p>The buffer queue will be cleared if this method is called with a null
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700607 * callback, {@link #setPreviewCallback(Camera.PreviewCallback)} is called,
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800608 * or {@link #setOneShotPreviewCallback(Camera.PreviewCallback)} is
609 * called.</p>
610 *
611 * <p>If you are using the preview data to create video or still images,
612 * strongly consider using {@link android.media.MediaActionSound} to
613 * properly indicate image capture or recording start/stop to the user.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400614 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700615 * @param cb a callback object that receives a copy of the preview frame,
616 * or null to stop receiving callbacks and clear the buffer queue.
617 * @see #addCallbackBuffer(byte[])
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800618 * @see android.media.MediaActionSound
Andrew Harp94927df2009-10-20 01:47:05 -0400619 */
620 public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
621 mPreviewCallback = cb;
622 mOneShot = false;
623 mWithBuffer = true;
624 setHasPreviewCallback(cb != null, true);
625 }
626
627 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800628 * Adds a pre-allocated buffer to the preview callback buffer queue.
629 * Applications can add one or more buffers to the queue. When a preview
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700630 * frame arrives and there is still at least one available buffer, the
631 * buffer will be used and removed from the queue. Then preview callback is
632 * invoked with the buffer. If a frame arrives and there is no buffer left,
633 * the frame is discarded. Applications should add buffers back when they
634 * finish processing the data in them.
Wu-cheng Lic10275a2010-03-09 13:49:21 -0800635 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700636 * <p>For formats besides YV12, the size of the buffer is determined by
637 * multiplying the preview image width, height, and bytes per pixel. The
638 * width and height can be read from
639 * {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel can be
640 * computed from {@link android.graphics.ImageFormat#getBitsPerPixel(int)} /
641 * 8, using the image format from
642 * {@link Camera.Parameters#getPreviewFormat()}.
643 *
644 * <p>If using the {@link android.graphics.ImageFormat#YV12} format, the
645 * size can be calculated using the equations listed in
646 * {@link Camera.Parameters#setPreviewFormat}.
Andrew Harp94927df2009-10-20 01:47:05 -0400647 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700648 * <p>This method is only necessary when
James Donge00cab72011-02-17 16:38:06 -0800649 * {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700650 * {@link #setPreviewCallback(PreviewCallback)} or
651 * {@link #setOneShotPreviewCallback(PreviewCallback)} are used, buffers
James Donge00cab72011-02-17 16:38:06 -0800652 * are automatically allocated. When a supplied buffer is too small to
653 * hold the preview frame data, preview callback will return null and
654 * the buffer will be removed from the buffer queue.
Andrew Harp94927df2009-10-20 01:47:05 -0400655 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700656 * @param callbackBuffer the buffer to add to the queue. The size of the
657 * buffer must match the values described above.
Wu-cheng Li5b9bcda2010-03-07 14:59:28 -0800658 * @see #setPreviewCallbackWithBuffer(PreviewCallback)
Andrew Harp94927df2009-10-20 01:47:05 -0400659 */
James Donge00cab72011-02-17 16:38:06 -0800660 public final void addCallbackBuffer(byte[] callbackBuffer)
661 {
662 _addCallbackBuffer(callbackBuffer, CAMERA_MSG_PREVIEW_FRAME);
663 }
664
665 /**
666 * Adds a pre-allocated buffer to the raw image callback buffer queue.
667 * Applications can add one or more buffers to the queue. When a raw image
668 * frame arrives and there is still at least one available buffer, the
669 * buffer will be used to hold the raw image data and removed from the
670 * queue. Then raw image callback is invoked with the buffer. If a raw
671 * image frame arrives but there is no buffer left, the frame is
672 * discarded. Applications should add buffers back when they finish
673 * processing the data in them by calling this method again in order
674 * to avoid running out of raw image callback buffers.
675 *
676 * <p>The size of the buffer is determined by multiplying the raw image
677 * width, height, and bytes per pixel. The width and height can be
678 * read from {@link Camera.Parameters#getPictureSize()}. Bytes per pixel
679 * can be computed from
680 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
681 * using the image format from {@link Camera.Parameters#getPreviewFormat()}.
682 *
683 * <p>This method is only necessary when the PictureCallbck for raw image
684 * is used while calling {@link #takePicture(Camera.ShutterCallback,
685 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
686 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700687 * <p>Please note that by calling this method, the mode for
688 * application-managed callback buffers is triggered. If this method has
689 * never been called, null will be returned by the raw image callback since
690 * there is no image callback buffer available. Furthermore, When a supplied
691 * buffer is too small to hold the raw image data, raw image callback will
692 * return null and the buffer will be removed from the buffer queue.
James Donge00cab72011-02-17 16:38:06 -0800693 *
694 * @param callbackBuffer the buffer to add to the raw image callback buffer
695 * queue. The size should be width * height * (bits per pixel) / 8. An
696 * null callbackBuffer will be ignored and won't be added to the queue.
697 *
698 * @see #takePicture(Camera.ShutterCallback,
699 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
700 *
701 * {@hide}
702 */
703 public final void addRawImageCallbackBuffer(byte[] callbackBuffer)
704 {
705 addCallbackBuffer(callbackBuffer, CAMERA_MSG_RAW_IMAGE);
706 }
707
708 private final void addCallbackBuffer(byte[] callbackBuffer, int msgType)
709 {
710 // CAMERA_MSG_VIDEO_FRAME may be allowed in the future.
711 if (msgType != CAMERA_MSG_PREVIEW_FRAME &&
712 msgType != CAMERA_MSG_RAW_IMAGE) {
713 throw new IllegalArgumentException(
714 "Unsupported message type: " + msgType);
715 }
716
717 _addCallbackBuffer(callbackBuffer, msgType);
718 }
719
720 private native final void _addCallbackBuffer(
721 byte[] callbackBuffer, int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722
723 private class EventHandler extends Handler
724 {
725 private Camera mCamera;
726
727 public EventHandler(Camera c, Looper looper) {
728 super(looper);
729 mCamera = c;
730 }
731
732 @Override
733 public void handleMessage(Message msg) {
734 switch(msg.what) {
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700735 case CAMERA_MSG_SHUTTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 if (mShutterCallback != null) {
737 mShutterCallback.onShutter();
738 }
739 return;
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700740
741 case CAMERA_MSG_RAW_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700742 if (mRawImageCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 return;
746
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700747 case CAMERA_MSG_COMPRESSED_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700748 if (mJpegCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800752
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700753 case CAMERA_MSG_PREVIEW_FRAME:
Eino-Ville Talvalaca367b72012-05-30 16:01:33 -0700754 PreviewCallback pCb = mPreviewCallback;
755 if (pCb != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 if (mOneShot) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700757 // Clear the callback variable before the callback
758 // in case the app calls setPreviewCallback from
759 // the callback function
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 mPreviewCallback = null;
Andrew Harp94927df2009-10-20 01:47:05 -0400761 } else if (!mWithBuffer) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700762 // We're faking the camera preview mode to prevent
763 // the app from being flooded with preview frames.
764 // Set to oneshot mode again.
Andrew Harp94927df2009-10-20 01:47:05 -0400765 setHasPreviewCallback(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 }
Eino-Ville Talvalaca367b72012-05-30 16:01:33 -0700767 pCb.onPreviewFrame((byte[])msg.obj, mCamera);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769 return;
770
Dave Sparkse8b26e12009-07-14 10:35:40 -0700771 case CAMERA_MSG_POSTVIEW_FRAME:
772 if (mPostviewCallback != null) {
773 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera);
774 }
775 return;
776
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700777 case CAMERA_MSG_FOCUS:
Wu-cheng Lif05c1d62012-05-02 23:29:47 +0800778 AutoFocusCallback cb = null;
779 synchronized (mAutoFocusCallbackLock) {
780 cb = mAutoFocusCallback;
781 }
782 if (cb != null) {
783 boolean success = msg.arg1 == 0 ? false : true;
784 cb.onAutoFocus(success, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700785 }
786 return;
787
788 case CAMERA_MSG_ZOOM:
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800789 if (mZoomListener != null) {
790 mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 return;
793
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800794 case CAMERA_MSG_PREVIEW_METADATA:
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800795 if (mFaceListener != null) {
Wu-cheng Lif0d6a482011-07-28 05:30:59 +0800796 mFaceListener.onFaceDetection((Face[])msg.obj, mCamera);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800797 }
798 return;
799
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700800 case CAMERA_MSG_ERROR :
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 Log.e(TAG, "Error " + msg.arg1);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700802 if (mErrorCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 mErrorCallback.onError(msg.arg1, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 return;
806
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800807 case CAMERA_MSG_FOCUS_MOVE:
808 if (mAutoFocusMoveCallback != null) {
809 mAutoFocusMoveCallback.onAutoFocusMoving(msg.arg1 == 0 ? false : true, mCamera);
810 }
811 return;
812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 default:
814 Log.e(TAG, "Unknown message type " + msg.what);
815 return;
816 }
817 }
818 }
819
820 private static void postEventFromNative(Object camera_ref,
821 int what, int arg1, int arg2, Object obj)
822 {
823 Camera c = (Camera)((WeakReference)camera_ref).get();
824 if (c == null)
825 return;
826
827 if (c.mEventHandler != null) {
828 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj);
829 c.mEventHandler.sendMessage(m);
830 }
831 }
832
833 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700834 * Callback interface used to notify on completion of camera auto focus.
835 *
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800836 * <p>Devices that do not support auto-focus will receive a "fake"
837 * callback to this interface. If your application needs auto-focus and
Scott Maindf4578e2009-09-10 12:22:07 -0700838 * should not be installed on devices <em>without</em> auto-focus, you must
839 * declare that your app uses the
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800840 * {@code android.hardware.camera.autofocus} feature, in the
Scott Maindf4578e2009-09-10 12:22:07 -0700841 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
842 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700843 *
844 * @see #autoFocus(AutoFocusCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 */
846 public interface AutoFocusCallback
847 {
848 /**
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800849 * Called when the camera auto focus completes. If the camera
850 * does not support auto-focus and autoFocus is called,
851 * onAutoFocus will be called immediately with a fake value of
852 * <code>success</code> set to <code>true</code>.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800853 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +0800854 * The auto-focus routine does not lock auto-exposure and auto-white
855 * balance after it completes.
Eino-Ville Talvala83d33522011-05-13 10:25:00 -0700856 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 * @param success true if focus was successful, false if otherwise
858 * @param camera the Camera service object
Eino-Ville Talvala83d33522011-05-13 10:25:00 -0700859 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
860 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 */
862 void onAutoFocus(boolean success, Camera camera);
Wu-cheng Li53b30912011-10-12 19:43:51 +0800863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864
865 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700866 * Starts camera auto-focus and registers a callback function to run when
867 * the camera is focused. This method is only valid when preview is active
868 * (between {@link #startPreview()} and before {@link #stopPreview()}).
869 *
870 * <p>Callers should check
871 * {@link android.hardware.Camera.Parameters#getFocusMode()} to determine if
872 * this method should be called. If the camera does not support auto-focus,
873 * it is a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)}
Wu-cheng Li36322db2009-09-18 18:59:21 +0800874 * callback will be called immediately.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700875 *
Scott Mainda0a56d2009-09-10 18:08:37 -0700876 * <p>If your application should not be installed
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800877 * on devices without auto-focus, you must declare that your application
878 * uses auto-focus with the
Scott Maindf4578e2009-09-10 12:22:07 -0700879 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
880 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700881 *
Wu-cheng Li068ef422009-09-27 13:19:36 -0700882 * <p>If the current flash mode is not
883 * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700884 * fired during auto-focus, depending on the driver and camera hardware.<p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800885 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800886 * <p>Auto-exposure lock {@link android.hardware.Camera.Parameters#getAutoExposureLock()}
Wu-cheng Lib4f95be2011-09-22 11:43:28 +0800887 * and auto-white balance locks {@link android.hardware.Camera.Parameters#getAutoWhiteBalanceLock()}
888 * do not change during and after autofocus. But auto-focus routine may stop
889 * auto-exposure and auto-white balance transiently during focusing.
Eino-Ville Talvala83d33522011-05-13 10:25:00 -0700890 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800891 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
892 * image capture with {@link #takePicture(Camera.ShutterCallback,
893 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
894 * the focus position. Applications must call cancelAutoFocus to reset the
895 * focus.</p>
896 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800897 * <p>If autofocus is successful, consider using
898 * {@link android.media.MediaActionSound} to properly play back an autofocus
899 * success sound to the user.</p>
900 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 * @param cb the callback to run
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700902 * @see #cancelAutoFocus()
Eino-Ville Talvala83d33522011-05-13 10:25:00 -0700903 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
904 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800905 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 */
907 public final void autoFocus(AutoFocusCallback cb)
908 {
Wu-cheng Lif05c1d62012-05-02 23:29:47 +0800909 synchronized (mAutoFocusCallbackLock) {
James Dong248ba232012-04-28 21:30:46 -0700910 mAutoFocusCallback = cb;
James Dong248ba232012-04-28 21:30:46 -0700911 }
Wu-cheng Lif05c1d62012-05-02 23:29:47 +0800912 native_autoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914 private native final void native_autoFocus();
915
916 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700917 * Cancels any auto-focus function in progress.
918 * Whether or not auto-focus is currently in progress,
919 * this function will return the focus position to the default.
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800920 * If the camera does not support auto-focus, this is a no-op.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700921 *
922 * @see #autoFocus(Camera.AutoFocusCallback)
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800923 */
924 public final void cancelAutoFocus()
925 {
Wu-cheng Lif05c1d62012-05-02 23:29:47 +0800926 synchronized (mAutoFocusCallbackLock) {
James Dong248ba232012-04-28 21:30:46 -0700927 mAutoFocusCallback = null;
James Dong248ba232012-04-28 21:30:46 -0700928 }
Wu-cheng Lif05c1d62012-05-02 23:29:47 +0800929 native_cancelAutoFocus();
930 // CAMERA_MSG_FOCUS should be removed here because the following
931 // scenario can happen:
932 // - An application uses the same thread for autoFocus, cancelAutoFocus
933 // and looper thread.
934 // - The application calls autoFocus.
935 // - HAL sends CAMERA_MSG_FOCUS, which enters the looper message queue.
936 // Before event handler's handleMessage() is invoked, the application
937 // calls cancelAutoFocus and autoFocus.
938 // - The application gets the old CAMERA_MSG_FOCUS and thinks autofocus
939 // has been completed. But in fact it is not.
940 //
941 // As documented in the beginning of the file, apps should not use
942 // multiple threads to call autoFocus and cancelAutoFocus at the same
943 // time. It is HAL's responsibility not to send a CAMERA_MSG_FOCUS
944 // message after native_cancelAutoFocus is called.
945 mEventHandler.removeMessages(CAMERA_MSG_FOCUS);
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800946 }
947 private native final void native_cancelAutoFocus();
948
949 /**
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800950 * Callback interface used to notify on auto focus start and stop.
951 *
Wu-cheng Li65745392012-04-12 18:07:16 +0800952 * <p>This is only supported in continuous autofocus modes -- {@link
953 * Parameters#FOCUS_MODE_CONTINUOUS_VIDEO} and {@link
954 * Parameters#FOCUS_MODE_CONTINUOUS_PICTURE}. Applications can show
955 * autofocus animation based on this.</p>
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800956 */
957 public interface AutoFocusMoveCallback
958 {
959 /**
960 * Called when the camera auto focus starts or stops.
961 *
962 * @param start true if focus starts to move, false if focus stops to move
Wu-cheng Li65745392012-04-12 18:07:16 +0800963 * @param camera the Camera service object
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800964 */
965 void onAutoFocusMoving(boolean start, Camera camera);
966 }
967
968 /**
969 * Sets camera auto-focus move callback.
970 *
971 * @param cb the callback to run
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800972 */
973 public void setAutoFocusMoveCallback(AutoFocusMoveCallback cb) {
974 mAutoFocusMoveCallback = cb;
975 enableFocusMoveCallback((mAutoFocusMoveCallback != null) ? 1 : 0);
976 }
977
978 private native void enableFocusMoveCallback(int enable);
979
980 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700981 * Callback interface used to signal the moment of actual image capture.
982 *
983 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 */
985 public interface ShutterCallback
986 {
987 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700988 * Called as near as possible to the moment when a photo is captured
989 * from the sensor. This is a good opportunity to play a shutter sound
990 * or give other feedback of camera operation. This may be some time
991 * after the photo was triggered, but some time before the actual data
992 * is available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 */
994 void onShutter();
995 }
996
997 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700998 * Callback interface used to supply image data from a photo capture.
999 *
1000 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 */
1002 public interface PictureCallback {
1003 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001004 * Called when image data is available after a picture is taken.
1005 * The format of the data depends on the context of the callback
1006 * and {@link Camera.Parameters} settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 * @param data a byte array of the picture data
1009 * @param camera the Camera service object
1010 */
1011 void onPictureTaken(byte[] data, Camera camera);
1012 };
1013
1014 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001015 * Equivalent to takePicture(shutter, raw, null, jpeg).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001016 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001017 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 */
1019 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
1020 PictureCallback jpeg) {
Dave Sparkse8b26e12009-07-14 10:35:40 -07001021 takePicture(shutter, raw, null, jpeg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
James Donge00cab72011-02-17 16:38:06 -08001023 private native final void native_takePicture(int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024
Dave Sparkse8b26e12009-07-14 10:35:40 -07001025 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001026 * Triggers an asynchronous image capture. The camera service will initiate
1027 * a series of callbacks to the application as the image capture progresses.
1028 * The shutter callback occurs after the image is captured. This can be used
1029 * to trigger a sound to let the user know that image has been captured. The
1030 * raw callback occurs when the raw image data is available (NOTE: the data
James Donge00cab72011-02-17 16:38:06 -08001031 * will be null if there is no raw image callback buffer available or the
1032 * raw image callback buffer is not large enough to hold the raw image).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001033 * The postview callback occurs when a scaled, fully processed postview
1034 * image is available (NOTE: not all hardware supports this). The jpeg
1035 * callback occurs when the compressed image is available. If the
1036 * application does not need a particular callback, a null can be passed
1037 * instead of a callback method.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001038 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001039 * <p>This method is only valid when preview is active (after
1040 * {@link #startPreview()}). Preview will be stopped after the image is
1041 * taken; callers must call {@link #startPreview()} again if they want to
Wu-cheng Li42419ce2011-06-01 17:22:24 +08001042 * re-start preview or take more pictures. This should not be called between
1043 * {@link android.media.MediaRecorder#start()} and
1044 * {@link android.media.MediaRecorder#stop()}.
Wu-cheng Li40057ce2009-12-02 18:57:29 +08001045 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001046 * <p>After calling this method, you must not call {@link #startPreview()}
1047 * or take another picture until the JPEG callback has returned.
1048 *
1049 * @param shutter the callback for image capture moment, or null
1050 * @param raw the callback for raw (uncompressed) image data, or null
Dave Sparkse8b26e12009-07-14 10:35:40 -07001051 * @param postview callback with postview image data, may be null
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001052 * @param jpeg the callback for JPEG image data, or null
Dave Sparkse8b26e12009-07-14 10:35:40 -07001053 */
1054 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
1055 PictureCallback postview, PictureCallback jpeg) {
1056 mShutterCallback = shutter;
1057 mRawImageCallback = raw;
1058 mPostviewCallback = postview;
1059 mJpegCallback = jpeg;
James Donge00cab72011-02-17 16:38:06 -08001060
1061 // If callback is not set, do not send me callbacks.
1062 int msgType = 0;
1063 if (mShutterCallback != null) {
1064 msgType |= CAMERA_MSG_SHUTTER;
1065 }
1066 if (mRawImageCallback != null) {
1067 msgType |= CAMERA_MSG_RAW_IMAGE;
1068 }
1069 if (mPostviewCallback != null) {
1070 msgType |= CAMERA_MSG_POSTVIEW_FRAME;
1071 }
1072 if (mJpegCallback != null) {
1073 msgType |= CAMERA_MSG_COMPRESSED_IMAGE;
1074 }
1075
1076 native_takePicture(msgType);
Wu-cheng Lie9c6c9c2012-05-29 16:47:44 +08001077 mFaceDetectionRunning = false;
Dave Sparkse8b26e12009-07-14 10:35:40 -07001078 }
1079
1080 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001081 * Zooms to the requested value smoothly. The driver will notify {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001082 * OnZoomChangeListener} of the zoom value and whether zoom is stopped at
1083 * the time. For example, suppose the current zoom is 0 and startSmoothZoom
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001084 * is called with value 3. The
1085 * {@link Camera.OnZoomChangeListener#onZoomChange(int, boolean, Camera)}
1086 * method will be called three times with zoom values 1, 2, and 3.
1087 * Applications can call {@link #stopSmoothZoom} to stop the zoom earlier.
1088 * Applications should not call startSmoothZoom again or change the zoom
1089 * value before zoom stops. If the supplied zoom value equals to the current
1090 * zoom value, no zoom callback will be generated. This method is supported
1091 * if {@link android.hardware.Camera.Parameters#isSmoothZoomSupported}
1092 * returns true.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001093 *
1094 * @param value zoom value. The valid range is 0 to {@link
1095 * android.hardware.Camera.Parameters#getMaxZoom}.
Wu-cheng Li0ca25192010-03-29 16:21:12 +08001096 * @throws IllegalArgumentException if the zoom value is invalid.
1097 * @throws RuntimeException if the method fails.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001098 * @see #setZoomChangeListener(OnZoomChangeListener)
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001099 */
1100 public native final void startSmoothZoom(int value);
1101
1102 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001103 * Stops the smooth zoom. Applications should wait for the {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001104 * OnZoomChangeListener} to know when the zoom is actually stopped. This
1105 * method is supported if {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001106 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true.
Wu-cheng Li0ca25192010-03-29 16:21:12 +08001107 *
1108 * @throws RuntimeException if the method fails.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001109 */
1110 public native final void stopSmoothZoom();
1111
1112 /**
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001113 * Set the clockwise rotation of preview display in degrees. This affects
1114 * the preview frames and the picture displayed after snapshot. This method
1115 * is useful for portrait mode applications. Note that preview display of
Wu-cheng Lib982fb42010-10-19 17:19:09 +08001116 * front-facing cameras is flipped horizontally before the rotation, that
1117 * is, the image is reflected along the central vertical axis of the camera
1118 * sensor. So the users can see themselves as looking into a mirror.
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001119 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001120 * <p>This does not affect the order of byte array passed in {@link
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001121 * PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This
1122 * method is not allowed to be called during preview.
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001123 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001124 * <p>If you want to make the camera image show in the same orientation as
1125 * the display, you can use the following code.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001126 * <pre>
Chih-Chung Chang724c5222010-06-14 18:57:15 +08001127 * public static void setCameraDisplayOrientation(Activity activity,
1128 * int cameraId, android.hardware.Camera camera) {
1129 * android.hardware.Camera.CameraInfo info =
1130 * new android.hardware.Camera.CameraInfo();
1131 * android.hardware.Camera.getCameraInfo(cameraId, info);
1132 * int rotation = activity.getWindowManager().getDefaultDisplay()
1133 * .getRotation();
1134 * int degrees = 0;
1135 * switch (rotation) {
1136 * case Surface.ROTATION_0: degrees = 0; break;
1137 * case Surface.ROTATION_90: degrees = 90; break;
1138 * case Surface.ROTATION_180: degrees = 180; break;
1139 * case Surface.ROTATION_270: degrees = 270; break;
1140 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001141 *
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001142 * int result;
1143 * if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
1144 * result = (info.orientation + degrees) % 360;
1145 * result = (360 - result) % 360; // compensate the mirror
1146 * } else { // back-facing
1147 * result = (info.orientation - degrees + 360) % 360;
1148 * }
Chih-Chung Chang724c5222010-06-14 18:57:15 +08001149 * camera.setDisplayOrientation(result);
1150 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001151 * </pre>
Wu-cheng Lid3033622011-10-07 13:13:54 +08001152 *
1153 * <p>Starting from API level 14, this method can be called when preview is
1154 * active.
1155 *
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001156 * @param degrees the angle that the picture will be rotated clockwise.
Chih-Chung Change7bd22a2010-01-27 10:24:42 -08001157 * Valid values are 0, 90, 180, and 270. The starting
1158 * position is 0 (landscape).
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001159 * @see #setPreviewDisplay(SurfaceHolder)
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001160 */
1161 public native final void setDisplayOrientation(int degrees);
1162
1163 /**
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001164 * <p>Enable or disable the default shutter sound when taking a picture.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001165 *
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001166 * <p>By default, the camera plays the system-defined camera shutter sound
1167 * when {@link #takePicture} is called. Using this method, the shutter sound
1168 * can be disabled. It is strongly recommended that an alternative shutter
1169 * sound is played in the {@link ShutterCallback} when the system shutter
1170 * sound is disabled.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001171 *
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001172 * <p>Note that devices may not always allow disabling the camera shutter
1173 * sound. If the shutter sound state cannot be set to the desired value,
1174 * this method will return false. {@link CameraInfo#canDisableShutterSound}
1175 * can be used to determine whether the device will allow the shutter sound
1176 * to be disabled.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001177 *
1178 * @param enabled whether the camera should play the system shutter sound
1179 * when {@link #takePicture takePicture} is called.
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001180 * @return {@code true} if the shutter sound state was successfully
1181 * changed. {@code false} if the shutter sound state could not be
1182 * changed. {@code true} is also returned if shutter sound playback
1183 * is already set to the requested state.
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001184 * @see #takePicture
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001185 * @see CameraInfo#canDisableShutterSound
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001186 * @see ShutterCallback
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001187 */
1188 public native final boolean enableShutterSound(boolean enabled);
1189
1190 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001191 * Callback interface for zoom changes during a smooth zoom operation.
1192 *
1193 * @see #setZoomChangeListener(OnZoomChangeListener)
1194 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001195 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001196 public interface OnZoomChangeListener
Dave Sparkse8b26e12009-07-14 10:35:40 -07001197 {
1198 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001199 * Called when the zoom value has changed during a smooth zoom.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001200 *
1201 * @param zoomValue the current zoom value. In smooth zoom mode, camera
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001202 * calls this for every new zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001203 * @param stopped whether smooth zoom is stopped. If the value is true,
1204 * this is the last zoom update for the application.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001205 * @param camera the Camera service object
1206 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001207 void onZoomChange(int zoomValue, boolean stopped, Camera camera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001208 };
1209
1210 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001211 * Registers a listener to be notified when the zoom value is updated by the
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001212 * camera driver during smooth zoom.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001213 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001214 * @param listener the listener to notify
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001215 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001216 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001217 public final void setZoomChangeListener(OnZoomChangeListener listener)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001218 {
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001219 mZoomListener = listener;
Dave Sparkse8b26e12009-07-14 10:35:40 -07001220 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001221
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001222 /**
1223 * Callback interface for face detected in the preview frame.
1224 *
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001225 */
1226 public interface FaceDetectionListener
1227 {
1228 /**
1229 * Notify the listener of the detected faces in the preview frame.
1230 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001231 * @param faces The detected faces in a list
Joe Fernandez464cb212011-10-04 16:56:47 -07001232 * @param camera The {@link Camera} service object
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001233 */
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001234 void onFaceDetection(Face[] faces, Camera camera);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001235 }
1236
1237 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001238 * Registers a listener to be notified about the faces detected in the
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001239 * preview frame.
1240 *
1241 * @param listener the listener to notify
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001242 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001243 */
1244 public final void setFaceDetectionListener(FaceDetectionListener listener)
1245 {
1246 mFaceListener = listener;
1247 }
1248
1249 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001250 * Starts the face detection. This should be called after preview is started.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001251 * The camera will notify {@link FaceDetectionListener} of the detected
1252 * faces in the preview frame. The detected faces may be the same as the
1253 * previous ones. Applications should call {@link #stopFaceDetection} to
1254 * stop the face detection. This method is supported if {@link
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001255 * Parameters#getMaxNumDetectedFaces()} returns a number larger than 0.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001256 * If the face detection has started, apps should not call this again.
1257 *
Wu-cheng Li8c136702011-08-12 20:25:00 +08001258 * <p>When the face detection is running, {@link Parameters#setWhiteBalance(String)},
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001259 * {@link Parameters#setFocusAreas(List)}, and {@link Parameters#setMeteringAreas(List)}
Wu-cheng Li8c136702011-08-12 20:25:00 +08001260 * have no effect. The camera uses the detected faces to do auto-white balance,
1261 * auto exposure, and autofocus.
1262 *
1263 * <p>If the apps call {@link #autoFocus(AutoFocusCallback)}, the camera
1264 * will stop sending face callbacks. The last face callback indicates the
1265 * areas used to do autofocus. After focus completes, face detection will
1266 * resume sending face callbacks. If the apps call {@link
1267 * #cancelAutoFocus()}, the face callbacks will also resume.</p>
1268 *
1269 * <p>After calling {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
1270 * Camera.PictureCallback)} or {@link #stopPreview()}, and then resuming
1271 * preview with {@link #startPreview()}, the apps should call this method
1272 * again to resume face detection.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001273 *
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001274 * @throws IllegalArgumentException if the face detection is unsupported.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001275 * @throws RuntimeException if the method fails or the face detection is
1276 * already running.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001277 * @see FaceDetectionListener
1278 * @see #stopFaceDetection()
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001279 * @see Parameters#getMaxNumDetectedFaces()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001280 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001281 public final void startFaceDetection() {
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001282 if (mFaceDetectionRunning) {
1283 throw new RuntimeException("Face detection is already running");
1284 }
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001285 _startFaceDetection(CAMERA_FACE_DETECTION_HW);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001286 mFaceDetectionRunning = true;
1287 }
1288
1289 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001290 * Stops the face detection.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001291 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001292 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001293 */
1294 public final void stopFaceDetection() {
1295 _stopFaceDetection();
1296 mFaceDetectionRunning = false;
1297 }
1298
1299 private native final void _startFaceDetection(int type);
1300 private native final void _stopFaceDetection();
1301
1302 /**
Joe Fernandez464cb212011-10-04 16:56:47 -07001303 * Information about a face identified through camera face detection.
Wu-cheng Li53b30912011-10-12 19:43:51 +08001304 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001305 * <p>When face detection is used with a camera, the {@link FaceDetectionListener} returns a
1306 * list of face objects for use in focusing and metering.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001307 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001308 * @see FaceDetectionListener
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001309 */
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001310 public static class Face {
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001311 /**
1312 * Create an empty face.
1313 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001314 public Face() {
1315 }
1316
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001317 /**
1318 * Bounds of the face. (-1000, -1000) represents the top-left of the
1319 * camera field of view, and (1000, 1000) represents the bottom-right of
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001320 * the field of view. For example, suppose the size of the viewfinder UI
1321 * is 800x480. The rect passed from the driver is (-1000, -1000, 0, 0).
Wu-cheng Li8c136702011-08-12 20:25:00 +08001322 * The corresponding viewfinder rect should be (0, 0, 400, 240). It is
1323 * guaranteed left < right and top < bottom. The coordinates can be
1324 * smaller than -1000 or bigger than 1000. But at least one vertex will
1325 * be within (-1000, -1000) and (1000, 1000).
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001326 *
1327 * <p>The direction is relative to the sensor orientation, that is, what
1328 * the sensor sees. The direction is not affected by the rotation or
Wu-cheng Li8c136702011-08-12 20:25:00 +08001329 * mirroring of {@link #setDisplayOrientation(int)}. The face bounding
1330 * rectangle does not provide any information about face orientation.</p>
1331 *
1332 * <p>Here is the matrix to convert driver coordinates to View coordinates
1333 * in pixels.</p>
1334 * <pre>
1335 * Matrix matrix = new Matrix();
1336 * CameraInfo info = CameraHolder.instance().getCameraInfo()[cameraId];
1337 * // Need mirror for front camera.
1338 * boolean mirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
1339 * matrix.setScale(mirror ? -1 : 1, 1);
1340 * // This is the value for android.hardware.Camera.setDisplayOrientation.
1341 * matrix.postRotate(displayOrientation);
1342 * // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
1343 * // UI coordinates range from (0, 0) to (width, height).
1344 * matrix.postScale(view.getWidth() / 2000f, view.getHeight() / 2000f);
1345 * matrix.postTranslate(view.getWidth() / 2f, view.getHeight() / 2f);
1346 * </pre>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001347 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001348 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001349 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001350 public Rect rect;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001351
1352 /**
Eino-Ville Talvala8df3b2b2012-09-15 14:30:12 -07001353 * <p>The confidence level for the detection of the face. The range is 1 to
1354 * 100. 100 is the highest confidence.</p>
1355 *
1356 * <p>Depending on the device, even very low-confidence faces may be
1357 * listed, so applications should filter out faces with low confidence,
1358 * depending on the use case. For a typical point-and-shoot camera
1359 * application that wishes to display rectangles around detected faces,
1360 * filtering out faces with confidence less than 50 is recommended.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001361 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001362 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001363 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001364 public int score;
Wei Huad52b3082011-08-19 13:01:51 -07001365
1366 /**
1367 * An unique id per face while the face is visible to the tracker. If
1368 * the face leaves the field-of-view and comes back, it will get a new
1369 * id. This is an optional field, may not be supported on all devices.
1370 * If not supported, id will always be set to -1. The optional fields
1371 * are supported as a set. Either they are all valid, or none of them
1372 * are.
1373 */
1374 public int id = -1;
1375
1376 /**
1377 * The coordinates of the center of the left eye. The coordinates are in
1378 * the same space as the ones for {@link #rect}. This is an optional
1379 * field, may not be supported on all devices. If not supported, the
1380 * value will always be set to null. The optional fields are supported
1381 * as a set. Either they are all valid, or none of them are.
1382 */
1383 public Point leftEye = null;
1384
1385 /**
1386 * The coordinates of the center of the right eye. The coordinates are
1387 * in the same space as the ones for {@link #rect}.This is an optional
1388 * field, may not be supported on all devices. If not supported, the
1389 * value will always be set to null. The optional fields are supported
1390 * as a set. Either they are all valid, or none of them are.
1391 */
1392 public Point rightEye = null;
1393
1394 /**
1395 * The coordinates of the center of the mouth. The coordinates are in
1396 * the same space as the ones for {@link #rect}. This is an optional
1397 * field, may not be supported on all devices. If not supported, the
1398 * value will always be set to null. The optional fields are supported
1399 * as a set. Either they are all valid, or none of them are.
1400 */
1401 public Point mouth = null;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001402 }
1403
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001404 // Error codes match the enum in include/ui/Camera.h
1405
1406 /**
1407 * Unspecified camera error.
1408 * @see Camera.ErrorCallback
1409 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 public static final int CAMERA_ERROR_UNKNOWN = 1;
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001411
1412 /**
1413 * Media server died. In this case, the application must release the
1414 * Camera object and instantiate a new one.
1415 * @see Camera.ErrorCallback
1416 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 public static final int CAMERA_ERROR_SERVER_DIED = 100;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001420 * Callback interface for camera error notification.
1421 *
1422 * @see #setErrorCallback(ErrorCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 */
1424 public interface ErrorCallback
1425 {
1426 /**
1427 * Callback for camera errors.
1428 * @param error error code:
1429 * <ul>
1430 * <li>{@link #CAMERA_ERROR_UNKNOWN}
1431 * <li>{@link #CAMERA_ERROR_SERVER_DIED}
1432 * </ul>
1433 * @param camera the Camera service object
1434 */
1435 void onError(int error, Camera camera);
1436 };
1437
1438 /**
1439 * Registers a callback to be invoked when an error occurs.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001440 * @param cb The callback to run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 */
1442 public final void setErrorCallback(ErrorCallback cb)
1443 {
1444 mErrorCallback = cb;
1445 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 private native final void native_setParameters(String params);
1448 private native final String native_getParameters();
1449
1450 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001451 * Changes the settings for this Camera service.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 * @param params the Parameters to use for this Camera service
Wu-cheng Li99a3f3e2010-11-19 15:56:16 +08001454 * @throws RuntimeException if any parameter is invalid or not supported.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001455 * @see #getParameters()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 */
1457 public void setParameters(Parameters params) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 native_setParameters(params.flatten());
1459 }
1460
1461 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001462 * Returns the current settings for this Camera service.
1463 * If modifications are made to the returned Parameters, they must be passed
1464 * to {@link #setParameters(Camera.Parameters)} to take effect.
1465 *
1466 * @see #setParameters(Camera.Parameters)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 */
1468 public Parameters getParameters() {
1469 Parameters p = new Parameters();
1470 String s = native_getParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 p.unflatten(s);
1472 return p;
1473 }
1474
1475 /**
Wu-cheng Li1c04a332011-11-22 18:21:18 +08001476 * Returns an empty {@link Parameters} for testing purpose.
1477 *
Ken Wakasaf76a50c2012-03-09 19:56:35 +09001478 * @return a Parameter object.
Wu-cheng Li1c04a332011-11-22 18:21:18 +08001479 *
1480 * @hide
1481 */
1482 public static Parameters getEmptyParameters() {
1483 Camera camera = new Camera();
1484 return camera.new Parameters();
1485 }
1486
1487 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001488 * Image size (width and height dimensions).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 */
1490 public class Size {
1491 /**
1492 * Sets the dimensions for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001493 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 * @param w the photo width (pixels)
1495 * @param h the photo height (pixels)
1496 */
1497 public Size(int w, int h) {
1498 width = w;
1499 height = h;
1500 }
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08001501 /**
1502 * Compares {@code obj} to this size.
1503 *
1504 * @param obj the object to compare this size with.
1505 * @return {@code true} if the width and height of {@code obj} is the
1506 * same as those of this size. {@code false} otherwise.
1507 */
1508 @Override
1509 public boolean equals(Object obj) {
1510 if (!(obj instanceof Size)) {
1511 return false;
1512 }
1513 Size s = (Size) obj;
1514 return width == s.width && height == s.height;
1515 }
1516 @Override
1517 public int hashCode() {
1518 return width * 32713 + height;
1519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 /** width of the picture */
1521 public int width;
1522 /** height of the picture */
1523 public int height;
1524 };
1525
1526 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07001527 * <p>The Area class is used for choosing specific metering and focus areas for
1528 * the camera to use when calculating auto-exposure, auto-white balance, and
1529 * auto-focus.</p>
1530 *
1531 * <p>To find out how many simultaneous areas a given camera supports, use
1532 * {@link Parameters#getMaxNumMeteringAreas()} and
1533 * {@link Parameters#getMaxNumFocusAreas()}. If metering or focusing area
1534 * selection is unsupported, these methods will return 0.</p>
1535 *
1536 * <p>Each Area consists of a rectangle specifying its bounds, and a weight
1537 * that determines its importance. The bounds are relative to the camera's
1538 * current field of view. The coordinates are mapped so that (-1000, -1000)
1539 * is always the top-left corner of the current field of view, and (1000,
1540 * 1000) is always the bottom-right corner of the current field of
1541 * view. Setting Areas with bounds outside that range is not allowed. Areas
1542 * with zero or negative width or height are not allowed.</p>
1543 *
1544 * <p>The weight must range from 1 to 1000, and represents a weight for
1545 * every pixel in the area. This means that a large metering area with
1546 * the same weight as a smaller area will have more effect in the
1547 * metering result. Metering areas can overlap and the driver
1548 * will add the weights in the overlap region.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08001549 *
Wu-cheng Libde61a52011-06-07 18:23:14 +08001550 * @see Parameters#setFocusAreas(List)
1551 * @see Parameters#getFocusAreas()
1552 * @see Parameters#getMaxNumFocusAreas()
1553 * @see Parameters#setMeteringAreas(List)
1554 * @see Parameters#getMeteringAreas()
1555 * @see Parameters#getMaxNumMeteringAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08001556 */
1557 public static class Area {
1558 /**
1559 * Create an area with specified rectangle and weight.
1560 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07001561 * @param rect the bounds of the area.
1562 * @param weight the weight of the area.
Wu-cheng Li30771b72011-04-02 06:19:46 +08001563 */
1564 public Area(Rect rect, int weight) {
1565 this.rect = rect;
1566 this.weight = weight;
1567 }
1568 /**
1569 * Compares {@code obj} to this area.
1570 *
1571 * @param obj the object to compare this area with.
1572 * @return {@code true} if the rectangle and weight of {@code obj} is
1573 * the same as those of this area. {@code false} otherwise.
1574 */
1575 @Override
1576 public boolean equals(Object obj) {
1577 if (!(obj instanceof Area)) {
1578 return false;
1579 }
1580 Area a = (Area) obj;
1581 if (rect == null) {
1582 if (a.rect != null) return false;
1583 } else {
1584 if (!rect.equals(a.rect)) return false;
1585 }
1586 return weight == a.weight;
1587 }
1588
Wu-cheng Libde61a52011-06-07 18:23:14 +08001589 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07001590 * Bounds of the area. (-1000, -1000) represents the top-left of the
1591 * camera field of view, and (1000, 1000) represents the bottom-right of
1592 * the field of view. Setting bounds outside that range is not
1593 * allowed. Bounds with zero or negative width or height are not
1594 * allowed.
Wu-cheng Libde61a52011-06-07 18:23:14 +08001595 *
1596 * @see Parameters#getFocusAreas()
1597 * @see Parameters#getMeteringAreas()
1598 */
Wu-cheng Li30771b72011-04-02 06:19:46 +08001599 public Rect rect;
1600
Wu-cheng Libde61a52011-06-07 18:23:14 +08001601 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07001602 * Weight of the area. The weight must range from 1 to 1000, and
1603 * represents a weight for every pixel in the area. This means that a
1604 * large metering area with the same weight as a smaller area will have
1605 * more effect in the metering result. Metering areas can overlap and
1606 * the driver will add the weights in the overlap region.
Wu-cheng Libde61a52011-06-07 18:23:14 +08001607 *
1608 * @see Parameters#getFocusAreas()
1609 * @see Parameters#getMeteringAreas()
1610 */
Wu-cheng Li30771b72011-04-02 06:19:46 +08001611 public int weight;
Wu-cheng Libde61a52011-06-07 18:23:14 +08001612 }
Wu-cheng Li30771b72011-04-02 06:19:46 +08001613
1614 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001615 * Camera service settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001616 *
1617 * <p>To make camera parameters take effect, applications have to call
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001618 * {@link Camera#setParameters(Camera.Parameters)}. For example, after
1619 * {@link Camera.Parameters#setWhiteBalance} is called, white balance is not
1620 * actually changed until {@link Camera#setParameters(Camera.Parameters)}
1621 * is called with the changed parameters object.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001622 *
1623 * <p>Different devices may have different camera capabilities, such as
1624 * picture size or flash modes. The application should query the camera
1625 * capabilities before setting parameters. For example, the application
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001626 * should call {@link Camera.Parameters#getSupportedColorEffects()} before
1627 * calling {@link Camera.Parameters#setColorEffect(String)}. If the
1628 * camera does not support color effects,
1629 * {@link Camera.Parameters#getSupportedColorEffects()} will return null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 */
1631 public class Parameters {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001632 // Parameter keys to communicate with the camera driver.
1633 private static final String KEY_PREVIEW_SIZE = "preview-size";
1634 private static final String KEY_PREVIEW_FORMAT = "preview-format";
1635 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate";
Wu-cheng Li454630f2010-08-11 16:48:05 -07001636 private static final String KEY_PREVIEW_FPS_RANGE = "preview-fps-range";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001637 private static final String KEY_PICTURE_SIZE = "picture-size";
1638 private static final String KEY_PICTURE_FORMAT = "picture-format";
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08001639 private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001640 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width";
1641 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height";
1642 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality";
1643 private static final String KEY_JPEG_QUALITY = "jpeg-quality";
1644 private static final String KEY_ROTATION = "rotation";
1645 private static final String KEY_GPS_LATITUDE = "gps-latitude";
1646 private static final String KEY_GPS_LONGITUDE = "gps-longitude";
1647 private static final String KEY_GPS_ALTITUDE = "gps-altitude";
1648 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp";
Ray Chen055c9862010-02-23 10:45:42 +08001649 private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001650 private static final String KEY_WHITE_BALANCE = "whitebalance";
1651 private static final String KEY_EFFECT = "effect";
1652 private static final String KEY_ANTIBANDING = "antibanding";
1653 private static final String KEY_SCENE_MODE = "scene-mode";
1654 private static final String KEY_FLASH_MODE = "flash-mode";
Wu-cheng Li36322db2009-09-18 18:59:21 +08001655 private static final String KEY_FOCUS_MODE = "focus-mode";
Wu-cheng Li30771b72011-04-02 06:19:46 +08001656 private static final String KEY_FOCUS_AREAS = "focus-areas";
1657 private static final String KEY_MAX_NUM_FOCUS_AREAS = "max-num-focus-areas";
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08001658 private static final String KEY_FOCAL_LENGTH = "focal-length";
1659 private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle";
1660 private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle";
Wu-cheng Liff723b62010-02-09 13:38:19 +08001661 private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation";
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001662 private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation";
1663 private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation";
1664 private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step";
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07001665 private static final String KEY_AUTO_EXPOSURE_LOCK = "auto-exposure-lock";
1666 private static final String KEY_AUTO_EXPOSURE_LOCK_SUPPORTED = "auto-exposure-lock-supported";
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07001667 private static final String KEY_AUTO_WHITEBALANCE_LOCK = "auto-whitebalance-lock";
1668 private static final String KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED = "auto-whitebalance-lock-supported";
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08001669 private static final String KEY_METERING_AREAS = "metering-areas";
1670 private static final String KEY_MAX_NUM_METERING_AREAS = "max-num-metering-areas";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001671 private static final String KEY_ZOOM = "zoom";
1672 private static final String KEY_MAX_ZOOM = "max-zoom";
1673 private static final String KEY_ZOOM_RATIOS = "zoom-ratios";
1674 private static final String KEY_ZOOM_SUPPORTED = "zoom-supported";
1675 private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08001676 private static final String KEY_FOCUS_DISTANCES = "focus-distances";
James Dongdd0b16c2010-09-21 16:23:48 -07001677 private static final String KEY_VIDEO_SIZE = "video-size";
1678 private static final String KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO =
1679 "preferred-preview-size-for-video";
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001680 private static final String KEY_MAX_NUM_DETECTED_FACES_HW = "max-num-detected-faces-hw";
1681 private static final String KEY_MAX_NUM_DETECTED_FACES_SW = "max-num-detected-faces-sw";
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08001682 private static final String KEY_RECORDING_HINT = "recording-hint";
Wu-cheng Li98bb2512011-08-30 21:33:10 +08001683 private static final String KEY_VIDEO_SNAPSHOT_SUPPORTED = "video-snapshot-supported";
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07001684 private static final String KEY_VIDEO_STABILIZATION = "video-stabilization";
1685 private static final String KEY_VIDEO_STABILIZATION_SUPPORTED = "video-stabilization-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08001686
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001687 // Parameter key suffix for supported values.
1688 private static final String SUPPORTED_VALUES_SUFFIX = "-values";
1689
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001690 private static final String TRUE = "true";
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07001691 private static final String FALSE = "false";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001692
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001693 // Values for white balance settings.
1694 public static final String WHITE_BALANCE_AUTO = "auto";
1695 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent";
1696 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent";
1697 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent";
1698 public static final String WHITE_BALANCE_DAYLIGHT = "daylight";
1699 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight";
1700 public static final String WHITE_BALANCE_TWILIGHT = "twilight";
1701 public static final String WHITE_BALANCE_SHADE = "shade";
1702
1703 // Values for color effect settings.
1704 public static final String EFFECT_NONE = "none";
1705 public static final String EFFECT_MONO = "mono";
1706 public static final String EFFECT_NEGATIVE = "negative";
1707 public static final String EFFECT_SOLARIZE = "solarize";
1708 public static final String EFFECT_SEPIA = "sepia";
1709 public static final String EFFECT_POSTERIZE = "posterize";
1710 public static final String EFFECT_WHITEBOARD = "whiteboard";
1711 public static final String EFFECT_BLACKBOARD = "blackboard";
1712 public static final String EFFECT_AQUA = "aqua";
1713
1714 // Values for antibanding settings.
1715 public static final String ANTIBANDING_AUTO = "auto";
1716 public static final String ANTIBANDING_50HZ = "50hz";
1717 public static final String ANTIBANDING_60HZ = "60hz";
1718 public static final String ANTIBANDING_OFF = "off";
1719
1720 // Values for flash mode settings.
1721 /**
1722 * Flash will not be fired.
1723 */
1724 public static final String FLASH_MODE_OFF = "off";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001725
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001726 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07001727 * Flash will be fired automatically when required. The flash may be fired
1728 * during preview, auto-focus, or snapshot depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001729 */
1730 public static final String FLASH_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001731
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001732 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07001733 * Flash will always be fired during snapshot. The flash may also be
1734 * fired during preview or auto-focus depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001735 */
1736 public static final String FLASH_MODE_ON = "on";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001737
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001738 /**
1739 * Flash will be fired in red-eye reduction mode.
1740 */
1741 public static final String FLASH_MODE_RED_EYE = "red-eye";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001742
Wu-cheng Li36322db2009-09-18 18:59:21 +08001743 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07001744 * Constant emission of light during preview, auto-focus and snapshot.
1745 * This can also be used for video recording.
Wu-cheng Li36322db2009-09-18 18:59:21 +08001746 */
Wu-cheng Li068ef422009-09-27 13:19:36 -07001747 public static final String FLASH_MODE_TORCH = "torch";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001748
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001749 /**
1750 * Scene mode is off.
1751 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001752 public static final String SCENE_MODE_AUTO = "auto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001753
1754 /**
1755 * Take photos of fast moving objects. Same as {@link
1756 * #SCENE_MODE_SPORTS}.
1757 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001758 public static final String SCENE_MODE_ACTION = "action";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001759
1760 /**
1761 * Take people pictures.
1762 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001763 public static final String SCENE_MODE_PORTRAIT = "portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001764
1765 /**
1766 * Take pictures on distant objects.
1767 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001768 public static final String SCENE_MODE_LANDSCAPE = "landscape";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001769
1770 /**
1771 * Take photos at night.
1772 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001773 public static final String SCENE_MODE_NIGHT = "night";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001774
1775 /**
1776 * Take people pictures at night.
1777 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001778 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001779
1780 /**
1781 * Take photos in a theater. Flash light is off.
1782 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001783 public static final String SCENE_MODE_THEATRE = "theatre";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001784
1785 /**
1786 * Take pictures on the beach.
1787 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001788 public static final String SCENE_MODE_BEACH = "beach";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001789
1790 /**
1791 * Take pictures on the snow.
1792 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001793 public static final String SCENE_MODE_SNOW = "snow";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001794
1795 /**
1796 * Take sunset photos.
1797 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001798 public static final String SCENE_MODE_SUNSET = "sunset";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001799
1800 /**
1801 * Avoid blurry pictures (for example, due to hand shake).
1802 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001803 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001804
1805 /**
1806 * For shooting firework displays.
1807 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001808 public static final String SCENE_MODE_FIREWORKS = "fireworks";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001809
1810 /**
1811 * Take photos of fast moving objects. Same as {@link
1812 * #SCENE_MODE_ACTION}.
1813 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001814 public static final String SCENE_MODE_SPORTS = "sports";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001815
1816 /**
1817 * Take indoor low-light shot.
1818 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001819 public static final String SCENE_MODE_PARTY = "party";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08001820
1821 /**
1822 * Capture the naturally warm color of scenes lit by candles.
1823 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001824 public static final String SCENE_MODE_CANDLELIGHT = "candlelight";
1825
Wu-cheng Lic58b4232010-03-29 17:21:28 +08001826 /**
1827 * Applications are looking for a barcode. Camera driver will be
1828 * optimized for barcode reading.
1829 */
1830 public static final String SCENE_MODE_BARCODE = "barcode";
1831
Wu-cheng Li36322db2009-09-18 18:59:21 +08001832 /**
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07001833 * Capture a scene using high dynamic range imaging techniques. The
1834 * camera will return an image that has an extended dynamic range
1835 * compared to a regular capture. Capturing such an image may take
1836 * longer than a regular capture.
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07001837 */
1838 public static final String SCENE_MODE_HDR = "hdr";
1839
1840 /**
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07001841 * Auto-focus mode. Applications should call {@link
1842 * #autoFocus(AutoFocusCallback)} to start the focus in this mode.
Wu-cheng Li36322db2009-09-18 18:59:21 +08001843 */
1844 public static final String FOCUS_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001845
Wu-cheng Li36322db2009-09-18 18:59:21 +08001846 /**
1847 * Focus is set at infinity. Applications should not call
1848 * {@link #autoFocus(AutoFocusCallback)} in this mode.
1849 */
1850 public static final String FOCUS_MODE_INFINITY = "infinity";
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07001851
1852 /**
1853 * Macro (close-up) focus mode. Applications should call
1854 * {@link #autoFocus(AutoFocusCallback)} to start the focus in this
1855 * mode.
1856 */
Wu-cheng Li36322db2009-09-18 18:59:21 +08001857 public static final String FOCUS_MODE_MACRO = "macro";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001858
Wu-cheng Li36322db2009-09-18 18:59:21 +08001859 /**
1860 * Focus is fixed. The camera is always in this mode if the focus is not
1861 * adjustable. If the camera has auto-focus, this mode can fix the
1862 * focus, which is usually at hyperfocal distance. Applications should
1863 * not call {@link #autoFocus(AutoFocusCallback)} in this mode.
1864 */
1865 public static final String FOCUS_MODE_FIXED = "fixed";
1866
Wu-cheng Lic58b4232010-03-29 17:21:28 +08001867 /**
1868 * Extended depth of field (EDOF). Focusing is done digitally and
1869 * continuously. Applications should not call {@link
1870 * #autoFocus(AutoFocusCallback)} in this mode.
1871 */
1872 public static final String FOCUS_MODE_EDOF = "edof";
1873
Wu-cheng Li699fe932010-08-05 11:50:25 -07001874 /**
Wu-cheng Lid45cb722010-09-20 16:15:32 -07001875 * Continuous auto focus mode intended for video recording. The camera
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08001876 * continuously tries to focus. This is the best choice for video
1877 * recording because the focus changes smoothly . Applications still can
1878 * call {@link #takePicture(Camera.ShutterCallback,
1879 * Camera.PictureCallback, Camera.PictureCallback)} in this mode but the
1880 * subject may not be in focus. Auto focus starts when the parameter is
Wu-cheng Li53b30912011-10-12 19:43:51 +08001881 * set.
1882 *
1883 * <p>Since API level 14, applications can call {@link
1884 * #autoFocus(AutoFocusCallback)} in this mode. The focus callback will
1885 * immediately return with a boolean that indicates whether the focus is
1886 * sharp or not. The focus position is locked after autoFocus call. If
1887 * applications want to resume the continuous focus, cancelAutoFocus
1888 * must be called. Restarting the preview will not resume the continuous
1889 * autofocus. To stop continuous focus, applications should change the
1890 * focus mode to other modes.
1891 *
1892 * @see #FOCUS_MODE_CONTINUOUS_PICTURE
Wu-cheng Li699fe932010-08-05 11:50:25 -07001893 */
Wu-cheng Lid45cb722010-09-20 16:15:32 -07001894 public static final String FOCUS_MODE_CONTINUOUS_VIDEO = "continuous-video";
Wu-cheng Li699fe932010-08-05 11:50:25 -07001895
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08001896 /**
1897 * Continuous auto focus mode intended for taking pictures. The camera
1898 * continuously tries to focus. The speed of focus change is more
1899 * aggressive than {@link #FOCUS_MODE_CONTINUOUS_VIDEO}. Auto focus
Wu-cheng Li53b30912011-10-12 19:43:51 +08001900 * starts when the parameter is set.
1901 *
Wu-cheng Li0f4f97b2011-10-27 18:07:01 +08001902 * <p>Applications can call {@link #autoFocus(AutoFocusCallback)} in
1903 * this mode. If the autofocus is in the middle of scanning, the focus
1904 * callback will return when it completes. If the autofocus is not
1905 * scanning, the focus callback will immediately return with a boolean
1906 * that indicates whether the focus is sharp or not. The apps can then
1907 * decide if they want to take a picture immediately or to change the
1908 * focus mode to auto, and run a full autofocus cycle. The focus
1909 * position is locked after autoFocus call. If applications want to
1910 * resume the continuous focus, cancelAutoFocus must be called.
1911 * Restarting the preview will not resume the continuous autofocus. To
1912 * stop continuous focus, applications should change the focus mode to
1913 * other modes.
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08001914 *
1915 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08001916 */
1917 public static final String FOCUS_MODE_CONTINUOUS_PICTURE = "continuous-picture";
1918
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08001919 // Indices for focus distance array.
1920 /**
1921 * The array index of near focus distance for use with
1922 * {@link #getFocusDistances(float[])}.
1923 */
1924 public static final int FOCUS_DISTANCE_NEAR_INDEX = 0;
1925
1926 /**
1927 * The array index of optimal focus distance for use with
1928 * {@link #getFocusDistances(float[])}.
1929 */
1930 public static final int FOCUS_DISTANCE_OPTIMAL_INDEX = 1;
1931
1932 /**
1933 * The array index of far focus distance for use with
1934 * {@link #getFocusDistances(float[])}.
1935 */
1936 public static final int FOCUS_DISTANCE_FAR_INDEX = 2;
1937
Wu-cheng Lica099612010-05-06 16:47:30 +08001938 /**
Wu-cheng Li454630f2010-08-11 16:48:05 -07001939 * The array index of minimum preview fps for use with {@link
1940 * #getPreviewFpsRange(int[])} or {@link
1941 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07001942 */
1943 public static final int PREVIEW_FPS_MIN_INDEX = 0;
1944
1945 /**
1946 * The array index of maximum preview fps for use with {@link
1947 * #getPreviewFpsRange(int[])} or {@link
1948 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07001949 */
1950 public static final int PREVIEW_FPS_MAX_INDEX = 1;
1951
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001952 // Formats for setPreviewFormat and setPictureFormat.
1953 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp";
1954 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp";
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001955 private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv";
Wu-cheng Li10a1b302011-02-22 15:49:25 +08001956 private static final String PIXEL_FORMAT_YUV420P = "yuv420p";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001957 private static final String PIXEL_FORMAT_RGB565 = "rgb565";
1958 private static final String PIXEL_FORMAT_JPEG = "jpeg";
Wu-cheng Li70fb9082011-08-02 17:49:50 +08001959 private static final String PIXEL_FORMAT_BAYER_RGGB = "bayer-rggb";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 private HashMap<String, String> mMap;
1962
1963 private Parameters() {
Ali Utku Selen0a120182011-02-09 14:11:22 +01001964 mMap = new HashMap<String, String>(64);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966
1967 /**
1968 * Writes the current Parameters to the log.
1969 * @hide
1970 * @deprecated
1971 */
1972 public void dump() {
1973 Log.e(TAG, "dump: size=" + mMap.size());
1974 for (String k : mMap.keySet()) {
1975 Log.e(TAG, "dump: " + k + "=" + mMap.get(k));
1976 }
1977 }
1978
1979 /**
1980 * Creates a single string with all the parameters set in
1981 * this Parameters object.
1982 * <p>The {@link #unflatten(String)} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001983 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 * @return a String with all values from this Parameters object, in
1985 * semi-colon delimited key-value pairs
1986 */
1987 public String flatten() {
Ali Utku Selen0a120182011-02-09 14:11:22 +01001988 StringBuilder flattened = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 for (String k : mMap.keySet()) {
1990 flattened.append(k);
1991 flattened.append("=");
1992 flattened.append(mMap.get(k));
1993 flattened.append(";");
1994 }
1995 // chop off the extra semicolon at the end
1996 flattened.deleteCharAt(flattened.length()-1);
1997 return flattened.toString();
1998 }
1999
2000 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002001 * Takes a flattened string of parameters and adds each one to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 * this Parameters object.
2003 * <p>The {@link #flatten()} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002004 *
2005 * @param flattened a String of parameters (key-value paired) that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 * are semi-colon delimited
2007 */
2008 public void unflatten(String flattened) {
2009 mMap.clear();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002010
Ali Utku Selen0a120182011-02-09 14:11:22 +01002011 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(';');
2012 splitter.setString(flattened);
2013 for (String kv : splitter) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 int pos = kv.indexOf('=');
2015 if (pos == -1) {
2016 continue;
2017 }
2018 String k = kv.substring(0, pos);
2019 String v = kv.substring(pos + 1);
2020 mMap.put(k, v);
2021 }
2022 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 public void remove(String key) {
2025 mMap.remove(key);
2026 }
2027
2028 /**
2029 * Sets a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002030 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 * @param key the key name for the parameter
2032 * @param value the String value of the parameter
2033 */
2034 public void set(String key, String value) {
Eino-Ville Talvalacb569232012-03-12 11:36:58 -07002035 if (key.indexOf('=') != -1 || key.indexOf(';') != -1 || key.indexOf(0) != -1) {
2036 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ; or \\0)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 return;
2038 }
Eino-Ville Talvalacb569232012-03-12 11:36:58 -07002039 if (value.indexOf('=') != -1 || value.indexOf(';') != -1 || value.indexOf(0) != -1) {
2040 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ; or \\0)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 return;
2042 }
2043
2044 mMap.put(key, value);
2045 }
2046
2047 /**
2048 * Sets an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002049 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 * @param key the key name for the parameter
2051 * @param value the int value of the parameter
2052 */
2053 public void set(String key, int value) {
2054 mMap.put(key, Integer.toString(value));
2055 }
2056
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002057 private void set(String key, List<Area> areas) {
Wu-cheng Lif715bf92011-04-14 14:04:18 +08002058 if (areas == null) {
2059 set(key, "(0,0,0,0,0)");
2060 } else {
2061 StringBuilder buffer = new StringBuilder();
2062 for (int i = 0; i < areas.size(); i++) {
2063 Area area = areas.get(i);
2064 Rect rect = area.rect;
2065 buffer.append('(');
2066 buffer.append(rect.left);
2067 buffer.append(',');
2068 buffer.append(rect.top);
2069 buffer.append(',');
2070 buffer.append(rect.right);
2071 buffer.append(',');
2072 buffer.append(rect.bottom);
2073 buffer.append(',');
2074 buffer.append(area.weight);
2075 buffer.append(')');
2076 if (i != areas.size() - 1) buffer.append(',');
2077 }
2078 set(key, buffer.toString());
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002079 }
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002080 }
2081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 /**
2083 * Returns the value of a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002084 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 * @param key the key name for the parameter
2086 * @return the String value of the parameter
2087 */
2088 public String get(String key) {
2089 return mMap.get(key);
2090 }
2091
2092 /**
2093 * Returns the value of an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002094 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 * @param key the key name for the parameter
2096 * @return the int value of the parameter
2097 */
2098 public int getInt(String key) {
2099 return Integer.parseInt(mMap.get(key));
2100 }
2101
2102 /**
Wu-cheng Li26274fa2011-05-05 14:36:28 +08002103 * Sets the dimensions for preview pictures. If the preview has already
2104 * started, applications should stop the preview first before changing
2105 * preview size.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002106 *
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002107 * The sides of width and height are based on camera orientation. That
2108 * is, the preview size is the size before it is rotated by display
2109 * orientation. So applications need to consider the display orientation
2110 * while setting preview size. For example, suppose the camera supports
2111 * both 480x320 and 320x480 preview sizes. The application wants a 3:2
2112 * preview ratio. If the display orientation is set to 0 or 180, preview
2113 * size should be set to 480x320. If the display orientation is set to
2114 * 90 or 270, preview size should be set to 320x480. The display
2115 * orientation should also be considered while setting picture size and
2116 * thumbnail size.
2117 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 * @param width the width of the pictures, in pixels
2119 * @param height the height of the pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002120 * @see #setDisplayOrientation(int)
2121 * @see #getCameraInfo(int, CameraInfo)
2122 * @see #setPictureSize(int, int)
2123 * @see #setJpegThumbnailSize(int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 */
2125 public void setPreviewSize(int width, int height) {
2126 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002127 set(KEY_PREVIEW_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
2129
2130 /**
2131 * Returns the dimensions setting for preview pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002132 *
James Dongdd0b16c2010-09-21 16:23:48 -07002133 * @return a Size object with the width and height setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 * for the preview picture
2135 */
2136 public Size getPreviewSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002137 String pair = get(KEY_PREVIEW_SIZE);
2138 return strToSize(pair);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 }
2140
2141 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002142 * Gets the supported preview sizes.
2143 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002144 * @return a list of Size object. This method will always return a list
Wu-cheng Li9c799382009-12-04 19:59:18 +08002145 * with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002146 */
2147 public List<Size> getSupportedPreviewSizes() {
2148 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX);
2149 return splitSize(str);
2150 }
2151
2152 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002153 * <p>Gets the supported video frame sizes that can be used by
2154 * MediaRecorder.</p>
James Dongdd0b16c2010-09-21 16:23:48 -07002155 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002156 * <p>If the returned list is not null, the returned list will contain at
James Dongdd0b16c2010-09-21 16:23:48 -07002157 * least one Size and one of the sizes in the returned list must be
2158 * passed to MediaRecorder.setVideoSize() for camcorder application if
2159 * camera is used as the video source. In this case, the size of the
2160 * preview can be different from the resolution of the recorded video
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002161 * during video recording.</p>
James Dongdd0b16c2010-09-21 16:23:48 -07002162 *
2163 * @return a list of Size object if camera has separate preview and
2164 * video output; otherwise, null is returned.
2165 * @see #getPreferredPreviewSizeForVideo()
2166 */
2167 public List<Size> getSupportedVideoSizes() {
2168 String str = get(KEY_VIDEO_SIZE + SUPPORTED_VALUES_SUFFIX);
2169 return splitSize(str);
2170 }
2171
2172 /**
2173 * Returns the preferred or recommended preview size (width and height)
2174 * in pixels for video recording. Camcorder applications should
2175 * set the preview size to a value that is not larger than the
2176 * preferred preview size. In other words, the product of the width
2177 * and height of the preview size should not be larger than that of
2178 * the preferred preview size. In addition, we recommend to choose a
2179 * preview size that has the same aspect ratio as the resolution of
2180 * video to be recorded.
2181 *
2182 * @return the preferred preview size (width and height) in pixels for
2183 * video recording if getSupportedVideoSizes() does not return
2184 * null; otherwise, null is returned.
2185 * @see #getSupportedVideoSizes()
2186 */
2187 public Size getPreferredPreviewSizeForVideo() {
2188 String pair = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
2189 return strToSize(pair);
2190 }
2191
2192 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002193 * <p>Sets the dimensions for EXIF thumbnail in Jpeg picture. If
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002194 * applications set both width and height to 0, EXIF will not contain
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002195 * thumbnail.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002196 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002197 * <p>Applications need to consider the display orientation. See {@link
2198 * #setPreviewSize(int,int)} for reference.</p>
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002199 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 * @param width the width of the thumbnail, in pixels
2201 * @param height the height of the thumbnail, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002202 * @see #setPreviewSize(int,int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002204 public void setJpegThumbnailSize(int width, int height) {
2205 set(KEY_JPEG_THUMBNAIL_WIDTH, width);
2206 set(KEY_JPEG_THUMBNAIL_HEIGHT, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 }
2208
2209 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002210 * Returns the dimensions for EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002212 * @return a Size object with the height and width setting for the EXIF
2213 * thumbnails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002215 public Size getJpegThumbnailSize() {
2216 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH),
2217 getInt(KEY_JPEG_THUMBNAIL_HEIGHT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 }
2219
2220 /**
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002221 * Gets the supported jpeg thumbnail sizes.
2222 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002223 * @return a list of Size object. This method will always return a list
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002224 * with at least two elements. Size 0,0 (no thumbnail) is always
2225 * supported.
2226 */
2227 public List<Size> getSupportedJpegThumbnailSizes() {
2228 String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX);
2229 return splitSize(str);
2230 }
2231
2232 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002233 * Sets the quality of the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002235 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1
2236 * to 100, with 100 being the best.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002238 public void setJpegThumbnailQuality(int quality) {
2239 set(KEY_JPEG_THUMBNAIL_QUALITY, quality);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 }
2241
2242 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002243 * Returns the quality setting for the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002245 * @return the JPEG quality setting of the EXIF thumbnail.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002247 public int getJpegThumbnailQuality() {
2248 return getInt(KEY_JPEG_THUMBNAIL_QUALITY);
2249 }
2250
2251 /**
2252 * Sets Jpeg quality of captured picture.
2253 *
2254 * @param quality the JPEG quality of captured picture. The range is 1
2255 * to 100, with 100 being the best.
2256 */
2257 public void setJpegQuality(int quality) {
2258 set(KEY_JPEG_QUALITY, quality);
2259 }
2260
2261 /**
2262 * Returns the quality setting for the JPEG picture.
2263 *
2264 * @return the JPEG picture quality setting.
2265 */
2266 public int getJpegQuality() {
2267 return getInt(KEY_JPEG_QUALITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 }
2269
2270 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08002271 * Sets the rate at which preview frames are received. This is the
2272 * target frame rate. The actual frame rate depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 * @param fps the frame rate (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002275 * @deprecated replaced by {@link #setPreviewFpsRange(int,int)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002277 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 public void setPreviewFrameRate(int fps) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002279 set(KEY_PREVIEW_FRAME_RATE, fps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 }
2281
2282 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08002283 * Returns the setting for the rate at which preview frames are
2284 * received. This is the target frame rate. The actual frame rate
2285 * depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002286 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 * @return the frame rate setting (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002288 * @deprecated replaced by {@link #getPreviewFpsRange(int[])}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002290 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 public int getPreviewFrameRate() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002292 return getInt(KEY_PREVIEW_FRAME_RATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 }
2294
2295 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002296 * Gets the supported preview frame rates.
2297 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002298 * @return a list of supported preview frame rates. null if preview
2299 * frame rate setting is not supported.
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002300 * @deprecated replaced by {@link #getSupportedPreviewFpsRange()}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002301 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002302 @Deprecated
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002303 public List<Integer> getSupportedPreviewFrameRates() {
2304 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX);
2305 return splitInt(str);
2306 }
2307
2308 /**
Wu-cheng Li454630f2010-08-11 16:48:05 -07002309 * Sets the maximum and maximum preview fps. This controls the rate of
Wu-cheng Li1620d112010-08-27 15:09:20 -07002310 * preview frames received in {@link PreviewCallback}. The minimum and
Wu-cheng Li454630f2010-08-11 16:48:05 -07002311 * maximum preview fps must be one of the elements from {@link
2312 * #getSupportedPreviewFpsRange}.
2313 *
2314 * @param min the minimum preview fps (scaled by 1000).
2315 * @param max the maximum preview fps (scaled by 1000).
2316 * @throws RuntimeException if fps range is invalid.
2317 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
2318 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07002319 */
2320 public void setPreviewFpsRange(int min, int max) {
2321 set(KEY_PREVIEW_FPS_RANGE, "" + min + "," + max);
2322 }
2323
2324 /**
2325 * Returns the current minimum and maximum preview fps. The values are
2326 * one of the elements returned by {@link #getSupportedPreviewFpsRange}.
2327 *
2328 * @return range the minimum and maximum preview fps (scaled by 1000).
2329 * @see #PREVIEW_FPS_MIN_INDEX
2330 * @see #PREVIEW_FPS_MAX_INDEX
2331 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07002332 */
2333 public void getPreviewFpsRange(int[] range) {
2334 if (range == null || range.length != 2) {
2335 throw new IllegalArgumentException(
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002336 "range must be an array with two elements.");
Wu-cheng Li454630f2010-08-11 16:48:05 -07002337 }
2338 splitInt(get(KEY_PREVIEW_FPS_RANGE), range);
2339 }
2340
2341 /**
2342 * Gets the supported preview fps (frame-per-second) ranges. Each range
2343 * contains a minimum fps and maximum fps. If minimum fps equals to
2344 * maximum fps, the camera outputs frames in fixed frame rate. If not,
2345 * the camera outputs frames in auto frame rate. The actual frame rate
2346 * fluctuates between the minimum and the maximum. The values are
2347 * multiplied by 1000 and represented in integers. For example, if frame
2348 * rate is 26.623 frames per second, the value is 26623.
2349 *
2350 * @return a list of supported preview fps ranges. This method returns a
2351 * list with at least one element. Every element is an int array
2352 * of two values - minimum fps and maximum fps. The list is
2353 * sorted from small to large (first by maximum fps and then
2354 * minimum fps).
2355 * @see #PREVIEW_FPS_MIN_INDEX
2356 * @see #PREVIEW_FPS_MAX_INDEX
Wu-cheng Li454630f2010-08-11 16:48:05 -07002357 */
2358 public List<int[]> getSupportedPreviewFpsRange() {
2359 String str = get(KEY_PREVIEW_FPS_RANGE + SUPPORTED_VALUES_SUFFIX);
2360 return splitRange(str);
2361 }
2362
2363 /**
Wu-cheng Li7478ea62009-09-16 18:52:55 +08002364 * Sets the image format for preview pictures.
Scott Mainda0a56d2009-09-10 18:08:37 -07002365 * <p>If this is never called, the default format will be
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002366 * {@link android.graphics.ImageFormat#NV21}, which
Scott Maindf4578e2009-09-10 12:22:07 -07002367 * uses the NV21 encoding format.</p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +08002368 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -07002369 * <p>Use {@link Parameters#getSupportedPreviewFormats} to get a list of
2370 * the available preview formats.
2371 *
2372 * <p>It is strongly recommended that either
2373 * {@link android.graphics.ImageFormat#NV21} or
2374 * {@link android.graphics.ImageFormat#YV12} is used, since
2375 * they are supported by all camera devices.</p>
2376 *
2377 * <p>For YV12, the image buffer that is received is not necessarily
2378 * tightly packed, as there may be padding at the end of each row of
2379 * pixel data, as described in
2380 * {@link android.graphics.ImageFormat#YV12}. For camera callback data,
2381 * it can be assumed that the stride of the Y and UV data is the
2382 * smallest possible that meets the alignment requirements. That is, if
2383 * the preview size is <var>width x height</var>, then the following
2384 * equations describe the buffer index for the beginning of row
2385 * <var>y</var> for the Y plane and row <var>c</var> for the U and V
2386 * planes:
2387 *
2388 * {@code
2389 * <pre>
2390 * yStride = (int) ceil(width / 16.0) * 16;
2391 * uvStride = (int) ceil( (yStride / 2) / 16.0) * 16;
2392 * ySize = yStride * height;
2393 * uvSize = uvStride * height / 2;
2394 * yRowIndex = yStride * y;
2395 * uRowIndex = ySize + uvSize + uvStride * c;
2396 * vRowIndex = ySize + uvStride * c;
2397 * size = ySize + uvSize * 2;</pre>
2398 * }
2399 *
2400 * @param pixel_format the desired preview picture format, defined by
2401 * one of the {@link android.graphics.ImageFormat} constants. (E.g.,
2402 * <var>ImageFormat.NV21</var> (default), or
2403 * <var>ImageFormat.YV12</var>)
2404 *
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002405 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07002406 * @see android.hardware.Camera.Parameters#getSupportedPreviewFormats
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 */
2408 public void setPreviewFormat(int pixel_format) {
2409 String s = cameraFormatForPixelFormat(pixel_format);
2410 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002411 throw new IllegalArgumentException(
2412 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 }
2414
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002415 set(KEY_PREVIEW_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 }
2417
2418 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002419 * Returns the image format for preview frames got from
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002420 * {@link PreviewCallback}.
Wu-cheng Li7478ea62009-09-16 18:52:55 +08002421 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002422 * @return the preview format.
2423 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07002424 * @see #setPreviewFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 */
2426 public int getPreviewFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002427 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT));
2428 }
2429
2430 /**
Wu-cheng Lif9293e72011-02-25 13:35:10 +08002431 * Gets the supported preview formats. {@link android.graphics.ImageFormat#NV21}
2432 * is always supported. {@link android.graphics.ImageFormat#YV12}
2433 * is always supported since API level 12.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002434 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002435 * @return a list of supported preview formats. This method will always
2436 * return a list with at least one element.
2437 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07002438 * @see #setPreviewFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002439 */
2440 public List<Integer> getSupportedPreviewFormats() {
2441 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX);
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002442 ArrayList<Integer> formats = new ArrayList<Integer>();
2443 for (String s : split(str)) {
2444 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002445 if (f == ImageFormat.UNKNOWN) continue;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002446 formats.add(f);
2447 }
2448 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 }
2450
2451 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002452 * <p>Sets the dimensions for pictures.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002453 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002454 * <p>Applications need to consider the display orientation. See {@link
2455 * #setPreviewSize(int,int)} for reference.</p>
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 * @param width the width for pictures, in pixels
2458 * @param height the height for pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002459 * @see #setPreviewSize(int,int)
2460 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 */
2462 public void setPictureSize(int width, int height) {
2463 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002464 set(KEY_PICTURE_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 }
2466
2467 /**
2468 * Returns the dimension setting for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002469 *
2470 * @return a Size object with the height and width setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 * for pictures
2472 */
2473 public Size getPictureSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002474 String pair = get(KEY_PICTURE_SIZE);
2475 return strToSize(pair);
2476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002478 /**
2479 * Gets the supported picture sizes.
2480 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002481 * @return a list of supported picture sizes. This method will always
2482 * return a list with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002483 */
2484 public List<Size> getSupportedPictureSizes() {
2485 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX);
2486 return splitSize(str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 }
2488
2489 /**
2490 * Sets the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002491 *
2492 * @param pixel_format the desired picture format
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002493 * (<var>ImageFormat.NV21</var>,
2494 * <var>ImageFormat.RGB_565</var>, or
2495 * <var>ImageFormat.JPEG</var>)
2496 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 */
2498 public void setPictureFormat(int pixel_format) {
2499 String s = cameraFormatForPixelFormat(pixel_format);
2500 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002501 throw new IllegalArgumentException(
2502 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 }
2504
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002505 set(KEY_PICTURE_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 }
2507
2508 /**
2509 * Returns the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002510 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002511 * @return the picture format
2512 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 */
2514 public int getPictureFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002515 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT));
2516 }
2517
2518 /**
2519 * Gets the supported picture formats.
2520 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002521 * @return supported picture formats. This method will always return a
2522 * list with at least one element.
2523 * @see android.graphics.ImageFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002524 */
2525 public List<Integer> getSupportedPictureFormats() {
Wu-cheng Li9c799382009-12-04 19:59:18 +08002526 String str = get(KEY_PICTURE_FORMAT + SUPPORTED_VALUES_SUFFIX);
2527 ArrayList<Integer> formats = new ArrayList<Integer>();
2528 for (String s : split(str)) {
2529 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002530 if (f == ImageFormat.UNKNOWN) continue;
Wu-cheng Li9c799382009-12-04 19:59:18 +08002531 formats.add(f);
2532 }
2533 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 }
2535
2536 private String cameraFormatForPixelFormat(int pixel_format) {
2537 switch(pixel_format) {
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002538 case ImageFormat.NV16: return PIXEL_FORMAT_YUV422SP;
2539 case ImageFormat.NV21: return PIXEL_FORMAT_YUV420SP;
2540 case ImageFormat.YUY2: return PIXEL_FORMAT_YUV422I;
Wu-cheng Li10a1b302011-02-22 15:49:25 +08002541 case ImageFormat.YV12: return PIXEL_FORMAT_YUV420P;
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002542 case ImageFormat.RGB_565: return PIXEL_FORMAT_RGB565;
2543 case ImageFormat.JPEG: return PIXEL_FORMAT_JPEG;
Wu-cheng Li70fb9082011-08-02 17:49:50 +08002544 case ImageFormat.BAYER_RGGB: return PIXEL_FORMAT_BAYER_RGGB;
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002545 default: return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 }
2547 }
2548
2549 private int pixelFormatForCameraFormat(String format) {
2550 if (format == null)
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002551 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002553 if (format.equals(PIXEL_FORMAT_YUV422SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002554 return ImageFormat.NV16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002556 if (format.equals(PIXEL_FORMAT_YUV420SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002557 return ImageFormat.NV21;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002559 if (format.equals(PIXEL_FORMAT_YUV422I))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002560 return ImageFormat.YUY2;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002561
Wu-cheng Li10a1b302011-02-22 15:49:25 +08002562 if (format.equals(PIXEL_FORMAT_YUV420P))
2563 return ImageFormat.YV12;
2564
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002565 if (format.equals(PIXEL_FORMAT_RGB565))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002566 return ImageFormat.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002568 if (format.equals(PIXEL_FORMAT_JPEG))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002569 return ImageFormat.JPEG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570
Mathias Agopiana696f5d2010-02-17 17:53:09 -08002571 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 }
2573
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002574 /**
Wu-cheng Li8969ea12012-04-20 17:38:09 +08002575 * Sets the clockwise rotation angle in degrees relative to the
2576 * orientation of the camera. This affects the pictures returned from
2577 * JPEG {@link PictureCallback}. The camera driver may set orientation
2578 * in the EXIF header without rotating the picture. Or the driver may
2579 * rotate the picture and the EXIF thumbnail. If the Jpeg picture is
2580 * rotated, the orientation in the EXIF header will be missing or 1
2581 * (row #0 is top and column #0 is left side).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002582 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08002583 * <p>If applications want to rotate the picture to match the orientation
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08002584 * of what users see, apps should use {@link
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002585 * android.view.OrientationEventListener} and {@link CameraInfo}.
2586 * The value from OrientationEventListener is relative to the natural
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08002587 * orientation of the device. CameraInfo.orientation is the angle
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08002588 * between camera orientation and natural device orientation. The sum
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08002589 * of the two is the rotation angle for back-facing camera. The
2590 * difference of the two is the rotation angle for front-facing camera.
2591 * Note that the JPEG pictures of front-facing cameras are not mirrored
2592 * as in preview display.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002593 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08002594 * <p>For example, suppose the natural orientation of the device is
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002595 * portrait. The device is rotated 270 degrees clockwise, so the device
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08002596 * orientation is 270. Suppose a back-facing camera sensor is mounted in
2597 * landscape and the top side of the camera sensor is aligned with the
2598 * right edge of the display in natural orientation. So the camera
2599 * orientation is 90. The rotation should be set to 0 (270 + 90).
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002600 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08002601 * <p>The reference code is as follows.
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002602 *
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -08002603 * <pre>
Scott Main9a10bf02011-08-24 19:09:48 -07002604 * public void onOrientationChanged(int orientation) {
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002605 * if (orientation == ORIENTATION_UNKNOWN) return;
2606 * android.hardware.Camera.CameraInfo info =
2607 * new android.hardware.Camera.CameraInfo();
2608 * android.hardware.Camera.getCameraInfo(cameraId, info);
2609 * orientation = (orientation + 45) / 90 * 90;
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08002610 * int rotation = 0;
2611 * if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
2612 * rotation = (info.orientation - orientation + 360) % 360;
2613 * } else { // back-facing camera
2614 * rotation = (info.orientation + orientation) % 360;
2615 * }
2616 * mParameters.setRotation(rotation);
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002617 * }
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -08002618 * </pre>
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002619 *
2620 * @param rotation The rotation angle in degrees relative to the
2621 * orientation of the camera. Rotation can only be 0,
2622 * 90, 180 or 270.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002623 * @throws IllegalArgumentException if rotation value is invalid.
2624 * @see android.view.OrientationEventListener
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07002625 * @see #getCameraInfo(int, CameraInfo)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002626 */
2627 public void setRotation(int rotation) {
2628 if (rotation == 0 || rotation == 90 || rotation == 180
2629 || rotation == 270) {
2630 set(KEY_ROTATION, Integer.toString(rotation));
2631 } else {
2632 throw new IllegalArgumentException(
2633 "Invalid rotation=" + rotation);
2634 }
2635 }
2636
2637 /**
2638 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF
2639 * header.
2640 *
2641 * @param latitude GPS latitude coordinate.
2642 */
2643 public void setGpsLatitude(double latitude) {
2644 set(KEY_GPS_LATITUDE, Double.toString(latitude));
2645 }
2646
2647 /**
2648 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF
2649 * header.
2650 *
2651 * @param longitude GPS longitude coordinate.
2652 */
2653 public void setGpsLongitude(double longitude) {
2654 set(KEY_GPS_LONGITUDE, Double.toString(longitude));
2655 }
2656
2657 /**
2658 * Sets GPS altitude. This will be stored in JPEG EXIF header.
2659 *
2660 * @param altitude GPS altitude in meters.
2661 */
2662 public void setGpsAltitude(double altitude) {
2663 set(KEY_GPS_ALTITUDE, Double.toString(altitude));
2664 }
2665
2666 /**
2667 * Sets GPS timestamp. This will be stored in JPEG EXIF header.
2668 *
2669 * @param timestamp GPS timestamp (UTC in seconds since January 1,
2670 * 1970).
2671 */
2672 public void setGpsTimestamp(long timestamp) {
2673 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp));
2674 }
2675
2676 /**
Ray Chene2083772010-03-10 15:02:49 -08002677 * Sets GPS processing method. It will store up to 32 characters
Ray Chen055c9862010-02-23 10:45:42 +08002678 * in JPEG EXIF header.
2679 *
2680 * @param processing_method The processing method to get this location.
2681 */
2682 public void setGpsProcessingMethod(String processing_method) {
2683 set(KEY_GPS_PROCESSING_METHOD, processing_method);
2684 }
2685
2686 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002687 * Removes GPS latitude, longitude, altitude, and timestamp from the
2688 * parameters.
2689 */
2690 public void removeGpsData() {
2691 remove(KEY_GPS_LATITUDE);
2692 remove(KEY_GPS_LONGITUDE);
2693 remove(KEY_GPS_ALTITUDE);
2694 remove(KEY_GPS_TIMESTAMP);
Ray Chen055c9862010-02-23 10:45:42 +08002695 remove(KEY_GPS_PROCESSING_METHOD);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002696 }
2697
2698 /**
2699 * Gets the current white balance setting.
2700 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002701 * @return current white balance. null if white balance setting is not
2702 * supported.
2703 * @see #WHITE_BALANCE_AUTO
2704 * @see #WHITE_BALANCE_INCANDESCENT
2705 * @see #WHITE_BALANCE_FLUORESCENT
2706 * @see #WHITE_BALANCE_WARM_FLUORESCENT
2707 * @see #WHITE_BALANCE_DAYLIGHT
2708 * @see #WHITE_BALANCE_CLOUDY_DAYLIGHT
2709 * @see #WHITE_BALANCE_TWILIGHT
2710 * @see #WHITE_BALANCE_SHADE
2711 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002712 */
2713 public String getWhiteBalance() {
2714 return get(KEY_WHITE_BALANCE);
2715 }
2716
2717 /**
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07002718 * Sets the white balance. Changing the setting will release the
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08002719 * auto-white balance lock. It is recommended not to change white
2720 * balance and AWB lock at the same time.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002721 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002722 * @param value new white balance.
2723 * @see #getWhiteBalance()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07002724 * @see #setAutoWhiteBalanceLock(boolean)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002725 */
2726 public void setWhiteBalance(String value) {
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08002727 String oldValue = get(KEY_WHITE_BALANCE);
2728 if (same(value, oldValue)) return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002729 set(KEY_WHITE_BALANCE, value);
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07002730 set(KEY_AUTO_WHITEBALANCE_LOCK, FALSE);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002731 }
2732
2733 /**
2734 * Gets the supported white balance.
2735 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002736 * @return a list of supported white balance. null if white balance
2737 * setting is not supported.
2738 * @see #getWhiteBalance()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002739 */
2740 public List<String> getSupportedWhiteBalance() {
2741 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX);
2742 return split(str);
2743 }
2744
2745 /**
2746 * Gets the current color effect setting.
2747 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002748 * @return current color effect. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002749 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002750 * @see #EFFECT_NONE
2751 * @see #EFFECT_MONO
2752 * @see #EFFECT_NEGATIVE
2753 * @see #EFFECT_SOLARIZE
2754 * @see #EFFECT_SEPIA
2755 * @see #EFFECT_POSTERIZE
2756 * @see #EFFECT_WHITEBOARD
2757 * @see #EFFECT_BLACKBOARD
2758 * @see #EFFECT_AQUA
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002759 */
2760 public String getColorEffect() {
2761 return get(KEY_EFFECT);
2762 }
2763
2764 /**
2765 * Sets the current color effect setting.
2766 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002767 * @param value new color effect.
2768 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002769 */
2770 public void setColorEffect(String value) {
2771 set(KEY_EFFECT, value);
2772 }
2773
2774 /**
2775 * Gets the supported color effects.
2776 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002777 * @return a list of supported color effects. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002778 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002779 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002780 */
2781 public List<String> getSupportedColorEffects() {
2782 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX);
2783 return split(str);
2784 }
2785
2786
2787 /**
2788 * Gets the current antibanding setting.
2789 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002790 * @return current antibanding. null if antibanding setting is not
2791 * supported.
2792 * @see #ANTIBANDING_AUTO
2793 * @see #ANTIBANDING_50HZ
2794 * @see #ANTIBANDING_60HZ
2795 * @see #ANTIBANDING_OFF
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002796 */
2797 public String getAntibanding() {
2798 return get(KEY_ANTIBANDING);
2799 }
2800
2801 /**
2802 * Sets the antibanding.
2803 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002804 * @param antibanding new antibanding value.
2805 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002806 */
2807 public void setAntibanding(String antibanding) {
2808 set(KEY_ANTIBANDING, antibanding);
2809 }
2810
2811 /**
2812 * Gets the supported antibanding values.
2813 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002814 * @return a list of supported antibanding values. null if antibanding
2815 * setting is not supported.
2816 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002817 */
2818 public List<String> getSupportedAntibanding() {
2819 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX);
2820 return split(str);
2821 }
2822
2823 /**
2824 * Gets the current scene mode setting.
2825 *
2826 * @return one of SCENE_MODE_XXX string constant. null if scene mode
2827 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002828 * @see #SCENE_MODE_AUTO
2829 * @see #SCENE_MODE_ACTION
2830 * @see #SCENE_MODE_PORTRAIT
2831 * @see #SCENE_MODE_LANDSCAPE
2832 * @see #SCENE_MODE_NIGHT
2833 * @see #SCENE_MODE_NIGHT_PORTRAIT
2834 * @see #SCENE_MODE_THEATRE
2835 * @see #SCENE_MODE_BEACH
2836 * @see #SCENE_MODE_SNOW
2837 * @see #SCENE_MODE_SUNSET
2838 * @see #SCENE_MODE_STEADYPHOTO
2839 * @see #SCENE_MODE_FIREWORKS
2840 * @see #SCENE_MODE_SPORTS
2841 * @see #SCENE_MODE_PARTY
2842 * @see #SCENE_MODE_CANDLELIGHT
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07002843 * @see #SCENE_MODE_BARCODE
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002844 */
2845 public String getSceneMode() {
2846 return get(KEY_SCENE_MODE);
2847 }
2848
2849 /**
Wu-cheng Lic58b4232010-03-29 17:21:28 +08002850 * Sets the scene mode. Changing scene mode may override other
2851 * parameters (such as flash mode, focus mode, white balance). For
2852 * example, suppose originally flash mode is on and supported flash
2853 * modes are on/off. In night scene mode, both flash mode and supported
2854 * flash mode may be changed to off. After setting scene mode,
Wu-cheng Li2988ab72009-09-30 17:08:19 -07002855 * applications should call getParameters to know if some parameters are
2856 * changed.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002857 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002858 * @param value scene mode.
2859 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002860 */
2861 public void setSceneMode(String value) {
2862 set(KEY_SCENE_MODE, value);
2863 }
2864
2865 /**
2866 * Gets the supported scene modes.
2867 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002868 * @return a list of supported scene modes. null if scene mode setting
2869 * is not supported.
2870 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002871 */
2872 public List<String> getSupportedSceneModes() {
2873 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX);
2874 return split(str);
2875 }
2876
2877 /**
2878 * Gets the current flash mode setting.
2879 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002880 * @return current flash mode. null if flash mode setting is not
2881 * supported.
2882 * @see #FLASH_MODE_OFF
2883 * @see #FLASH_MODE_AUTO
2884 * @see #FLASH_MODE_ON
2885 * @see #FLASH_MODE_RED_EYE
2886 * @see #FLASH_MODE_TORCH
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002887 */
2888 public String getFlashMode() {
2889 return get(KEY_FLASH_MODE);
2890 }
2891
2892 /**
2893 * Sets the flash mode.
2894 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002895 * @param value flash mode.
2896 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002897 */
2898 public void setFlashMode(String value) {
2899 set(KEY_FLASH_MODE, value);
2900 }
2901
2902 /**
2903 * Gets the supported flash modes.
2904 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002905 * @return a list of supported flash modes. null if flash mode setting
2906 * is not supported.
2907 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002908 */
2909 public List<String> getSupportedFlashModes() {
2910 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX);
2911 return split(str);
2912 }
2913
Wu-cheng Li36322db2009-09-18 18:59:21 +08002914 /**
2915 * Gets the current focus mode setting.
2916 *
Wu-cheng Li699fe932010-08-05 11:50:25 -07002917 * @return current focus mode. This method will always return a non-null
2918 * value. Applications should call {@link
2919 * #autoFocus(AutoFocusCallback)} to start the focus if focus
2920 * mode is FOCUS_MODE_AUTO or FOCUS_MODE_MACRO.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002921 * @see #FOCUS_MODE_AUTO
2922 * @see #FOCUS_MODE_INFINITY
2923 * @see #FOCUS_MODE_MACRO
2924 * @see #FOCUS_MODE_FIXED
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07002925 * @see #FOCUS_MODE_EDOF
Wu-cheng Lid45cb722010-09-20 16:15:32 -07002926 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Li36322db2009-09-18 18:59:21 +08002927 */
2928 public String getFocusMode() {
2929 return get(KEY_FOCUS_MODE);
2930 }
2931
2932 /**
2933 * Sets the focus mode.
2934 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002935 * @param value focus mode.
2936 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08002937 */
2938 public void setFocusMode(String value) {
2939 set(KEY_FOCUS_MODE, value);
2940 }
2941
2942 /**
2943 * Gets the supported focus modes.
2944 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002945 * @return a list of supported focus modes. This method will always
2946 * return a list with at least one element.
2947 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08002948 */
2949 public List<String> getSupportedFocusModes() {
2950 String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX);
2951 return split(str);
2952 }
2953
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002954 /**
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08002955 * Gets the focal length (in millimeter) of the camera.
2956 *
2957 * @return the focal length. This method will always return a valid
2958 * value.
2959 */
2960 public float getFocalLength() {
2961 return Float.parseFloat(get(KEY_FOCAL_LENGTH));
2962 }
2963
2964 /**
2965 * Gets the horizontal angle of view in degrees.
2966 *
2967 * @return horizontal angle of view. This method will always return a
2968 * valid value.
2969 */
2970 public float getHorizontalViewAngle() {
2971 return Float.parseFloat(get(KEY_HORIZONTAL_VIEW_ANGLE));
2972 }
2973
2974 /**
2975 * Gets the vertical angle of view in degrees.
2976 *
2977 * @return vertical angle of view. This method will always return a
2978 * valid value.
2979 */
2980 public float getVerticalViewAngle() {
2981 return Float.parseFloat(get(KEY_VERTICAL_VIEW_ANGLE));
2982 }
2983
2984 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002985 * Gets the current exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002986 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002987 * @return current exposure compensation index. The range is {@link
2988 * #getMinExposureCompensation} to {@link
2989 * #getMaxExposureCompensation}. 0 means exposure is not
2990 * adjusted.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002991 */
2992 public int getExposureCompensation() {
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002993 return getInt(KEY_EXPOSURE_COMPENSATION, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08002994 }
2995
2996 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002997 * Sets the exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08002998 *
Wu-cheng Li0402e7d2010-02-26 15:04:55 +08002999 * @param value exposure compensation index. The valid value range is
3000 * from {@link #getMinExposureCompensation} (inclusive) to {@link
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003001 * #getMaxExposureCompensation} (inclusive). 0 means exposure is
3002 * not adjusted. Application should call
3003 * getMinExposureCompensation and getMaxExposureCompensation to
3004 * know if exposure compensation is supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003005 */
3006 public void setExposureCompensation(int value) {
3007 set(KEY_EXPOSURE_COMPENSATION, value);
3008 }
3009
3010 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003011 * Gets the maximum exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003012 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003013 * @return maximum exposure compensation index (>=0). If both this
3014 * method and {@link #getMinExposureCompensation} return 0,
3015 * exposure compensation is not supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003016 */
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003017 public int getMaxExposureCompensation() {
3018 return getInt(KEY_MAX_EXPOSURE_COMPENSATION, 0);
3019 }
3020
3021 /**
3022 * Gets the minimum exposure compensation index.
3023 *
3024 * @return minimum exposure compensation index (<=0). If both this
3025 * method and {@link #getMaxExposureCompensation} return 0,
3026 * exposure compensation is not supported.
3027 */
3028 public int getMinExposureCompensation() {
3029 return getInt(KEY_MIN_EXPOSURE_COMPENSATION, 0);
3030 }
3031
3032 /**
3033 * Gets the exposure compensation step.
3034 *
3035 * @return exposure compensation step. Applications can get EV by
3036 * multiplying the exposure compensation index and step. Ex: if
3037 * exposure compensation index is -6 and step is 0.333333333, EV
3038 * is -2.
3039 */
3040 public float getExposureCompensationStep() {
3041 return getFloat(KEY_EXPOSURE_COMPENSATION_STEP, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08003042 }
3043
3044 /**
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003045 * <p>Sets the auto-exposure lock state. Applications should check
3046 * {@link #isAutoExposureLockSupported} before using this method.</p>
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003047 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003048 * <p>If set to true, the camera auto-exposure routine will immediately
3049 * pause until the lock is set to false. Exposure compensation settings
3050 * changes will still take effect while auto-exposure is locked.</p>
3051 *
3052 * <p>If auto-exposure is already locked, setting this to true again has
3053 * no effect (the driver will not recalculate exposure values).</p>
3054 *
3055 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
3056 * image capture with {@link #takePicture(Camera.ShutterCallback,
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003057 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
3058 * lock.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003059 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003060 * <p>Exposure compensation, auto-exposure lock, and auto-white balance
3061 * lock can be used to capture an exposure-bracketed burst of images,
3062 * for example.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003063 *
3064 * <p>Auto-exposure state, including the lock state, will not be
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003065 * maintained after camera {@link #release()} is called. Locking
3066 * auto-exposure after {@link #open()} but before the first call to
3067 * {@link #startPreview()} will not allow the auto-exposure routine to
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003068 * run at all, and may result in severely over- or under-exposed
3069 * images.</p>
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003070 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003071 * @param toggle new state of the auto-exposure lock. True means that
3072 * auto-exposure is locked, false means that the auto-exposure
3073 * routine is free to run normally.
3074 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003075 * @see #getAutoExposureLock()
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003076 */
3077 public void setAutoExposureLock(boolean toggle) {
3078 set(KEY_AUTO_EXPOSURE_LOCK, toggle ? TRUE : FALSE);
3079 }
3080
3081 /**
3082 * Gets the state of the auto-exposure lock. Applications should check
3083 * {@link #isAutoExposureLockSupported} before using this method. See
3084 * {@link #setAutoExposureLock} for details about the lock.
3085 *
3086 * @return State of the auto-exposure lock. Returns true if
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003087 * auto-exposure is currently locked, and false otherwise.
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003088 *
3089 * @see #setAutoExposureLock(boolean)
3090 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003091 */
3092 public boolean getAutoExposureLock() {
3093 String str = get(KEY_AUTO_EXPOSURE_LOCK);
3094 return TRUE.equals(str);
3095 }
3096
3097 /**
3098 * Returns true if auto-exposure locking is supported. Applications
3099 * should call this before trying to lock auto-exposure. See
3100 * {@link #setAutoExposureLock} for details about the lock.
3101 *
3102 * @return true if auto-exposure lock is supported.
3103 * @see #setAutoExposureLock(boolean)
3104 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003105 */
3106 public boolean isAutoExposureLockSupported() {
3107 String str = get(KEY_AUTO_EXPOSURE_LOCK_SUPPORTED);
3108 return TRUE.equals(str);
3109 }
3110
3111 /**
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003112 * <p>Sets the auto-white balance lock state. Applications should check
3113 * {@link #isAutoWhiteBalanceLockSupported} before using this
3114 * method.</p>
3115 *
3116 * <p>If set to true, the camera auto-white balance routine will
3117 * immediately pause until the lock is set to false.</p>
3118 *
3119 * <p>If auto-white balance is already locked, setting this to true
3120 * again has no effect (the driver will not recalculate white balance
3121 * values).</p>
3122 *
3123 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
3124 * image capture with {@link #takePicture(Camera.ShutterCallback,
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003125 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
3126 * the lock.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003127 *
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003128 * <p> Changing the white balance mode with {@link #setWhiteBalance}
3129 * will release the auto-white balance lock if it is set.</p>
3130 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003131 * <p>Exposure compensation, AE lock, and AWB lock can be used to
3132 * capture an exposure-bracketed burst of images, for example.
3133 * Auto-white balance state, including the lock state, will not be
3134 * maintained after camera {@link #release()} is called. Locking
3135 * auto-white balance after {@link #open()} but before the first call to
3136 * {@link #startPreview()} will not allow the auto-white balance routine
3137 * to run at all, and may result in severely incorrect color in captured
3138 * images.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003139 *
3140 * @param toggle new state of the auto-white balance lock. True means
3141 * that auto-white balance is locked, false means that the
3142 * auto-white balance routine is free to run normally.
3143 *
3144 * @see #getAutoWhiteBalanceLock()
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003145 * @see #setWhiteBalance(String)
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003146 */
3147 public void setAutoWhiteBalanceLock(boolean toggle) {
3148 set(KEY_AUTO_WHITEBALANCE_LOCK, toggle ? TRUE : FALSE);
3149 }
3150
3151 /**
3152 * Gets the state of the auto-white balance lock. Applications should
3153 * check {@link #isAutoWhiteBalanceLockSupported} before using this
3154 * method. See {@link #setAutoWhiteBalanceLock} for details about the
3155 * lock.
3156 *
3157 * @return State of the auto-white balance lock. Returns true if
3158 * auto-white balance is currently locked, and false
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003159 * otherwise.
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003160 *
3161 * @see #setAutoWhiteBalanceLock(boolean)
3162 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003163 */
3164 public boolean getAutoWhiteBalanceLock() {
3165 String str = get(KEY_AUTO_WHITEBALANCE_LOCK);
3166 return TRUE.equals(str);
3167 }
3168
3169 /**
3170 * Returns true if auto-white balance locking is supported. Applications
3171 * should call this before trying to lock auto-white balance. See
3172 * {@link #setAutoWhiteBalanceLock} for details about the lock.
3173 *
3174 * @return true if auto-white balance lock is supported.
3175 * @see #setAutoWhiteBalanceLock(boolean)
3176 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003177 */
3178 public boolean isAutoWhiteBalanceLockSupported() {
3179 String str = get(KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED);
3180 return TRUE.equals(str);
3181 }
3182
3183 /**
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003184 * Gets current zoom value. This also works when smooth zoom is in
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003185 * progress. Applications should check {@link #isZoomSupported} before
3186 * using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003187 *
3188 * @return the current zoom value. The range is 0 to {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003189 * #getMaxZoom}. 0 means the camera is not zoomed.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003190 */
3191 public int getZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003192 return getInt(KEY_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003193 }
3194
3195 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003196 * Sets current zoom value. If the camera is zoomed (value > 0), the
3197 * actual picture size may be smaller than picture size setting.
3198 * Applications can check the actual picture size after picture is
3199 * returned from {@link PictureCallback}. The preview size remains the
3200 * same in zoom. Applications should check {@link #isZoomSupported}
3201 * before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003202 *
3203 * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003204 */
3205 public void setZoom(int value) {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003206 set(KEY_ZOOM, value);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003207 }
3208
3209 /**
3210 * Returns true if zoom is supported. Applications should call this
3211 * before using other zoom methods.
3212 *
3213 * @return true if zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003214 */
3215 public boolean isZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003216 String str = get(KEY_ZOOM_SUPPORTED);
3217 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003218 }
3219
3220 /**
3221 * Gets the maximum zoom value allowed for snapshot. This is the maximum
3222 * value that applications can set to {@link #setZoom(int)}.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003223 * Applications should call {@link #isZoomSupported} before using this
3224 * method. This value may change in different preview size. Applications
3225 * should call this again after setting preview size.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003226 *
3227 * @return the maximum zoom value supported by the camera.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003228 */
3229 public int getMaxZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003230 return getInt(KEY_MAX_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003231 }
3232
3233 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003234 * Gets the zoom ratios of all zoom values. Applications should check
3235 * {@link #isZoomSupported} before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003236 *
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003237 * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is
3238 * returned as 320. The number of elements is {@link
3239 * #getMaxZoom} + 1. The list is sorted from small to large. The
3240 * first element is always 100. The last element is the zoom
3241 * ratio of the maximum zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003242 */
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003243 public List<Integer> getZoomRatios() {
3244 return splitInt(get(KEY_ZOOM_RATIOS));
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003245 }
3246
3247 /**
3248 * Returns true if smooth zoom is supported. Applications should call
3249 * this before using other smooth zoom methods.
3250 *
3251 * @return true if smooth zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003252 */
3253 public boolean isSmoothZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003254 String str = get(KEY_SMOOTH_ZOOM_SUPPORTED);
3255 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003256 }
3257
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003258 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003259 * <p>Gets the distances from the camera to where an object appears to be
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003260 * in focus. The object is sharpest at the optimal focus distance. The
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003261 * depth of field is the far focus distance minus near focus distance.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003262 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003263 * <p>Focus distances may change after calling {@link
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003264 * #autoFocus(AutoFocusCallback)}, {@link #cancelAutoFocus}, or {@link
3265 * #startPreview()}. Applications can call {@link #getParameters()}
3266 * and this method anytime to get the latest focus distances. If the
Wu-cheng Lid45cb722010-09-20 16:15:32 -07003267 * focus mode is FOCUS_MODE_CONTINUOUS_VIDEO, focus distances may change
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003268 * from time to time.</p>
Wu-cheng Li699fe932010-08-05 11:50:25 -07003269 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003270 * <p>This method is intended to estimate the distance between the camera
Wu-cheng Li699fe932010-08-05 11:50:25 -07003271 * and the subject. After autofocus, the subject distance may be within
3272 * near and far focus distance. However, the precision depends on the
3273 * camera hardware, autofocus algorithm, the focus area, and the scene.
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003274 * The error can be large and it should be only used as a reference.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003275 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003276 * <p>Far focus distance >= optimal focus distance >= near focus distance.
Wu-cheng Li185cc452010-05-20 15:36:13 +08003277 * If the focus distance is infinity, the value will be
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003278 * {@code Float.POSITIVE_INFINITY}.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003279 *
3280 * @param output focus distances in meters. output must be a float
3281 * array with three elements. Near focus distance, optimal focus
3282 * distance, and far focus distance will be filled in the array.
Wu-cheng Li185cc452010-05-20 15:36:13 +08003283 * @see #FOCUS_DISTANCE_NEAR_INDEX
3284 * @see #FOCUS_DISTANCE_OPTIMAL_INDEX
3285 * @see #FOCUS_DISTANCE_FAR_INDEX
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003286 */
3287 public void getFocusDistances(float[] output) {
3288 if (output == null || output.length != 3) {
3289 throw new IllegalArgumentException(
Ken Wakasaf76a50c2012-03-09 19:56:35 +09003290 "output must be a float array with three elements.");
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003291 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07003292 splitFloat(get(KEY_FOCUS_DISTANCES), output);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003293 }
3294
Wu-cheng Li30771b72011-04-02 06:19:46 +08003295 /**
3296 * Gets the maximum number of focus areas supported. This is the maximum
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003297 * length of the list in {@link #setFocusAreas(List)} and
3298 * {@link #getFocusAreas()}.
Wu-cheng Li30771b72011-04-02 06:19:46 +08003299 *
3300 * @return the maximum number of focus areas supported by the camera.
3301 * @see #getFocusAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08003302 */
3303 public int getMaxNumFocusAreas() {
3304 return getInt(KEY_MAX_NUM_FOCUS_AREAS, 0);
3305 }
3306
3307 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003308 * <p>Gets the current focus areas. Camera driver uses the areas to decide
3309 * focus.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003310 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003311 * <p>Before using this API or {@link #setFocusAreas(List)}, apps should
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003312 * call {@link #getMaxNumFocusAreas()} to know the maximum number of
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003313 * focus areas first. If the value is 0, focus area is not supported.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003314 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003315 * <p>Each focus area is a rectangle with specified weight. The direction
Wu-cheng Li30771b72011-04-02 06:19:46 +08003316 * is relative to the sensor orientation, that is, what the sensor sees.
3317 * The direction is not affected by the rotation or mirroring of
3318 * {@link #setDisplayOrientation(int)}. Coordinates of the rectangle
3319 * range from -1000 to 1000. (-1000, -1000) is the upper left point.
Wu-cheng Libde61a52011-06-07 18:23:14 +08003320 * (1000, 1000) is the lower right point. The width and height of focus
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003321 * areas cannot be 0 or negative.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003322 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003323 * <p>The weight must range from 1 to 1000. The weight should be
Eino-Ville Talvala4e396e02011-04-21 09:23:15 -07003324 * interpreted as a per-pixel weight - all pixels in the area have the
3325 * specified weight. This means a small area with the same weight as a
3326 * larger area will have less influence on the focusing than the larger
3327 * area. Focus areas can partially overlap and the driver will add the
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003328 * weights in the overlap region.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003329 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003330 * <p>A special case of a {@code null} focus area list means the driver is
3331 * free to select focus targets as it wants. For example, the driver may
3332 * use more signals to select focus areas and change them
3333 * dynamically. Apps can set the focus area list to {@code null} if they
3334 * want the driver to completely control focusing.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003335 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003336 * <p>Focus areas are relative to the current field of view
Wu-cheng Li30771b72011-04-02 06:19:46 +08003337 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000)
3338 * represents the top of the currently visible camera frame. The focus
3339 * area cannot be set to be outside the current field of view, even
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003340 * when using zoom.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003341 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003342 * <p>Focus area only has effect if the current focus mode is
Wu-cheng Li53b30912011-10-12 19:43:51 +08003343 * {@link #FOCUS_MODE_AUTO}, {@link #FOCUS_MODE_MACRO},
3344 * {@link #FOCUS_MODE_CONTINUOUS_VIDEO}, or
3345 * {@link #FOCUS_MODE_CONTINUOUS_PICTURE}.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08003346 *
3347 * @return a list of current focus areas
Wu-cheng Li30771b72011-04-02 06:19:46 +08003348 */
3349 public List<Area> getFocusAreas() {
Wu-cheng Lif715bf92011-04-14 14:04:18 +08003350 return splitArea(get(KEY_FOCUS_AREAS));
Wu-cheng Li30771b72011-04-02 06:19:46 +08003351 }
3352
3353 /**
3354 * Sets focus areas. See {@link #getFocusAreas()} for documentation.
3355 *
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003356 * @param focusAreas the focus areas
Wu-cheng Li30771b72011-04-02 06:19:46 +08003357 * @see #getFocusAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08003358 */
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003359 public void setFocusAreas(List<Area> focusAreas) {
3360 set(KEY_FOCUS_AREAS, focusAreas);
3361 }
3362
3363 /**
3364 * Gets the maximum number of metering areas supported. This is the
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003365 * maximum length of the list in {@link #setMeteringAreas(List)} and
3366 * {@link #getMeteringAreas()}.
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003367 *
3368 * @return the maximum number of metering areas supported by the camera.
3369 * @see #getMeteringAreas()
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003370 */
3371 public int getMaxNumMeteringAreas() {
3372 return getInt(KEY_MAX_NUM_METERING_AREAS, 0);
3373 }
3374
3375 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003376 * <p>Gets the current metering areas. Camera driver uses these areas to
3377 * decide exposure.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003378 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003379 * <p>Before using this API or {@link #setMeteringAreas(List)}, apps should
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003380 * call {@link #getMaxNumMeteringAreas()} to know the maximum number of
3381 * metering areas first. If the value is 0, metering area is not
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003382 * supported.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003383 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003384 * <p>Each metering area is a rectangle with specified weight. The
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003385 * direction is relative to the sensor orientation, that is, what the
3386 * sensor sees. The direction is not affected by the rotation or
3387 * mirroring of {@link #setDisplayOrientation(int)}. Coordinates of the
3388 * rectangle range from -1000 to 1000. (-1000, -1000) is the upper left
Wu-cheng Libde61a52011-06-07 18:23:14 +08003389 * point. (1000, 1000) is the lower right point. The width and height of
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003390 * metering areas cannot be 0 or negative.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003391 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003392 * <p>The weight must range from 1 to 1000, and represents a weight for
Eino-Ville Talvala4e396e02011-04-21 09:23:15 -07003393 * every pixel in the area. This means that a large metering area with
3394 * the same weight as a smaller area will have more effect in the
3395 * metering result. Metering areas can partially overlap and the driver
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003396 * will add the weights in the overlap region.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003397 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003398 * <p>A special case of a {@code null} metering area list means the driver
3399 * is free to meter as it chooses. For example, the driver may use more
3400 * signals to select metering areas and change them dynamically. Apps
3401 * can set the metering area list to {@code null} if they want the
3402 * driver to completely control metering.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003403 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003404 * <p>Metering areas are relative to the current field of view
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003405 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000)
3406 * represents the top of the currently visible camera frame. The
3407 * metering area cannot be set to be outside the current field of view,
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003408 * even when using zoom.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003409 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003410 * <p>No matter what metering areas are, the final exposure are compensated
3411 * by {@link #setExposureCompensation(int)}.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003412 *
3413 * @return a list of current metering areas
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003414 */
3415 public List<Area> getMeteringAreas() {
Wu-cheng Lid8aab932011-06-21 11:50:43 +08003416 return splitArea(get(KEY_METERING_AREAS));
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003417 }
3418
3419 /**
3420 * Sets metering areas. See {@link #getMeteringAreas()} for
3421 * documentation.
3422 *
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003423 * @param meteringAreas the metering areas
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003424 * @see #getMeteringAreas()
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08003425 */
3426 public void setMeteringAreas(List<Area> meteringAreas) {
3427 set(KEY_METERING_AREAS, meteringAreas);
Wu-cheng Li30771b72011-04-02 06:19:46 +08003428 }
3429
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08003430 /**
3431 * Gets the maximum number of detected faces supported. This is the
3432 * maximum length of the list returned from {@link FaceDetectionListener}.
3433 * If the return value is 0, face detection of the specified type is not
3434 * supported.
3435 *
3436 * @return the maximum number of detected face supported by the camera.
Joe Fernandez464cb212011-10-04 16:56:47 -07003437 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08003438 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08003439 public int getMaxNumDetectedFaces() {
3440 return getInt(KEY_MAX_NUM_DETECTED_FACES_HW, 0);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08003441 }
3442
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08003443 /**
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08003444 * Sets recording mode hint. This tells the camera that the intent of
3445 * the application is to record videos {@link
3446 * android.media.MediaRecorder#start()}, not to take still pictures
3447 * {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
3448 * Camera.PictureCallback, Camera.PictureCallback)}. Using this hint can
3449 * allow MediaRecorder.start() to start faster or with fewer glitches on
3450 * output. This should be called before starting preview for the best
3451 * result, but can be changed while the preview is active. The default
3452 * value is false.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08003453 *
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08003454 * The app can still call takePicture() when the hint is true or call
3455 * MediaRecorder.start() when the hint is false. But the performance may
3456 * be worse.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08003457 *
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08003458 * @param hint true if the apps intend to record videos using
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08003459 * {@link android.media.MediaRecorder}.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08003460 */
3461 public void setRecordingHint(boolean hint) {
3462 set(KEY_RECORDING_HINT, hint ? TRUE : FALSE);
3463 }
3464
Wu-cheng Li98bb2512011-08-30 21:33:10 +08003465 /**
3466 * Returns true if video snapshot is supported. That is, applications
3467 * can call {@link #takePicture(Camera.ShutterCallback,
3468 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}
3469 * during recording. Applications do not need to call {@link
3470 * #startPreview()} after taking a picture. The preview will be still
3471 * active. Other than that, taking a picture during recording is
3472 * identical to taking a picture normally. All settings and methods
3473 * related to takePicture work identically. Ex: {@link
3474 * #getPictureSize()}, {@link #getSupportedPictureSizes()}, {@link
3475 * #setJpegQuality(int)}, {@link #setRotation(int)}, and etc. The
3476 * picture will have an EXIF header. {@link #FLASH_MODE_AUTO} and {@link
3477 * #FLASH_MODE_ON} also still work, but the video will record the flash.
3478 *
3479 * Applications can set shutter callback as null to avoid the shutter
3480 * sound. It is also recommended to set raw picture and post view
3481 * callbacks to null to avoid the interrupt of preview display.
3482 *
3483 * Field-of-view of the recorded video may be different from that of the
3484 * captured pictures.
3485 *
3486 * @return true if video snapshot is supported.
Wu-cheng Li98bb2512011-08-30 21:33:10 +08003487 */
3488 public boolean isVideoSnapshotSupported() {
3489 String str = get(KEY_VIDEO_SNAPSHOT_SUPPORTED);
3490 return TRUE.equals(str);
3491 }
3492
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003493 /**
3494 * <p>Enables and disables video stabilization. Use
3495 * {@link #isVideoStabilizationSupported} to determine if calling this
3496 * method is valid.</p>
3497 *
3498 * <p>Video stabilization reduces the shaking due to the motion of the
3499 * camera in both the preview stream and in recorded videos, including
3500 * data received from the preview callback. It does not reduce motion
3501 * blur in images captured with
3502 * {@link Camera#takePicture takePicture}.</p>
3503 *
3504 * <p>Video stabilization can be enabled and disabled while preview or
3505 * recording is active, but toggling it may cause a jump in the video
3506 * stream that may be undesirable in a recorded video.</p>
3507 *
3508 * @param toggle Set to true to enable video stabilization, and false to
3509 * disable video stabilization.
3510 * @see #isVideoStabilizationSupported()
3511 * @see #getVideoStabilization()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003512 */
3513 public void setVideoStabilization(boolean toggle) {
3514 set(KEY_VIDEO_STABILIZATION, toggle ? TRUE : FALSE);
3515 }
3516
3517 /**
3518 * Get the current state of video stabilization. See
3519 * {@link #setVideoStabilization} for details of video stabilization.
3520 *
3521 * @return true if video stabilization is enabled
3522 * @see #isVideoStabilizationSupported()
3523 * @see #setVideoStabilization(boolean)
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003524 */
3525 public boolean getVideoStabilization() {
3526 String str = get(KEY_VIDEO_STABILIZATION);
3527 return TRUE.equals(str);
3528 }
3529
3530 /**
3531 * Returns true if video stabilization is supported. See
3532 * {@link #setVideoStabilization} for details of video stabilization.
3533 *
3534 * @return true if video stabilization is supported
3535 * @see #setVideoStabilization(boolean)
3536 * @see #getVideoStabilization()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003537 */
3538 public boolean isVideoStabilizationSupported() {
3539 String str = get(KEY_VIDEO_STABILIZATION_SUPPORTED);
3540 return TRUE.equals(str);
3541 }
3542
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003543 // Splits a comma delimited string to an ArrayList of String.
3544 // Return null if the passing string is null or the size is 0.
3545 private ArrayList<String> split(String str) {
3546 if (str == null) return null;
3547
Ali Utku Selen0a120182011-02-09 14:11:22 +01003548 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
3549 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003550 ArrayList<String> substrings = new ArrayList<String>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01003551 for (String s : splitter) {
3552 substrings.add(s);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003553 }
3554 return substrings;
3555 }
3556
3557 // Splits a comma delimited string to an ArrayList of Integer.
3558 // Return null if the passing string is null or the size is 0.
3559 private ArrayList<Integer> splitInt(String str) {
3560 if (str == null) return null;
3561
Ali Utku Selen0a120182011-02-09 14:11:22 +01003562 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
3563 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003564 ArrayList<Integer> substrings = new ArrayList<Integer>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01003565 for (String s : splitter) {
3566 substrings.add(Integer.parseInt(s));
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003567 }
3568 if (substrings.size() == 0) return null;
3569 return substrings;
3570 }
3571
Wu-cheng Li454630f2010-08-11 16:48:05 -07003572 private void splitInt(String str, int[] output) {
3573 if (str == null) return;
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003574
Ali Utku Selen0a120182011-02-09 14:11:22 +01003575 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
3576 splitter.setString(str);
Wu-cheng Li454630f2010-08-11 16:48:05 -07003577 int index = 0;
Ali Utku Selen0a120182011-02-09 14:11:22 +01003578 for (String s : splitter) {
3579 output[index++] = Integer.parseInt(s);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003580 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07003581 }
3582
3583 // Splits a comma delimited string to an ArrayList of Float.
3584 private void splitFloat(String str, float[] output) {
3585 if (str == null) return;
3586
Ali Utku Selen0a120182011-02-09 14:11:22 +01003587 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
3588 splitter.setString(str);
Wu-cheng Li454630f2010-08-11 16:48:05 -07003589 int index = 0;
Ali Utku Selen0a120182011-02-09 14:11:22 +01003590 for (String s : splitter) {
3591 output[index++] = Float.parseFloat(s);
Wu-cheng Li454630f2010-08-11 16:48:05 -07003592 }
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003593 }
3594
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003595 // Returns the value of a float parameter.
3596 private float getFloat(String key, float defaultValue) {
3597 try {
3598 return Float.parseFloat(mMap.get(key));
3599 } catch (NumberFormatException ex) {
3600 return defaultValue;
3601 }
3602 }
3603
3604 // Returns the value of a integer parameter.
3605 private int getInt(String key, int defaultValue) {
3606 try {
3607 return Integer.parseInt(mMap.get(key));
3608 } catch (NumberFormatException ex) {
3609 return defaultValue;
3610 }
3611 }
3612
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003613 // Splits a comma delimited string to an ArrayList of Size.
3614 // Return null if the passing string is null or the size is 0.
3615 private ArrayList<Size> splitSize(String str) {
3616 if (str == null) return null;
3617
Ali Utku Selen0a120182011-02-09 14:11:22 +01003618 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
3619 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003620 ArrayList<Size> sizeList = new ArrayList<Size>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01003621 for (String s : splitter) {
3622 Size size = strToSize(s);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003623 if (size != null) sizeList.add(size);
3624 }
3625 if (sizeList.size() == 0) return null;
3626 return sizeList;
3627 }
3628
3629 // Parses a string (ex: "480x320") to Size object.
3630 // Return null if the passing string is null.
3631 private Size strToSize(String str) {
3632 if (str == null) return null;
3633
3634 int pos = str.indexOf('x');
3635 if (pos != -1) {
3636 String width = str.substring(0, pos);
3637 String height = str.substring(pos + 1);
3638 return new Size(Integer.parseInt(width),
3639 Integer.parseInt(height));
3640 }
3641 Log.e(TAG, "Invalid size parameter string=" + str);
3642 return null;
3643 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07003644
3645 // Splits a comma delimited string to an ArrayList of int array.
3646 // Example string: "(10000,26623),(10000,30000)". Return null if the
3647 // passing string is null or the size is 0.
3648 private ArrayList<int[]> splitRange(String str) {
3649 if (str == null || str.charAt(0) != '('
3650 || str.charAt(str.length() - 1) != ')') {
3651 Log.e(TAG, "Invalid range list string=" + str);
3652 return null;
3653 }
3654
3655 ArrayList<int[]> rangeList = new ArrayList<int[]>();
3656 int endIndex, fromIndex = 1;
3657 do {
3658 int[] range = new int[2];
3659 endIndex = str.indexOf("),(", fromIndex);
3660 if (endIndex == -1) endIndex = str.length() - 1;
3661 splitInt(str.substring(fromIndex, endIndex), range);
3662 rangeList.add(range);
3663 fromIndex = endIndex + 3;
3664 } while (endIndex != str.length() - 1);
3665
3666 if (rangeList.size() == 0) return null;
3667 return rangeList;
3668 }
Wu-cheng Li30771b72011-04-02 06:19:46 +08003669
3670 // Splits a comma delimited string to an ArrayList of Area objects.
3671 // Example string: "(-10,-10,0,0,300),(0,0,10,10,700)". Return null if
Wu-cheng Lif715bf92011-04-14 14:04:18 +08003672 // the passing string is null or the size is 0 or (0,0,0,0,0).
Wu-cheng Li30771b72011-04-02 06:19:46 +08003673 private ArrayList<Area> splitArea(String str) {
3674 if (str == null || str.charAt(0) != '('
3675 || str.charAt(str.length() - 1) != ')') {
3676 Log.e(TAG, "Invalid area string=" + str);
3677 return null;
3678 }
3679
3680 ArrayList<Area> result = new ArrayList<Area>();
3681 int endIndex, fromIndex = 1;
3682 int[] array = new int[5];
3683 do {
3684 endIndex = str.indexOf("),(", fromIndex);
3685 if (endIndex == -1) endIndex = str.length() - 1;
3686 splitInt(str.substring(fromIndex, endIndex), array);
3687 Rect rect = new Rect(array[0], array[1], array[2], array[3]);
3688 result.add(new Area(rect, array[4]));
3689 fromIndex = endIndex + 3;
3690 } while (endIndex != str.length() - 1);
3691
3692 if (result.size() == 0) return null;
Wu-cheng Lif715bf92011-04-14 14:04:18 +08003693
3694 if (result.size() == 1) {
Wu-cheng Libde61a52011-06-07 18:23:14 +08003695 Area area = result.get(0);
Wu-cheng Lif715bf92011-04-14 14:04:18 +08003696 Rect rect = area.rect;
3697 if (rect.left == 0 && rect.top == 0 && rect.right == 0
3698 && rect.bottom == 0 && area.weight == 0) {
3699 return null;
3700 }
3701 }
3702
Wu-cheng Li30771b72011-04-02 06:19:46 +08003703 return result;
3704 }
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08003705
3706 private boolean same(String s1, String s2) {
3707 if (s1 == null && s2 == null) return true;
3708 if (s1 != null && s1.equals(s2)) return true;
3709 return false;
3710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 };
3712}