blob: 9154ce0b145561d07ebe2cc52f87881453b58403 [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
Eino-Ville Talvala06a77182017-09-15 12:43:24 -070019import static android.system.OsConstants.*;
20
jiabin503e1fd2018-04-19 16:39:44 -070021import android.annotation.Nullable;
Wu-cheng Li10e09c62011-07-18 09:09:41 +080022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Eino-Ville Talvala06a77182017-09-15 12:43:24 -070024import android.app.ActivityThread;
jiabin503e1fd2018-04-19 16:39:44 -070025import android.app.AppOpsManager;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -070026import android.content.Context;
Mathias Agopiana696f5d2010-02-17 17:53:09 -080027import android.graphics.ImageFormat;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +080028import android.graphics.Point;
Wu-cheng Li30771b72011-04-02 06:19:46 +080029import android.graphics.Rect;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -080030import android.graphics.SurfaceTexture;
jiabin503e1fd2018-04-19 16:39:44 -070031import android.media.AudioAttributes;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -070032import android.media.IAudioService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Handler;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -070034import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.Looper;
36import android.os.Message;
jiabin503e1fd2018-04-19 16:39:44 -070037import android.os.Process;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -070038import android.os.RemoteException;
39import android.os.ServiceManager;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -070040import android.renderscript.Allocation;
41import android.renderscript.Element;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -070042import android.renderscript.RSIllegalArgumentException;
Eino-Ville Talvala06a77182017-09-15 12:43:24 -070043import android.renderscript.RenderScript;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -070044import android.renderscript.Type;
Ali Utku Selen0a120182011-02-09 14:11:22 +010045import android.text.TextUtils;
Eino-Ville Talvala06a77182017-09-15 12:43:24 -070046import android.util.Log;
Wu-cheng Libde61a52011-06-07 18:23:14 +080047import android.view.Surface;
48import android.view.SurfaceHolder;
49
jiabin503e1fd2018-04-19 16:39:44 -070050import com.android.internal.annotations.GuardedBy;
51import com.android.internal.app.IAppOpsCallback;
52import com.android.internal.app.IAppOpsService;
53
Wu-cheng Libde61a52011-06-07 18:23:14 +080054import java.io.IOException;
55import java.lang.ref.WeakReference;
56import java.util.ArrayList;
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -070057import java.util.LinkedHashMap;
Wu-cheng Libde61a52011-06-07 18:23:14 +080058import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60/**
Dan Egnorbfcbeff2010-07-12 15:12:54 -070061 * The Camera class is used to set image capture settings, start/stop preview,
62 * snap pictures, and retrieve frames for encoding for video. This class is a
63 * client for the Camera service, which manages the actual camera hardware.
Scott Maindf4578e2009-09-10 12:22:07 -070064 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070065 * <p>To access the device camera, you must declare the
Wu-cheng Li7478ea62009-09-16 18:52:55 +080066 * {@link android.Manifest.permission#CAMERA} permission in your Android
Scott Maindf4578e2009-09-10 12:22:07 -070067 * Manifest. Also be sure to include the
68 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
Dan Egnorbfcbeff2010-07-12 15:12:54 -070069 * manifest element to declare camera features used by your application.
Wu-cheng Li7478ea62009-09-16 18:52:55 +080070 * For example, if you use the camera and auto-focus feature, your Manifest
Scott Maindf4578e2009-09-10 12:22:07 -070071 * should include the following:</p>
72 * <pre> &lt;uses-permission android:name="android.permission.CAMERA" />
73 * &lt;uses-feature android:name="android.hardware.camera" />
74 * &lt;uses-feature android:name="android.hardware.camera.autofocus" /></pre>
75 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070076 * <p>To take pictures with this class, use the following steps:</p>
77 *
78 * <ol>
Dan Egnor341ff132010-07-20 11:30:17 -070079 * <li>Obtain an instance of Camera from {@link #open(int)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -070080 *
81 * <li>Get existing (default) settings with {@link #getParameters()}.
82 *
83 * <li>If necessary, modify the returned {@link Camera.Parameters} object and call
84 * {@link #setParameters(Camera.Parameters)}.
85 *
Eino-Ville Talvala6c91e2c2016-03-25 11:54:39 -070086 * <li>Call {@link #setDisplayOrientation(int)} to ensure correct orientation of preview.
Dan Egnorbfcbeff2010-07-12 15:12:54 -070087 *
88 * <li><b>Important</b>: Pass a fully initialized {@link SurfaceHolder} to
89 * {@link #setPreviewDisplay(SurfaceHolder)}. Without a surface, the camera
90 * will be unable to start the preview.
91 *
92 * <li><b>Important</b>: Call {@link #startPreview()} to start updating the
93 * preview surface. Preview must be started before you can take a picture.
94 *
95 * <li>When you want, call {@link #takePicture(Camera.ShutterCallback,
96 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)} to
97 * capture a photo. Wait for the callbacks to provide the actual image data.
98 *
99 * <li>After taking a picture, preview display will have stopped. To take more
100 * photos, call {@link #startPreview()} again first.
101 *
102 * <li>Call {@link #stopPreview()} to stop updating the preview surface.
103 *
104 * <li><b>Important:</b> Call {@link #release()} to release the camera for
105 * use by other applications. Applications should release the camera
106 * immediately in {@link android.app.Activity#onPause()} (and re-{@link #open()}
107 * it in {@link android.app.Activity#onResume()}).
108 * </ol>
109 *
110 * <p>To quickly switch to video recording mode, use these steps:</p>
111 *
112 * <ol>
113 * <li>Obtain and initialize a Camera and start preview as described above.
114 *
115 * <li>Call {@link #unlock()} to allow the media process to access the camera.
116 *
117 * <li>Pass the camera to {@link android.media.MediaRecorder#setCamera(Camera)}.
118 * See {@link android.media.MediaRecorder} information about video recording.
119 *
120 * <li>When finished recording, call {@link #reconnect()} to re-acquire
121 * and re-lock the camera.
122 *
123 * <li>If desired, restart preview and take more photos or videos.
124 *
125 * <li>Call {@link #stopPreview()} and {@link #release()} as described above.
126 * </ol>
127 *
128 * <p>This class is not thread-safe, and is meant for use from one event thread.
129 * Most long-running operations (preview, focus, photo capture, etc) happen
130 * asynchronously and invoke callbacks as necessary. Callbacks will be invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700131 * on the event thread {@link #open(int)} was called from. This class's methods
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700132 * must never be called from multiple threads at once.</p>
133 *
Scott Maindf4578e2009-09-10 12:22:07 -0700134 * <p class="caution"><strong>Caution:</strong> Different Android-powered devices
135 * may have different hardware specifications, such as megapixel ratings and
136 * auto-focus capabilities. In order for your application to be compatible with
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800137 * more devices, you should not make assumptions about the device camera
Scott Maindf4578e2009-09-10 12:22:07 -0700138 * specifications.</p>
Joe Fernandez6c5c3c32011-10-18 14:57:05 -0700139 *
140 * <div class="special reference">
141 * <h3>Developer Guides</h3>
142 * <p>For more information about using cameras, read the
143 * <a href="{@docRoot}guide/topics/media/camera.html">Camera</a> developer guide.</p>
144 * </div>
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700145 *
146 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
147 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700149@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150public class Camera {
151 private static final String TAG = "Camera";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800152
Wu-cheng Lid2c29292010-05-28 17:32:41 +0800153 // These match the enums in frameworks/base/include/camera/Camera.h
Benny Wongda83f462009-08-12 12:01:27 -0500154 private static final int CAMERA_MSG_ERROR = 0x001;
155 private static final int CAMERA_MSG_SHUTTER = 0x002;
156 private static final int CAMERA_MSG_FOCUS = 0x004;
157 private static final int CAMERA_MSG_ZOOM = 0x008;
158 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010;
159 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020;
160 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040;
161 private static final int CAMERA_MSG_RAW_IMAGE = 0x080;
162 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800163 private static final int CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x200;
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800164 private static final int CAMERA_MSG_PREVIEW_METADATA = 0x400;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800165 private static final int CAMERA_MSG_FOCUS_MOVE = 0x800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
Ashok Bhat4838e332014-01-03 14:37:19 +0000167 private long mNativeContext; // accessed by native methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 private EventHandler mEventHandler;
169 private ShutterCallback mShutterCallback;
170 private PictureCallback mRawImageCallback;
171 private PictureCallback mJpegCallback;
172 private PreviewCallback mPreviewCallback;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700173 private boolean mUsingPreviewAllocation;
Dave Sparkse8b26e12009-07-14 10:35:40 -0700174 private PictureCallback mPostviewCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 private AutoFocusCallback mAutoFocusCallback;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800176 private AutoFocusMoveCallback mAutoFocusMoveCallback;
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800177 private OnZoomChangeListener mZoomListener;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800178 private FaceDetectionListener mFaceListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 private ErrorCallback mErrorCallback;
180 private boolean mOneShot;
Andrew Harp94927df2009-10-20 01:47:05 -0400181 private boolean mWithBuffer;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800182 private boolean mFaceDetectionRunning = false;
Igor Murashkina1d66272014-06-20 11:22:11 -0700183 private final Object mAutoFocusCallbackLock = new Object();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800184
jiabin503e1fd2018-04-19 16:39:44 -0700185 private final Object mShutterSoundLock = new Object();
186 // for AppOps
187 private @Nullable IAppOpsService mAppOps;
188 private IAppOpsCallback mAppOpsCallback;
189 @GuardedBy("mShutterSoundLock")
190 private boolean mHasAppOpsPlayAudio = true;
191 @GuardedBy("mShutterSoundLock")
192 private boolean mShutterSoundEnabledFromApp = true;
193
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700194 private static final int NO_ERROR = 0;
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 /**
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800197 * Broadcast Action: A new picture is taken by the camera, and the entry of
198 * the picture has been added to the media store.
199 * {@link android.content.Intent#getData} is URI of the picture.
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800200 *
201 * <p>In {@link android.os.Build.VERSION_CODES#N Android N} this broadcast was removed, and
202 * applications are recommended to use
203 * {@link android.app.job.JobInfo.Builder JobInfo.Builder}.{@link android.app.job.JobInfo.Builder#addTriggerContentUri}
204 * instead.</p>
205 *
206 * <p>In {@link android.os.Build.VERSION_CODES#O Android O} this broadcast has been brought
207 * back, but only for <em>registered</em> receivers. Apps that are actively running can
Dianne Hackbornc1541952017-04-27 10:55:49 -0700208 * again listen to the broadcast if they want an immediate clear signal about a picture
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800209 * being taken, however anything doing heavy work (or needing to be launched) as a result of
210 * this should still use JobScheduler.</p>
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800211 */
212 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
213 public static final String ACTION_NEW_PICTURE = "android.hardware.action.NEW_PICTURE";
214
215 /**
216 * Broadcast Action: A new video is recorded by the camera, and the entry
217 * of the video has been added to the media store.
218 * {@link android.content.Intent#getData} is URI of the video.
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800219 *
220 * <p>In {@link android.os.Build.VERSION_CODES#N Android N} this broadcast was removed, and
221 * applications are recommended to use
222 * {@link android.app.job.JobInfo.Builder JobInfo.Builder}.{@link android.app.job.JobInfo.Builder#addTriggerContentUri}
223 * instead.</p>
224 *
225 * <p>In {@link android.os.Build.VERSION_CODES#O Android O} this broadcast has been brought
226 * back, but only for <em>registered</em> receivers. Apps that are actively running can
Dianne Hackbornc1541952017-04-27 10:55:49 -0700227 * again listen to the broadcast if they want an immediate clear signal about a video
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800228 * being taken, however anything doing heavy work (or needing to be launched) as a result of
229 * this should still use JobScheduler.</p>
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800230 */
231 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
232 public static final String ACTION_NEW_VIDEO = "android.hardware.action.NEW_VIDEO";
233
234 /**
Zhijun He4c913802014-06-16 16:42:35 -0700235 * Camera HAL device API version 1.0
236 * @hide
237 */
238 public static final int CAMERA_HAL_API_VERSION_1_0 = 0x100;
239
240 /**
241 * A constant meaning the normal camera connect/open will be used.
Zhijun He4c913802014-06-16 16:42:35 -0700242 */
Igor Murashkina1d66272014-06-20 11:22:11 -0700243 private static final int CAMERA_HAL_API_VERSION_NORMAL_CONNECT = -2;
Zhijun He4c913802014-06-16 16:42:35 -0700244
245 /**
246 * Used to indicate HAL version un-specified.
247 */
248 private static final int CAMERA_HAL_API_VERSION_UNSPECIFIED = -1;
Igor Murashkina1d66272014-06-20 11:22:11 -0700249
Zhijun He4c913802014-06-16 16:42:35 -0700250 /**
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800251 * Hardware face detection. It does not use much CPU.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800252 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800253 private static final int CAMERA_FACE_DETECTION_HW = 0;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800254
255 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800256 * Software face detection. It uses some CPU.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800257 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800258 private static final int CAMERA_FACE_DETECTION_SW = 1;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800259
260 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700261 * Returns the number of physical cameras available on this device.
Yin-Chia Yeh564ae822018-02-08 16:24:27 -0800262 * The return value of this method might change dynamically if the device
263 * supports external cameras and an external camera is connected or
264 * disconnected.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700265 *
266 * @return total number of accessible camera devices, or 0 if there are no
267 * cameras or an error was encountered enumerating them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 */
Chih-Chung Change25cc652010-05-06 16:36:58 +0800269 public native static int getNumberOfCameras();
270
271 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700272 * Returns the information about a particular camera.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800273 * If {@link #getNumberOfCameras()} returns N, the valid id is 0 to N-1.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700274 *
275 * @throws RuntimeException if an invalid ID is provided, or if there is an
276 * error retrieving the information (generally due to a hardware or other
277 * low-level failure).
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800278 */
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -0700279 public static void getCameraInfo(int cameraId, CameraInfo cameraInfo) {
280 _getCameraInfo(cameraId, cameraInfo);
281 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
282 IAudioService audioService = IAudioService.Stub.asInterface(b);
283 try {
284 if (audioService.isCameraSoundForced()) {
285 // Only set this when sound is forced; otherwise let native code
286 // decide.
287 cameraInfo.canDisableShutterSound = false;
288 }
289 } catch (RemoteException e) {
290 Log.e(TAG, "Audio service is unavailable for queries");
291 }
292 }
293 private native static void _getCameraInfo(int cameraId, CameraInfo cameraInfo);
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800294
295 /**
296 * Information about a camera
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700297 *
298 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
299 * applications.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800300 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700301 @Deprecated
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800302 public static class CameraInfo {
Wu-cheng Li78366602010-09-15 14:08:15 -0700303 /**
304 * The facing of the camera is opposite to that of the screen.
305 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800306 public static final int CAMERA_FACING_BACK = 0;
Wu-cheng Li78366602010-09-15 14:08:15 -0700307
308 /**
309 * The facing of the camera is the same as that of the screen.
310 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800311 public static final int CAMERA_FACING_FRONT = 1;
312
313 /**
Joe Fernandez464cb212011-10-04 16:56:47 -0700314 * The direction that the camera faces. It should be
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800315 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
316 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700317 public int facing;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800318
319 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700320 * <p>The orientation of the camera image. The value is the angle that the
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800321 * camera image needs to be rotated clockwise so it shows correctly on
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700322 * the display in its natural orientation. It should be 0, 90, 180, or 270.</p>
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800323 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700324 * <p>For example, suppose a device has a naturally tall screen. The
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +0800325 * back-facing camera sensor is mounted in landscape. You are looking at
326 * the screen. If the top side of the camera sensor is aligned with the
327 * right edge of the screen in natural orientation, the value should be
328 * 90. If the top side of a front-facing camera sensor is aligned with
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700329 * the right of the screen, the value should be 270.</p>
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800330 *
331 * @see #setDisplayOrientation(int)
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800332 * @see Parameters#setRotation(int)
333 * @see Parameters#setPreviewSize(int, int)
334 * @see Parameters#setPictureSize(int, int)
335 * @see Parameters#setJpegThumbnailSize(int, int)
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800336 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700337 public int orientation;
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -0700338
339 /**
340 * <p>Whether the shutter sound can be disabled.</p>
341 *
342 * <p>On some devices, the camera shutter sound cannot be turned off
343 * through {@link #enableShutterSound enableShutterSound}. This field
344 * can be used to determine whether a call to disable the shutter sound
345 * will succeed.</p>
346 *
347 * <p>If this field is set to true, then a call of
348 * {@code enableShutterSound(false)} will be successful. If set to
349 * false, then that call will fail, and the shutter sound will be played
350 * when {@link Camera#takePicture takePicture} is called.</p>
351 */
352 public boolean canDisableShutterSound;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800353 };
354
355 /**
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800356 * Creates a new Camera object to access a particular hardware camera. If
357 * the same camera is opened by other applications, this will throw a
358 * RuntimeException.
359 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700360 * <p>You must call {@link #release()} when you are done using the camera,
361 * otherwise it will remain locked and be unavailable to other applications.
362 *
Dan Egnor341ff132010-07-20 11:30:17 -0700363 * <p>Your application should only have one Camera object active at a time
364 * for a particular hardware camera.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700365 *
366 * <p>Callbacks from other methods are delivered to the event loop of the
367 * thread which called open(). If this thread has no event loop, then
368 * callbacks are delivered to the main application event loop. If there
369 * is no main application event loop, callbacks are not delivered.
370 *
371 * <p class="caution"><b>Caution:</b> On some devices, this method may
372 * take a long time to complete. It is best to call this method from a
373 * worker thread (possibly using {@link android.os.AsyncTask}) to avoid
374 * blocking the main application UI thread.
375 *
Dan Egnor341ff132010-07-20 11:30:17 -0700376 * @param cameraId the hardware camera to access, between 0 and
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800377 * {@link #getNumberOfCameras()}-1.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700378 * @return a new Camera object, connected, locked and ready for use.
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800379 * @throws RuntimeException if opening the camera fails (for example, if the
380 * camera is in use by another process or device policy manager has
381 * disabled the camera).
Wu-cheng Lifacc8ce2011-06-17 13:09:40 +0800382 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800383 */
384 public static Camera open(int cameraId) {
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800385 return new Camera(cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 }
387
Chih-Chung Change25cc652010-05-06 16:36:58 +0800388 /**
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800389 * Creates a new Camera object to access the first back-facing camera on the
390 * device. If the device does not have a back-facing camera, this returns
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700391 * null. Otherwise acts like the {@link #open(int)} call.
392 *
393 * @return a new Camera object for the first back-facing camera, or null if there is no
394 * backfacing camera
Wu-cheng Li78366602010-09-15 14:08:15 -0700395 * @see #open(int)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800396 */
397 public static Camera open() {
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800398 int numberOfCameras = getNumberOfCameras();
399 CameraInfo cameraInfo = new CameraInfo();
400 for (int i = 0; i < numberOfCameras; i++) {
401 getCameraInfo(i, cameraInfo);
402 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800403 return new Camera(i);
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800404 }
405 }
406 return null;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800407 }
408
Zhijun He4c913802014-06-16 16:42:35 -0700409 /**
410 * Creates a new Camera object to access a particular hardware camera with
411 * given hal API version. If the same camera is opened by other applications
412 * or the hal API version is not supported by this device, this will throw a
413 * RuntimeException.
414 * <p>
415 * You must call {@link #release()} when you are done using the camera,
416 * otherwise it will remain locked and be unavailable to other applications.
417 * <p>
418 * Your application should only have one Camera object active at a time for
419 * a particular hardware camera.
420 * <p>
421 * Callbacks from other methods are delivered to the event loop of the
422 * thread which called open(). If this thread has no event loop, then
423 * callbacks are delivered to the main application event loop. If there is
424 * no main application event loop, callbacks are not delivered.
425 * <p class="caution">
426 * <b>Caution:</b> On some devices, this method may take a long time to
427 * complete. It is best to call this method from a worker thread (possibly
428 * using {@link android.os.AsyncTask}) to avoid blocking the main
429 * application UI thread.
430 *
431 * @param cameraId The hardware camera to access, between 0 and
432 * {@link #getNumberOfCameras()}-1.
Igor Murashkina1d66272014-06-20 11:22:11 -0700433 * @param halVersion The HAL API version this camera device to be opened as.
Zhijun He4c913802014-06-16 16:42:35 -0700434 * @return a new Camera object, connected, locked and ready for use.
Igor Murashkina1d66272014-06-20 11:22:11 -0700435 *
436 * @throws IllegalArgumentException if the {@code halVersion} is invalid
437 *
Zhijun He4c913802014-06-16 16:42:35 -0700438 * @throws RuntimeException if opening the camera fails (for example, if the
439 * camera is in use by another process or device policy manager has disabled
440 * the camera).
Igor Murashkina1d66272014-06-20 11:22:11 -0700441 *
Zhijun He4c913802014-06-16 16:42:35 -0700442 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName)
Igor Murashkina1d66272014-06-20 11:22:11 -0700443 * @see #CAMERA_HAL_API_VERSION_1_0
Zhijun He4c913802014-06-16 16:42:35 -0700444 *
445 * @hide
446 */
447 public static Camera openLegacy(int cameraId, int halVersion) {
Igor Murashkina1d66272014-06-20 11:22:11 -0700448 if (halVersion < CAMERA_HAL_API_VERSION_1_0) {
449 throw new IllegalArgumentException("Invalid HAL version " + halVersion);
450 }
451
Zhijun He4c913802014-06-16 16:42:35 -0700452 return new Camera(cameraId, halVersion);
453 }
454
455 /**
456 * Create a legacy camera object.
457 *
458 * @param cameraId The hardware camera to access, between 0 and
459 * {@link #getNumberOfCameras()}-1.
460 * @param halVersion The HAL API version this camera device to be opened as.
461 */
462 private Camera(int cameraId, int halVersion) {
Igor Murashkina1d66272014-06-20 11:22:11 -0700463 int err = cameraInitVersion(cameraId, halVersion);
Zhijun He4c913802014-06-16 16:42:35 -0700464 if (checkInitErrors(err)) {
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200465 if (err == -EACCES) {
466 throw new RuntimeException("Fail to connect to camera service");
467 } else if (err == -ENODEV) {
468 throw new RuntimeException("Camera initialization failed");
469 } else if (err == -ENOSYS) {
470 throw new RuntimeException("Camera initialization failed because some methods"
471 + " are not implemented");
472 } else if (err == -EOPNOTSUPP) {
473 throw new RuntimeException("Camera initialization failed because the hal"
474 + " version is not supported by this device");
475 } else if (err == -EINVAL) {
476 throw new RuntimeException("Camera initialization failed because the input"
477 + " arugments are invalid");
478 } else if (err == -EBUSY) {
479 throw new RuntimeException("Camera initialization failed because the camera"
480 + " device was already opened");
481 } else if (err == -EUSERS) {
482 throw new RuntimeException("Camera initialization failed because the max"
483 + " number of camera devices were already opened");
Zhijun He4c913802014-06-16 16:42:35 -0700484 }
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200485 // Should never hit this.
486 throw new RuntimeException("Unknown camera error");
Zhijun He4c913802014-06-16 16:42:35 -0700487 }
488 }
489
Igor Murashkina1d66272014-06-20 11:22:11 -0700490 private int cameraInitVersion(int cameraId, int halVersion) {
Zhijun He4c913802014-06-16 16:42:35 -0700491 mShutterCallback = null;
492 mRawImageCallback = null;
493 mJpegCallback = null;
494 mPreviewCallback = null;
495 mPostviewCallback = null;
496 mUsingPreviewAllocation = false;
497 mZoomListener = null;
498
499 Looper looper;
500 if ((looper = Looper.myLooper()) != null) {
501 mEventHandler = new EventHandler(this, looper);
502 } else if ((looper = Looper.getMainLooper()) != null) {
503 mEventHandler = new EventHandler(this, looper);
504 } else {
505 mEventHandler = null;
506 }
507
Svetoslavfbf0eca2015-05-01 16:52:41 -0700508 return native_setup(new WeakReference<Camera>(this), cameraId, halVersion,
509 ActivityThread.currentOpPackageName());
Zhijun He4c913802014-06-16 16:42:35 -0700510 }
511
Igor Murashkina1d66272014-06-20 11:22:11 -0700512 private int cameraInitNormal(int cameraId) {
513 return cameraInitVersion(cameraId, CAMERA_HAL_API_VERSION_NORMAL_CONNECT);
514 }
515
516 /**
517 * Connect to the camera service using #connectLegacy
518 *
519 * <p>
520 * This acts the same as normal except that it will return
521 * the detailed error code if open fails instead of
522 * converting everything into {@code NO_INIT}.</p>
523 *
524 * <p>Intended to use by the camera2 shim only, do <i>not</i> use this for other code.</p>
525 *
526 * @return a detailed errno error code, or {@code NO_ERROR} on success
527 *
528 * @hide
529 */
530 public int cameraInitUnspecified(int cameraId) {
531 return cameraInitVersion(cameraId, CAMERA_HAL_API_VERSION_UNSPECIFIED);
532 }
533
534 /** used by Camera#open, Camera#open(int) */
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800535 Camera(int cameraId) {
Igor Murashkina1d66272014-06-20 11:22:11 -0700536 int err = cameraInitNormal(cameraId);
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700537 if (checkInitErrors(err)) {
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200538 if (err == -EACCES) {
539 throw new RuntimeException("Fail to connect to camera service");
540 } else if (err == -ENODEV) {
541 throw new RuntimeException("Camera initialization failed");
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700542 }
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200543 // Should never hit this.
544 throw new RuntimeException("Unknown camera error");
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700545 }
jiabin503e1fd2018-04-19 16:39:44 -0700546 initAppOps();
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700547 }
548
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700549
550 /**
551 * @hide
552 */
553 public static boolean checkInitErrors(int err) {
554 return err != NO_ERROR;
555 }
556
557 /**
558 * @hide
559 */
560 public static Camera openUninitialized() {
561 return new Camera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800563
Wu-cheng Li1c04a332011-11-22 18:21:18 +0800564 /**
565 * An empty Camera for testing purpose.
566 */
567 Camera() {
jiabin503e1fd2018-04-19 16:39:44 -0700568 initAppOps();
569 }
570
571 private void initAppOps() {
572 IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE);
573 mAppOps = IAppOpsService.Stub.asInterface(b);
574 // initialize mHasAppOpsPlayAudio
575 updateAppOpsPlayAudio();
576 // register a callback to monitor whether the OP_PLAY_AUDIO is still allowed
577 mAppOpsCallback = new IAppOpsCallbackWrapper(this);
578 try {
579 mAppOps.startWatchingMode(AppOpsManager.OP_PLAY_AUDIO,
580 ActivityThread.currentPackageName(), mAppOpsCallback);
581 } catch (RemoteException e) {
582 Log.e(TAG, "Error registering appOps callback", e);
583 mHasAppOpsPlayAudio = false;
584 }
585 }
586
587 private void releaseAppOps() {
588 try {
589 if (mAppOps != null) {
590 mAppOps.stopWatchingMode(mAppOpsCallback);
591 }
592 } catch (Exception e) {
593 // nothing to do here, the object is supposed to be released anyway
594 }
Wu-cheng Li1c04a332011-11-22 18:21:18 +0800595 }
596
Igor Murashkina1d66272014-06-20 11:22:11 -0700597 @Override
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800598 protected void finalize() {
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -0800599 release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800601
Zhijun He4c913802014-06-16 16:42:35 -0700602 private native final int native_setup(Object camera_this, int cameraId, int halVersion,
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800603 String packageName);
604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 private native final void native_release();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607
608 /**
609 * Disconnects and releases the Camera object resources.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700610 *
611 * <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 -0800612 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800613 public final void release() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 native_release();
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800615 mFaceDetectionRunning = false;
jiabin503e1fd2018-04-19 16:39:44 -0700616 releaseAppOps();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
618
619 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700620 * Unlocks the camera to allow another process to access it.
621 * Normally, the camera is locked to the process with an active Camera
622 * object until {@link #release()} is called. To allow rapid handoff
623 * between processes, you can call this method to release the camera
624 * temporarily for another process to use; once the other process is done
625 * you can call {@link #reconnect()} to reclaim the camera.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700627 * <p>This must be done before calling
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800628 * {@link android.media.MediaRecorder#setCamera(Camera)}. This cannot be
629 * called after recording starts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700631 * <p>If you are not recording video, you probably do not need this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700633 * @throws RuntimeException if the camera cannot be unlocked.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 */
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800635 public native final void unlock();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700638 * Re-locks the camera to prevent other processes from accessing it.
639 * Camera objects are locked by default unless {@link #unlock()} is
640 * called. Normally {@link #reconnect()} is used instead.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800641 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800642 * <p>Since API level 14, camera is automatically locked for applications in
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800643 * {@link android.media.MediaRecorder#start()}. Applications can use the
644 * camera (ex: zoom) after recording starts. There is no need to call this
645 * after recording starts or stops.
646 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700647 * <p>If you are not recording video, you probably do not need this method.
648 *
649 * @throws RuntimeException if the camera cannot be re-locked (for
650 * example, if the camera is still in use by another process).
651 */
652 public native final void lock();
653
654 /**
655 * Reconnects to the camera service after another process used it.
656 * After {@link #unlock()} is called, another process may use the
657 * camera; when the process is done, you must reconnect to the camera,
658 * which will re-acquire the lock and allow you to continue using the
659 * camera.
660 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800661 * <p>Since API level 14, camera is automatically locked for applications in
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800662 * {@link android.media.MediaRecorder#start()}. Applications can use the
663 * camera (ex: zoom) after recording starts. There is no need to call this
664 * after recording starts or stops.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700665 *
666 * <p>If you are not recording video, you probably do not need this method.
667 *
668 * @throws IOException if a connection cannot be re-established (for
669 * example, if the camera is still in use by another process).
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700670 * @throws RuntimeException if release() has been called on this Camera
671 * instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700672 */
673 public native final void reconnect() throws IOException;
674
675 /**
676 * Sets the {@link Surface} to be used for live preview.
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800677 * Either a surface or surface texture is necessary for preview, and
678 * preview is necessary to take pictures. The same surface can be re-set
679 * without harm. Setting a preview surface will un-set any preview surface
680 * texture that was set via {@link #setPreviewTexture}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700681 *
682 * <p>The {@link SurfaceHolder} must already contain a surface when this
683 * method is called. If you are using {@link android.view.SurfaceView},
684 * you will need to register a {@link SurfaceHolder.Callback} with
685 * {@link SurfaceHolder#addCallback(SurfaceHolder.Callback)} and wait for
686 * {@link SurfaceHolder.Callback#surfaceCreated(SurfaceHolder)} before
687 * calling setPreviewDisplay() or starting preview.
688 *
689 * <p>This method must be called before {@link #startPreview()}. The
690 * one exception is that if the preview surface is not set (or set to null)
691 * before startPreview() is called, then this method may be called once
692 * with a non-null parameter to set the preview surface. (This allows
693 * camera setup and surface creation to happen in parallel, saving time.)
694 * The preview surface may not otherwise change while preview is running.
695 *
696 * @param holder containing the Surface on which to place the preview,
697 * or null to remove the preview surface
698 * @throws IOException if the method fails (for example, if the surface
699 * is unavailable or unsuitable).
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700700 * @throws RuntimeException if release() has been called on this Camera
701 * instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 */
703 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException {
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800704 if (holder != null) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700705 setPreviewSurface(holder.getSurface());
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800706 } else {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700707 setPreviewSurface((Surface)null);
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700711 /**
712 * @hide
713 */
714 public native final void setPreviewSurface(Surface surface) throws IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715
716 /**
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800717 * Sets the {@link SurfaceTexture} to be used for live preview.
718 * Either a surface or surface texture is necessary for preview, and
719 * preview is necessary to take pictures. The same surface texture can be
720 * re-set without harm. Setting a preview surface texture will un-set any
721 * preview surface that was set via {@link #setPreviewDisplay}.
722 *
723 * <p>This method must be called before {@link #startPreview()}. The
724 * one exception is that if the preview surface texture is not set (or set
725 * to null) before startPreview() is called, then this method may be called
726 * once with a non-null parameter to set the preview surface. (This allows
727 * camera setup and surface creation to happen in parallel, saving time.)
728 * The preview surface texture may not otherwise change while preview is
729 * running.
730 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700731 * <p>The timestamps provided by {@link SurfaceTexture#getTimestamp()} for a
Eino-Ville Talvalae309a0f2011-03-21 11:04:34 -0700732 * SurfaceTexture set as the preview texture have an unspecified zero point,
733 * and cannot be directly compared between different cameras or different
734 * instances of the same camera, or across multiple runs of the same
735 * program.
736 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800737 * <p>If you are using the preview data to create video or still images,
738 * strongly consider using {@link android.media.MediaActionSound} to
739 * properly indicate image capture or recording start/stop to the user.</p>
740 *
741 * @see android.media.MediaActionSound
742 * @see android.graphics.SurfaceTexture
743 * @see android.view.TextureView
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800744 * @param surfaceTexture the {@link SurfaceTexture} to which the preview
745 * images are to be sent or null to remove the current preview surface
746 * texture
747 * @throws IOException if the method fails (for example, if the surface
748 * texture is unavailable or unsuitable).
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700749 * @throws RuntimeException if release() has been called on this Camera
750 * instance.
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800751 */
Glenn Kastendbc289d2011-02-09 10:15:44 -0800752 public native final void setPreviewTexture(SurfaceTexture surfaceTexture) throws IOException;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800753
754 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700755 * Callback interface used to deliver copies of preview frames as
756 * they are displayed.
757 *
758 * @see #setPreviewCallback(Camera.PreviewCallback)
759 * @see #setOneShotPreviewCallback(Camera.PreviewCallback)
760 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
761 * @see #startPreview()
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700762 *
763 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
764 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700766 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 public interface PreviewCallback
768 {
769 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700770 * Called as preview frames are displayed. This callback is invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700771 * on the event thread {@link #open(int)} was called from.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700773 * <p>If using the {@link android.graphics.ImageFormat#YV12} format,
774 * refer to the equations in {@link Camera.Parameters#setPreviewFormat}
775 * for the arrangement of the pixel data in the preview callback
776 * buffers.
777 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700778 * @param data the contents of the preview frame in the format defined
Mathias Agopiana696f5d2010-02-17 17:53:09 -0800779 * by {@link android.graphics.ImageFormat}, which can be queried
Scott Maindf4578e2009-09-10 12:22:07 -0700780 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
Scott Mainda0a56d2009-09-10 18:08:37 -0700781 * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800782 * is never called, the default will be the YCbCr_420_SP
783 * (NV21) format.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700784 * @param camera the Camera service object.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 */
786 void onPreviewFrame(byte[] data, Camera camera);
787 };
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700790 * Starts capturing and drawing preview frames to the screen.
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800791 * Preview will not actually start until a surface is supplied
792 * with {@link #setPreviewDisplay(SurfaceHolder)} or
793 * {@link #setPreviewTexture(SurfaceTexture)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700794 *
795 * <p>If {@link #setPreviewCallback(Camera.PreviewCallback)},
796 * {@link #setOneShotPreviewCallback(Camera.PreviewCallback)}, or
797 * {@link #setPreviewCallbackWithBuffer(Camera.PreviewCallback)} were
798 * called, {@link Camera.PreviewCallback#onPreviewFrame(byte[], Camera)}
799 * will be called when preview data becomes available.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700800 *
801 * @throws RuntimeException if starting preview fails; usually this would be
802 * because of a hardware or other low-level error, or because release()
803 * has been called on this Camera instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 */
805 public native final void startPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700808 * Stops capturing and drawing preview frames to the surface, and
809 * resets the camera for a future call to {@link #startPreview()}.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700810 *
811 * @throws RuntimeException if stopping preview fails; usually this would be
812 * because of a hardware or other low-level error, or because release()
813 * has been called on this Camera instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 */
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800815 public final void stopPreview() {
816 _stopPreview();
817 mFaceDetectionRunning = false;
Chih-yu Huang664d72e2011-09-23 18:59:38 +0800818
819 mShutterCallback = null;
820 mRawImageCallback = null;
821 mPostviewCallback = null;
822 mJpegCallback = null;
Wu-cheng Lif05c1d62012-05-02 23:29:47 +0800823 synchronized (mAutoFocusCallbackLock) {
824 mAutoFocusCallback = null;
825 }
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800826 mAutoFocusMoveCallback = null;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800827 }
828
829 private native final void _stopPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 /**
832 * Return current preview state.
833 *
834 * FIXME: Unhide before release
835 * @hide
836 */
837 public native final boolean previewEnabled();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800840 * <p>Installs a callback to be invoked for every preview frame in addition
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700841 * to displaying them on the screen. The callback will be repeatedly called
842 * for as long as preview is active. This method can be called at any time,
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800843 * even while preview is live. Any other preview callbacks are
844 * overridden.</p>
845 *
846 * <p>If you are using the preview data to create video or still images,
847 * strongly consider using {@link android.media.MediaActionSound} to
848 * properly indicate image capture or recording start/stop to the user.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700850 * @param cb a callback object that receives a copy of each preview frame,
851 * or null to stop receiving callbacks.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700852 * @throws RuntimeException if release() has been called on this Camera
853 * instance.
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800854 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 */
856 public final void setPreviewCallback(PreviewCallback cb) {
857 mPreviewCallback = cb;
858 mOneShot = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400859 mWithBuffer = false;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700860 if (cb != null) {
861 mUsingPreviewAllocation = false;
862 }
Dave Sparksa6118c62009-10-13 02:28:54 -0700863 // Always use one-shot mode. We fake camera preview mode by
864 // doing one-shot preview continuously.
Andrew Harp94927df2009-10-20 01:47:05 -0400865 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 }
867
868 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800869 * <p>Installs a callback to be invoked for the next preview frame in
870 * addition to displaying it on the screen. After one invocation, the
871 * callback is cleared. This method can be called any time, even when
872 * preview is live. Any other preview callbacks are overridden.</p>
873 *
874 * <p>If you are using the preview data to create video or still images,
875 * strongly consider using {@link android.media.MediaActionSound} to
876 * properly indicate image capture or recording start/stop to the user.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700878 * @param cb a callback object that receives a copy of the next preview frame,
879 * or null to stop receiving callbacks.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700880 * @throws RuntimeException if release() has been called on this Camera
881 * instance.
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800882 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 */
884 public final void setOneShotPreviewCallback(PreviewCallback cb) {
Andrew Harp94927df2009-10-20 01:47:05 -0400885 mPreviewCallback = cb;
886 mOneShot = true;
887 mWithBuffer = false;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700888 if (cb != null) {
889 mUsingPreviewAllocation = false;
890 }
Andrew Harp94927df2009-10-20 01:47:05 -0400891 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 }
893
Andrew Harp94927df2009-10-20 01:47:05 -0400894 private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer);
895
896 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800897 * <p>Installs a callback to be invoked for every preview frame, using
898 * buffers supplied with {@link #addCallbackBuffer(byte[])}, in addition to
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700899 * displaying them on the screen. The callback will be repeatedly called
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800900 * for as long as preview is active and buffers are available. Any other
901 * preview callbacks are overridden.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400902 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700903 * <p>The purpose of this method is to improve preview efficiency and frame
904 * rate by allowing preview frame memory reuse. You must call
905 * {@link #addCallbackBuffer(byte[])} at some point -- before or after
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800906 * calling this method -- or no callbacks will received.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400907 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800908 * <p>The buffer queue will be cleared if this method is called with a null
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700909 * callback, {@link #setPreviewCallback(Camera.PreviewCallback)} is called,
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800910 * or {@link #setOneShotPreviewCallback(Camera.PreviewCallback)} is
911 * called.</p>
912 *
913 * <p>If you are using the preview data to create video or still images,
914 * strongly consider using {@link android.media.MediaActionSound} to
915 * properly indicate image capture or recording start/stop to the user.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400916 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700917 * @param cb a callback object that receives a copy of the preview frame,
918 * or null to stop receiving callbacks and clear the buffer queue.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700919 * @throws RuntimeException if release() has been called on this Camera
920 * instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700921 * @see #addCallbackBuffer(byte[])
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800922 * @see android.media.MediaActionSound
Andrew Harp94927df2009-10-20 01:47:05 -0400923 */
924 public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
925 mPreviewCallback = cb;
926 mOneShot = false;
927 mWithBuffer = true;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700928 if (cb != null) {
929 mUsingPreviewAllocation = false;
930 }
Andrew Harp94927df2009-10-20 01:47:05 -0400931 setHasPreviewCallback(cb != null, true);
932 }
933
934 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800935 * Adds a pre-allocated buffer to the preview callback buffer queue.
936 * Applications can add one or more buffers to the queue. When a preview
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700937 * frame arrives and there is still at least one available buffer, the
938 * buffer will be used and removed from the queue. Then preview callback is
939 * invoked with the buffer. If a frame arrives and there is no buffer left,
940 * the frame is discarded. Applications should add buffers back when they
941 * finish processing the data in them.
Wu-cheng Lic10275a2010-03-09 13:49:21 -0800942 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700943 * <p>For formats besides YV12, the size of the buffer is determined by
944 * multiplying the preview image width, height, and bytes per pixel. The
945 * width and height can be read from
946 * {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel can be
947 * computed from {@link android.graphics.ImageFormat#getBitsPerPixel(int)} /
948 * 8, using the image format from
949 * {@link Camera.Parameters#getPreviewFormat()}.
950 *
951 * <p>If using the {@link android.graphics.ImageFormat#YV12} format, the
952 * size can be calculated using the equations listed in
953 * {@link Camera.Parameters#setPreviewFormat}.
Andrew Harp94927df2009-10-20 01:47:05 -0400954 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700955 * <p>This method is only necessary when
James Donge00cab72011-02-17 16:38:06 -0800956 * {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700957 * {@link #setPreviewCallback(PreviewCallback)} or
958 * {@link #setOneShotPreviewCallback(PreviewCallback)} are used, buffers
James Donge00cab72011-02-17 16:38:06 -0800959 * are automatically allocated. When a supplied buffer is too small to
960 * hold the preview frame data, preview callback will return null and
961 * the buffer will be removed from the buffer queue.
Andrew Harp94927df2009-10-20 01:47:05 -0400962 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700963 * @param callbackBuffer the buffer to add to the queue. The size of the
964 * buffer must match the values described above.
Wu-cheng Li5b9bcda2010-03-07 14:59:28 -0800965 * @see #setPreviewCallbackWithBuffer(PreviewCallback)
Andrew Harp94927df2009-10-20 01:47:05 -0400966 */
James Donge00cab72011-02-17 16:38:06 -0800967 public final void addCallbackBuffer(byte[] callbackBuffer)
968 {
969 _addCallbackBuffer(callbackBuffer, CAMERA_MSG_PREVIEW_FRAME);
970 }
971
972 /**
973 * Adds a pre-allocated buffer to the raw image callback buffer queue.
974 * Applications can add one or more buffers to the queue. When a raw image
975 * frame arrives and there is still at least one available buffer, the
976 * buffer will be used to hold the raw image data and removed from the
977 * queue. Then raw image callback is invoked with the buffer. If a raw
978 * image frame arrives but there is no buffer left, the frame is
979 * discarded. Applications should add buffers back when they finish
980 * processing the data in them by calling this method again in order
981 * to avoid running out of raw image callback buffers.
982 *
983 * <p>The size of the buffer is determined by multiplying the raw image
984 * width, height, and bytes per pixel. The width and height can be
985 * read from {@link Camera.Parameters#getPictureSize()}. Bytes per pixel
986 * can be computed from
987 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
988 * using the image format from {@link Camera.Parameters#getPreviewFormat()}.
989 *
990 * <p>This method is only necessary when the PictureCallbck for raw image
991 * is used while calling {@link #takePicture(Camera.ShutterCallback,
992 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
993 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700994 * <p>Please note that by calling this method, the mode for
995 * application-managed callback buffers is triggered. If this method has
996 * never been called, null will be returned by the raw image callback since
997 * there is no image callback buffer available. Furthermore, When a supplied
998 * buffer is too small to hold the raw image data, raw image callback will
999 * return null and the buffer will be removed from the buffer queue.
James Donge00cab72011-02-17 16:38:06 -08001000 *
1001 * @param callbackBuffer the buffer to add to the raw image callback buffer
1002 * queue. The size should be width * height * (bits per pixel) / 8. An
1003 * null callbackBuffer will be ignored and won't be added to the queue.
1004 *
1005 * @see #takePicture(Camera.ShutterCallback,
1006 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
1007 *
1008 * {@hide}
1009 */
1010 public final void addRawImageCallbackBuffer(byte[] callbackBuffer)
1011 {
1012 addCallbackBuffer(callbackBuffer, CAMERA_MSG_RAW_IMAGE);
1013 }
1014
1015 private final void addCallbackBuffer(byte[] callbackBuffer, int msgType)
1016 {
1017 // CAMERA_MSG_VIDEO_FRAME may be allowed in the future.
1018 if (msgType != CAMERA_MSG_PREVIEW_FRAME &&
1019 msgType != CAMERA_MSG_RAW_IMAGE) {
1020 throw new IllegalArgumentException(
1021 "Unsupported message type: " + msgType);
1022 }
1023
1024 _addCallbackBuffer(callbackBuffer, msgType);
1025 }
1026
1027 private native final void _addCallbackBuffer(
1028 byte[] callbackBuffer, int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029
Eino-Ville Talvala7005b672013-04-02 15:46:38 -07001030 /**
1031 * <p>Create a {@link android.renderscript RenderScript}
1032 * {@link android.renderscript.Allocation Allocation} to use as a
1033 * destination of preview callback frames. Use
1034 * {@link #setPreviewCallbackAllocation setPreviewCallbackAllocation} to use
1035 * the created Allocation as a destination for camera preview frames.</p>
1036 *
1037 * <p>The Allocation will be created with a YUV type, and its contents must
1038 * be accessed within Renderscript with the {@code rsGetElementAtYuv_*}
1039 * accessor methods. Its size will be based on the current
1040 * {@link Parameters#getPreviewSize preview size} configured for this
1041 * camera.</p>
1042 *
1043 * @param rs the RenderScript context for this Allocation.
1044 * @param usage additional usage flags to set for the Allocation. The usage
1045 * flag {@link android.renderscript.Allocation#USAGE_IO_INPUT} will always
1046 * be set on the created Allocation, but additional flags may be provided
1047 * here.
1048 * @return a new YUV-type Allocation with dimensions equal to the current
1049 * preview size.
1050 * @throws RSIllegalArgumentException if the usage flags are not compatible
1051 * with an YUV Allocation.
1052 * @see #setPreviewCallbackAllocation
1053 * @hide
1054 */
1055 public final Allocation createPreviewAllocation(RenderScript rs, int usage)
1056 throws RSIllegalArgumentException {
1057 Parameters p = getParameters();
1058 Size previewSize = p.getPreviewSize();
1059 Type.Builder yuvBuilder = new Type.Builder(rs,
1060 Element.createPixel(rs,
1061 Element.DataType.UNSIGNED_8,
1062 Element.DataKind.PIXEL_YUV));
1063 // Use YV12 for wide compatibility. Changing this requires also
1064 // adjusting camera service's format selection.
1065 yuvBuilder.setYuvFormat(ImageFormat.YV12);
1066 yuvBuilder.setX(previewSize.width);
1067 yuvBuilder.setY(previewSize.height);
1068
1069 Allocation a = Allocation.createTyped(rs, yuvBuilder.create(),
1070 usage | Allocation.USAGE_IO_INPUT);
1071
1072 return a;
1073 }
1074
1075 /**
1076 * <p>Set an {@link android.renderscript.Allocation Allocation} as the
1077 * target of preview callback data. Use this method for efficient processing
1078 * of camera preview data with RenderScript. The Allocation must be created
1079 * with the {@link #createPreviewAllocation createPreviewAllocation }
1080 * method.</p>
1081 *
1082 * <p>Setting a preview allocation will disable any active preview callbacks
1083 * set by {@link #setPreviewCallback setPreviewCallback} or
1084 * {@link #setPreviewCallbackWithBuffer setPreviewCallbackWithBuffer}, and
1085 * vice versa. Using a preview allocation still requires an active standard
1086 * preview target to be set, either with
1087 * {@link #setPreviewTexture setPreviewTexture} or
1088 * {@link #setPreviewDisplay setPreviewDisplay}.</p>
1089 *
1090 * <p>To be notified when new frames are available to the Allocation, use
1091 * {@link android.renderscript.Allocation#setIoInputNotificationHandler Allocation.setIoInputNotificationHandler}. To
1092 * update the frame currently accessible from the Allocation to the latest
1093 * preview frame, call
1094 * {@link android.renderscript.Allocation#ioReceive Allocation.ioReceive}.</p>
1095 *
1096 * <p>To disable preview into the Allocation, call this method with a
1097 * {@code null} parameter.</p>
1098 *
1099 * <p>Once a preview allocation is set, the preview size set by
1100 * {@link Parameters#setPreviewSize setPreviewSize} cannot be changed. If
1101 * you wish to change the preview size, first remove the preview allocation
1102 * by calling {@code setPreviewCallbackAllocation(null)}, then change the
1103 * preview size, create a new preview Allocation with
1104 * {@link #createPreviewAllocation createPreviewAllocation}, and set it as
1105 * the new preview callback allocation target.</p>
1106 *
1107 * <p>If you are using the preview data to create video or still images,
1108 * strongly consider using {@link android.media.MediaActionSound} to
1109 * properly indicate image capture or recording start/stop to the user.</p>
1110 *
1111 * @param previewAllocation the allocation to use as destination for preview
1112 * @throws IOException if configuring the camera to use the Allocation for
1113 * preview fails.
1114 * @throws IllegalArgumentException if the Allocation's dimensions or other
1115 * parameters don't meet the requirements.
1116 * @see #createPreviewAllocation
1117 * @see #setPreviewCallback
1118 * @see #setPreviewCallbackWithBuffer
1119 * @hide
1120 */
1121 public final void setPreviewCallbackAllocation(Allocation previewAllocation)
1122 throws IOException {
1123 Surface previewSurface = null;
1124 if (previewAllocation != null) {
1125 Parameters p = getParameters();
1126 Size previewSize = p.getPreviewSize();
1127 if (previewSize.width != previewAllocation.getType().getX() ||
1128 previewSize.height != previewAllocation.getType().getY()) {
1129 throw new IllegalArgumentException(
1130 "Allocation dimensions don't match preview dimensions: " +
1131 "Allocation is " +
1132 previewAllocation.getType().getX() +
1133 ", " +
1134 previewAllocation.getType().getY() +
1135 ". Preview is " + previewSize.width + ", " +
1136 previewSize.height);
1137 }
1138 if ((previewAllocation.getUsage() &
1139 Allocation.USAGE_IO_INPUT) == 0) {
1140 throw new IllegalArgumentException(
1141 "Allocation usage does not include USAGE_IO_INPUT");
1142 }
1143 if (previewAllocation.getType().getElement().getDataKind() !=
1144 Element.DataKind.PIXEL_YUV) {
1145 throw new IllegalArgumentException(
1146 "Allocation is not of a YUV type");
1147 }
1148 previewSurface = previewAllocation.getSurface();
1149 mUsingPreviewAllocation = true;
1150 } else {
1151 mUsingPreviewAllocation = false;
1152 }
1153 setPreviewCallbackSurface(previewSurface);
1154 }
1155
1156 private native final void setPreviewCallbackSurface(Surface s);
1157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 private class EventHandler extends Handler
1159 {
Igor Murashkina1d66272014-06-20 11:22:11 -07001160 private final Camera mCamera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161
1162 public EventHandler(Camera c, Looper looper) {
1163 super(looper);
1164 mCamera = c;
1165 }
1166
1167 @Override
1168 public void handleMessage(Message msg) {
1169 switch(msg.what) {
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001170 case CAMERA_MSG_SHUTTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 if (mShutterCallback != null) {
1172 mShutterCallback.onShutter();
1173 }
1174 return;
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001175
1176 case CAMERA_MSG_RAW_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -07001177 if (mRawImageCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 return;
1181
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001182 case CAMERA_MSG_COMPRESSED_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -07001183 if (mJpegCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001187
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001188 case CAMERA_MSG_PREVIEW_FRAME:
Eino-Ville Talvalaca367b72012-05-30 16:01:33 -07001189 PreviewCallback pCb = mPreviewCallback;
1190 if (pCb != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 if (mOneShot) {
Dave Sparksa6118c62009-10-13 02:28:54 -07001192 // Clear the callback variable before the callback
1193 // in case the app calls setPreviewCallback from
1194 // the callback function
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 mPreviewCallback = null;
Andrew Harp94927df2009-10-20 01:47:05 -04001196 } else if (!mWithBuffer) {
Dave Sparksa6118c62009-10-13 02:28:54 -07001197 // We're faking the camera preview mode to prevent
1198 // the app from being flooded with preview frames.
1199 // Set to oneshot mode again.
Andrew Harp94927df2009-10-20 01:47:05 -04001200 setHasPreviewCallback(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
Eino-Ville Talvalaca367b72012-05-30 16:01:33 -07001202 pCb.onPreviewFrame((byte[])msg.obj, mCamera);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
1204 return;
1205
Dave Sparkse8b26e12009-07-14 10:35:40 -07001206 case CAMERA_MSG_POSTVIEW_FRAME:
1207 if (mPostviewCallback != null) {
1208 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera);
1209 }
1210 return;
1211
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001212 case CAMERA_MSG_FOCUS:
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001213 AutoFocusCallback cb = null;
1214 synchronized (mAutoFocusCallbackLock) {
1215 cb = mAutoFocusCallback;
1216 }
1217 if (cb != null) {
1218 boolean success = msg.arg1 == 0 ? false : true;
1219 cb.onAutoFocus(success, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001220 }
1221 return;
1222
1223 case CAMERA_MSG_ZOOM:
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001224 if (mZoomListener != null) {
1225 mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 return;
1228
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001229 case CAMERA_MSG_PREVIEW_METADATA:
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001230 if (mFaceListener != null) {
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001231 mFaceListener.onFaceDetection((Face[])msg.obj, mCamera);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001232 }
1233 return;
1234
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001235 case CAMERA_MSG_ERROR :
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 Log.e(TAG, "Error " + msg.arg1);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001237 if (mErrorCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 mErrorCallback.onError(msg.arg1, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 return;
1241
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001242 case CAMERA_MSG_FOCUS_MOVE:
1243 if (mAutoFocusMoveCallback != null) {
1244 mAutoFocusMoveCallback.onAutoFocusMoving(msg.arg1 == 0 ? false : true, mCamera);
1245 }
1246 return;
1247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 default:
1249 Log.e(TAG, "Unknown message type " + msg.what);
1250 return;
1251 }
1252 }
1253 }
1254
1255 private static void postEventFromNative(Object camera_ref,
1256 int what, int arg1, int arg2, Object obj)
1257 {
1258 Camera c = (Camera)((WeakReference)camera_ref).get();
1259 if (c == null)
1260 return;
1261
1262 if (c.mEventHandler != null) {
1263 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj);
1264 c.mEventHandler.sendMessage(m);
1265 }
1266 }
1267
1268 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001269 * Callback interface used to notify on completion of camera auto focus.
1270 *
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001271 * <p>Devices that do not support auto-focus will receive a "fake"
1272 * callback to this interface. If your application needs auto-focus and
Scott Maindf4578e2009-09-10 12:22:07 -07001273 * should not be installed on devices <em>without</em> auto-focus, you must
1274 * declare that your app uses the
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001275 * {@code android.hardware.camera.autofocus} feature, in the
Scott Maindf4578e2009-09-10 12:22:07 -07001276 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
1277 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001278 *
1279 * @see #autoFocus(AutoFocusCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001280 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1281 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001283 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 public interface AutoFocusCallback
1285 {
1286 /**
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001287 * Called when the camera auto focus completes. If the camera
1288 * does not support auto-focus and autoFocus is called,
1289 * onAutoFocus will be called immediately with a fake value of
1290 * <code>success</code> set to <code>true</code>.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001291 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08001292 * The auto-focus routine does not lock auto-exposure and auto-white
1293 * balance after it completes.
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001294 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 * @param success true if focus was successful, false if otherwise
1296 * @param camera the Camera service object
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001297 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
1298 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 */
1300 void onAutoFocus(boolean success, Camera camera);
Wu-cheng Li53b30912011-10-12 19:43:51 +08001301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302
1303 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001304 * Starts camera auto-focus and registers a callback function to run when
1305 * the camera is focused. This method is only valid when preview is active
1306 * (between {@link #startPreview()} and before {@link #stopPreview()}).
1307 *
1308 * <p>Callers should check
1309 * {@link android.hardware.Camera.Parameters#getFocusMode()} to determine if
1310 * this method should be called. If the camera does not support auto-focus,
1311 * it is a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)}
Wu-cheng Li36322db2009-09-18 18:59:21 +08001312 * callback will be called immediately.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001313 *
Scott Mainda0a56d2009-09-10 18:08:37 -07001314 * <p>If your application should not be installed
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001315 * on devices without auto-focus, you must declare that your application
1316 * uses auto-focus with the
Scott Maindf4578e2009-09-10 12:22:07 -07001317 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
1318 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001319 *
Wu-cheng Li068ef422009-09-27 13:19:36 -07001320 * <p>If the current flash mode is not
1321 * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001322 * fired during auto-focus, depending on the driver and camera hardware.<p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001323 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001324 * <p>Auto-exposure lock {@link android.hardware.Camera.Parameters#getAutoExposureLock()}
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08001325 * and auto-white balance locks {@link android.hardware.Camera.Parameters#getAutoWhiteBalanceLock()}
1326 * do not change during and after autofocus. But auto-focus routine may stop
1327 * auto-exposure and auto-white balance transiently during focusing.
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001328 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001329 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
1330 * image capture with {@link #takePicture(Camera.ShutterCallback,
1331 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
1332 * the focus position. Applications must call cancelAutoFocus to reset the
1333 * focus.</p>
1334 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -08001335 * <p>If autofocus is successful, consider using
1336 * {@link android.media.MediaActionSound} to properly play back an autofocus
1337 * success sound to the user.</p>
1338 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 * @param cb the callback to run
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001340 * @throws RuntimeException if starting autofocus fails; usually this would
1341 * be because of a hardware or other low-level error, or because
1342 * release() has been called on this Camera instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001343 * @see #cancelAutoFocus()
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001344 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
1345 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
Eino-Ville Talvala108708b2012-03-05 11:00:43 -08001346 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 */
1348 public final void autoFocus(AutoFocusCallback cb)
1349 {
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001350 synchronized (mAutoFocusCallbackLock) {
James Dong248ba232012-04-28 21:30:46 -07001351 mAutoFocusCallback = cb;
James Dong248ba232012-04-28 21:30:46 -07001352 }
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001353 native_autoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
1355 private native final void native_autoFocus();
1356
1357 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001358 * Cancels any auto-focus function in progress.
1359 * Whether or not auto-focus is currently in progress,
1360 * this function will return the focus position to the default.
Chih-Chung Chang244f8c22009-09-15 14:51:56 +08001361 * If the camera does not support auto-focus, this is a no-op.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001362 *
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001363 * @throws RuntimeException if canceling autofocus fails; usually this would
1364 * be because of a hardware or other low-level error, or because
1365 * release() has been called on this Camera instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001366 * @see #autoFocus(Camera.AutoFocusCallback)
Chih-Chung Chang244f8c22009-09-15 14:51:56 +08001367 */
1368 public final void cancelAutoFocus()
1369 {
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001370 synchronized (mAutoFocusCallbackLock) {
James Dong248ba232012-04-28 21:30:46 -07001371 mAutoFocusCallback = null;
James Dong248ba232012-04-28 21:30:46 -07001372 }
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001373 native_cancelAutoFocus();
1374 // CAMERA_MSG_FOCUS should be removed here because the following
1375 // scenario can happen:
1376 // - An application uses the same thread for autoFocus, cancelAutoFocus
1377 // and looper thread.
1378 // - The application calls autoFocus.
1379 // - HAL sends CAMERA_MSG_FOCUS, which enters the looper message queue.
1380 // Before event handler's handleMessage() is invoked, the application
1381 // calls cancelAutoFocus and autoFocus.
1382 // - The application gets the old CAMERA_MSG_FOCUS and thinks autofocus
1383 // has been completed. But in fact it is not.
1384 //
1385 // As documented in the beginning of the file, apps should not use
1386 // multiple threads to call autoFocus and cancelAutoFocus at the same
1387 // time. It is HAL's responsibility not to send a CAMERA_MSG_FOCUS
1388 // message after native_cancelAutoFocus is called.
1389 mEventHandler.removeMessages(CAMERA_MSG_FOCUS);
Chih-Chung Chang244f8c22009-09-15 14:51:56 +08001390 }
1391 private native final void native_cancelAutoFocus();
1392
1393 /**
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001394 * Callback interface used to notify on auto focus start and stop.
1395 *
Wu-cheng Li65745392012-04-12 18:07:16 +08001396 * <p>This is only supported in continuous autofocus modes -- {@link
1397 * Parameters#FOCUS_MODE_CONTINUOUS_VIDEO} and {@link
1398 * Parameters#FOCUS_MODE_CONTINUOUS_PICTURE}. Applications can show
1399 * autofocus animation based on this.</p>
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001400 *
1401 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1402 * applications.
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001403 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001404 @Deprecated
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001405 public interface AutoFocusMoveCallback
1406 {
1407 /**
1408 * Called when the camera auto focus starts or stops.
1409 *
1410 * @param start true if focus starts to move, false if focus stops to move
Wu-cheng Li65745392012-04-12 18:07:16 +08001411 * @param camera the Camera service object
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001412 */
1413 void onAutoFocusMoving(boolean start, Camera camera);
1414 }
1415
1416 /**
1417 * Sets camera auto-focus move callback.
1418 *
1419 * @param cb the callback to run
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001420 * @throws RuntimeException if enabling the focus move callback fails;
1421 * usually this would be because of a hardware or other low-level error,
1422 * or because release() has been called on this Camera instance.
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001423 */
1424 public void setAutoFocusMoveCallback(AutoFocusMoveCallback cb) {
1425 mAutoFocusMoveCallback = cb;
1426 enableFocusMoveCallback((mAutoFocusMoveCallback != null) ? 1 : 0);
1427 }
1428
1429 private native void enableFocusMoveCallback(int enable);
1430
1431 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001432 * Callback interface used to signal the moment of actual image capture.
1433 *
1434 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001435 *
1436 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1437 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001439 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 public interface ShutterCallback
1441 {
1442 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001443 * Called as near as possible to the moment when a photo is captured
1444 * from the sensor. This is a good opportunity to play a shutter sound
1445 * or give other feedback of camera operation. This may be some time
1446 * after the photo was triggered, but some time before the actual data
1447 * is available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 */
1449 void onShutter();
1450 }
1451
1452 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001453 * Callback interface used to supply image data from a photo capture.
1454 *
1455 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001456 *
1457 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1458 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001460 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 public interface PictureCallback {
1462 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001463 * Called when image data is available after a picture is taken.
1464 * The format of the data depends on the context of the callback
1465 * and {@link Camera.Parameters} settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 * @param data a byte array of the picture data
1468 * @param camera the Camera service object
1469 */
1470 void onPictureTaken(byte[] data, Camera camera);
1471 };
1472
1473 /**
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001474 * Equivalent to <pre>takePicture(Shutter, raw, null, jpeg)</pre>.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001475 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001476 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 */
1478 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
1479 PictureCallback jpeg) {
Dave Sparkse8b26e12009-07-14 10:35:40 -07001480 takePicture(shutter, raw, null, jpeg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 }
James Donge00cab72011-02-17 16:38:06 -08001482 private native final void native_takePicture(int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483
Dave Sparkse8b26e12009-07-14 10:35:40 -07001484 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001485 * Triggers an asynchronous image capture. The camera service will initiate
1486 * a series of callbacks to the application as the image capture progresses.
1487 * The shutter callback occurs after the image is captured. This can be used
1488 * to trigger a sound to let the user know that image has been captured. The
1489 * raw callback occurs when the raw image data is available (NOTE: the data
James Donge00cab72011-02-17 16:38:06 -08001490 * will be null if there is no raw image callback buffer available or the
1491 * raw image callback buffer is not large enough to hold the raw image).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001492 * The postview callback occurs when a scaled, fully processed postview
1493 * image is available (NOTE: not all hardware supports this). The jpeg
1494 * callback occurs when the compressed image is available. If the
1495 * application does not need a particular callback, a null can be passed
1496 * instead of a callback method.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001497 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001498 * <p>This method is only valid when preview is active (after
1499 * {@link #startPreview()}). Preview will be stopped after the image is
1500 * taken; callers must call {@link #startPreview()} again if they want to
Wu-cheng Li42419ce2011-06-01 17:22:24 +08001501 * re-start preview or take more pictures. This should not be called between
1502 * {@link android.media.MediaRecorder#start()} and
1503 * {@link android.media.MediaRecorder#stop()}.
Wu-cheng Li40057ce2009-12-02 18:57:29 +08001504 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001505 * <p>After calling this method, you must not call {@link #startPreview()}
1506 * or take another picture until the JPEG callback has returned.
1507 *
1508 * @param shutter the callback for image capture moment, or null
1509 * @param raw the callback for raw (uncompressed) image data, or null
Dave Sparkse8b26e12009-07-14 10:35:40 -07001510 * @param postview callback with postview image data, may be null
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001511 * @param jpeg the callback for JPEG image data, or null
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001512 * @throws RuntimeException if starting picture capture fails; usually this
1513 * would be because of a hardware or other low-level error, or because
1514 * release() has been called on this Camera instance.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001515 */
1516 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
1517 PictureCallback postview, PictureCallback jpeg) {
1518 mShutterCallback = shutter;
1519 mRawImageCallback = raw;
1520 mPostviewCallback = postview;
1521 mJpegCallback = jpeg;
James Donge00cab72011-02-17 16:38:06 -08001522
1523 // If callback is not set, do not send me callbacks.
1524 int msgType = 0;
1525 if (mShutterCallback != null) {
1526 msgType |= CAMERA_MSG_SHUTTER;
1527 }
1528 if (mRawImageCallback != null) {
1529 msgType |= CAMERA_MSG_RAW_IMAGE;
1530 }
1531 if (mPostviewCallback != null) {
1532 msgType |= CAMERA_MSG_POSTVIEW_FRAME;
1533 }
1534 if (mJpegCallback != null) {
1535 msgType |= CAMERA_MSG_COMPRESSED_IMAGE;
1536 }
1537
1538 native_takePicture(msgType);
Wu-cheng Lie9c6c9c2012-05-29 16:47:44 +08001539 mFaceDetectionRunning = false;
Dave Sparkse8b26e12009-07-14 10:35:40 -07001540 }
1541
1542 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001543 * Zooms to the requested value smoothly. The driver will notify {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001544 * OnZoomChangeListener} of the zoom value and whether zoom is stopped at
1545 * the time. For example, suppose the current zoom is 0 and startSmoothZoom
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001546 * is called with value 3. The
1547 * {@link Camera.OnZoomChangeListener#onZoomChange(int, boolean, Camera)}
1548 * method will be called three times with zoom values 1, 2, and 3.
1549 * Applications can call {@link #stopSmoothZoom} to stop the zoom earlier.
1550 * Applications should not call startSmoothZoom again or change the zoom
1551 * value before zoom stops. If the supplied zoom value equals to the current
1552 * zoom value, no zoom callback will be generated. This method is supported
1553 * if {@link android.hardware.Camera.Parameters#isSmoothZoomSupported}
1554 * returns true.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001555 *
1556 * @param value zoom value. The valid range is 0 to {@link
1557 * android.hardware.Camera.Parameters#getMaxZoom}.
Wu-cheng Li0ca25192010-03-29 16:21:12 +08001558 * @throws IllegalArgumentException if the zoom value is invalid.
1559 * @throws RuntimeException if the method fails.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001560 * @see #setZoomChangeListener(OnZoomChangeListener)
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001561 */
1562 public native final void startSmoothZoom(int value);
1563
1564 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001565 * Stops the smooth zoom. Applications should wait for the {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001566 * OnZoomChangeListener} to know when the zoom is actually stopped. This
1567 * method is supported if {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001568 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true.
Wu-cheng Li0ca25192010-03-29 16:21:12 +08001569 *
1570 * @throws RuntimeException if the method fails.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001571 */
1572 public native final void stopSmoothZoom();
1573
1574 /**
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001575 * Set the clockwise rotation of preview display in degrees. This affects
1576 * the preview frames and the picture displayed after snapshot. This method
1577 * is useful for portrait mode applications. Note that preview display of
Wu-cheng Lib982fb42010-10-19 17:19:09 +08001578 * front-facing cameras is flipped horizontally before the rotation, that
1579 * is, the image is reflected along the central vertical axis of the camera
1580 * sensor. So the users can see themselves as looking into a mirror.
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001581 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001582 * <p>This does not affect the order of byte array passed in {@link
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001583 * PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This
1584 * method is not allowed to be called during preview.
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001585 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001586 * <p>If you want to make the camera image show in the same orientation as
1587 * the display, you can use the following code.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001588 * <pre>
Chih-Chung Chang724c5222010-06-14 18:57:15 +08001589 * public static void setCameraDisplayOrientation(Activity activity,
1590 * int cameraId, android.hardware.Camera camera) {
1591 * android.hardware.Camera.CameraInfo info =
1592 * new android.hardware.Camera.CameraInfo();
1593 * android.hardware.Camera.getCameraInfo(cameraId, info);
1594 * int rotation = activity.getWindowManager().getDefaultDisplay()
1595 * .getRotation();
1596 * int degrees = 0;
1597 * switch (rotation) {
1598 * case Surface.ROTATION_0: degrees = 0; break;
1599 * case Surface.ROTATION_90: degrees = 90; break;
1600 * case Surface.ROTATION_180: degrees = 180; break;
1601 * case Surface.ROTATION_270: degrees = 270; break;
1602 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001603 *
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001604 * int result;
1605 * if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
1606 * result = (info.orientation + degrees) % 360;
1607 * result = (360 - result) % 360; // compensate the mirror
1608 * } else { // back-facing
1609 * result = (info.orientation - degrees + 360) % 360;
1610 * }
Chih-Chung Chang724c5222010-06-14 18:57:15 +08001611 * camera.setDisplayOrientation(result);
1612 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001613 * </pre>
Wu-cheng Lid3033622011-10-07 13:13:54 +08001614 *
1615 * <p>Starting from API level 14, this method can be called when preview is
1616 * active.
1617 *
Eino-Ville Talvala6c91e2c2016-03-25 11:54:39 -07001618 * <p><b>Note: </b>Before API level 24, the default value for orientation is 0. Starting in
1619 * API level 24, the default orientation will be such that applications in forced-landscape mode
1620 * will have correct preview orientation, which may be either a default of 0 or
1621 * 180. Applications that operate in portrait mode or allow for changing orientation must still
1622 * call this method after each orientation change to ensure correct preview display in all
1623 * cases.</p>
1624 *
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001625 * @param degrees the angle that the picture will be rotated clockwise.
Eino-Ville Talvala6c91e2c2016-03-25 11:54:39 -07001626 * Valid values are 0, 90, 180, and 270.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001627 * @throws RuntimeException if setting orientation fails; usually this would
1628 * be because of a hardware or other low-level error, or because
1629 * release() has been called on this Camera instance.
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001630 * @see #setPreviewDisplay(SurfaceHolder)
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001631 */
1632 public native final void setDisplayOrientation(int degrees);
1633
1634 /**
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001635 * <p>Enable or disable the default shutter sound when taking a picture.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001636 *
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001637 * <p>By default, the camera plays the system-defined camera shutter sound
1638 * when {@link #takePicture} is called. Using this method, the shutter sound
1639 * can be disabled. It is strongly recommended that an alternative shutter
1640 * sound is played in the {@link ShutterCallback} when the system shutter
1641 * sound is disabled.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001642 *
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001643 * <p>Note that devices may not always allow disabling the camera shutter
1644 * sound. If the shutter sound state cannot be set to the desired value,
1645 * this method will return false. {@link CameraInfo#canDisableShutterSound}
1646 * can be used to determine whether the device will allow the shutter sound
1647 * to be disabled.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001648 *
1649 * @param enabled whether the camera should play the system shutter sound
1650 * when {@link #takePicture takePicture} is called.
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001651 * @return {@code true} if the shutter sound state was successfully
1652 * changed. {@code false} if the shutter sound state could not be
1653 * changed. {@code true} is also returned if shutter sound playback
1654 * is already set to the requested state.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001655 * @throws RuntimeException if the call fails; usually this would be because
1656 * of a hardware or other low-level error, or because release() has been
1657 * called on this Camera instance.
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001658 * @see #takePicture
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001659 * @see CameraInfo#canDisableShutterSound
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001660 * @see ShutterCallback
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001661 */
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001662 public final boolean enableShutterSound(boolean enabled) {
jiabine059e452018-05-07 15:16:44 -07001663 boolean canDisableShutterSound = true;
1664 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
1665 IAudioService audioService = IAudioService.Stub.asInterface(b);
1666 try {
1667 if (audioService.isCameraSoundForced()) {
1668 canDisableShutterSound = false;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001669 }
jiabine059e452018-05-07 15:16:44 -07001670 } catch (RemoteException e) {
1671 Log.e(TAG, "Audio service is unavailable for queries");
1672 }
1673 if (!enabled && !canDisableShutterSound) {
1674 return false;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001675 }
jiabin503e1fd2018-04-19 16:39:44 -07001676 synchronized (mShutterSoundLock) {
jiabine059e452018-05-07 15:16:44 -07001677 mShutterSoundEnabledFromApp = enabled;
1678 // Return the result of _enableShutterSound(enabled) in all cases.
1679 // If the shutter sound can be disabled, disable it when the device is in DnD mode.
jiabin503e1fd2018-04-19 16:39:44 -07001680 boolean ret = _enableShutterSound(enabled);
jiabine059e452018-05-07 15:16:44 -07001681 if (enabled && !mHasAppOpsPlayAudio) {
1682 Log.i(TAG, "Shutter sound is not allowed by AppOpsManager");
1683 if (canDisableShutterSound) {
1684 _enableShutterSound(false);
1685 }
jiabin503e1fd2018-04-19 16:39:44 -07001686 }
1687 return ret;
1688 }
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001689 }
1690
Igor Murashkin66533622014-08-19 14:51:47 -07001691 /**
1692 * Disable the shutter sound unconditionally.
1693 *
1694 * <p>
1695 * This is only guaranteed to work for legacy cameras
1696 * (i.e. initialized with {@link #cameraInitUnspecified}). Trying to call this on
1697 * a regular camera will force a conditional check in the camera service.
1698 * </p>
1699 *
1700 * @return {@code true} if the shutter sound state was successfully
1701 * changed. {@code false} if the shutter sound state could not be
1702 * changed. {@code true} is also returned if shutter sound playback
1703 * is already set to the requested state.
1704 *
1705 * @hide
1706 */
1707 public final boolean disableShutterSound() {
1708 return _enableShutterSound(/*enabled*/false);
1709 }
1710
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001711 private native final boolean _enableShutterSound(boolean enabled);
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001712
jiabin503e1fd2018-04-19 16:39:44 -07001713 private static class IAppOpsCallbackWrapper extends IAppOpsCallback.Stub {
1714 private final WeakReference<Camera> mWeakCamera;
1715
1716 IAppOpsCallbackWrapper(Camera camera) {
1717 mWeakCamera = new WeakReference<Camera>(camera);
1718 }
1719
1720 @Override
1721 public void opChanged(int op, int uid, String packageName) {
1722 if (op == AppOpsManager.OP_PLAY_AUDIO) {
1723 final Camera camera = mWeakCamera.get();
1724 if (camera != null) {
1725 camera.updateAppOpsPlayAudio();
1726 }
1727 }
1728 }
1729 }
1730
1731 private void updateAppOpsPlayAudio() {
1732 synchronized (mShutterSoundLock) {
1733 boolean oldHasAppOpsPlayAudio = mHasAppOpsPlayAudio;
1734 try {
1735 int mode = AppOpsManager.MODE_IGNORED;
1736 if (mAppOps != null) {
1737 mode = mAppOps.checkAudioOperation(AppOpsManager.OP_PLAY_AUDIO,
1738 AudioAttributes.USAGE_ASSISTANCE_SONIFICATION,
1739 Process.myUid(), ActivityThread.currentPackageName());
1740 }
1741 mHasAppOpsPlayAudio = mode == AppOpsManager.MODE_ALLOWED;
1742 } catch (RemoteException e) {
1743 Log.e(TAG, "AppOpsService check audio operation failed");
1744 mHasAppOpsPlayAudio = false;
1745 }
1746 if (oldHasAppOpsPlayAudio != mHasAppOpsPlayAudio) {
1747 if (!mHasAppOpsPlayAudio) {
jiabine059e452018-05-07 15:16:44 -07001748 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
1749 IAudioService audioService = IAudioService.Stub.asInterface(b);
1750 try {
1751 if (audioService.isCameraSoundForced()) {
1752 return;
1753 }
1754 } catch (RemoteException e) {
1755 Log.e(TAG, "Audio service is unavailable for queries");
1756 }
jiabin503e1fd2018-04-19 16:39:44 -07001757 _enableShutterSound(false);
1758 } else {
jiabine059e452018-05-07 15:16:44 -07001759 enableShutterSound(mShutterSoundEnabledFromApp);
jiabin503e1fd2018-04-19 16:39:44 -07001760 }
1761 }
1762 }
1763 }
1764
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001765 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001766 * Callback interface for zoom changes during a smooth zoom operation.
1767 *
1768 * @see #setZoomChangeListener(OnZoomChangeListener)
1769 * @see #startSmoothZoom(int)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001770 *
1771 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1772 * applications.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001773 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001774 @Deprecated
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001775 public interface OnZoomChangeListener
Dave Sparkse8b26e12009-07-14 10:35:40 -07001776 {
1777 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001778 * Called when the zoom value has changed during a smooth zoom.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001779 *
1780 * @param zoomValue the current zoom value. In smooth zoom mode, camera
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001781 * calls this for every new zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001782 * @param stopped whether smooth zoom is stopped. If the value is true,
1783 * this is the last zoom update for the application.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001784 * @param camera the Camera service object
1785 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001786 void onZoomChange(int zoomValue, boolean stopped, Camera camera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001787 };
1788
1789 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001790 * Registers a listener to be notified when the zoom value is updated by the
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001791 * camera driver during smooth zoom.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001792 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001793 * @param listener the listener to notify
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001794 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001795 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001796 public final void setZoomChangeListener(OnZoomChangeListener listener)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001797 {
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001798 mZoomListener = listener;
Dave Sparkse8b26e12009-07-14 10:35:40 -07001799 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001800
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001801 /**
1802 * Callback interface for face detected in the preview frame.
1803 *
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001804 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1805 * applications.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001806 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001807 @Deprecated
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001808 public interface FaceDetectionListener
1809 {
1810 /**
1811 * Notify the listener of the detected faces in the preview frame.
1812 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001813 * @param faces The detected faces in a list
Joe Fernandez464cb212011-10-04 16:56:47 -07001814 * @param camera The {@link Camera} service object
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001815 */
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001816 void onFaceDetection(Face[] faces, Camera camera);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001817 }
1818
1819 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001820 * Registers a listener to be notified about the faces detected in the
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001821 * preview frame.
1822 *
1823 * @param listener the listener to notify
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001824 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001825 */
1826 public final void setFaceDetectionListener(FaceDetectionListener listener)
1827 {
1828 mFaceListener = listener;
1829 }
1830
1831 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001832 * Starts the face detection. This should be called after preview is started.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001833 * The camera will notify {@link FaceDetectionListener} of the detected
1834 * faces in the preview frame. The detected faces may be the same as the
1835 * previous ones. Applications should call {@link #stopFaceDetection} to
1836 * stop the face detection. This method is supported if {@link
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001837 * Parameters#getMaxNumDetectedFaces()} returns a number larger than 0.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001838 * If the face detection has started, apps should not call this again.
1839 *
Wu-cheng Li8c136702011-08-12 20:25:00 +08001840 * <p>When the face detection is running, {@link Parameters#setWhiteBalance(String)},
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001841 * {@link Parameters#setFocusAreas(List)}, and {@link Parameters#setMeteringAreas(List)}
Wu-cheng Li8c136702011-08-12 20:25:00 +08001842 * have no effect. The camera uses the detected faces to do auto-white balance,
1843 * auto exposure, and autofocus.
1844 *
1845 * <p>If the apps call {@link #autoFocus(AutoFocusCallback)}, the camera
1846 * will stop sending face callbacks. The last face callback indicates the
1847 * areas used to do autofocus. After focus completes, face detection will
1848 * resume sending face callbacks. If the apps call {@link
1849 * #cancelAutoFocus()}, the face callbacks will also resume.</p>
1850 *
1851 * <p>After calling {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
1852 * Camera.PictureCallback)} or {@link #stopPreview()}, and then resuming
1853 * preview with {@link #startPreview()}, the apps should call this method
1854 * again to resume face detection.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001855 *
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001856 * @throws IllegalArgumentException if the face detection is unsupported.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001857 * @throws RuntimeException if the method fails or the face detection is
1858 * already running.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001859 * @see FaceDetectionListener
1860 * @see #stopFaceDetection()
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001861 * @see Parameters#getMaxNumDetectedFaces()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001862 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001863 public final void startFaceDetection() {
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001864 if (mFaceDetectionRunning) {
1865 throw new RuntimeException("Face detection is already running");
1866 }
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001867 _startFaceDetection(CAMERA_FACE_DETECTION_HW);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001868 mFaceDetectionRunning = true;
1869 }
1870
1871 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001872 * Stops the face detection.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001873 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001874 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001875 */
1876 public final void stopFaceDetection() {
1877 _stopFaceDetection();
1878 mFaceDetectionRunning = false;
1879 }
1880
1881 private native final void _startFaceDetection(int type);
1882 private native final void _stopFaceDetection();
1883
1884 /**
Joe Fernandez464cb212011-10-04 16:56:47 -07001885 * Information about a face identified through camera face detection.
Wu-cheng Li53b30912011-10-12 19:43:51 +08001886 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001887 * <p>When face detection is used with a camera, the {@link FaceDetectionListener} returns a
1888 * list of face objects for use in focusing and metering.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001889 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001890 * @see FaceDetectionListener
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001891 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1892 * applications.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001893 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001894 @Deprecated
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001895 public static class Face {
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001896 /**
1897 * Create an empty face.
1898 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001899 public Face() {
1900 }
1901
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001902 /**
1903 * Bounds of the face. (-1000, -1000) represents the top-left of the
1904 * camera field of view, and (1000, 1000) represents the bottom-right of
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001905 * the field of view. For example, suppose the size of the viewfinder UI
1906 * is 800x480. The rect passed from the driver is (-1000, -1000, 0, 0).
Wu-cheng Li8c136702011-08-12 20:25:00 +08001907 * The corresponding viewfinder rect should be (0, 0, 400, 240). It is
1908 * guaranteed left < right and top < bottom. The coordinates can be
1909 * smaller than -1000 or bigger than 1000. But at least one vertex will
1910 * be within (-1000, -1000) and (1000, 1000).
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001911 *
1912 * <p>The direction is relative to the sensor orientation, that is, what
1913 * the sensor sees. The direction is not affected by the rotation or
Wu-cheng Li8c136702011-08-12 20:25:00 +08001914 * mirroring of {@link #setDisplayOrientation(int)}. The face bounding
1915 * rectangle does not provide any information about face orientation.</p>
1916 *
1917 * <p>Here is the matrix to convert driver coordinates to View coordinates
1918 * in pixels.</p>
1919 * <pre>
1920 * Matrix matrix = new Matrix();
1921 * CameraInfo info = CameraHolder.instance().getCameraInfo()[cameraId];
1922 * // Need mirror for front camera.
1923 * boolean mirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
1924 * matrix.setScale(mirror ? -1 : 1, 1);
1925 * // This is the value for android.hardware.Camera.setDisplayOrientation.
1926 * matrix.postRotate(displayOrientation);
1927 * // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
1928 * // UI coordinates range from (0, 0) to (width, height).
1929 * matrix.postScale(view.getWidth() / 2000f, view.getHeight() / 2000f);
1930 * matrix.postTranslate(view.getWidth() / 2f, view.getHeight() / 2f);
1931 * </pre>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001932 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001933 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001934 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001935 public Rect rect;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001936
1937 /**
Eino-Ville Talvala8df3b2b2012-09-15 14:30:12 -07001938 * <p>The confidence level for the detection of the face. The range is 1 to
1939 * 100. 100 is the highest confidence.</p>
1940 *
1941 * <p>Depending on the device, even very low-confidence faces may be
1942 * listed, so applications should filter out faces with low confidence,
1943 * depending on the use case. For a typical point-and-shoot camera
1944 * application that wishes to display rectangles around detected faces,
1945 * filtering out faces with confidence less than 50 is recommended.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001946 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001947 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001948 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001949 public int score;
Wei Huad52b3082011-08-19 13:01:51 -07001950
1951 /**
1952 * An unique id per face while the face is visible to the tracker. If
1953 * the face leaves the field-of-view and comes back, it will get a new
1954 * id. This is an optional field, may not be supported on all devices.
1955 * If not supported, id will always be set to -1. The optional fields
1956 * are supported as a set. Either they are all valid, or none of them
1957 * are.
1958 */
1959 public int id = -1;
1960
1961 /**
1962 * The coordinates of the center of the left eye. The coordinates are in
1963 * the same space as the ones for {@link #rect}. This is an optional
1964 * field, may not be supported on all devices. If not supported, the
1965 * value will always be set to null. The optional fields are supported
1966 * as a set. Either they are all valid, or none of them are.
1967 */
1968 public Point leftEye = null;
1969
1970 /**
1971 * The coordinates of the center of the right eye. The coordinates are
1972 * in the same space as the ones for {@link #rect}.This is an optional
1973 * field, may not be supported on all devices. If not supported, the
1974 * value will always be set to null. The optional fields are supported
1975 * as a set. Either they are all valid, or none of them are.
1976 */
1977 public Point rightEye = null;
1978
1979 /**
1980 * The coordinates of the center of the mouth. The coordinates are in
1981 * the same space as the ones for {@link #rect}. This is an optional
1982 * field, may not be supported on all devices. If not supported, the
1983 * value will always be set to null. The optional fields are supported
1984 * as a set. Either they are all valid, or none of them are.
1985 */
1986 public Point mouth = null;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001987 }
1988
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001989 /**
1990 * Unspecified camera error.
1991 * @see Camera.ErrorCallback
1992 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 public static final int CAMERA_ERROR_UNKNOWN = 1;
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001994
1995 /**
Ruben Brunke91b9252015-04-14 13:38:06 -07001996 * Camera was disconnected due to use by higher priority user.
1997 * @see Camera.ErrorCallback
1998 */
1999 public static final int CAMERA_ERROR_EVICTED = 2;
2000
2001 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002002 * Media server died. In this case, the application must release the
2003 * Camera object and instantiate a new one.
2004 * @see Camera.ErrorCallback
2005 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 public static final int CAMERA_ERROR_SERVER_DIED = 100;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002009 * Callback interface for camera error notification.
2010 *
2011 * @see #setErrorCallback(ErrorCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002012 *
2013 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2014 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002016 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 public interface ErrorCallback
2018 {
2019 /**
2020 * Callback for camera errors.
2021 * @param error error code:
2022 * <ul>
2023 * <li>{@link #CAMERA_ERROR_UNKNOWN}
2024 * <li>{@link #CAMERA_ERROR_SERVER_DIED}
2025 * </ul>
2026 * @param camera the Camera service object
2027 */
2028 void onError(int error, Camera camera);
2029 };
2030
2031 /**
2032 * Registers a callback to be invoked when an error occurs.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002033 * @param cb The callback to run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 */
2035 public final void setErrorCallback(ErrorCallback cb)
2036 {
2037 mErrorCallback = cb;
2038 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 private native final void native_setParameters(String params);
2041 private native final String native_getParameters();
2042
2043 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002044 * Changes the settings for this Camera service.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002045 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 * @param params the Parameters to use for this Camera service
Wu-cheng Li99a3f3e2010-11-19 15:56:16 +08002047 * @throws RuntimeException if any parameter is invalid or not supported.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002048 * @see #getParameters()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 */
2050 public void setParameters(Parameters params) {
Eino-Ville Talvala7005b672013-04-02 15:46:38 -07002051 // If using preview allocations, don't allow preview size changes
2052 if (mUsingPreviewAllocation) {
2053 Size newPreviewSize = params.getPreviewSize();
2054 Size currentPreviewSize = getParameters().getPreviewSize();
2055 if (newPreviewSize.width != currentPreviewSize.width ||
2056 newPreviewSize.height != currentPreviewSize.height) {
2057 throw new IllegalStateException("Cannot change preview size" +
2058 " while a preview allocation is configured.");
2059 }
2060 }
2061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 native_setParameters(params.flatten());
2063 }
2064
2065 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002066 * Returns the current settings for this Camera service.
2067 * If modifications are made to the returned Parameters, they must be passed
2068 * to {@link #setParameters(Camera.Parameters)} to take effect.
2069 *
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07002070 * @throws RuntimeException if reading parameters fails; usually this would
2071 * be because of a hardware or other low-level error, or because
2072 * release() has been called on this Camera instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002073 * @see #setParameters(Camera.Parameters)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 */
2075 public Parameters getParameters() {
2076 Parameters p = new Parameters();
2077 String s = native_getParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 p.unflatten(s);
2079 return p;
2080 }
2081
2082 /**
Wu-cheng Li1c04a332011-11-22 18:21:18 +08002083 * Returns an empty {@link Parameters} for testing purpose.
2084 *
Ken Wakasaf76a50c2012-03-09 19:56:35 +09002085 * @return a Parameter object.
Wu-cheng Li1c04a332011-11-22 18:21:18 +08002086 *
2087 * @hide
2088 */
2089 public static Parameters getEmptyParameters() {
2090 Camera camera = new Camera();
2091 return camera.new Parameters();
2092 }
2093
2094 /**
Igor Murashkindf6242e2014-07-01 18:06:13 -07002095 * Returns a copied {@link Parameters}; for shim use only.
2096 *
2097 * @param parameters a non-{@code null} parameters
2098 * @return a Parameter object, with all the parameters copied from {@code parameters}.
2099 *
2100 * @throws NullPointerException if {@code parameters} was {@code null}
2101 * @hide
2102 */
2103 public static Parameters getParametersCopy(Camera.Parameters parameters) {
2104 if (parameters == null) {
2105 throw new NullPointerException("parameters must not be null");
2106 }
2107
2108 Camera camera = parameters.getOuter();
2109 Parameters p = camera.new Parameters();
2110 p.copyFrom(parameters);
2111
2112 return p;
2113 }
2114
2115 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002116 * Image size (width and height dimensions).
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002117 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2118 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002120 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 public class Size {
2122 /**
2123 * Sets the dimensions for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002124 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 * @param w the photo width (pixels)
2126 * @param h the photo height (pixels)
2127 */
2128 public Size(int w, int h) {
2129 width = w;
2130 height = h;
2131 }
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002132 /**
2133 * Compares {@code obj} to this size.
2134 *
2135 * @param obj the object to compare this size with.
2136 * @return {@code true} if the width and height of {@code obj} is the
2137 * same as those of this size. {@code false} otherwise.
2138 */
2139 @Override
2140 public boolean equals(Object obj) {
2141 if (!(obj instanceof Size)) {
2142 return false;
2143 }
2144 Size s = (Size) obj;
2145 return width == s.width && height == s.height;
2146 }
2147 @Override
2148 public int hashCode() {
2149 return width * 32713 + height;
2150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 /** width of the picture */
2152 public int width;
2153 /** height of the picture */
2154 public int height;
2155 };
2156
2157 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002158 * <p>The Area class is used for choosing specific metering and focus areas for
2159 * the camera to use when calculating auto-exposure, auto-white balance, and
2160 * auto-focus.</p>
2161 *
2162 * <p>To find out how many simultaneous areas a given camera supports, use
2163 * {@link Parameters#getMaxNumMeteringAreas()} and
2164 * {@link Parameters#getMaxNumFocusAreas()}. If metering or focusing area
2165 * selection is unsupported, these methods will return 0.</p>
2166 *
2167 * <p>Each Area consists of a rectangle specifying its bounds, and a weight
2168 * that determines its importance. The bounds are relative to the camera's
2169 * current field of view. The coordinates are mapped so that (-1000, -1000)
2170 * is always the top-left corner of the current field of view, and (1000,
2171 * 1000) is always the bottom-right corner of the current field of
2172 * view. Setting Areas with bounds outside that range is not allowed. Areas
2173 * with zero or negative width or height are not allowed.</p>
2174 *
2175 * <p>The weight must range from 1 to 1000, and represents a weight for
2176 * every pixel in the area. This means that a large metering area with
2177 * the same weight as a smaller area will have more effect in the
2178 * metering result. Metering areas can overlap and the driver
2179 * will add the weights in the overlap region.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08002180 *
Wu-cheng Libde61a52011-06-07 18:23:14 +08002181 * @see Parameters#setFocusAreas(List)
2182 * @see Parameters#getFocusAreas()
2183 * @see Parameters#getMaxNumFocusAreas()
2184 * @see Parameters#setMeteringAreas(List)
2185 * @see Parameters#getMeteringAreas()
2186 * @see Parameters#getMaxNumMeteringAreas()
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002187 *
2188 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2189 * applications.
Wu-cheng Li30771b72011-04-02 06:19:46 +08002190 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002191 @Deprecated
Wu-cheng Li30771b72011-04-02 06:19:46 +08002192 public static class Area {
2193 /**
2194 * Create an area with specified rectangle and weight.
2195 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002196 * @param rect the bounds of the area.
2197 * @param weight the weight of the area.
Wu-cheng Li30771b72011-04-02 06:19:46 +08002198 */
2199 public Area(Rect rect, int weight) {
2200 this.rect = rect;
2201 this.weight = weight;
2202 }
2203 /**
2204 * Compares {@code obj} to this area.
2205 *
2206 * @param obj the object to compare this area with.
2207 * @return {@code true} if the rectangle and weight of {@code obj} is
2208 * the same as those of this area. {@code false} otherwise.
2209 */
2210 @Override
2211 public boolean equals(Object obj) {
2212 if (!(obj instanceof Area)) {
2213 return false;
2214 }
2215 Area a = (Area) obj;
2216 if (rect == null) {
2217 if (a.rect != null) return false;
2218 } else {
2219 if (!rect.equals(a.rect)) return false;
2220 }
2221 return weight == a.weight;
2222 }
2223
Wu-cheng Libde61a52011-06-07 18:23:14 +08002224 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002225 * Bounds of the area. (-1000, -1000) represents the top-left of the
2226 * camera field of view, and (1000, 1000) represents the bottom-right of
2227 * the field of view. Setting bounds outside that range is not
2228 * allowed. Bounds with zero or negative width or height are not
2229 * allowed.
Wu-cheng Libde61a52011-06-07 18:23:14 +08002230 *
2231 * @see Parameters#getFocusAreas()
2232 * @see Parameters#getMeteringAreas()
2233 */
Wu-cheng Li30771b72011-04-02 06:19:46 +08002234 public Rect rect;
2235
Wu-cheng Libde61a52011-06-07 18:23:14 +08002236 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002237 * Weight of the area. The weight must range from 1 to 1000, and
2238 * represents a weight for every pixel in the area. This means that a
2239 * large metering area with the same weight as a smaller area will have
2240 * more effect in the metering result. Metering areas can overlap and
2241 * the driver will add the weights in the overlap region.
Wu-cheng Libde61a52011-06-07 18:23:14 +08002242 *
2243 * @see Parameters#getFocusAreas()
2244 * @see Parameters#getMeteringAreas()
2245 */
Wu-cheng Li30771b72011-04-02 06:19:46 +08002246 public int weight;
Wu-cheng Libde61a52011-06-07 18:23:14 +08002247 }
Wu-cheng Li30771b72011-04-02 06:19:46 +08002248
2249 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002250 * Camera service settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002251 *
2252 * <p>To make camera parameters take effect, applications have to call
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002253 * {@link Camera#setParameters(Camera.Parameters)}. For example, after
2254 * {@link Camera.Parameters#setWhiteBalance} is called, white balance is not
2255 * actually changed until {@link Camera#setParameters(Camera.Parameters)}
2256 * is called with the changed parameters object.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002257 *
2258 * <p>Different devices may have different camera capabilities, such as
2259 * picture size or flash modes. The application should query the camera
2260 * capabilities before setting parameters. For example, the application
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002261 * should call {@link Camera.Parameters#getSupportedColorEffects()} before
2262 * calling {@link Camera.Parameters#setColorEffect(String)}. If the
2263 * camera does not support color effects,
2264 * {@link Camera.Parameters#getSupportedColorEffects()} will return null.
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002265 *
2266 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2267 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002269 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 public class Parameters {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002271 // Parameter keys to communicate with the camera driver.
2272 private static final String KEY_PREVIEW_SIZE = "preview-size";
2273 private static final String KEY_PREVIEW_FORMAT = "preview-format";
2274 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate";
Wu-cheng Li454630f2010-08-11 16:48:05 -07002275 private static final String KEY_PREVIEW_FPS_RANGE = "preview-fps-range";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002276 private static final String KEY_PICTURE_SIZE = "picture-size";
2277 private static final String KEY_PICTURE_FORMAT = "picture-format";
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002278 private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002279 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width";
2280 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height";
2281 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality";
2282 private static final String KEY_JPEG_QUALITY = "jpeg-quality";
2283 private static final String KEY_ROTATION = "rotation";
2284 private static final String KEY_GPS_LATITUDE = "gps-latitude";
2285 private static final String KEY_GPS_LONGITUDE = "gps-longitude";
2286 private static final String KEY_GPS_ALTITUDE = "gps-altitude";
2287 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp";
Ray Chen055c9862010-02-23 10:45:42 +08002288 private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002289 private static final String KEY_WHITE_BALANCE = "whitebalance";
2290 private static final String KEY_EFFECT = "effect";
2291 private static final String KEY_ANTIBANDING = "antibanding";
2292 private static final String KEY_SCENE_MODE = "scene-mode";
2293 private static final String KEY_FLASH_MODE = "flash-mode";
Wu-cheng Li36322db2009-09-18 18:59:21 +08002294 private static final String KEY_FOCUS_MODE = "focus-mode";
Wu-cheng Li30771b72011-04-02 06:19:46 +08002295 private static final String KEY_FOCUS_AREAS = "focus-areas";
2296 private static final String KEY_MAX_NUM_FOCUS_AREAS = "max-num-focus-areas";
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08002297 private static final String KEY_FOCAL_LENGTH = "focal-length";
2298 private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle";
2299 private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle";
Wu-cheng Liff723b62010-02-09 13:38:19 +08002300 private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation";
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002301 private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation";
2302 private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation";
2303 private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step";
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002304 private static final String KEY_AUTO_EXPOSURE_LOCK = "auto-exposure-lock";
2305 private static final String KEY_AUTO_EXPOSURE_LOCK_SUPPORTED = "auto-exposure-lock-supported";
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002306 private static final String KEY_AUTO_WHITEBALANCE_LOCK = "auto-whitebalance-lock";
2307 private static final String KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED = "auto-whitebalance-lock-supported";
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002308 private static final String KEY_METERING_AREAS = "metering-areas";
2309 private static final String KEY_MAX_NUM_METERING_AREAS = "max-num-metering-areas";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002310 private static final String KEY_ZOOM = "zoom";
2311 private static final String KEY_MAX_ZOOM = "max-zoom";
2312 private static final String KEY_ZOOM_RATIOS = "zoom-ratios";
2313 private static final String KEY_ZOOM_SUPPORTED = "zoom-supported";
2314 private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002315 private static final String KEY_FOCUS_DISTANCES = "focus-distances";
James Dongdd0b16c2010-09-21 16:23:48 -07002316 private static final String KEY_VIDEO_SIZE = "video-size";
2317 private static final String KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO =
2318 "preferred-preview-size-for-video";
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08002319 private static final String KEY_MAX_NUM_DETECTED_FACES_HW = "max-num-detected-faces-hw";
2320 private static final String KEY_MAX_NUM_DETECTED_FACES_SW = "max-num-detected-faces-sw";
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08002321 private static final String KEY_RECORDING_HINT = "recording-hint";
Wu-cheng Li98bb2512011-08-30 21:33:10 +08002322 private static final String KEY_VIDEO_SNAPSHOT_SUPPORTED = "video-snapshot-supported";
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07002323 private static final String KEY_VIDEO_STABILIZATION = "video-stabilization";
2324 private static final String KEY_VIDEO_STABILIZATION_SUPPORTED = "video-stabilization-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002325
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002326 // Parameter key suffix for supported values.
2327 private static final String SUPPORTED_VALUES_SUFFIX = "-values";
2328
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002329 private static final String TRUE = "true";
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002330 private static final String FALSE = "false";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002331
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002332 // Values for white balance settings.
2333 public static final String WHITE_BALANCE_AUTO = "auto";
2334 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent";
2335 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent";
2336 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent";
2337 public static final String WHITE_BALANCE_DAYLIGHT = "daylight";
2338 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight";
2339 public static final String WHITE_BALANCE_TWILIGHT = "twilight";
2340 public static final String WHITE_BALANCE_SHADE = "shade";
2341
2342 // Values for color effect settings.
2343 public static final String EFFECT_NONE = "none";
2344 public static final String EFFECT_MONO = "mono";
2345 public static final String EFFECT_NEGATIVE = "negative";
2346 public static final String EFFECT_SOLARIZE = "solarize";
2347 public static final String EFFECT_SEPIA = "sepia";
2348 public static final String EFFECT_POSTERIZE = "posterize";
2349 public static final String EFFECT_WHITEBOARD = "whiteboard";
2350 public static final String EFFECT_BLACKBOARD = "blackboard";
2351 public static final String EFFECT_AQUA = "aqua";
2352
2353 // Values for antibanding settings.
2354 public static final String ANTIBANDING_AUTO = "auto";
2355 public static final String ANTIBANDING_50HZ = "50hz";
2356 public static final String ANTIBANDING_60HZ = "60hz";
2357 public static final String ANTIBANDING_OFF = "off";
2358
2359 // Values for flash mode settings.
2360 /**
2361 * Flash will not be fired.
2362 */
2363 public static final String FLASH_MODE_OFF = "off";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002364
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002365 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07002366 * Flash will be fired automatically when required. The flash may be fired
2367 * during preview, auto-focus, or snapshot depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002368 */
2369 public static final String FLASH_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002370
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002371 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07002372 * Flash will always be fired during snapshot. The flash may also be
2373 * fired during preview or auto-focus depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002374 */
2375 public static final String FLASH_MODE_ON = "on";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002376
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002377 /**
2378 * Flash will be fired in red-eye reduction mode.
2379 */
2380 public static final String FLASH_MODE_RED_EYE = "red-eye";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002381
Wu-cheng Li36322db2009-09-18 18:59:21 +08002382 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07002383 * Constant emission of light during preview, auto-focus and snapshot.
2384 * This can also be used for video recording.
Wu-cheng Li36322db2009-09-18 18:59:21 +08002385 */
Wu-cheng Li068ef422009-09-27 13:19:36 -07002386 public static final String FLASH_MODE_TORCH = "torch";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002387
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002388 /**
2389 * Scene mode is off.
2390 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002391 public static final String SCENE_MODE_AUTO = "auto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002392
2393 /**
2394 * Take photos of fast moving objects. Same as {@link
2395 * #SCENE_MODE_SPORTS}.
2396 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002397 public static final String SCENE_MODE_ACTION = "action";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002398
2399 /**
2400 * Take people pictures.
2401 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002402 public static final String SCENE_MODE_PORTRAIT = "portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002403
2404 /**
2405 * Take pictures on distant objects.
2406 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002407 public static final String SCENE_MODE_LANDSCAPE = "landscape";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002408
2409 /**
2410 * Take photos at night.
2411 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002412 public static final String SCENE_MODE_NIGHT = "night";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002413
2414 /**
2415 * Take people pictures at night.
2416 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002417 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002418
2419 /**
2420 * Take photos in a theater. Flash light is off.
2421 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002422 public static final String SCENE_MODE_THEATRE = "theatre";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002423
2424 /**
2425 * Take pictures on the beach.
2426 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002427 public static final String SCENE_MODE_BEACH = "beach";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002428
2429 /**
2430 * Take pictures on the snow.
2431 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002432 public static final String SCENE_MODE_SNOW = "snow";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002433
2434 /**
2435 * Take sunset photos.
2436 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002437 public static final String SCENE_MODE_SUNSET = "sunset";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002438
2439 /**
2440 * Avoid blurry pictures (for example, due to hand shake).
2441 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002442 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002443
2444 /**
2445 * For shooting firework displays.
2446 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002447 public static final String SCENE_MODE_FIREWORKS = "fireworks";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002448
2449 /**
2450 * Take photos of fast moving objects. Same as {@link
2451 * #SCENE_MODE_ACTION}.
2452 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002453 public static final String SCENE_MODE_SPORTS = "sports";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002454
2455 /**
2456 * Take indoor low-light shot.
2457 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002458 public static final String SCENE_MODE_PARTY = "party";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002459
2460 /**
2461 * Capture the naturally warm color of scenes lit by candles.
2462 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002463 public static final String SCENE_MODE_CANDLELIGHT = "candlelight";
2464
Wu-cheng Lic58b4232010-03-29 17:21:28 +08002465 /**
2466 * Applications are looking for a barcode. Camera driver will be
2467 * optimized for barcode reading.
2468 */
2469 public static final String SCENE_MODE_BARCODE = "barcode";
2470
Wu-cheng Li36322db2009-09-18 18:59:21 +08002471 /**
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07002472 * Capture a scene using high dynamic range imaging techniques. The
2473 * camera will return an image that has an extended dynamic range
2474 * compared to a regular capture. Capturing such an image may take
2475 * longer than a regular capture.
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07002476 */
2477 public static final String SCENE_MODE_HDR = "hdr";
2478
2479 /**
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07002480 * Auto-focus mode. Applications should call {@link
2481 * #autoFocus(AutoFocusCallback)} to start the focus in this mode.
Wu-cheng Li36322db2009-09-18 18:59:21 +08002482 */
2483 public static final String FOCUS_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002484
Wu-cheng Li36322db2009-09-18 18:59:21 +08002485 /**
2486 * Focus is set at infinity. Applications should not call
2487 * {@link #autoFocus(AutoFocusCallback)} in this mode.
2488 */
2489 public static final String FOCUS_MODE_INFINITY = "infinity";
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07002490
2491 /**
2492 * Macro (close-up) focus mode. Applications should call
2493 * {@link #autoFocus(AutoFocusCallback)} to start the focus in this
2494 * mode.
2495 */
Wu-cheng Li36322db2009-09-18 18:59:21 +08002496 public static final String FOCUS_MODE_MACRO = "macro";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002497
Wu-cheng Li36322db2009-09-18 18:59:21 +08002498 /**
2499 * Focus is fixed. The camera is always in this mode if the focus is not
2500 * adjustable. If the camera has auto-focus, this mode can fix the
2501 * focus, which is usually at hyperfocal distance. Applications should
2502 * not call {@link #autoFocus(AutoFocusCallback)} in this mode.
2503 */
2504 public static final String FOCUS_MODE_FIXED = "fixed";
2505
Wu-cheng Lic58b4232010-03-29 17:21:28 +08002506 /**
2507 * Extended depth of field (EDOF). Focusing is done digitally and
2508 * continuously. Applications should not call {@link
2509 * #autoFocus(AutoFocusCallback)} in this mode.
2510 */
2511 public static final String FOCUS_MODE_EDOF = "edof";
2512
Wu-cheng Li699fe932010-08-05 11:50:25 -07002513 /**
Wu-cheng Lid45cb722010-09-20 16:15:32 -07002514 * Continuous auto focus mode intended for video recording. The camera
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002515 * continuously tries to focus. This is the best choice for video
2516 * recording because the focus changes smoothly . Applications still can
2517 * call {@link #takePicture(Camera.ShutterCallback,
2518 * Camera.PictureCallback, Camera.PictureCallback)} in this mode but the
2519 * subject may not be in focus. Auto focus starts when the parameter is
Wu-cheng Li53b30912011-10-12 19:43:51 +08002520 * set.
2521 *
2522 * <p>Since API level 14, applications can call {@link
2523 * #autoFocus(AutoFocusCallback)} in this mode. The focus callback will
2524 * immediately return with a boolean that indicates whether the focus is
2525 * sharp or not. The focus position is locked after autoFocus call. If
2526 * applications want to resume the continuous focus, cancelAutoFocus
2527 * must be called. Restarting the preview will not resume the continuous
2528 * autofocus. To stop continuous focus, applications should change the
2529 * focus mode to other modes.
2530 *
2531 * @see #FOCUS_MODE_CONTINUOUS_PICTURE
Wu-cheng Li699fe932010-08-05 11:50:25 -07002532 */
Wu-cheng Lid45cb722010-09-20 16:15:32 -07002533 public static final String FOCUS_MODE_CONTINUOUS_VIDEO = "continuous-video";
Wu-cheng Li699fe932010-08-05 11:50:25 -07002534
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002535 /**
2536 * Continuous auto focus mode intended for taking pictures. The camera
2537 * continuously tries to focus. The speed of focus change is more
2538 * aggressive than {@link #FOCUS_MODE_CONTINUOUS_VIDEO}. Auto focus
Wu-cheng Li53b30912011-10-12 19:43:51 +08002539 * starts when the parameter is set.
2540 *
Wu-cheng Li0f4f97b2011-10-27 18:07:01 +08002541 * <p>Applications can call {@link #autoFocus(AutoFocusCallback)} in
2542 * this mode. If the autofocus is in the middle of scanning, the focus
2543 * callback will return when it completes. If the autofocus is not
2544 * scanning, the focus callback will immediately return with a boolean
2545 * that indicates whether the focus is sharp or not. The apps can then
2546 * decide if they want to take a picture immediately or to change the
2547 * focus mode to auto, and run a full autofocus cycle. The focus
2548 * position is locked after autoFocus call. If applications want to
2549 * resume the continuous focus, cancelAutoFocus must be called.
2550 * Restarting the preview will not resume the continuous autofocus. To
2551 * stop continuous focus, applications should change the focus mode to
2552 * other modes.
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002553 *
2554 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002555 */
2556 public static final String FOCUS_MODE_CONTINUOUS_PICTURE = "continuous-picture";
2557
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002558 // Indices for focus distance array.
2559 /**
2560 * The array index of near focus distance for use with
2561 * {@link #getFocusDistances(float[])}.
2562 */
2563 public static final int FOCUS_DISTANCE_NEAR_INDEX = 0;
2564
2565 /**
2566 * The array index of optimal focus distance for use with
2567 * {@link #getFocusDistances(float[])}.
2568 */
2569 public static final int FOCUS_DISTANCE_OPTIMAL_INDEX = 1;
2570
2571 /**
2572 * The array index of far focus distance for use with
2573 * {@link #getFocusDistances(float[])}.
2574 */
2575 public static final int FOCUS_DISTANCE_FAR_INDEX = 2;
2576
Wu-cheng Lica099612010-05-06 16:47:30 +08002577 /**
Wu-cheng Li454630f2010-08-11 16:48:05 -07002578 * The array index of minimum preview fps for use with {@link
2579 * #getPreviewFpsRange(int[])} or {@link
2580 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07002581 */
2582 public static final int PREVIEW_FPS_MIN_INDEX = 0;
2583
2584 /**
2585 * The array index of maximum preview fps for use with {@link
2586 * #getPreviewFpsRange(int[])} or {@link
2587 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07002588 */
2589 public static final int PREVIEW_FPS_MAX_INDEX = 1;
2590
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002591 // Formats for setPreviewFormat and setPictureFormat.
2592 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp";
2593 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp";
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002594 private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv";
Wu-cheng Li10a1b302011-02-22 15:49:25 +08002595 private static final String PIXEL_FORMAT_YUV420P = "yuv420p";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002596 private static final String PIXEL_FORMAT_RGB565 = "rgb565";
2597 private static final String PIXEL_FORMAT_JPEG = "jpeg";
Wu-cheng Li70fb9082011-08-02 17:49:50 +08002598 private static final String PIXEL_FORMAT_BAYER_RGGB = "bayer-rggb";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002599
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002600 /**
2601 * Order matters: Keys that are {@link #set(String, String) set} later
2602 * will take precedence over keys that are set earlier (if the two keys
2603 * conflict with each other).
2604 *
2605 * <p>One example is {@link #setPreviewFpsRange(int, int)} , since it
2606 * conflicts with {@link #setPreviewFrameRate(int)} whichever key is set later
2607 * is the one that will take precedence.
2608 * </p>
2609 */
2610 private final LinkedHashMap<String, String> mMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611
2612 private Parameters() {
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002613 mMap = new LinkedHashMap<String, String>(/*initialCapacity*/64);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 }
2615
2616 /**
Igor Murashkindf6242e2014-07-01 18:06:13 -07002617 * Overwrite existing parameters with a copy of the ones from {@code other}.
2618 *
2619 * <b>For use by the legacy shim only.</b>
2620 *
2621 * @hide
2622 */
2623 public void copyFrom(Parameters other) {
2624 if (other == null) {
2625 throw new NullPointerException("other must not be null");
2626 }
2627
2628 mMap.putAll(other.mMap);
2629 }
2630
2631 private Camera getOuter() {
2632 return Camera.this;
2633 }
2634
Ruben Brunkd1f113d2014-07-11 11:46:20 -07002635
2636 /**
2637 * Value equality check.
2638 *
2639 * @hide
2640 */
2641 public boolean same(Parameters other) {
2642 if (this == other) {
2643 return true;
2644 }
2645 return other != null && Parameters.this.mMap.equals(other.mMap);
2646 }
2647
Igor Murashkindf6242e2014-07-01 18:06:13 -07002648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 * Writes the current Parameters to the log.
2650 * @hide
2651 * @deprecated
2652 */
Igor Murashkina1d66272014-06-20 11:22:11 -07002653 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 public void dump() {
2655 Log.e(TAG, "dump: size=" + mMap.size());
2656 for (String k : mMap.keySet()) {
2657 Log.e(TAG, "dump: " + k + "=" + mMap.get(k));
2658 }
2659 }
2660
2661 /**
2662 * Creates a single string with all the parameters set in
2663 * this Parameters object.
2664 * <p>The {@link #unflatten(String)} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002665 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 * @return a String with all values from this Parameters object, in
2667 * semi-colon delimited key-value pairs
2668 */
2669 public String flatten() {
Ali Utku Selen0a120182011-02-09 14:11:22 +01002670 StringBuilder flattened = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 for (String k : mMap.keySet()) {
2672 flattened.append(k);
2673 flattened.append("=");
2674 flattened.append(mMap.get(k));
2675 flattened.append(";");
2676 }
2677 // chop off the extra semicolon at the end
2678 flattened.deleteCharAt(flattened.length()-1);
2679 return flattened.toString();
2680 }
2681
2682 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002683 * Takes a flattened string of parameters and adds each one to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 * this Parameters object.
2685 * <p>The {@link #flatten()} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002686 *
2687 * @param flattened a String of parameters (key-value paired) that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 * are semi-colon delimited
2689 */
2690 public void unflatten(String flattened) {
2691 mMap.clear();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002692
Ali Utku Selen0a120182011-02-09 14:11:22 +01002693 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(';');
2694 splitter.setString(flattened);
2695 for (String kv : splitter) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 int pos = kv.indexOf('=');
2697 if (pos == -1) {
2698 continue;
2699 }
2700 String k = kv.substring(0, pos);
2701 String v = kv.substring(pos + 1);
2702 mMap.put(k, v);
2703 }
2704 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 public void remove(String key) {
2707 mMap.remove(key);
2708 }
2709
2710 /**
2711 * Sets a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002712 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 * @param key the key name for the parameter
2714 * @param value the String value of the parameter
2715 */
2716 public void set(String key, String value) {
Eino-Ville Talvalacb569232012-03-12 11:36:58 -07002717 if (key.indexOf('=') != -1 || key.indexOf(';') != -1 || key.indexOf(0) != -1) {
2718 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ; or \\0)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 return;
2720 }
Eino-Ville Talvalacb569232012-03-12 11:36:58 -07002721 if (value.indexOf('=') != -1 || value.indexOf(';') != -1 || value.indexOf(0) != -1) {
2722 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ; or \\0)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 return;
2724 }
2725
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002726 put(key, value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 }
2728
2729 /**
2730 * Sets an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002731 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 * @param key the key name for the parameter
2733 * @param value the int value of the parameter
2734 */
2735 public void set(String key, int value) {
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002736 put(key, Integer.toString(value));
2737 }
2738
2739 private void put(String key, String value) {
2740 /*
2741 * Remove the key if it already exists.
2742 *
2743 * This way setting a new value for an already existing key will always move
2744 * that key to be ordered the latest in the map.
2745 */
2746 mMap.remove(key);
2747 mMap.put(key, value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 }
2749
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002750 private void set(String key, List<Area> areas) {
Wu-cheng Lif715bf92011-04-14 14:04:18 +08002751 if (areas == null) {
2752 set(key, "(0,0,0,0,0)");
2753 } else {
2754 StringBuilder buffer = new StringBuilder();
2755 for (int i = 0; i < areas.size(); i++) {
2756 Area area = areas.get(i);
2757 Rect rect = area.rect;
2758 buffer.append('(');
2759 buffer.append(rect.left);
2760 buffer.append(',');
2761 buffer.append(rect.top);
2762 buffer.append(',');
2763 buffer.append(rect.right);
2764 buffer.append(',');
2765 buffer.append(rect.bottom);
2766 buffer.append(',');
2767 buffer.append(area.weight);
2768 buffer.append(')');
2769 if (i != areas.size() - 1) buffer.append(',');
2770 }
2771 set(key, buffer.toString());
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002772 }
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002773 }
2774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 /**
2776 * Returns the value of a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002777 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 * @param key the key name for the parameter
2779 * @return the String value of the parameter
2780 */
2781 public String get(String key) {
2782 return mMap.get(key);
2783 }
2784
2785 /**
2786 * Returns the value of an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002787 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 * @param key the key name for the parameter
2789 * @return the int value of the parameter
2790 */
2791 public int getInt(String key) {
2792 return Integer.parseInt(mMap.get(key));
2793 }
2794
2795 /**
Wu-cheng Li26274fa2011-05-05 14:36:28 +08002796 * Sets the dimensions for preview pictures. If the preview has already
2797 * started, applications should stop the preview first before changing
2798 * preview size.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002799 *
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002800 * The sides of width and height are based on camera orientation. That
2801 * is, the preview size is the size before it is rotated by display
2802 * orientation. So applications need to consider the display orientation
2803 * while setting preview size. For example, suppose the camera supports
2804 * both 480x320 and 320x480 preview sizes. The application wants a 3:2
2805 * preview ratio. If the display orientation is set to 0 or 180, preview
2806 * size should be set to 480x320. If the display orientation is set to
2807 * 90 or 270, preview size should be set to 320x480. The display
2808 * orientation should also be considered while setting picture size and
2809 * thumbnail size.
2810 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 * @param width the width of the pictures, in pixels
2812 * @param height the height of the pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002813 * @see #setDisplayOrientation(int)
2814 * @see #getCameraInfo(int, CameraInfo)
2815 * @see #setPictureSize(int, int)
2816 * @see #setJpegThumbnailSize(int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 */
2818 public void setPreviewSize(int width, int height) {
2819 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002820 set(KEY_PREVIEW_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 }
2822
2823 /**
2824 * Returns the dimensions setting for preview pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002825 *
James Dongdd0b16c2010-09-21 16:23:48 -07002826 * @return a Size object with the width and height setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 * for the preview picture
2828 */
2829 public Size getPreviewSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002830 String pair = get(KEY_PREVIEW_SIZE);
2831 return strToSize(pair);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 }
2833
2834 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002835 * Gets the supported preview sizes.
2836 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002837 * @return a list of Size object. This method will always return a list
Wu-cheng Li9c799382009-12-04 19:59:18 +08002838 * with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002839 */
2840 public List<Size> getSupportedPreviewSizes() {
2841 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX);
2842 return splitSize(str);
2843 }
2844
2845 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002846 * <p>Gets the supported video frame sizes that can be used by
2847 * MediaRecorder.</p>
James Dongdd0b16c2010-09-21 16:23:48 -07002848 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002849 * <p>If the returned list is not null, the returned list will contain at
James Dongdd0b16c2010-09-21 16:23:48 -07002850 * least one Size and one of the sizes in the returned list must be
2851 * passed to MediaRecorder.setVideoSize() for camcorder application if
2852 * camera is used as the video source. In this case, the size of the
2853 * preview can be different from the resolution of the recorded video
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002854 * during video recording.</p>
James Dongdd0b16c2010-09-21 16:23:48 -07002855 *
2856 * @return a list of Size object if camera has separate preview and
2857 * video output; otherwise, null is returned.
2858 * @see #getPreferredPreviewSizeForVideo()
2859 */
2860 public List<Size> getSupportedVideoSizes() {
2861 String str = get(KEY_VIDEO_SIZE + SUPPORTED_VALUES_SUFFIX);
2862 return splitSize(str);
2863 }
2864
2865 /**
2866 * Returns the preferred or recommended preview size (width and height)
2867 * in pixels for video recording. Camcorder applications should
2868 * set the preview size to a value that is not larger than the
2869 * preferred preview size. In other words, the product of the width
2870 * and height of the preview size should not be larger than that of
2871 * the preferred preview size. In addition, we recommend to choose a
2872 * preview size that has the same aspect ratio as the resolution of
2873 * video to be recorded.
2874 *
2875 * @return the preferred preview size (width and height) in pixels for
2876 * video recording if getSupportedVideoSizes() does not return
2877 * null; otherwise, null is returned.
2878 * @see #getSupportedVideoSizes()
2879 */
2880 public Size getPreferredPreviewSizeForVideo() {
2881 String pair = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
2882 return strToSize(pair);
2883 }
2884
2885 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002886 * <p>Sets the dimensions for EXIF thumbnail in Jpeg picture. If
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002887 * applications set both width and height to 0, EXIF will not contain
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002888 * thumbnail.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002889 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002890 * <p>Applications need to consider the display orientation. See {@link
2891 * #setPreviewSize(int,int)} for reference.</p>
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002892 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 * @param width the width of the thumbnail, in pixels
2894 * @param height the height of the thumbnail, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002895 * @see #setPreviewSize(int,int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002897 public void setJpegThumbnailSize(int width, int height) {
2898 set(KEY_JPEG_THUMBNAIL_WIDTH, width);
2899 set(KEY_JPEG_THUMBNAIL_HEIGHT, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 }
2901
2902 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002903 * Returns the dimensions for EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002905 * @return a Size object with the height and width setting for the EXIF
2906 * thumbnails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002908 public Size getJpegThumbnailSize() {
2909 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH),
2910 getInt(KEY_JPEG_THUMBNAIL_HEIGHT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 }
2912
2913 /**
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002914 * Gets the supported jpeg thumbnail sizes.
2915 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002916 * @return a list of Size object. This method will always return a list
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002917 * with at least two elements. Size 0,0 (no thumbnail) is always
2918 * supported.
2919 */
2920 public List<Size> getSupportedJpegThumbnailSizes() {
2921 String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX);
2922 return splitSize(str);
2923 }
2924
2925 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002926 * Sets the quality of the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002928 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1
2929 * to 100, with 100 being the best.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002931 public void setJpegThumbnailQuality(int quality) {
2932 set(KEY_JPEG_THUMBNAIL_QUALITY, quality);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 }
2934
2935 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002936 * Returns the quality setting for the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002938 * @return the JPEG quality setting of the EXIF thumbnail.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002940 public int getJpegThumbnailQuality() {
2941 return getInt(KEY_JPEG_THUMBNAIL_QUALITY);
2942 }
2943
2944 /**
2945 * Sets Jpeg quality of captured picture.
2946 *
2947 * @param quality the JPEG quality of captured picture. The range is 1
2948 * to 100, with 100 being the best.
2949 */
2950 public void setJpegQuality(int quality) {
2951 set(KEY_JPEG_QUALITY, quality);
2952 }
2953
2954 /**
2955 * Returns the quality setting for the JPEG picture.
2956 *
2957 * @return the JPEG picture quality setting.
2958 */
2959 public int getJpegQuality() {
2960 return getInt(KEY_JPEG_QUALITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 }
2962
2963 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08002964 * Sets the rate at which preview frames are received. This is the
2965 * target frame rate. The actual frame rate depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002966 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 * @param fps the frame rate (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002968 * @deprecated replaced by {@link #setPreviewFpsRange(int,int)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002970 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 public void setPreviewFrameRate(int fps) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002972 set(KEY_PREVIEW_FRAME_RATE, fps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 }
2974
2975 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08002976 * Returns the setting for the rate at which preview frames are
2977 * received. This is the target frame rate. The actual frame rate
2978 * depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002979 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 * @return the frame rate setting (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002981 * @deprecated replaced by {@link #getPreviewFpsRange(int[])}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002983 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 public int getPreviewFrameRate() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002985 return getInt(KEY_PREVIEW_FRAME_RATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 }
2987
2988 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002989 * Gets the supported preview frame rates.
2990 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002991 * @return a list of supported preview frame rates. null if preview
2992 * frame rate setting is not supported.
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002993 * @deprecated replaced by {@link #getSupportedPreviewFpsRange()}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002994 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07002995 @Deprecated
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002996 public List<Integer> getSupportedPreviewFrameRates() {
2997 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX);
2998 return splitInt(str);
2999 }
3000
3001 /**
Hai Guo4a68e3c2012-12-05 21:50:28 +08003002 * Sets the minimum and maximum preview fps. This controls the rate of
Wu-cheng Li1620d112010-08-27 15:09:20 -07003003 * preview frames received in {@link PreviewCallback}. The minimum and
Wu-cheng Li454630f2010-08-11 16:48:05 -07003004 * maximum preview fps must be one of the elements from {@link
3005 * #getSupportedPreviewFpsRange}.
3006 *
3007 * @param min the minimum preview fps (scaled by 1000).
3008 * @param max the maximum preview fps (scaled by 1000).
3009 * @throws RuntimeException if fps range is invalid.
3010 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
3011 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07003012 */
3013 public void setPreviewFpsRange(int min, int max) {
3014 set(KEY_PREVIEW_FPS_RANGE, "" + min + "," + max);
3015 }
3016
3017 /**
3018 * Returns the current minimum and maximum preview fps. The values are
3019 * one of the elements returned by {@link #getSupportedPreviewFpsRange}.
3020 *
3021 * @return range the minimum and maximum preview fps (scaled by 1000).
3022 * @see #PREVIEW_FPS_MIN_INDEX
3023 * @see #PREVIEW_FPS_MAX_INDEX
3024 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07003025 */
3026 public void getPreviewFpsRange(int[] range) {
3027 if (range == null || range.length != 2) {
3028 throw new IllegalArgumentException(
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003029 "range must be an array with two elements.");
Wu-cheng Li454630f2010-08-11 16:48:05 -07003030 }
3031 splitInt(get(KEY_PREVIEW_FPS_RANGE), range);
3032 }
3033
3034 /**
3035 * Gets the supported preview fps (frame-per-second) ranges. Each range
3036 * contains a minimum fps and maximum fps. If minimum fps equals to
3037 * maximum fps, the camera outputs frames in fixed frame rate. If not,
3038 * the camera outputs frames in auto frame rate. The actual frame rate
3039 * fluctuates between the minimum and the maximum. The values are
3040 * multiplied by 1000 and represented in integers. For example, if frame
3041 * rate is 26.623 frames per second, the value is 26623.
3042 *
3043 * @return a list of supported preview fps ranges. This method returns a
3044 * list with at least one element. Every element is an int array
3045 * of two values - minimum fps and maximum fps. The list is
3046 * sorted from small to large (first by maximum fps and then
3047 * minimum fps).
3048 * @see #PREVIEW_FPS_MIN_INDEX
3049 * @see #PREVIEW_FPS_MAX_INDEX
Wu-cheng Li454630f2010-08-11 16:48:05 -07003050 */
3051 public List<int[]> getSupportedPreviewFpsRange() {
3052 String str = get(KEY_PREVIEW_FPS_RANGE + SUPPORTED_VALUES_SUFFIX);
3053 return splitRange(str);
3054 }
3055
3056 /**
Wu-cheng Li7478ea62009-09-16 18:52:55 +08003057 * Sets the image format for preview pictures.
Scott Mainda0a56d2009-09-10 18:08:37 -07003058 * <p>If this is never called, the default format will be
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003059 * {@link android.graphics.ImageFormat#NV21}, which
Scott Maindf4578e2009-09-10 12:22:07 -07003060 * uses the NV21 encoding format.</p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +08003061 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003062 * <p>Use {@link Parameters#getSupportedPreviewFormats} to get a list of
3063 * the available preview formats.
3064 *
3065 * <p>It is strongly recommended that either
3066 * {@link android.graphics.ImageFormat#NV21} or
3067 * {@link android.graphics.ImageFormat#YV12} is used, since
3068 * they are supported by all camera devices.</p>
3069 *
3070 * <p>For YV12, the image buffer that is received is not necessarily
3071 * tightly packed, as there may be padding at the end of each row of
3072 * pixel data, as described in
3073 * {@link android.graphics.ImageFormat#YV12}. For camera callback data,
3074 * it can be assumed that the stride of the Y and UV data is the
3075 * smallest possible that meets the alignment requirements. That is, if
3076 * the preview size is <var>width x height</var>, then the following
3077 * equations describe the buffer index for the beginning of row
3078 * <var>y</var> for the Y plane and row <var>c</var> for the U and V
3079 * planes:
3080 *
Neil Fuller71fbb812015-11-30 09:51:33 +00003081 * <pre>{@code
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003082 * yStride = (int) ceil(width / 16.0) * 16;
3083 * uvStride = (int) ceil( (yStride / 2) / 16.0) * 16;
3084 * ySize = yStride * height;
3085 * uvSize = uvStride * height / 2;
3086 * yRowIndex = yStride * y;
3087 * uRowIndex = ySize + uvSize + uvStride * c;
3088 * vRowIndex = ySize + uvStride * c;
Neil Fuller71fbb812015-11-30 09:51:33 +00003089 * size = ySize + uvSize * 2;
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003090 * }
Neil Fuller71fbb812015-11-30 09:51:33 +00003091 *</pre>
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003092 *
3093 * @param pixel_format the desired preview picture format, defined by
3094 * one of the {@link android.graphics.ImageFormat} constants. (E.g.,
3095 * <var>ImageFormat.NV21</var> (default), or
3096 * <var>ImageFormat.YV12</var>)
3097 *
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003098 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003099 * @see android.hardware.Camera.Parameters#getSupportedPreviewFormats
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 */
3101 public void setPreviewFormat(int pixel_format) {
3102 String s = cameraFormatForPixelFormat(pixel_format);
3103 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003104 throw new IllegalArgumentException(
3105 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 }
3107
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003108 set(KEY_PREVIEW_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 }
3110
3111 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003112 * Returns the image format for preview frames got from
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003113 * {@link PreviewCallback}.
Wu-cheng Li7478ea62009-09-16 18:52:55 +08003114 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003115 * @return the preview format.
3116 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003117 * @see #setPreviewFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 */
3119 public int getPreviewFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003120 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT));
3121 }
3122
3123 /**
Wu-cheng Lif9293e72011-02-25 13:35:10 +08003124 * Gets the supported preview formats. {@link android.graphics.ImageFormat#NV21}
3125 * is always supported. {@link android.graphics.ImageFormat#YV12}
3126 * is always supported since API level 12.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003127 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003128 * @return a list of supported preview formats. This method will always
3129 * return a list with at least one element.
3130 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003131 * @see #setPreviewFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003132 */
3133 public List<Integer> getSupportedPreviewFormats() {
3134 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX);
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003135 ArrayList<Integer> formats = new ArrayList<Integer>();
3136 for (String s : split(str)) {
3137 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003138 if (f == ImageFormat.UNKNOWN) continue;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003139 formats.add(f);
3140 }
3141 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 }
3143
3144 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003145 * <p>Sets the dimensions for pictures.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003146 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003147 * <p>Applications need to consider the display orientation. See {@link
3148 * #setPreviewSize(int,int)} for reference.</p>
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08003149 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 * @param width the width for pictures, in pixels
3151 * @param height the height for pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08003152 * @see #setPreviewSize(int,int)
3153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 */
3155 public void setPictureSize(int width, int height) {
3156 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003157 set(KEY_PICTURE_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 }
3159
3160 /**
3161 * Returns the dimension setting for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003162 *
3163 * @return a Size object with the height and width setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 * for pictures
3165 */
3166 public Size getPictureSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003167 String pair = get(KEY_PICTURE_SIZE);
3168 return strToSize(pair);
3169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003171 /**
3172 * Gets the supported picture sizes.
3173 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003174 * @return a list of supported picture sizes. This method will always
3175 * return a list with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003176 */
3177 public List<Size> getSupportedPictureSizes() {
3178 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX);
3179 return splitSize(str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 }
3181
3182 /**
3183 * Sets the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003184 *
3185 * @param pixel_format the desired picture format
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003186 * (<var>ImageFormat.NV21</var>,
3187 * <var>ImageFormat.RGB_565</var>, or
3188 * <var>ImageFormat.JPEG</var>)
3189 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 */
3191 public void setPictureFormat(int pixel_format) {
3192 String s = cameraFormatForPixelFormat(pixel_format);
3193 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003194 throw new IllegalArgumentException(
3195 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 }
3197
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003198 set(KEY_PICTURE_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 }
3200
3201 /**
3202 * Returns the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003203 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003204 * @return the picture format
3205 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 */
3207 public int getPictureFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003208 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT));
3209 }
3210
3211 /**
3212 * Gets the supported picture formats.
3213 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003214 * @return supported picture formats. This method will always return a
3215 * list with at least one element.
3216 * @see android.graphics.ImageFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003217 */
3218 public List<Integer> getSupportedPictureFormats() {
Wu-cheng Li9c799382009-12-04 19:59:18 +08003219 String str = get(KEY_PICTURE_FORMAT + SUPPORTED_VALUES_SUFFIX);
3220 ArrayList<Integer> formats = new ArrayList<Integer>();
3221 for (String s : split(str)) {
3222 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003223 if (f == ImageFormat.UNKNOWN) continue;
Wu-cheng Li9c799382009-12-04 19:59:18 +08003224 formats.add(f);
3225 }
3226 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
3228
3229 private String cameraFormatForPixelFormat(int pixel_format) {
3230 switch(pixel_format) {
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003231 case ImageFormat.NV16: return PIXEL_FORMAT_YUV422SP;
3232 case ImageFormat.NV21: return PIXEL_FORMAT_YUV420SP;
3233 case ImageFormat.YUY2: return PIXEL_FORMAT_YUV422I;
Wu-cheng Li10a1b302011-02-22 15:49:25 +08003234 case ImageFormat.YV12: return PIXEL_FORMAT_YUV420P;
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003235 case ImageFormat.RGB_565: return PIXEL_FORMAT_RGB565;
3236 case ImageFormat.JPEG: return PIXEL_FORMAT_JPEG;
3237 default: return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
3239 }
3240
3241 private int pixelFormatForCameraFormat(String format) {
3242 if (format == null)
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003243 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003245 if (format.equals(PIXEL_FORMAT_YUV422SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003246 return ImageFormat.NV16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003248 if (format.equals(PIXEL_FORMAT_YUV420SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003249 return ImageFormat.NV21;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003251 if (format.equals(PIXEL_FORMAT_YUV422I))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003252 return ImageFormat.YUY2;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003253
Wu-cheng Li10a1b302011-02-22 15:49:25 +08003254 if (format.equals(PIXEL_FORMAT_YUV420P))
3255 return ImageFormat.YV12;
3256
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003257 if (format.equals(PIXEL_FORMAT_RGB565))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003258 return ImageFormat.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003260 if (format.equals(PIXEL_FORMAT_JPEG))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003261 return ImageFormat.JPEG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003263 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 }
3265
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003266 /**
Wu-cheng Li8969ea12012-04-20 17:38:09 +08003267 * Sets the clockwise rotation angle in degrees relative to the
3268 * orientation of the camera. This affects the pictures returned from
3269 * JPEG {@link PictureCallback}. The camera driver may set orientation
3270 * in the EXIF header without rotating the picture. Or the driver may
3271 * rotate the picture and the EXIF thumbnail. If the Jpeg picture is
Igor Murashkina1b02db2013-06-11 15:25:20 -07003272 * rotated, the orientation in the EXIF header will be missing or 1 (row
3273 * #0 is top and column #0 is left side).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003274 *
Igor Murashkina1b02db2013-06-11 15:25:20 -07003275 * <p>
3276 * If applications want to rotate the picture to match the orientation
3277 * of what users see, apps should use
3278 * {@link android.view.OrientationEventListener} and
3279 * {@link android.hardware.Camera.CameraInfo}. The value from
3280 * OrientationEventListener is relative to the natural orientation of
3281 * the device. CameraInfo.orientation is the angle between camera
3282 * orientation and natural device orientation. The sum of the two is the
3283 * rotation angle for back-facing camera. The difference of the two is
3284 * the rotation angle for front-facing camera. Note that the JPEG
3285 * pictures of front-facing cameras are not mirrored as in preview
3286 * display.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003287 *
Igor Murashkina1b02db2013-06-11 15:25:20 -07003288 * <p>
3289 * For example, suppose the natural orientation of the device is
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003290 * portrait. The device is rotated 270 degrees clockwise, so the device
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08003291 * orientation is 270. Suppose a back-facing camera sensor is mounted in
3292 * landscape and the top side of the camera sensor is aligned with the
3293 * right edge of the display in natural orientation. So the camera
3294 * orientation is 90. The rotation should be set to 0 (270 + 90).
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003295 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08003296 * <p>The reference code is as follows.
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003297 *
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -08003298 * <pre>
Scott Main9a10bf02011-08-24 19:09:48 -07003299 * public void onOrientationChanged(int orientation) {
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003300 * if (orientation == ORIENTATION_UNKNOWN) return;
3301 * android.hardware.Camera.CameraInfo info =
3302 * new android.hardware.Camera.CameraInfo();
3303 * android.hardware.Camera.getCameraInfo(cameraId, info);
3304 * orientation = (orientation + 45) / 90 * 90;
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08003305 * int rotation = 0;
3306 * if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
3307 * rotation = (info.orientation - orientation + 360) % 360;
3308 * } else { // back-facing camera
3309 * rotation = (info.orientation + orientation) % 360;
3310 * }
3311 * mParameters.setRotation(rotation);
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003312 * }
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -08003313 * </pre>
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003314 *
3315 * @param rotation The rotation angle in degrees relative to the
3316 * orientation of the camera. Rotation can only be 0,
3317 * 90, 180 or 270.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003318 * @throws IllegalArgumentException if rotation value is invalid.
3319 * @see android.view.OrientationEventListener
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003320 * @see #getCameraInfo(int, CameraInfo)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003321 */
3322 public void setRotation(int rotation) {
3323 if (rotation == 0 || rotation == 90 || rotation == 180
3324 || rotation == 270) {
3325 set(KEY_ROTATION, Integer.toString(rotation));
3326 } else {
3327 throw new IllegalArgumentException(
3328 "Invalid rotation=" + rotation);
3329 }
3330 }
3331
3332 /**
3333 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF
3334 * header.
3335 *
3336 * @param latitude GPS latitude coordinate.
3337 */
3338 public void setGpsLatitude(double latitude) {
3339 set(KEY_GPS_LATITUDE, Double.toString(latitude));
3340 }
3341
3342 /**
3343 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF
3344 * header.
3345 *
3346 * @param longitude GPS longitude coordinate.
3347 */
3348 public void setGpsLongitude(double longitude) {
3349 set(KEY_GPS_LONGITUDE, Double.toString(longitude));
3350 }
3351
3352 /**
3353 * Sets GPS altitude. This will be stored in JPEG EXIF header.
3354 *
3355 * @param altitude GPS altitude in meters.
3356 */
3357 public void setGpsAltitude(double altitude) {
3358 set(KEY_GPS_ALTITUDE, Double.toString(altitude));
3359 }
3360
3361 /**
3362 * Sets GPS timestamp. This will be stored in JPEG EXIF header.
3363 *
3364 * @param timestamp GPS timestamp (UTC in seconds since January 1,
3365 * 1970).
3366 */
3367 public void setGpsTimestamp(long timestamp) {
3368 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp));
3369 }
3370
3371 /**
Eino-Ville Talvalad8407272015-11-08 18:27:20 -08003372 * Sets GPS processing method. The method will be stored in a UTF-8 string up to 31 bytes
3373 * long, in the JPEG EXIF header.
Ray Chen055c9862010-02-23 10:45:42 +08003374 *
3375 * @param processing_method The processing method to get this location.
3376 */
3377 public void setGpsProcessingMethod(String processing_method) {
3378 set(KEY_GPS_PROCESSING_METHOD, processing_method);
3379 }
3380
3381 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003382 * Removes GPS latitude, longitude, altitude, and timestamp from the
3383 * parameters.
3384 */
3385 public void removeGpsData() {
3386 remove(KEY_GPS_LATITUDE);
3387 remove(KEY_GPS_LONGITUDE);
3388 remove(KEY_GPS_ALTITUDE);
3389 remove(KEY_GPS_TIMESTAMP);
Ray Chen055c9862010-02-23 10:45:42 +08003390 remove(KEY_GPS_PROCESSING_METHOD);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003391 }
3392
3393 /**
3394 * Gets the current white balance setting.
3395 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003396 * @return current white balance. null if white balance setting is not
3397 * supported.
3398 * @see #WHITE_BALANCE_AUTO
3399 * @see #WHITE_BALANCE_INCANDESCENT
3400 * @see #WHITE_BALANCE_FLUORESCENT
3401 * @see #WHITE_BALANCE_WARM_FLUORESCENT
3402 * @see #WHITE_BALANCE_DAYLIGHT
3403 * @see #WHITE_BALANCE_CLOUDY_DAYLIGHT
3404 * @see #WHITE_BALANCE_TWILIGHT
3405 * @see #WHITE_BALANCE_SHADE
3406 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003407 */
3408 public String getWhiteBalance() {
3409 return get(KEY_WHITE_BALANCE);
3410 }
3411
3412 /**
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003413 * Sets the white balance. Changing the setting will release the
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08003414 * auto-white balance lock. It is recommended not to change white
3415 * balance and AWB lock at the same time.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003416 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003417 * @param value new white balance.
3418 * @see #getWhiteBalance()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003419 * @see #setAutoWhiteBalanceLock(boolean)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003420 */
3421 public void setWhiteBalance(String value) {
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08003422 String oldValue = get(KEY_WHITE_BALANCE);
3423 if (same(value, oldValue)) return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003424 set(KEY_WHITE_BALANCE, value);
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003425 set(KEY_AUTO_WHITEBALANCE_LOCK, FALSE);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003426 }
3427
3428 /**
3429 * Gets the supported white balance.
3430 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003431 * @return a list of supported white balance. null if white balance
3432 * setting is not supported.
3433 * @see #getWhiteBalance()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003434 */
3435 public List<String> getSupportedWhiteBalance() {
3436 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX);
3437 return split(str);
3438 }
3439
3440 /**
3441 * Gets the current color effect setting.
3442 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003443 * @return current color effect. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003444 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003445 * @see #EFFECT_NONE
3446 * @see #EFFECT_MONO
3447 * @see #EFFECT_NEGATIVE
3448 * @see #EFFECT_SOLARIZE
3449 * @see #EFFECT_SEPIA
3450 * @see #EFFECT_POSTERIZE
3451 * @see #EFFECT_WHITEBOARD
3452 * @see #EFFECT_BLACKBOARD
3453 * @see #EFFECT_AQUA
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003454 */
3455 public String getColorEffect() {
3456 return get(KEY_EFFECT);
3457 }
3458
3459 /**
3460 * Sets the current color effect setting.
3461 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003462 * @param value new color effect.
3463 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003464 */
3465 public void setColorEffect(String value) {
3466 set(KEY_EFFECT, value);
3467 }
3468
3469 /**
3470 * Gets the supported color effects.
3471 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003472 * @return a list of supported color effects. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003473 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003474 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003475 */
3476 public List<String> getSupportedColorEffects() {
3477 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX);
3478 return split(str);
3479 }
3480
3481
3482 /**
3483 * Gets the current antibanding setting.
3484 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003485 * @return current antibanding. null if antibanding setting is not
3486 * supported.
3487 * @see #ANTIBANDING_AUTO
3488 * @see #ANTIBANDING_50HZ
3489 * @see #ANTIBANDING_60HZ
3490 * @see #ANTIBANDING_OFF
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003491 */
3492 public String getAntibanding() {
3493 return get(KEY_ANTIBANDING);
3494 }
3495
3496 /**
3497 * Sets the antibanding.
3498 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003499 * @param antibanding new antibanding value.
3500 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003501 */
3502 public void setAntibanding(String antibanding) {
3503 set(KEY_ANTIBANDING, antibanding);
3504 }
3505
3506 /**
3507 * Gets the supported antibanding values.
3508 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003509 * @return a list of supported antibanding values. null if antibanding
3510 * setting is not supported.
3511 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003512 */
3513 public List<String> getSupportedAntibanding() {
3514 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX);
3515 return split(str);
3516 }
3517
3518 /**
3519 * Gets the current scene mode setting.
3520 *
3521 * @return one of SCENE_MODE_XXX string constant. null if scene mode
3522 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003523 * @see #SCENE_MODE_AUTO
3524 * @see #SCENE_MODE_ACTION
3525 * @see #SCENE_MODE_PORTRAIT
3526 * @see #SCENE_MODE_LANDSCAPE
3527 * @see #SCENE_MODE_NIGHT
3528 * @see #SCENE_MODE_NIGHT_PORTRAIT
3529 * @see #SCENE_MODE_THEATRE
3530 * @see #SCENE_MODE_BEACH
3531 * @see #SCENE_MODE_SNOW
3532 * @see #SCENE_MODE_SUNSET
3533 * @see #SCENE_MODE_STEADYPHOTO
3534 * @see #SCENE_MODE_FIREWORKS
3535 * @see #SCENE_MODE_SPORTS
3536 * @see #SCENE_MODE_PARTY
3537 * @see #SCENE_MODE_CANDLELIGHT
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07003538 * @see #SCENE_MODE_BARCODE
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003539 */
3540 public String getSceneMode() {
3541 return get(KEY_SCENE_MODE);
3542 }
3543
3544 /**
Wu-cheng Lic58b4232010-03-29 17:21:28 +08003545 * Sets the scene mode. Changing scene mode may override other
3546 * parameters (such as flash mode, focus mode, white balance). For
3547 * example, suppose originally flash mode is on and supported flash
3548 * modes are on/off. In night scene mode, both flash mode and supported
3549 * flash mode may be changed to off. After setting scene mode,
Wu-cheng Li2988ab72009-09-30 17:08:19 -07003550 * applications should call getParameters to know if some parameters are
3551 * changed.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003552 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003553 * @param value scene mode.
3554 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003555 */
3556 public void setSceneMode(String value) {
3557 set(KEY_SCENE_MODE, value);
3558 }
3559
3560 /**
3561 * Gets the supported scene modes.
3562 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003563 * @return a list of supported scene modes. null if scene mode setting
3564 * is not supported.
3565 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003566 */
3567 public List<String> getSupportedSceneModes() {
3568 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX);
3569 return split(str);
3570 }
3571
3572 /**
3573 * Gets the current flash mode setting.
3574 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003575 * @return current flash mode. null if flash mode setting is not
3576 * supported.
3577 * @see #FLASH_MODE_OFF
3578 * @see #FLASH_MODE_AUTO
3579 * @see #FLASH_MODE_ON
3580 * @see #FLASH_MODE_RED_EYE
3581 * @see #FLASH_MODE_TORCH
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003582 */
3583 public String getFlashMode() {
3584 return get(KEY_FLASH_MODE);
3585 }
3586
3587 /**
3588 * Sets the flash mode.
3589 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003590 * @param value flash mode.
3591 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003592 */
3593 public void setFlashMode(String value) {
3594 set(KEY_FLASH_MODE, value);
3595 }
3596
3597 /**
3598 * Gets the supported flash modes.
3599 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003600 * @return a list of supported flash modes. null if flash mode setting
3601 * is not supported.
3602 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003603 */
3604 public List<String> getSupportedFlashModes() {
3605 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX);
3606 return split(str);
3607 }
3608
Wu-cheng Li36322db2009-09-18 18:59:21 +08003609 /**
3610 * Gets the current focus mode setting.
3611 *
Wu-cheng Li699fe932010-08-05 11:50:25 -07003612 * @return current focus mode. This method will always return a non-null
3613 * value. Applications should call {@link
3614 * #autoFocus(AutoFocusCallback)} to start the focus if focus
3615 * mode is FOCUS_MODE_AUTO or FOCUS_MODE_MACRO.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003616 * @see #FOCUS_MODE_AUTO
3617 * @see #FOCUS_MODE_INFINITY
3618 * @see #FOCUS_MODE_MACRO
3619 * @see #FOCUS_MODE_FIXED
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07003620 * @see #FOCUS_MODE_EDOF
Wu-cheng Lid45cb722010-09-20 16:15:32 -07003621 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Li36322db2009-09-18 18:59:21 +08003622 */
3623 public String getFocusMode() {
3624 return get(KEY_FOCUS_MODE);
3625 }
3626
3627 /**
3628 * Sets the focus mode.
3629 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003630 * @param value focus mode.
3631 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08003632 */
3633 public void setFocusMode(String value) {
3634 set(KEY_FOCUS_MODE, value);
3635 }
3636
3637 /**
3638 * Gets the supported focus modes.
3639 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003640 * @return a list of supported focus modes. This method will always
3641 * return a list with at least one element.
3642 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08003643 */
3644 public List<String> getSupportedFocusModes() {
3645 String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX);
3646 return split(str);
3647 }
3648
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003649 /**
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003650 * Gets the focal length (in millimeter) of the camera.
3651 *
Yin-Chia Yehbb85e1c2018-02-27 11:50:48 -08003652 * @return the focal length. Returns -1.0 when the device
3653 * doesn't report focal length information.
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003654 */
3655 public float getFocalLength() {
3656 return Float.parseFloat(get(KEY_FOCAL_LENGTH));
3657 }
3658
3659 /**
3660 * Gets the horizontal angle of view in degrees.
3661 *
Yin-Chia Yeh564ae822018-02-08 16:24:27 -08003662 * @return horizontal angle of view. Returns -1.0 when the device
3663 * doesn't report view angle information.
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003664 */
3665 public float getHorizontalViewAngle() {
3666 return Float.parseFloat(get(KEY_HORIZONTAL_VIEW_ANGLE));
3667 }
3668
3669 /**
3670 * Gets the vertical angle of view in degrees.
3671 *
Yin-Chia Yeh564ae822018-02-08 16:24:27 -08003672 * @return vertical angle of view. Returns -1.0 when the device
3673 * doesn't report view angle information.
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003674 */
3675 public float getVerticalViewAngle() {
3676 return Float.parseFloat(get(KEY_VERTICAL_VIEW_ANGLE));
3677 }
3678
3679 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003680 * Gets the current exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003681 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003682 * @return current exposure compensation index. The range is {@link
3683 * #getMinExposureCompensation} to {@link
3684 * #getMaxExposureCompensation}. 0 means exposure is not
3685 * adjusted.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003686 */
3687 public int getExposureCompensation() {
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003688 return getInt(KEY_EXPOSURE_COMPENSATION, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08003689 }
3690
3691 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003692 * Sets the exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003693 *
Wu-cheng Li0402e7d2010-02-26 15:04:55 +08003694 * @param value exposure compensation index. The valid value range is
3695 * from {@link #getMinExposureCompensation} (inclusive) to {@link
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003696 * #getMaxExposureCompensation} (inclusive). 0 means exposure is
3697 * not adjusted. Application should call
3698 * getMinExposureCompensation and getMaxExposureCompensation to
3699 * know if exposure compensation is supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003700 */
3701 public void setExposureCompensation(int value) {
3702 set(KEY_EXPOSURE_COMPENSATION, value);
3703 }
3704
3705 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003706 * Gets the maximum exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003707 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003708 * @return maximum exposure compensation index (>=0). If both this
3709 * method and {@link #getMinExposureCompensation} return 0,
3710 * exposure compensation is not supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003711 */
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003712 public int getMaxExposureCompensation() {
3713 return getInt(KEY_MAX_EXPOSURE_COMPENSATION, 0);
3714 }
3715
3716 /**
3717 * Gets the minimum exposure compensation index.
3718 *
3719 * @return minimum exposure compensation index (<=0). If both this
3720 * method and {@link #getMaxExposureCompensation} return 0,
3721 * exposure compensation is not supported.
3722 */
3723 public int getMinExposureCompensation() {
3724 return getInt(KEY_MIN_EXPOSURE_COMPENSATION, 0);
3725 }
3726
3727 /**
3728 * Gets the exposure compensation step.
3729 *
3730 * @return exposure compensation step. Applications can get EV by
3731 * multiplying the exposure compensation index and step. Ex: if
3732 * exposure compensation index is -6 and step is 0.333333333, EV
3733 * is -2.
3734 */
3735 public float getExposureCompensationStep() {
3736 return getFloat(KEY_EXPOSURE_COMPENSATION_STEP, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08003737 }
3738
3739 /**
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003740 * <p>Sets the auto-exposure lock state. Applications should check
3741 * {@link #isAutoExposureLockSupported} before using this method.</p>
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003742 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003743 * <p>If set to true, the camera auto-exposure routine will immediately
3744 * pause until the lock is set to false. Exposure compensation settings
3745 * changes will still take effect while auto-exposure is locked.</p>
3746 *
3747 * <p>If auto-exposure is already locked, setting this to true again has
3748 * no effect (the driver will not recalculate exposure values).</p>
3749 *
3750 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
3751 * image capture with {@link #takePicture(Camera.ShutterCallback,
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003752 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
3753 * lock.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003754 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003755 * <p>Exposure compensation, auto-exposure lock, and auto-white balance
3756 * lock can be used to capture an exposure-bracketed burst of images,
3757 * for example.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003758 *
3759 * <p>Auto-exposure state, including the lock state, will not be
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003760 * maintained after camera {@link #release()} is called. Locking
3761 * auto-exposure after {@link #open()} but before the first call to
3762 * {@link #startPreview()} will not allow the auto-exposure routine to
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003763 * run at all, and may result in severely over- or under-exposed
3764 * images.</p>
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003765 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003766 * @param toggle new state of the auto-exposure lock. True means that
3767 * auto-exposure is locked, false means that the auto-exposure
3768 * routine is free to run normally.
3769 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003770 * @see #getAutoExposureLock()
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003771 */
3772 public void setAutoExposureLock(boolean toggle) {
3773 set(KEY_AUTO_EXPOSURE_LOCK, toggle ? TRUE : FALSE);
3774 }
3775
3776 /**
3777 * Gets the state of the auto-exposure lock. Applications should check
3778 * {@link #isAutoExposureLockSupported} before using this method. See
3779 * {@link #setAutoExposureLock} for details about the lock.
3780 *
3781 * @return State of the auto-exposure lock. Returns true if
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003782 * auto-exposure is currently locked, and false otherwise.
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003783 *
3784 * @see #setAutoExposureLock(boolean)
3785 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003786 */
3787 public boolean getAutoExposureLock() {
3788 String str = get(KEY_AUTO_EXPOSURE_LOCK);
3789 return TRUE.equals(str);
3790 }
3791
3792 /**
3793 * Returns true if auto-exposure locking is supported. Applications
3794 * should call this before trying to lock auto-exposure. See
3795 * {@link #setAutoExposureLock} for details about the lock.
3796 *
3797 * @return true if auto-exposure lock is supported.
3798 * @see #setAutoExposureLock(boolean)
3799 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003800 */
3801 public boolean isAutoExposureLockSupported() {
3802 String str = get(KEY_AUTO_EXPOSURE_LOCK_SUPPORTED);
3803 return TRUE.equals(str);
3804 }
3805
3806 /**
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003807 * <p>Sets the auto-white balance lock state. Applications should check
3808 * {@link #isAutoWhiteBalanceLockSupported} before using this
3809 * method.</p>
3810 *
3811 * <p>If set to true, the camera auto-white balance routine will
3812 * immediately pause until the lock is set to false.</p>
3813 *
3814 * <p>If auto-white balance is already locked, setting this to true
3815 * again has no effect (the driver will not recalculate white balance
3816 * values).</p>
3817 *
3818 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
3819 * image capture with {@link #takePicture(Camera.ShutterCallback,
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003820 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
3821 * the lock.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003822 *
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003823 * <p> Changing the white balance mode with {@link #setWhiteBalance}
3824 * will release the auto-white balance lock if it is set.</p>
3825 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003826 * <p>Exposure compensation, AE lock, and AWB lock can be used to
3827 * capture an exposure-bracketed burst of images, for example.
3828 * Auto-white balance state, including the lock state, will not be
3829 * maintained after camera {@link #release()} is called. Locking
3830 * auto-white balance after {@link #open()} but before the first call to
3831 * {@link #startPreview()} will not allow the auto-white balance routine
3832 * to run at all, and may result in severely incorrect color in captured
3833 * images.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003834 *
3835 * @param toggle new state of the auto-white balance lock. True means
3836 * that auto-white balance is locked, false means that the
3837 * auto-white balance routine is free to run normally.
3838 *
3839 * @see #getAutoWhiteBalanceLock()
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003840 * @see #setWhiteBalance(String)
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003841 */
3842 public void setAutoWhiteBalanceLock(boolean toggle) {
3843 set(KEY_AUTO_WHITEBALANCE_LOCK, toggle ? TRUE : FALSE);
3844 }
3845
3846 /**
3847 * Gets the state of the auto-white balance lock. Applications should
3848 * check {@link #isAutoWhiteBalanceLockSupported} before using this
3849 * method. See {@link #setAutoWhiteBalanceLock} for details about the
3850 * lock.
3851 *
3852 * @return State of the auto-white balance lock. Returns true if
3853 * auto-white balance is currently locked, and false
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003854 * otherwise.
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003855 *
3856 * @see #setAutoWhiteBalanceLock(boolean)
3857 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003858 */
3859 public boolean getAutoWhiteBalanceLock() {
3860 String str = get(KEY_AUTO_WHITEBALANCE_LOCK);
3861 return TRUE.equals(str);
3862 }
3863
3864 /**
3865 * Returns true if auto-white balance locking is supported. Applications
3866 * should call this before trying to lock auto-white balance. See
3867 * {@link #setAutoWhiteBalanceLock} for details about the lock.
3868 *
3869 * @return true if auto-white balance lock is supported.
3870 * @see #setAutoWhiteBalanceLock(boolean)
3871 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003872 */
3873 public boolean isAutoWhiteBalanceLockSupported() {
3874 String str = get(KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED);
3875 return TRUE.equals(str);
3876 }
3877
3878 /**
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003879 * Gets current zoom value. This also works when smooth zoom is in
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003880 * progress. Applications should check {@link #isZoomSupported} before
3881 * using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003882 *
3883 * @return the current zoom value. The range is 0 to {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003884 * #getMaxZoom}. 0 means the camera is not zoomed.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003885 */
3886 public int getZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003887 return getInt(KEY_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003888 }
3889
3890 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003891 * Sets current zoom value. If the camera is zoomed (value > 0), the
3892 * actual picture size may be smaller than picture size setting.
3893 * Applications can check the actual picture size after picture is
3894 * returned from {@link PictureCallback}. The preview size remains the
3895 * same in zoom. Applications should check {@link #isZoomSupported}
3896 * before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003897 *
3898 * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003899 */
3900 public void setZoom(int value) {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003901 set(KEY_ZOOM, value);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003902 }
3903
3904 /**
3905 * Returns true if zoom is supported. Applications should call this
3906 * before using other zoom methods.
3907 *
3908 * @return true if zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003909 */
3910 public boolean isZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003911 String str = get(KEY_ZOOM_SUPPORTED);
3912 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003913 }
3914
3915 /**
3916 * Gets the maximum zoom value allowed for snapshot. This is the maximum
3917 * value that applications can set to {@link #setZoom(int)}.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003918 * Applications should call {@link #isZoomSupported} before using this
3919 * method. This value may change in different preview size. Applications
3920 * should call this again after setting preview size.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003921 *
3922 * @return the maximum zoom value supported by the camera.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003923 */
3924 public int getMaxZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003925 return getInt(KEY_MAX_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003926 }
3927
3928 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003929 * Gets the zoom ratios of all zoom values. Applications should check
3930 * {@link #isZoomSupported} before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003931 *
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003932 * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is
3933 * returned as 320. The number of elements is {@link
3934 * #getMaxZoom} + 1. The list is sorted from small to large. The
3935 * first element is always 100. The last element is the zoom
3936 * ratio of the maximum zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003937 */
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003938 public List<Integer> getZoomRatios() {
3939 return splitInt(get(KEY_ZOOM_RATIOS));
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003940 }
3941
3942 /**
3943 * Returns true if smooth zoom is supported. Applications should call
3944 * this before using other smooth zoom methods.
3945 *
3946 * @return true if smooth zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003947 */
3948 public boolean isSmoothZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003949 String str = get(KEY_SMOOTH_ZOOM_SUPPORTED);
3950 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003951 }
3952
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003953 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003954 * <p>Gets the distances from the camera to where an object appears to be
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003955 * in focus. The object is sharpest at the optimal focus distance. The
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003956 * depth of field is the far focus distance minus near focus distance.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003957 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003958 * <p>Focus distances may change after calling {@link
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003959 * #autoFocus(AutoFocusCallback)}, {@link #cancelAutoFocus}, or {@link
3960 * #startPreview()}. Applications can call {@link #getParameters()}
3961 * and this method anytime to get the latest focus distances. If the
Wu-cheng Lid45cb722010-09-20 16:15:32 -07003962 * focus mode is FOCUS_MODE_CONTINUOUS_VIDEO, focus distances may change
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003963 * from time to time.</p>
Wu-cheng Li699fe932010-08-05 11:50:25 -07003964 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003965 * <p>This method is intended to estimate the distance between the camera
Wu-cheng Li699fe932010-08-05 11:50:25 -07003966 * and the subject. After autofocus, the subject distance may be within
3967 * near and far focus distance. However, the precision depends on the
3968 * camera hardware, autofocus algorithm, the focus area, and the scene.
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003969 * The error can be large and it should be only used as a reference.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003970 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003971 * <p>Far focus distance >= optimal focus distance >= near focus distance.
Wu-cheng Li185cc452010-05-20 15:36:13 +08003972 * If the focus distance is infinity, the value will be
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003973 * {@code Float.POSITIVE_INFINITY}.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003974 *
3975 * @param output focus distances in meters. output must be a float
3976 * array with three elements. Near focus distance, optimal focus
3977 * distance, and far focus distance will be filled in the array.
Wu-cheng Li185cc452010-05-20 15:36:13 +08003978 * @see #FOCUS_DISTANCE_NEAR_INDEX
3979 * @see #FOCUS_DISTANCE_OPTIMAL_INDEX
3980 * @see #FOCUS_DISTANCE_FAR_INDEX
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003981 */
3982 public void getFocusDistances(float[] output) {
3983 if (output == null || output.length != 3) {
3984 throw new IllegalArgumentException(
Ken Wakasaf76a50c2012-03-09 19:56:35 +09003985 "output must be a float array with three elements.");
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003986 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07003987 splitFloat(get(KEY_FOCUS_DISTANCES), output);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08003988 }
3989
Wu-cheng Li30771b72011-04-02 06:19:46 +08003990 /**
3991 * Gets the maximum number of focus areas supported. This is the maximum
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08003992 * length of the list in {@link #setFocusAreas(List)} and
3993 * {@link #getFocusAreas()}.
Wu-cheng Li30771b72011-04-02 06:19:46 +08003994 *
3995 * @return the maximum number of focus areas supported by the camera.
3996 * @see #getFocusAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08003997 */
3998 public int getMaxNumFocusAreas() {
3999 return getInt(KEY_MAX_NUM_FOCUS_AREAS, 0);
4000 }
4001
4002 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004003 * <p>Gets the current focus areas. Camera driver uses the areas to decide
4004 * focus.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004005 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004006 * <p>Before using this API or {@link #setFocusAreas(List)}, apps should
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004007 * call {@link #getMaxNumFocusAreas()} to know the maximum number of
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004008 * focus areas first. If the value is 0, focus area is not supported.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004009 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004010 * <p>Each focus area is a rectangle with specified weight. The direction
Wu-cheng Li30771b72011-04-02 06:19:46 +08004011 * is relative to the sensor orientation, that is, what the sensor sees.
4012 * The direction is not affected by the rotation or mirroring of
4013 * {@link #setDisplayOrientation(int)}. Coordinates of the rectangle
4014 * range from -1000 to 1000. (-1000, -1000) is the upper left point.
Wu-cheng Libde61a52011-06-07 18:23:14 +08004015 * (1000, 1000) is the lower right point. The width and height of focus
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004016 * areas cannot be 0 or negative.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004017 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004018 * <p>The weight must range from 1 to 1000. The weight should be
Eino-Ville Talvala4e396e02011-04-21 09:23:15 -07004019 * interpreted as a per-pixel weight - all pixels in the area have the
4020 * specified weight. This means a small area with the same weight as a
4021 * larger area will have less influence on the focusing than the larger
4022 * area. Focus areas can partially overlap and the driver will add the
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004023 * weights in the overlap region.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004024 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004025 * <p>A special case of a {@code null} focus area list means the driver is
4026 * free to select focus targets as it wants. For example, the driver may
4027 * use more signals to select focus areas and change them
4028 * dynamically. Apps can set the focus area list to {@code null} if they
4029 * want the driver to completely control focusing.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004030 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004031 * <p>Focus areas are relative to the current field of view
Wu-cheng Li30771b72011-04-02 06:19:46 +08004032 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000)
4033 * represents the top of the currently visible camera frame. The focus
4034 * area cannot be set to be outside the current field of view, even
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004035 * when using zoom.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004036 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004037 * <p>Focus area only has effect if the current focus mode is
Wu-cheng Li53b30912011-10-12 19:43:51 +08004038 * {@link #FOCUS_MODE_AUTO}, {@link #FOCUS_MODE_MACRO},
4039 * {@link #FOCUS_MODE_CONTINUOUS_VIDEO}, or
4040 * {@link #FOCUS_MODE_CONTINUOUS_PICTURE}.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004041 *
4042 * @return a list of current focus areas
Wu-cheng Li30771b72011-04-02 06:19:46 +08004043 */
4044 public List<Area> getFocusAreas() {
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004045 return splitArea(get(KEY_FOCUS_AREAS));
Wu-cheng Li30771b72011-04-02 06:19:46 +08004046 }
4047
4048 /**
4049 * Sets focus areas. See {@link #getFocusAreas()} for documentation.
4050 *
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004051 * @param focusAreas the focus areas
Wu-cheng Li30771b72011-04-02 06:19:46 +08004052 * @see #getFocusAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08004053 */
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004054 public void setFocusAreas(List<Area> focusAreas) {
4055 set(KEY_FOCUS_AREAS, focusAreas);
4056 }
4057
4058 /**
4059 * Gets the maximum number of metering areas supported. This is the
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004060 * maximum length of the list in {@link #setMeteringAreas(List)} and
4061 * {@link #getMeteringAreas()}.
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004062 *
4063 * @return the maximum number of metering areas supported by the camera.
4064 * @see #getMeteringAreas()
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004065 */
4066 public int getMaxNumMeteringAreas() {
4067 return getInt(KEY_MAX_NUM_METERING_AREAS, 0);
4068 }
4069
4070 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004071 * <p>Gets the current metering areas. Camera driver uses these areas to
4072 * decide exposure.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004073 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004074 * <p>Before using this API or {@link #setMeteringAreas(List)}, apps should
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004075 * call {@link #getMaxNumMeteringAreas()} to know the maximum number of
4076 * metering areas first. If the value is 0, metering area is not
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004077 * supported.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004078 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004079 * <p>Each metering area is a rectangle with specified weight. The
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004080 * direction is relative to the sensor orientation, that is, what the
4081 * sensor sees. The direction is not affected by the rotation or
4082 * mirroring of {@link #setDisplayOrientation(int)}. Coordinates of the
4083 * rectangle range from -1000 to 1000. (-1000, -1000) is the upper left
Wu-cheng Libde61a52011-06-07 18:23:14 +08004084 * point. (1000, 1000) is the lower right point. The width and height of
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004085 * metering areas cannot be 0 or negative.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004086 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004087 * <p>The weight must range from 1 to 1000, and represents a weight for
Eino-Ville Talvala4e396e02011-04-21 09:23:15 -07004088 * every pixel in the area. This means that a large metering area with
4089 * the same weight as a smaller area will have more effect in the
4090 * metering result. Metering areas can partially overlap and the driver
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004091 * will add the weights in the overlap region.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004092 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004093 * <p>A special case of a {@code null} metering area list means the driver
4094 * is free to meter as it chooses. For example, the driver may use more
4095 * signals to select metering areas and change them dynamically. Apps
4096 * can set the metering area list to {@code null} if they want the
4097 * driver to completely control metering.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004098 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004099 * <p>Metering areas are relative to the current field of view
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004100 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000)
4101 * represents the top of the currently visible camera frame. The
4102 * metering area cannot be set to be outside the current field of view,
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004103 * even when using zoom.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004104 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004105 * <p>No matter what metering areas are, the final exposure are compensated
4106 * by {@link #setExposureCompensation(int)}.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004107 *
4108 * @return a list of current metering areas
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004109 */
4110 public List<Area> getMeteringAreas() {
Wu-cheng Lid8aab932011-06-21 11:50:43 +08004111 return splitArea(get(KEY_METERING_AREAS));
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004112 }
4113
4114 /**
4115 * Sets metering areas. See {@link #getMeteringAreas()} for
4116 * documentation.
4117 *
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004118 * @param meteringAreas the metering areas
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004119 * @see #getMeteringAreas()
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004120 */
4121 public void setMeteringAreas(List<Area> meteringAreas) {
4122 set(KEY_METERING_AREAS, meteringAreas);
Wu-cheng Li30771b72011-04-02 06:19:46 +08004123 }
4124
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08004125 /**
4126 * Gets the maximum number of detected faces supported. This is the
4127 * maximum length of the list returned from {@link FaceDetectionListener}.
4128 * If the return value is 0, face detection of the specified type is not
4129 * supported.
4130 *
4131 * @return the maximum number of detected face supported by the camera.
Joe Fernandez464cb212011-10-04 16:56:47 -07004132 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08004133 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08004134 public int getMaxNumDetectedFaces() {
4135 return getInt(KEY_MAX_NUM_DETECTED_FACES_HW, 0);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08004136 }
4137
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004138 /**
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08004139 * Sets recording mode hint. This tells the camera that the intent of
4140 * the application is to record videos {@link
4141 * android.media.MediaRecorder#start()}, not to take still pictures
4142 * {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
4143 * Camera.PictureCallback, Camera.PictureCallback)}. Using this hint can
4144 * allow MediaRecorder.start() to start faster or with fewer glitches on
4145 * output. This should be called before starting preview for the best
4146 * result, but can be changed while the preview is active. The default
4147 * value is false.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004148 *
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08004149 * The app can still call takePicture() when the hint is true or call
4150 * MediaRecorder.start() when the hint is false. But the performance may
4151 * be worse.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004152 *
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08004153 * @param hint true if the apps intend to record videos using
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004154 * {@link android.media.MediaRecorder}.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004155 */
4156 public void setRecordingHint(boolean hint) {
4157 set(KEY_RECORDING_HINT, hint ? TRUE : FALSE);
4158 }
4159
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004160 /**
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004161 * <p>Returns true if video snapshot is supported. That is, applications
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004162 * can call {@link #takePicture(Camera.ShutterCallback,
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004163 * Camera.PictureCallback, Camera.PictureCallback,
4164 * Camera.PictureCallback)} during recording. Applications do not need
4165 * to call {@link #startPreview()} after taking a picture. The preview
4166 * will be still active. Other than that, taking a picture during
4167 * recording is identical to taking a picture normally. All settings and
4168 * methods related to takePicture work identically. Ex:
4169 * {@link #getPictureSize()}, {@link #getSupportedPictureSizes()},
4170 * {@link #setJpegQuality(int)}, {@link #setRotation(int)}, and etc. The
4171 * picture will have an EXIF header. {@link #FLASH_MODE_AUTO} and
4172 * {@link #FLASH_MODE_ON} also still work, but the video will record the
4173 * flash.</p>
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004174 *
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004175 * <p>Applications can set shutter callback as null to avoid the shutter
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004176 * sound. It is also recommended to set raw picture and post view
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004177 * callbacks to null to avoid the interrupt of preview display.</p>
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004178 *
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004179 * <p>Field-of-view of the recorded video may be different from that of the
4180 * captured pictures. The maximum size of a video snapshot may be
4181 * smaller than that for regular still captures. If the current picture
4182 * size is set higher than can be supported by video snapshot, the
4183 * picture will be captured at the maximum supported size instead.</p>
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004184 *
4185 * @return true if video snapshot is supported.
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004186 */
4187 public boolean isVideoSnapshotSupported() {
4188 String str = get(KEY_VIDEO_SNAPSHOT_SUPPORTED);
4189 return TRUE.equals(str);
4190 }
4191
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004192 /**
4193 * <p>Enables and disables video stabilization. Use
4194 * {@link #isVideoStabilizationSupported} to determine if calling this
4195 * method is valid.</p>
4196 *
4197 * <p>Video stabilization reduces the shaking due to the motion of the
4198 * camera in both the preview stream and in recorded videos, including
4199 * data received from the preview callback. It does not reduce motion
4200 * blur in images captured with
4201 * {@link Camera#takePicture takePicture}.</p>
4202 *
4203 * <p>Video stabilization can be enabled and disabled while preview or
4204 * recording is active, but toggling it may cause a jump in the video
4205 * stream that may be undesirable in a recorded video.</p>
4206 *
4207 * @param toggle Set to true to enable video stabilization, and false to
4208 * disable video stabilization.
4209 * @see #isVideoStabilizationSupported()
4210 * @see #getVideoStabilization()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004211 */
4212 public void setVideoStabilization(boolean toggle) {
4213 set(KEY_VIDEO_STABILIZATION, toggle ? TRUE : FALSE);
4214 }
4215
4216 /**
4217 * Get the current state of video stabilization. See
4218 * {@link #setVideoStabilization} for details of video stabilization.
4219 *
4220 * @return true if video stabilization is enabled
4221 * @see #isVideoStabilizationSupported()
4222 * @see #setVideoStabilization(boolean)
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004223 */
4224 public boolean getVideoStabilization() {
4225 String str = get(KEY_VIDEO_STABILIZATION);
4226 return TRUE.equals(str);
4227 }
4228
4229 /**
4230 * Returns true if video stabilization is supported. See
4231 * {@link #setVideoStabilization} for details of video stabilization.
4232 *
4233 * @return true if video stabilization is supported
4234 * @see #setVideoStabilization(boolean)
4235 * @see #getVideoStabilization()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004236 */
4237 public boolean isVideoStabilizationSupported() {
4238 String str = get(KEY_VIDEO_STABILIZATION_SUPPORTED);
4239 return TRUE.equals(str);
4240 }
4241
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004242 // Splits a comma delimited string to an ArrayList of String.
4243 // Return null if the passing string is null or the size is 0.
4244 private ArrayList<String> split(String str) {
4245 if (str == null) return null;
4246
Ali Utku Selen0a120182011-02-09 14:11:22 +01004247 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4248 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004249 ArrayList<String> substrings = new ArrayList<String>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01004250 for (String s : splitter) {
4251 substrings.add(s);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004252 }
4253 return substrings;
4254 }
4255
4256 // Splits a comma delimited string to an ArrayList of Integer.
4257 // Return null if the passing string is null or the size is 0.
4258 private ArrayList<Integer> splitInt(String str) {
4259 if (str == null) return null;
4260
Ali Utku Selen0a120182011-02-09 14:11:22 +01004261 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4262 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004263 ArrayList<Integer> substrings = new ArrayList<Integer>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01004264 for (String s : splitter) {
4265 substrings.add(Integer.parseInt(s));
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004266 }
4267 if (substrings.size() == 0) return null;
4268 return substrings;
4269 }
4270
Wu-cheng Li454630f2010-08-11 16:48:05 -07004271 private void splitInt(String str, int[] output) {
4272 if (str == null) return;
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004273
Ali Utku Selen0a120182011-02-09 14:11:22 +01004274 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4275 splitter.setString(str);
Wu-cheng Li454630f2010-08-11 16:48:05 -07004276 int index = 0;
Ali Utku Selen0a120182011-02-09 14:11:22 +01004277 for (String s : splitter) {
4278 output[index++] = Integer.parseInt(s);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004279 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07004280 }
4281
4282 // Splits a comma delimited string to an ArrayList of Float.
4283 private void splitFloat(String str, float[] output) {
4284 if (str == null) return;
4285
Ali Utku Selen0a120182011-02-09 14:11:22 +01004286 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4287 splitter.setString(str);
Wu-cheng Li454630f2010-08-11 16:48:05 -07004288 int index = 0;
Ali Utku Selen0a120182011-02-09 14:11:22 +01004289 for (String s : splitter) {
4290 output[index++] = Float.parseFloat(s);
Wu-cheng Li454630f2010-08-11 16:48:05 -07004291 }
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004292 }
4293
Wu-cheng Li24b326a2010-02-20 17:47:04 +08004294 // Returns the value of a float parameter.
4295 private float getFloat(String key, float defaultValue) {
4296 try {
4297 return Float.parseFloat(mMap.get(key));
4298 } catch (NumberFormatException ex) {
4299 return defaultValue;
4300 }
4301 }
4302
4303 // Returns the value of a integer parameter.
4304 private int getInt(String key, int defaultValue) {
4305 try {
4306 return Integer.parseInt(mMap.get(key));
4307 } catch (NumberFormatException ex) {
4308 return defaultValue;
4309 }
4310 }
4311
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004312 // Splits a comma delimited string to an ArrayList of Size.
4313 // Return null if the passing string is null or the size is 0.
4314 private ArrayList<Size> splitSize(String str) {
4315 if (str == null) return null;
4316
Ali Utku Selen0a120182011-02-09 14:11:22 +01004317 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4318 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004319 ArrayList<Size> sizeList = new ArrayList<Size>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01004320 for (String s : splitter) {
4321 Size size = strToSize(s);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004322 if (size != null) sizeList.add(size);
4323 }
4324 if (sizeList.size() == 0) return null;
4325 return sizeList;
4326 }
4327
4328 // Parses a string (ex: "480x320") to Size object.
4329 // Return null if the passing string is null.
4330 private Size strToSize(String str) {
4331 if (str == null) return null;
4332
4333 int pos = str.indexOf('x');
4334 if (pos != -1) {
4335 String width = str.substring(0, pos);
4336 String height = str.substring(pos + 1);
4337 return new Size(Integer.parseInt(width),
4338 Integer.parseInt(height));
4339 }
4340 Log.e(TAG, "Invalid size parameter string=" + str);
4341 return null;
4342 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07004343
4344 // Splits a comma delimited string to an ArrayList of int array.
4345 // Example string: "(10000,26623),(10000,30000)". Return null if the
4346 // passing string is null or the size is 0.
4347 private ArrayList<int[]> splitRange(String str) {
4348 if (str == null || str.charAt(0) != '('
4349 || str.charAt(str.length() - 1) != ')') {
4350 Log.e(TAG, "Invalid range list string=" + str);
4351 return null;
4352 }
4353
4354 ArrayList<int[]> rangeList = new ArrayList<int[]>();
4355 int endIndex, fromIndex = 1;
4356 do {
4357 int[] range = new int[2];
4358 endIndex = str.indexOf("),(", fromIndex);
4359 if (endIndex == -1) endIndex = str.length() - 1;
4360 splitInt(str.substring(fromIndex, endIndex), range);
4361 rangeList.add(range);
4362 fromIndex = endIndex + 3;
4363 } while (endIndex != str.length() - 1);
4364
4365 if (rangeList.size() == 0) return null;
4366 return rangeList;
4367 }
Wu-cheng Li30771b72011-04-02 06:19:46 +08004368
4369 // Splits a comma delimited string to an ArrayList of Area objects.
4370 // Example string: "(-10,-10,0,0,300),(0,0,10,10,700)". Return null if
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004371 // the passing string is null or the size is 0 or (0,0,0,0,0).
Wu-cheng Li30771b72011-04-02 06:19:46 +08004372 private ArrayList<Area> splitArea(String str) {
4373 if (str == null || str.charAt(0) != '('
4374 || str.charAt(str.length() - 1) != ')') {
4375 Log.e(TAG, "Invalid area string=" + str);
4376 return null;
4377 }
4378
4379 ArrayList<Area> result = new ArrayList<Area>();
4380 int endIndex, fromIndex = 1;
4381 int[] array = new int[5];
4382 do {
4383 endIndex = str.indexOf("),(", fromIndex);
4384 if (endIndex == -1) endIndex = str.length() - 1;
4385 splitInt(str.substring(fromIndex, endIndex), array);
4386 Rect rect = new Rect(array[0], array[1], array[2], array[3]);
4387 result.add(new Area(rect, array[4]));
4388 fromIndex = endIndex + 3;
4389 } while (endIndex != str.length() - 1);
4390
4391 if (result.size() == 0) return null;
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004392
4393 if (result.size() == 1) {
Wu-cheng Libde61a52011-06-07 18:23:14 +08004394 Area area = result.get(0);
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004395 Rect rect = area.rect;
4396 if (rect.left == 0 && rect.top == 0 && rect.right == 0
4397 && rect.bottom == 0 && area.weight == 0) {
4398 return null;
4399 }
4400 }
4401
Wu-cheng Li30771b72011-04-02 06:19:46 +08004402 return result;
4403 }
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08004404
4405 private boolean same(String s1, String s2) {
4406 if (s1 == null && s2 == null) return true;
4407 if (s1 != null && s1.equals(s2)) return true;
4408 return false;
4409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 };
4411}