blob: ac863b2a45af00744bf3bfc547c654eda734b587 [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;
Mathew Inwoodbcbe4402018-08-08 15:42:59 +010024import android.annotation.UnsupportedAppUsage;
Eino-Ville Talvala06a77182017-09-15 12:43:24 -070025import android.app.ActivityThread;
jiabin503e1fd2018-04-19 16:39:44 -070026import android.app.AppOpsManager;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -070027import android.content.Context;
Mathias Agopiana696f5d2010-02-17 17:53:09 -080028import android.graphics.ImageFormat;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +080029import android.graphics.Point;
Wu-cheng Li30771b72011-04-02 06:19:46 +080030import android.graphics.Rect;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -080031import android.graphics.SurfaceTexture;
jiabin503e1fd2018-04-19 16:39:44 -070032import android.media.AudioAttributes;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -070033import android.media.IAudioService;
Mathew Inwood8c854f82018-09-14 12:35:36 +010034import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.Handler;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -070036import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Looper;
38import android.os.Message;
jiabin503e1fd2018-04-19 16:39:44 -070039import android.os.Process;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -070040import android.os.RemoteException;
41import android.os.ServiceManager;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -070042import android.renderscript.Allocation;
43import android.renderscript.Element;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -070044import android.renderscript.RSIllegalArgumentException;
Eino-Ville Talvala06a77182017-09-15 12:43:24 -070045import android.renderscript.RenderScript;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -070046import android.renderscript.Type;
Ali Utku Selen0a120182011-02-09 14:11:22 +010047import android.text.TextUtils;
Eino-Ville Talvala06a77182017-09-15 12:43:24 -070048import android.util.Log;
Wu-cheng Libde61a52011-06-07 18:23:14 +080049import android.view.Surface;
50import android.view.SurfaceHolder;
51
jiabin503e1fd2018-04-19 16:39:44 -070052import com.android.internal.annotations.GuardedBy;
53import com.android.internal.app.IAppOpsCallback;
54import com.android.internal.app.IAppOpsService;
55
Wu-cheng Libde61a52011-06-07 18:23:14 +080056import java.io.IOException;
57import java.lang.ref.WeakReference;
58import java.util.ArrayList;
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -070059import java.util.LinkedHashMap;
Wu-cheng Libde61a52011-06-07 18:23:14 +080060import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62/**
Dan Egnorbfcbeff2010-07-12 15:12:54 -070063 * The Camera class is used to set image capture settings, start/stop preview,
64 * snap pictures, and retrieve frames for encoding for video. This class is a
65 * client for the Camera service, which manages the actual camera hardware.
Scott Maindf4578e2009-09-10 12:22:07 -070066 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070067 * <p>To access the device camera, you must declare the
Wu-cheng Li7478ea62009-09-16 18:52:55 +080068 * {@link android.Manifest.permission#CAMERA} permission in your Android
Scott Maindf4578e2009-09-10 12:22:07 -070069 * Manifest. Also be sure to include the
70 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
Dan Egnorbfcbeff2010-07-12 15:12:54 -070071 * manifest element to declare camera features used by your application.
Wu-cheng Li7478ea62009-09-16 18:52:55 +080072 * For example, if you use the camera and auto-focus feature, your Manifest
Scott Maindf4578e2009-09-10 12:22:07 -070073 * should include the following:</p>
74 * <pre> &lt;uses-permission android:name="android.permission.CAMERA" />
75 * &lt;uses-feature android:name="android.hardware.camera" />
76 * &lt;uses-feature android:name="android.hardware.camera.autofocus" /></pre>
77 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070078 * <p>To take pictures with this class, use the following steps:</p>
79 *
80 * <ol>
Dan Egnor341ff132010-07-20 11:30:17 -070081 * <li>Obtain an instance of Camera from {@link #open(int)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -070082 *
83 * <li>Get existing (default) settings with {@link #getParameters()}.
84 *
85 * <li>If necessary, modify the returned {@link Camera.Parameters} object and call
86 * {@link #setParameters(Camera.Parameters)}.
87 *
Eino-Ville Talvala6c91e2c2016-03-25 11:54:39 -070088 * <li>Call {@link #setDisplayOrientation(int)} to ensure correct orientation of preview.
Dan Egnorbfcbeff2010-07-12 15:12:54 -070089 *
90 * <li><b>Important</b>: Pass a fully initialized {@link SurfaceHolder} to
91 * {@link #setPreviewDisplay(SurfaceHolder)}. Without a surface, the camera
92 * will be unable to start the preview.
93 *
94 * <li><b>Important</b>: Call {@link #startPreview()} to start updating the
95 * preview surface. Preview must be started before you can take a picture.
96 *
97 * <li>When you want, call {@link #takePicture(Camera.ShutterCallback,
98 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)} to
99 * capture a photo. Wait for the callbacks to provide the actual image data.
100 *
101 * <li>After taking a picture, preview display will have stopped. To take more
102 * photos, call {@link #startPreview()} again first.
103 *
104 * <li>Call {@link #stopPreview()} to stop updating the preview surface.
105 *
106 * <li><b>Important:</b> Call {@link #release()} to release the camera for
107 * use by other applications. Applications should release the camera
108 * immediately in {@link android.app.Activity#onPause()} (and re-{@link #open()}
109 * it in {@link android.app.Activity#onResume()}).
110 * </ol>
111 *
112 * <p>To quickly switch to video recording mode, use these steps:</p>
113 *
114 * <ol>
115 * <li>Obtain and initialize a Camera and start preview as described above.
116 *
117 * <li>Call {@link #unlock()} to allow the media process to access the camera.
118 *
119 * <li>Pass the camera to {@link android.media.MediaRecorder#setCamera(Camera)}.
120 * See {@link android.media.MediaRecorder} information about video recording.
121 *
122 * <li>When finished recording, call {@link #reconnect()} to re-acquire
123 * and re-lock the camera.
124 *
125 * <li>If desired, restart preview and take more photos or videos.
126 *
127 * <li>Call {@link #stopPreview()} and {@link #release()} as described above.
128 * </ol>
129 *
130 * <p>This class is not thread-safe, and is meant for use from one event thread.
131 * Most long-running operations (preview, focus, photo capture, etc) happen
132 * asynchronously and invoke callbacks as necessary. Callbacks will be invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700133 * on the event thread {@link #open(int)} was called from. This class's methods
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700134 * must never be called from multiple threads at once.</p>
135 *
Scott Maindf4578e2009-09-10 12:22:07 -0700136 * <p class="caution"><strong>Caution:</strong> Different Android-powered devices
137 * may have different hardware specifications, such as megapixel ratings and
138 * auto-focus capabilities. In order for your application to be compatible with
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800139 * more devices, you should not make assumptions about the device camera
Scott Maindf4578e2009-09-10 12:22:07 -0700140 * specifications.</p>
Joe Fernandez6c5c3c32011-10-18 14:57:05 -0700141 *
142 * <div class="special reference">
143 * <h3>Developer Guides</h3>
144 * <p>For more information about using cameras, read the
145 * <a href="{@docRoot}guide/topics/media/camera.html">Camera</a> developer guide.</p>
146 * </div>
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700147 *
148 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
149 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700151@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152public class Camera {
153 private static final String TAG = "Camera";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800154
Wu-cheng Lid2c29292010-05-28 17:32:41 +0800155 // These match the enums in frameworks/base/include/camera/Camera.h
Benny Wongda83f462009-08-12 12:01:27 -0500156 private static final int CAMERA_MSG_ERROR = 0x001;
157 private static final int CAMERA_MSG_SHUTTER = 0x002;
158 private static final int CAMERA_MSG_FOCUS = 0x004;
159 private static final int CAMERA_MSG_ZOOM = 0x008;
160 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010;
161 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020;
162 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040;
163 private static final int CAMERA_MSG_RAW_IMAGE = 0x080;
164 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800165 private static final int CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x200;
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800166 private static final int CAMERA_MSG_PREVIEW_METADATA = 0x400;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800167 private static final int CAMERA_MSG_FOCUS_MOVE = 0x800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
Mathew Inwood8c854f82018-09-14 12:35:36 +0100169 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Ashok Bhat4838e332014-01-03 14:37:19 +0000170 private long mNativeContext; // accessed by native methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 private EventHandler mEventHandler;
172 private ShutterCallback mShutterCallback;
173 private PictureCallback mRawImageCallback;
174 private PictureCallback mJpegCallback;
175 private PreviewCallback mPreviewCallback;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700176 private boolean mUsingPreviewAllocation;
Dave Sparkse8b26e12009-07-14 10:35:40 -0700177 private PictureCallback mPostviewCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 private AutoFocusCallback mAutoFocusCallback;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800179 private AutoFocusMoveCallback mAutoFocusMoveCallback;
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800180 private OnZoomChangeListener mZoomListener;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800181 private FaceDetectionListener mFaceListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private ErrorCallback mErrorCallback;
Yin-Chia Yeha07cd322018-05-31 12:11:42 -0700183 private ErrorCallback mDetailedErrorCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 private boolean mOneShot;
Andrew Harp94927df2009-10-20 01:47:05 -0400185 private boolean mWithBuffer;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800186 private boolean mFaceDetectionRunning = false;
Igor Murashkina1d66272014-06-20 11:22:11 -0700187 private final Object mAutoFocusCallbackLock = new Object();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800188
jiabin503e1fd2018-04-19 16:39:44 -0700189 private final Object mShutterSoundLock = new Object();
190 // for AppOps
191 private @Nullable IAppOpsService mAppOps;
192 private IAppOpsCallback mAppOpsCallback;
193 @GuardedBy("mShutterSoundLock")
194 private boolean mHasAppOpsPlayAudio = true;
195 @GuardedBy("mShutterSoundLock")
196 private boolean mShutterSoundEnabledFromApp = true;
197
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700198 private static final int NO_ERROR = 0;
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 /**
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800201 * Broadcast Action: A new picture is taken by the camera, and the entry of
202 * the picture has been added to the media store.
203 * {@link android.content.Intent#getData} is URI of the picture.
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800204 *
205 * <p>In {@link android.os.Build.VERSION_CODES#N Android N} this broadcast was removed, and
206 * applications are recommended to use
207 * {@link android.app.job.JobInfo.Builder JobInfo.Builder}.{@link android.app.job.JobInfo.Builder#addTriggerContentUri}
208 * instead.</p>
209 *
210 * <p>In {@link android.os.Build.VERSION_CODES#O Android O} this broadcast has been brought
211 * back, but only for <em>registered</em> receivers. Apps that are actively running can
Dianne Hackbornc1541952017-04-27 10:55:49 -0700212 * again listen to the broadcast if they want an immediate clear signal about a picture
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800213 * being taken, however anything doing heavy work (or needing to be launched) as a result of
214 * this should still use JobScheduler.</p>
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800215 */
216 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
217 public static final String ACTION_NEW_PICTURE = "android.hardware.action.NEW_PICTURE";
218
219 /**
220 * Broadcast Action: A new video is recorded by the camera, and the entry
221 * of the video has been added to the media store.
222 * {@link android.content.Intent#getData} is URI of the video.
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800223 *
224 * <p>In {@link android.os.Build.VERSION_CODES#N Android N} this broadcast was removed, and
225 * applications are recommended to use
226 * {@link android.app.job.JobInfo.Builder JobInfo.Builder}.{@link android.app.job.JobInfo.Builder#addTriggerContentUri}
227 * instead.</p>
228 *
229 * <p>In {@link android.os.Build.VERSION_CODES#O Android O} this broadcast has been brought
230 * back, but only for <em>registered</em> receivers. Apps that are actively running can
Dianne Hackbornc1541952017-04-27 10:55:49 -0700231 * again listen to the broadcast if they want an immediate clear signal about a video
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800232 * being taken, however anything doing heavy work (or needing to be launched) as a result of
233 * this should still use JobScheduler.</p>
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800234 */
235 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
236 public static final String ACTION_NEW_VIDEO = "android.hardware.action.NEW_VIDEO";
237
238 /**
Zhijun He4c913802014-06-16 16:42:35 -0700239 * Camera HAL device API version 1.0
240 * @hide
241 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100242 @UnsupportedAppUsage
Zhijun He4c913802014-06-16 16:42:35 -0700243 public static final int CAMERA_HAL_API_VERSION_1_0 = 0x100;
244
245 /**
246 * A constant meaning the normal camera connect/open will be used.
Zhijun He4c913802014-06-16 16:42:35 -0700247 */
Igor Murashkina1d66272014-06-20 11:22:11 -0700248 private static final int CAMERA_HAL_API_VERSION_NORMAL_CONNECT = -2;
Zhijun He4c913802014-06-16 16:42:35 -0700249
250 /**
251 * Used to indicate HAL version un-specified.
252 */
253 private static final int CAMERA_HAL_API_VERSION_UNSPECIFIED = -1;
Igor Murashkina1d66272014-06-20 11:22:11 -0700254
Zhijun He4c913802014-06-16 16:42:35 -0700255 /**
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800256 * Hardware face detection. It does not use much 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_HW = 0;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800259
260 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800261 * Software face detection. It uses some CPU.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800262 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800263 private static final int CAMERA_FACE_DETECTION_SW = 1;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800264
265 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700266 * Returns the number of physical cameras available on this device.
Yin-Chia Yeh564ae822018-02-08 16:24:27 -0800267 * The return value of this method might change dynamically if the device
268 * supports external cameras and an external camera is connected or
269 * disconnected.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700270 *
Shuzhen Wange7d3cd02018-05-31 11:41:39 -0700271 * If there is a
272 * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA
273 * logical multi-camera} in the system, to maintain app backward compatibility, this method will
274 * only expose one camera for every logical camera and underlying physical cameras group.
275 * Use camera2 API to see all cameras.
276 *
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700277 * @return total number of accessible camera devices, or 0 if there are no
278 * cameras or an error was encountered enumerating them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 */
Chih-Chung Change25cc652010-05-06 16:36:58 +0800280 public native static int getNumberOfCameras();
281
282 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700283 * Returns the information about a particular camera.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800284 * If {@link #getNumberOfCameras()} returns N, the valid id is 0 to N-1.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700285 *
286 * @throws RuntimeException if an invalid ID is provided, or if there is an
287 * error retrieving the information (generally due to a hardware or other
288 * low-level failure).
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800289 */
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -0700290 public static void getCameraInfo(int cameraId, CameraInfo cameraInfo) {
291 _getCameraInfo(cameraId, cameraInfo);
292 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
293 IAudioService audioService = IAudioService.Stub.asInterface(b);
294 try {
295 if (audioService.isCameraSoundForced()) {
296 // Only set this when sound is forced; otherwise let native code
297 // decide.
298 cameraInfo.canDisableShutterSound = false;
299 }
300 } catch (RemoteException e) {
301 Log.e(TAG, "Audio service is unavailable for queries");
302 }
303 }
304 private native static void _getCameraInfo(int cameraId, CameraInfo cameraInfo);
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800305
306 /**
307 * Information about a camera
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700308 *
309 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
310 * applications.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800311 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700312 @Deprecated
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800313 public static class CameraInfo {
Wu-cheng Li78366602010-09-15 14:08:15 -0700314 /**
315 * The facing of the camera is opposite to that of the screen.
316 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800317 public static final int CAMERA_FACING_BACK = 0;
Wu-cheng Li78366602010-09-15 14:08:15 -0700318
319 /**
320 * The facing of the camera is the same as that of the screen.
321 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800322 public static final int CAMERA_FACING_FRONT = 1;
323
324 /**
Joe Fernandez464cb212011-10-04 16:56:47 -0700325 * The direction that the camera faces. It should be
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800326 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
327 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700328 public int facing;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800329
330 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700331 * <p>The orientation of the camera image. The value is the angle that the
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800332 * camera image needs to be rotated clockwise so it shows correctly on
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700333 * the display in its natural orientation. It should be 0, 90, 180, or 270.</p>
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800334 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700335 * <p>For example, suppose a device has a naturally tall screen. The
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +0800336 * back-facing camera sensor is mounted in landscape. You are looking at
337 * the screen. If the top side of the camera sensor is aligned with the
338 * right edge of the screen in natural orientation, the value should be
339 * 90. If the top side of a front-facing camera sensor is aligned with
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700340 * the right of the screen, the value should be 270.</p>
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800341 *
342 * @see #setDisplayOrientation(int)
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800343 * @see Parameters#setRotation(int)
344 * @see Parameters#setPreviewSize(int, int)
345 * @see Parameters#setPictureSize(int, int)
346 * @see Parameters#setJpegThumbnailSize(int, int)
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800347 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700348 public int orientation;
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -0700349
350 /**
351 * <p>Whether the shutter sound can be disabled.</p>
352 *
353 * <p>On some devices, the camera shutter sound cannot be turned off
354 * through {@link #enableShutterSound enableShutterSound}. This field
355 * can be used to determine whether a call to disable the shutter sound
356 * will succeed.</p>
357 *
358 * <p>If this field is set to true, then a call of
359 * {@code enableShutterSound(false)} will be successful. If set to
360 * false, then that call will fail, and the shutter sound will be played
361 * when {@link Camera#takePicture takePicture} is called.</p>
362 */
363 public boolean canDisableShutterSound;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800364 };
365
366 /**
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800367 * Creates a new Camera object to access a particular hardware camera. If
368 * the same camera is opened by other applications, this will throw a
369 * RuntimeException.
370 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700371 * <p>You must call {@link #release()} when you are done using the camera,
372 * otherwise it will remain locked and be unavailable to other applications.
373 *
Dan Egnor341ff132010-07-20 11:30:17 -0700374 * <p>Your application should only have one Camera object active at a time
375 * for a particular hardware camera.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700376 *
377 * <p>Callbacks from other methods are delivered to the event loop of the
378 * thread which called open(). If this thread has no event loop, then
379 * callbacks are delivered to the main application event loop. If there
380 * is no main application event loop, callbacks are not delivered.
381 *
382 * <p class="caution"><b>Caution:</b> On some devices, this method may
383 * take a long time to complete. It is best to call this method from a
384 * worker thread (possibly using {@link android.os.AsyncTask}) to avoid
385 * blocking the main application UI thread.
386 *
Dan Egnor341ff132010-07-20 11:30:17 -0700387 * @param cameraId the hardware camera to access, between 0 and
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800388 * {@link #getNumberOfCameras()}-1.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700389 * @return a new Camera object, connected, locked and ready for use.
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800390 * @throws RuntimeException if opening the camera fails (for example, if the
391 * camera is in use by another process or device policy manager has
392 * disabled the camera).
Wu-cheng Lifacc8ce2011-06-17 13:09:40 +0800393 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800394 */
395 public static Camera open(int cameraId) {
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800396 return new Camera(cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 }
398
Chih-Chung Change25cc652010-05-06 16:36:58 +0800399 /**
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800400 * Creates a new Camera object to access the first back-facing camera on the
401 * device. If the device does not have a back-facing camera, this returns
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700402 * null. Otherwise acts like the {@link #open(int)} call.
403 *
404 * @return a new Camera object for the first back-facing camera, or null if there is no
405 * backfacing camera
Wu-cheng Li78366602010-09-15 14:08:15 -0700406 * @see #open(int)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800407 */
408 public static Camera open() {
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800409 int numberOfCameras = getNumberOfCameras();
410 CameraInfo cameraInfo = new CameraInfo();
411 for (int i = 0; i < numberOfCameras; i++) {
412 getCameraInfo(i, cameraInfo);
413 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800414 return new Camera(i);
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800415 }
416 }
417 return null;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800418 }
419
Zhijun He4c913802014-06-16 16:42:35 -0700420 /**
421 * Creates a new Camera object to access a particular hardware camera with
422 * given hal API version. If the same camera is opened by other applications
423 * or the hal API version is not supported by this device, this will throw a
424 * RuntimeException.
425 * <p>
426 * You must call {@link #release()} when you are done using the camera,
427 * otherwise it will remain locked and be unavailable to other applications.
428 * <p>
429 * Your application should only have one Camera object active at a time for
430 * a particular hardware camera.
431 * <p>
432 * Callbacks from other methods are delivered to the event loop of the
433 * thread which called open(). If this thread has no event loop, then
434 * callbacks are delivered to the main application event loop. If there is
435 * no main application event loop, callbacks are not delivered.
436 * <p class="caution">
437 * <b>Caution:</b> On some devices, this method may take a long time to
438 * complete. It is best to call this method from a worker thread (possibly
439 * using {@link android.os.AsyncTask}) to avoid blocking the main
440 * application UI thread.
441 *
442 * @param cameraId The hardware camera to access, between 0 and
443 * {@link #getNumberOfCameras()}-1.
Igor Murashkina1d66272014-06-20 11:22:11 -0700444 * @param halVersion The HAL API version this camera device to be opened as.
Zhijun He4c913802014-06-16 16:42:35 -0700445 * @return a new Camera object, connected, locked and ready for use.
Igor Murashkina1d66272014-06-20 11:22:11 -0700446 *
447 * @throws IllegalArgumentException if the {@code halVersion} is invalid
448 *
Zhijun He4c913802014-06-16 16:42:35 -0700449 * @throws RuntimeException if opening the camera fails (for example, if the
450 * camera is in use by another process or device policy manager has disabled
451 * the camera).
Igor Murashkina1d66272014-06-20 11:22:11 -0700452 *
Zhijun He4c913802014-06-16 16:42:35 -0700453 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName)
Igor Murashkina1d66272014-06-20 11:22:11 -0700454 * @see #CAMERA_HAL_API_VERSION_1_0
Zhijun He4c913802014-06-16 16:42:35 -0700455 *
456 * @hide
457 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100458 @UnsupportedAppUsage
Zhijun He4c913802014-06-16 16:42:35 -0700459 public static Camera openLegacy(int cameraId, int halVersion) {
Igor Murashkina1d66272014-06-20 11:22:11 -0700460 if (halVersion < CAMERA_HAL_API_VERSION_1_0) {
461 throw new IllegalArgumentException("Invalid HAL version " + halVersion);
462 }
463
Zhijun He4c913802014-06-16 16:42:35 -0700464 return new Camera(cameraId, halVersion);
465 }
466
467 /**
468 * Create a legacy camera object.
469 *
470 * @param cameraId The hardware camera to access, between 0 and
471 * {@link #getNumberOfCameras()}-1.
472 * @param halVersion The HAL API version this camera device to be opened as.
473 */
474 private Camera(int cameraId, int halVersion) {
Igor Murashkina1d66272014-06-20 11:22:11 -0700475 int err = cameraInitVersion(cameraId, halVersion);
Zhijun He4c913802014-06-16 16:42:35 -0700476 if (checkInitErrors(err)) {
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200477 if (err == -EACCES) {
478 throw new RuntimeException("Fail to connect to camera service");
479 } else if (err == -ENODEV) {
480 throw new RuntimeException("Camera initialization failed");
481 } else if (err == -ENOSYS) {
482 throw new RuntimeException("Camera initialization failed because some methods"
483 + " are not implemented");
484 } else if (err == -EOPNOTSUPP) {
485 throw new RuntimeException("Camera initialization failed because the hal"
486 + " version is not supported by this device");
487 } else if (err == -EINVAL) {
488 throw new RuntimeException("Camera initialization failed because the input"
489 + " arugments are invalid");
490 } else if (err == -EBUSY) {
491 throw new RuntimeException("Camera initialization failed because the camera"
492 + " device was already opened");
493 } else if (err == -EUSERS) {
494 throw new RuntimeException("Camera initialization failed because the max"
495 + " number of camera devices were already opened");
Zhijun He4c913802014-06-16 16:42:35 -0700496 }
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200497 // Should never hit this.
498 throw new RuntimeException("Unknown camera error");
Zhijun He4c913802014-06-16 16:42:35 -0700499 }
500 }
501
Igor Murashkina1d66272014-06-20 11:22:11 -0700502 private int cameraInitVersion(int cameraId, int halVersion) {
Zhijun He4c913802014-06-16 16:42:35 -0700503 mShutterCallback = null;
504 mRawImageCallback = null;
505 mJpegCallback = null;
506 mPreviewCallback = null;
507 mPostviewCallback = null;
508 mUsingPreviewAllocation = false;
509 mZoomListener = null;
510
511 Looper looper;
512 if ((looper = Looper.myLooper()) != null) {
513 mEventHandler = new EventHandler(this, looper);
514 } else if ((looper = Looper.getMainLooper()) != null) {
515 mEventHandler = new EventHandler(this, looper);
516 } else {
517 mEventHandler = null;
518 }
519
Svetoslavfbf0eca2015-05-01 16:52:41 -0700520 return native_setup(new WeakReference<Camera>(this), cameraId, halVersion,
521 ActivityThread.currentOpPackageName());
Zhijun He4c913802014-06-16 16:42:35 -0700522 }
523
Igor Murashkina1d66272014-06-20 11:22:11 -0700524 private int cameraInitNormal(int cameraId) {
525 return cameraInitVersion(cameraId, CAMERA_HAL_API_VERSION_NORMAL_CONNECT);
526 }
527
528 /**
529 * Connect to the camera service using #connectLegacy
530 *
531 * <p>
532 * This acts the same as normal except that it will return
533 * the detailed error code if open fails instead of
534 * converting everything into {@code NO_INIT}.</p>
535 *
536 * <p>Intended to use by the camera2 shim only, do <i>not</i> use this for other code.</p>
537 *
538 * @return a detailed errno error code, or {@code NO_ERROR} on success
539 *
540 * @hide
541 */
542 public int cameraInitUnspecified(int cameraId) {
543 return cameraInitVersion(cameraId, CAMERA_HAL_API_VERSION_UNSPECIFIED);
544 }
545
546 /** used by Camera#open, Camera#open(int) */
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800547 Camera(int cameraId) {
Igor Murashkina1d66272014-06-20 11:22:11 -0700548 int err = cameraInitNormal(cameraId);
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700549 if (checkInitErrors(err)) {
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200550 if (err == -EACCES) {
551 throw new RuntimeException("Fail to connect to camera service");
552 } else if (err == -ENODEV) {
553 throw new RuntimeException("Camera initialization failed");
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700554 }
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200555 // Should never hit this.
556 throw new RuntimeException("Unknown camera error");
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700557 }
jiabin503e1fd2018-04-19 16:39:44 -0700558 initAppOps();
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700559 }
560
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700561
562 /**
563 * @hide
564 */
565 public static boolean checkInitErrors(int err) {
566 return err != NO_ERROR;
567 }
568
569 /**
570 * @hide
571 */
572 public static Camera openUninitialized() {
573 return new Camera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800575
Wu-cheng Li1c04a332011-11-22 18:21:18 +0800576 /**
577 * An empty Camera for testing purpose.
578 */
Daichi Hironocb63c642018-06-25 11:16:41 +0900579 Camera() {}
jiabin503e1fd2018-04-19 16:39:44 -0700580
581 private void initAppOps() {
582 IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE);
583 mAppOps = IAppOpsService.Stub.asInterface(b);
584 // initialize mHasAppOpsPlayAudio
585 updateAppOpsPlayAudio();
586 // register a callback to monitor whether the OP_PLAY_AUDIO is still allowed
587 mAppOpsCallback = new IAppOpsCallbackWrapper(this);
588 try {
589 mAppOps.startWatchingMode(AppOpsManager.OP_PLAY_AUDIO,
590 ActivityThread.currentPackageName(), mAppOpsCallback);
591 } catch (RemoteException e) {
592 Log.e(TAG, "Error registering appOps callback", e);
593 mHasAppOpsPlayAudio = false;
594 }
595 }
596
597 private void releaseAppOps() {
598 try {
599 if (mAppOps != null) {
600 mAppOps.stopWatchingMode(mAppOpsCallback);
601 }
602 } catch (Exception e) {
603 // nothing to do here, the object is supposed to be released anyway
604 }
Wu-cheng Li1c04a332011-11-22 18:21:18 +0800605 }
606
Igor Murashkina1d66272014-06-20 11:22:11 -0700607 @Override
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800608 protected void finalize() {
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -0800609 release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800611
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100612 @UnsupportedAppUsage
Zhijun He4c913802014-06-16 16:42:35 -0700613 private native final int native_setup(Object camera_this, int cameraId, int halVersion,
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800614 String packageName);
615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 private native final void native_release();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618
619 /**
620 * Disconnects and releases the Camera object resources.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700621 *
622 * <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 -0800623 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800624 public final void release() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 native_release();
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800626 mFaceDetectionRunning = false;
jiabin503e1fd2018-04-19 16:39:44 -0700627 releaseAppOps();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
629
630 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700631 * Unlocks the camera to allow another process to access it.
632 * Normally, the camera is locked to the process with an active Camera
633 * object until {@link #release()} is called. To allow rapid handoff
634 * between processes, you can call this method to release the camera
635 * temporarily for another process to use; once the other process is done
636 * you can call {@link #reconnect()} to reclaim the camera.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700638 * <p>This must be done before calling
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800639 * {@link android.media.MediaRecorder#setCamera(Camera)}. This cannot be
640 * called after recording starts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700642 * <p>If you are not recording video, you probably do not need this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700644 * @throws RuntimeException if the camera cannot be unlocked.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 */
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800646 public native final void unlock();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700649 * Re-locks the camera to prevent other processes from accessing it.
650 * Camera objects are locked by default unless {@link #unlock()} is
651 * called. Normally {@link #reconnect()} is used instead.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800652 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800653 * <p>Since API level 14, camera is automatically locked for applications in
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800654 * {@link android.media.MediaRecorder#start()}. Applications can use the
655 * camera (ex: zoom) after recording starts. There is no need to call this
656 * after recording starts or stops.
657 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700658 * <p>If you are not recording video, you probably do not need this method.
659 *
660 * @throws RuntimeException if the camera cannot be re-locked (for
661 * example, if the camera is still in use by another process).
662 */
663 public native final void lock();
664
665 /**
666 * Reconnects to the camera service after another process used it.
667 * After {@link #unlock()} is called, another process may use the
668 * camera; when the process is done, you must reconnect to the camera,
669 * which will re-acquire the lock and allow you to continue using the
670 * camera.
671 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800672 * <p>Since API level 14, camera is automatically locked for applications in
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800673 * {@link android.media.MediaRecorder#start()}. Applications can use the
674 * camera (ex: zoom) after recording starts. There is no need to call this
675 * after recording starts or stops.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700676 *
677 * <p>If you are not recording video, you probably do not need this method.
678 *
679 * @throws IOException if a connection cannot be re-established (for
680 * example, if the camera is still in use by another process).
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700681 * @throws RuntimeException if release() has been called on this Camera
682 * instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700683 */
684 public native final void reconnect() throws IOException;
685
686 /**
687 * Sets the {@link Surface} to be used for live preview.
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800688 * Either a surface or surface texture is necessary for preview, and
689 * preview is necessary to take pictures. The same surface can be re-set
690 * without harm. Setting a preview surface will un-set any preview surface
691 * texture that was set via {@link #setPreviewTexture}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700692 *
693 * <p>The {@link SurfaceHolder} must already contain a surface when this
694 * method is called. If you are using {@link android.view.SurfaceView},
695 * you will need to register a {@link SurfaceHolder.Callback} with
696 * {@link SurfaceHolder#addCallback(SurfaceHolder.Callback)} and wait for
697 * {@link SurfaceHolder.Callback#surfaceCreated(SurfaceHolder)} before
698 * calling setPreviewDisplay() or starting preview.
699 *
700 * <p>This method must be called before {@link #startPreview()}. The
701 * one exception is that if the preview surface is not set (or set to null)
702 * before startPreview() is called, then this method may be called once
703 * with a non-null parameter to set the preview surface. (This allows
704 * camera setup and surface creation to happen in parallel, saving time.)
705 * The preview surface may not otherwise change while preview is running.
706 *
707 * @param holder containing the Surface on which to place the preview,
708 * or null to remove the preview surface
709 * @throws IOException if the method fails (for example, if the surface
710 * is unavailable or unsuitable).
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700711 * @throws RuntimeException if release() has been called on this Camera
712 * instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException {
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800715 if (holder != null) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700716 setPreviewSurface(holder.getSurface());
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800717 } else {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700718 setPreviewSurface((Surface)null);
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 }
721
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700722 /**
723 * @hide
724 */
Mathew Inwood8c854f82018-09-14 12:35:36 +0100725 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700726 public native final void setPreviewSurface(Surface surface) throws IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727
728 /**
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800729 * Sets the {@link SurfaceTexture} to be used for live preview.
730 * Either a surface or surface texture is necessary for preview, and
731 * preview is necessary to take pictures. The same surface texture can be
732 * re-set without harm. Setting a preview surface texture will un-set any
733 * preview surface that was set via {@link #setPreviewDisplay}.
734 *
735 * <p>This method must be called before {@link #startPreview()}. The
736 * one exception is that if the preview surface texture is not set (or set
737 * to null) before startPreview() is called, then this method may be called
738 * once with a non-null parameter to set the preview surface. (This allows
739 * camera setup and surface creation to happen in parallel, saving time.)
740 * The preview surface texture may not otherwise change while preview is
741 * running.
742 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700743 * <p>The timestamps provided by {@link SurfaceTexture#getTimestamp()} for a
Eino-Ville Talvalae309a0f2011-03-21 11:04:34 -0700744 * SurfaceTexture set as the preview texture have an unspecified zero point,
745 * and cannot be directly compared between different cameras or different
746 * instances of the same camera, or across multiple runs of the same
747 * program.
748 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800749 * <p>If you are using the preview data to create video or still images,
750 * strongly consider using {@link android.media.MediaActionSound} to
751 * properly indicate image capture or recording start/stop to the user.</p>
752 *
753 * @see android.media.MediaActionSound
754 * @see android.graphics.SurfaceTexture
755 * @see android.view.TextureView
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800756 * @param surfaceTexture the {@link SurfaceTexture} to which the preview
757 * images are to be sent or null to remove the current preview surface
758 * texture
759 * @throws IOException if the method fails (for example, if the surface
760 * texture is unavailable or unsuitable).
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700761 * @throws RuntimeException if release() has been called on this Camera
762 * instance.
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800763 */
Glenn Kastendbc289d2011-02-09 10:15:44 -0800764 public native final void setPreviewTexture(SurfaceTexture surfaceTexture) throws IOException;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800765
766 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700767 * Callback interface used to deliver copies of preview frames as
768 * they are displayed.
769 *
770 * @see #setPreviewCallback(Camera.PreviewCallback)
771 * @see #setOneShotPreviewCallback(Camera.PreviewCallback)
772 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
773 * @see #startPreview()
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700774 *
775 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
776 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700778 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 public interface PreviewCallback
780 {
781 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700782 * Called as preview frames are displayed. This callback is invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700783 * on the event thread {@link #open(int)} was called from.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700785 * <p>If using the {@link android.graphics.ImageFormat#YV12} format,
786 * refer to the equations in {@link Camera.Parameters#setPreviewFormat}
787 * for the arrangement of the pixel data in the preview callback
788 * buffers.
789 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700790 * @param data the contents of the preview frame in the format defined
Mathias Agopiana696f5d2010-02-17 17:53:09 -0800791 * by {@link android.graphics.ImageFormat}, which can be queried
Scott Maindf4578e2009-09-10 12:22:07 -0700792 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
Scott Mainda0a56d2009-09-10 18:08:37 -0700793 * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800794 * is never called, the default will be the YCbCr_420_SP
795 * (NV21) format.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700796 * @param camera the Camera service object.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 */
798 void onPreviewFrame(byte[] data, Camera camera);
799 };
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700802 * Starts capturing and drawing preview frames to the screen.
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800803 * Preview will not actually start until a surface is supplied
804 * with {@link #setPreviewDisplay(SurfaceHolder)} or
805 * {@link #setPreviewTexture(SurfaceTexture)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700806 *
807 * <p>If {@link #setPreviewCallback(Camera.PreviewCallback)},
808 * {@link #setOneShotPreviewCallback(Camera.PreviewCallback)}, or
809 * {@link #setPreviewCallbackWithBuffer(Camera.PreviewCallback)} were
810 * called, {@link Camera.PreviewCallback#onPreviewFrame(byte[], Camera)}
811 * will be called when preview data becomes available.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700812 *
813 * @throws RuntimeException if starting preview fails; usually this would be
814 * because of a hardware or other low-level error, or because release()
815 * has been called on this Camera instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 */
817 public native final void startPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700820 * Stops capturing and drawing preview frames to the surface, and
821 * resets the camera for a future call to {@link #startPreview()}.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700822 *
823 * @throws RuntimeException if stopping preview fails; usually this would be
824 * because of a hardware or other low-level error, or because release()
825 * has been called on this Camera instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 */
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800827 public final void stopPreview() {
828 _stopPreview();
829 mFaceDetectionRunning = false;
Chih-yu Huang664d72e2011-09-23 18:59:38 +0800830
831 mShutterCallback = null;
832 mRawImageCallback = null;
833 mPostviewCallback = null;
834 mJpegCallback = null;
Wu-cheng Lif05c1d62012-05-02 23:29:47 +0800835 synchronized (mAutoFocusCallbackLock) {
836 mAutoFocusCallback = null;
837 }
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800838 mAutoFocusMoveCallback = null;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800839 }
840
841 private native final void _stopPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 /**
844 * Return current preview state.
845 *
846 * FIXME: Unhide before release
847 * @hide
848 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +0100849 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 public native final boolean previewEnabled();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800853 * <p>Installs a callback to be invoked for every preview frame in addition
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700854 * to displaying them on the screen. The callback will be repeatedly called
855 * for as long as preview is active. This method can be called at any time,
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800856 * even while preview is live. Any other preview callbacks are
857 * overridden.</p>
858 *
859 * <p>If you are using the preview data to create video or still images,
860 * strongly consider using {@link android.media.MediaActionSound} to
861 * properly indicate image capture or recording start/stop to the user.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700863 * @param cb a callback object that receives a copy of each preview frame,
864 * or null to stop receiving callbacks.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700865 * @throws RuntimeException if release() has been called on this Camera
866 * instance.
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800867 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 */
869 public final void setPreviewCallback(PreviewCallback cb) {
870 mPreviewCallback = cb;
871 mOneShot = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400872 mWithBuffer = false;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700873 if (cb != null) {
874 mUsingPreviewAllocation = false;
875 }
Dave Sparksa6118c62009-10-13 02:28:54 -0700876 // Always use one-shot mode. We fake camera preview mode by
877 // doing one-shot preview continuously.
Andrew Harp94927df2009-10-20 01:47:05 -0400878 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
880
881 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800882 * <p>Installs a callback to be invoked for the next preview frame in
883 * addition to displaying it on the screen. After one invocation, the
884 * callback is cleared. This method can be called any time, even when
885 * preview is live. Any other preview callbacks are overridden.</p>
886 *
887 * <p>If you are using the preview data to create video or still images,
888 * strongly consider using {@link android.media.MediaActionSound} to
889 * properly indicate image capture or recording start/stop to the user.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700891 * @param cb a callback object that receives a copy of the next preview frame,
892 * or null to stop receiving callbacks.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700893 * @throws RuntimeException if release() has been called on this Camera
894 * instance.
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800895 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 */
897 public final void setOneShotPreviewCallback(PreviewCallback cb) {
Andrew Harp94927df2009-10-20 01:47:05 -0400898 mPreviewCallback = cb;
899 mOneShot = true;
900 mWithBuffer = false;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700901 if (cb != null) {
902 mUsingPreviewAllocation = false;
903 }
Andrew Harp94927df2009-10-20 01:47:05 -0400904 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
906
Andrew Harp94927df2009-10-20 01:47:05 -0400907 private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer);
908
909 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800910 * <p>Installs a callback to be invoked for every preview frame, using
911 * buffers supplied with {@link #addCallbackBuffer(byte[])}, in addition to
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700912 * displaying them on the screen. The callback will be repeatedly called
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800913 * for as long as preview is active and buffers are available. Any other
914 * preview callbacks are overridden.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400915 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700916 * <p>The purpose of this method is to improve preview efficiency and frame
917 * rate by allowing preview frame memory reuse. You must call
918 * {@link #addCallbackBuffer(byte[])} at some point -- before or after
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800919 * calling this method -- or no callbacks will received.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400920 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800921 * <p>The buffer queue will be cleared if this method is called with a null
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700922 * callback, {@link #setPreviewCallback(Camera.PreviewCallback)} is called,
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800923 * or {@link #setOneShotPreviewCallback(Camera.PreviewCallback)} is
924 * called.</p>
925 *
926 * <p>If you are using the preview data to create video or still images,
927 * strongly consider using {@link android.media.MediaActionSound} to
928 * properly indicate image capture or recording start/stop to the user.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400929 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700930 * @param cb a callback object that receives a copy of the preview frame,
931 * or null to stop receiving callbacks and clear the buffer queue.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700932 * @throws RuntimeException if release() has been called on this Camera
933 * instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700934 * @see #addCallbackBuffer(byte[])
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800935 * @see android.media.MediaActionSound
Andrew Harp94927df2009-10-20 01:47:05 -0400936 */
937 public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
938 mPreviewCallback = cb;
939 mOneShot = false;
940 mWithBuffer = true;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700941 if (cb != null) {
942 mUsingPreviewAllocation = false;
943 }
Andrew Harp94927df2009-10-20 01:47:05 -0400944 setHasPreviewCallback(cb != null, true);
945 }
946
947 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800948 * Adds a pre-allocated buffer to the preview callback buffer queue.
949 * Applications can add one or more buffers to the queue. When a preview
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700950 * frame arrives and there is still at least one available buffer, the
951 * buffer will be used and removed from the queue. Then preview callback is
952 * invoked with the buffer. If a frame arrives and there is no buffer left,
953 * the frame is discarded. Applications should add buffers back when they
954 * finish processing the data in them.
Wu-cheng Lic10275a2010-03-09 13:49:21 -0800955 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700956 * <p>For formats besides YV12, the size of the buffer is determined by
957 * multiplying the preview image width, height, and bytes per pixel. The
958 * width and height can be read from
959 * {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel can be
960 * computed from {@link android.graphics.ImageFormat#getBitsPerPixel(int)} /
961 * 8, using the image format from
962 * {@link Camera.Parameters#getPreviewFormat()}.
963 *
964 * <p>If using the {@link android.graphics.ImageFormat#YV12} format, the
965 * size can be calculated using the equations listed in
966 * {@link Camera.Parameters#setPreviewFormat}.
Andrew Harp94927df2009-10-20 01:47:05 -0400967 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700968 * <p>This method is only necessary when
James Donge00cab72011-02-17 16:38:06 -0800969 * {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700970 * {@link #setPreviewCallback(PreviewCallback)} or
971 * {@link #setOneShotPreviewCallback(PreviewCallback)} are used, buffers
James Donge00cab72011-02-17 16:38:06 -0800972 * are automatically allocated. When a supplied buffer is too small to
973 * hold the preview frame data, preview callback will return null and
974 * the buffer will be removed from the buffer queue.
Andrew Harp94927df2009-10-20 01:47:05 -0400975 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700976 * @param callbackBuffer the buffer to add to the queue. The size of the
977 * buffer must match the values described above.
Wu-cheng Li5b9bcda2010-03-07 14:59:28 -0800978 * @see #setPreviewCallbackWithBuffer(PreviewCallback)
Andrew Harp94927df2009-10-20 01:47:05 -0400979 */
James Donge00cab72011-02-17 16:38:06 -0800980 public final void addCallbackBuffer(byte[] callbackBuffer)
981 {
982 _addCallbackBuffer(callbackBuffer, CAMERA_MSG_PREVIEW_FRAME);
983 }
984
985 /**
986 * Adds a pre-allocated buffer to the raw image callback buffer queue.
987 * Applications can add one or more buffers to the queue. When a raw image
988 * frame arrives and there is still at least one available buffer, the
989 * buffer will be used to hold the raw image data and removed from the
990 * queue. Then raw image callback is invoked with the buffer. If a raw
991 * image frame arrives but there is no buffer left, the frame is
992 * discarded. Applications should add buffers back when they finish
993 * processing the data in them by calling this method again in order
994 * to avoid running out of raw image callback buffers.
995 *
996 * <p>The size of the buffer is determined by multiplying the raw image
997 * width, height, and bytes per pixel. The width and height can be
998 * read from {@link Camera.Parameters#getPictureSize()}. Bytes per pixel
999 * can be computed from
1000 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
1001 * using the image format from {@link Camera.Parameters#getPreviewFormat()}.
1002 *
1003 * <p>This method is only necessary when the PictureCallbck for raw image
1004 * is used while calling {@link #takePicture(Camera.ShutterCallback,
1005 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
1006 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07001007 * <p>Please note that by calling this method, the mode for
1008 * application-managed callback buffers is triggered. If this method has
1009 * never been called, null will be returned by the raw image callback since
1010 * there is no image callback buffer available. Furthermore, When a supplied
1011 * buffer is too small to hold the raw image data, raw image callback will
1012 * return null and the buffer will be removed from the buffer queue.
James Donge00cab72011-02-17 16:38:06 -08001013 *
1014 * @param callbackBuffer the buffer to add to the raw image callback buffer
1015 * queue. The size should be width * height * (bits per pixel) / 8. An
1016 * null callbackBuffer will be ignored and won't be added to the queue.
1017 *
1018 * @see #takePicture(Camera.ShutterCallback,
1019 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
1020 *
1021 * {@hide}
1022 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +01001023 @UnsupportedAppUsage
James Donge00cab72011-02-17 16:38:06 -08001024 public final void addRawImageCallbackBuffer(byte[] callbackBuffer)
1025 {
1026 addCallbackBuffer(callbackBuffer, CAMERA_MSG_RAW_IMAGE);
1027 }
1028
Mathew Inwoodbcbe4402018-08-08 15:42:59 +01001029 @UnsupportedAppUsage
James Donge00cab72011-02-17 16:38:06 -08001030 private final void addCallbackBuffer(byte[] callbackBuffer, int msgType)
1031 {
1032 // CAMERA_MSG_VIDEO_FRAME may be allowed in the future.
1033 if (msgType != CAMERA_MSG_PREVIEW_FRAME &&
1034 msgType != CAMERA_MSG_RAW_IMAGE) {
1035 throw new IllegalArgumentException(
1036 "Unsupported message type: " + msgType);
1037 }
1038
1039 _addCallbackBuffer(callbackBuffer, msgType);
1040 }
1041
1042 private native final void _addCallbackBuffer(
1043 byte[] callbackBuffer, int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044
Eino-Ville Talvala7005b672013-04-02 15:46:38 -07001045 /**
1046 * <p>Create a {@link android.renderscript RenderScript}
1047 * {@link android.renderscript.Allocation Allocation} to use as a
1048 * destination of preview callback frames. Use
1049 * {@link #setPreviewCallbackAllocation setPreviewCallbackAllocation} to use
1050 * the created Allocation as a destination for camera preview frames.</p>
1051 *
1052 * <p>The Allocation will be created with a YUV type, and its contents must
1053 * be accessed within Renderscript with the {@code rsGetElementAtYuv_*}
1054 * accessor methods. Its size will be based on the current
1055 * {@link Parameters#getPreviewSize preview size} configured for this
1056 * camera.</p>
1057 *
1058 * @param rs the RenderScript context for this Allocation.
1059 * @param usage additional usage flags to set for the Allocation. The usage
1060 * flag {@link android.renderscript.Allocation#USAGE_IO_INPUT} will always
1061 * be set on the created Allocation, but additional flags may be provided
1062 * here.
1063 * @return a new YUV-type Allocation with dimensions equal to the current
1064 * preview size.
1065 * @throws RSIllegalArgumentException if the usage flags are not compatible
1066 * with an YUV Allocation.
1067 * @see #setPreviewCallbackAllocation
1068 * @hide
1069 */
1070 public final Allocation createPreviewAllocation(RenderScript rs, int usage)
1071 throws RSIllegalArgumentException {
1072 Parameters p = getParameters();
1073 Size previewSize = p.getPreviewSize();
1074 Type.Builder yuvBuilder = new Type.Builder(rs,
1075 Element.createPixel(rs,
1076 Element.DataType.UNSIGNED_8,
1077 Element.DataKind.PIXEL_YUV));
1078 // Use YV12 for wide compatibility. Changing this requires also
1079 // adjusting camera service's format selection.
1080 yuvBuilder.setYuvFormat(ImageFormat.YV12);
1081 yuvBuilder.setX(previewSize.width);
1082 yuvBuilder.setY(previewSize.height);
1083
1084 Allocation a = Allocation.createTyped(rs, yuvBuilder.create(),
1085 usage | Allocation.USAGE_IO_INPUT);
1086
1087 return a;
1088 }
1089
1090 /**
1091 * <p>Set an {@link android.renderscript.Allocation Allocation} as the
1092 * target of preview callback data. Use this method for efficient processing
1093 * of camera preview data with RenderScript. The Allocation must be created
1094 * with the {@link #createPreviewAllocation createPreviewAllocation }
1095 * method.</p>
1096 *
1097 * <p>Setting a preview allocation will disable any active preview callbacks
1098 * set by {@link #setPreviewCallback setPreviewCallback} or
1099 * {@link #setPreviewCallbackWithBuffer setPreviewCallbackWithBuffer}, and
1100 * vice versa. Using a preview allocation still requires an active standard
1101 * preview target to be set, either with
1102 * {@link #setPreviewTexture setPreviewTexture} or
1103 * {@link #setPreviewDisplay setPreviewDisplay}.</p>
1104 *
1105 * <p>To be notified when new frames are available to the Allocation, use
1106 * {@link android.renderscript.Allocation#setIoInputNotificationHandler Allocation.setIoInputNotificationHandler}. To
1107 * update the frame currently accessible from the Allocation to the latest
1108 * preview frame, call
1109 * {@link android.renderscript.Allocation#ioReceive Allocation.ioReceive}.</p>
1110 *
1111 * <p>To disable preview into the Allocation, call this method with a
1112 * {@code null} parameter.</p>
1113 *
1114 * <p>Once a preview allocation is set, the preview size set by
1115 * {@link Parameters#setPreviewSize setPreviewSize} cannot be changed. If
1116 * you wish to change the preview size, first remove the preview allocation
1117 * by calling {@code setPreviewCallbackAllocation(null)}, then change the
1118 * preview size, create a new preview Allocation with
1119 * {@link #createPreviewAllocation createPreviewAllocation}, and set it as
1120 * the new preview callback allocation target.</p>
1121 *
1122 * <p>If you are using the preview data to create video or still images,
1123 * strongly consider using {@link android.media.MediaActionSound} to
1124 * properly indicate image capture or recording start/stop to the user.</p>
1125 *
1126 * @param previewAllocation the allocation to use as destination for preview
1127 * @throws IOException if configuring the camera to use the Allocation for
1128 * preview fails.
1129 * @throws IllegalArgumentException if the Allocation's dimensions or other
1130 * parameters don't meet the requirements.
1131 * @see #createPreviewAllocation
1132 * @see #setPreviewCallback
1133 * @see #setPreviewCallbackWithBuffer
1134 * @hide
1135 */
1136 public final void setPreviewCallbackAllocation(Allocation previewAllocation)
1137 throws IOException {
1138 Surface previewSurface = null;
1139 if (previewAllocation != null) {
1140 Parameters p = getParameters();
1141 Size previewSize = p.getPreviewSize();
1142 if (previewSize.width != previewAllocation.getType().getX() ||
1143 previewSize.height != previewAllocation.getType().getY()) {
1144 throw new IllegalArgumentException(
1145 "Allocation dimensions don't match preview dimensions: " +
1146 "Allocation is " +
1147 previewAllocation.getType().getX() +
1148 ", " +
1149 previewAllocation.getType().getY() +
1150 ". Preview is " + previewSize.width + ", " +
1151 previewSize.height);
1152 }
1153 if ((previewAllocation.getUsage() &
1154 Allocation.USAGE_IO_INPUT) == 0) {
1155 throw new IllegalArgumentException(
1156 "Allocation usage does not include USAGE_IO_INPUT");
1157 }
1158 if (previewAllocation.getType().getElement().getDataKind() !=
1159 Element.DataKind.PIXEL_YUV) {
1160 throw new IllegalArgumentException(
1161 "Allocation is not of a YUV type");
1162 }
1163 previewSurface = previewAllocation.getSurface();
1164 mUsingPreviewAllocation = true;
1165 } else {
1166 mUsingPreviewAllocation = false;
1167 }
1168 setPreviewCallbackSurface(previewSurface);
1169 }
1170
1171 private native final void setPreviewCallbackSurface(Surface s);
1172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 private class EventHandler extends Handler
1174 {
Igor Murashkina1d66272014-06-20 11:22:11 -07001175 private final Camera mCamera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176
1177 public EventHandler(Camera c, Looper looper) {
1178 super(looper);
1179 mCamera = c;
1180 }
1181
1182 @Override
1183 public void handleMessage(Message msg) {
1184 switch(msg.what) {
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001185 case CAMERA_MSG_SHUTTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 if (mShutterCallback != null) {
1187 mShutterCallback.onShutter();
1188 }
1189 return;
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001190
1191 case CAMERA_MSG_RAW_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -07001192 if (mRawImageCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 return;
1196
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001197 case CAMERA_MSG_COMPRESSED_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -07001198 if (mJpegCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001202
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001203 case CAMERA_MSG_PREVIEW_FRAME:
Eino-Ville Talvalaca367b72012-05-30 16:01:33 -07001204 PreviewCallback pCb = mPreviewCallback;
1205 if (pCb != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 if (mOneShot) {
Dave Sparksa6118c62009-10-13 02:28:54 -07001207 // Clear the callback variable before the callback
1208 // in case the app calls setPreviewCallback from
1209 // the callback function
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 mPreviewCallback = null;
Andrew Harp94927df2009-10-20 01:47:05 -04001211 } else if (!mWithBuffer) {
Dave Sparksa6118c62009-10-13 02:28:54 -07001212 // We're faking the camera preview mode to prevent
1213 // the app from being flooded with preview frames.
1214 // Set to oneshot mode again.
Andrew Harp94927df2009-10-20 01:47:05 -04001215 setHasPreviewCallback(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 }
Eino-Ville Talvalaca367b72012-05-30 16:01:33 -07001217 pCb.onPreviewFrame((byte[])msg.obj, mCamera);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
1219 return;
1220
Dave Sparkse8b26e12009-07-14 10:35:40 -07001221 case CAMERA_MSG_POSTVIEW_FRAME:
1222 if (mPostviewCallback != null) {
1223 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera);
1224 }
1225 return;
1226
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001227 case CAMERA_MSG_FOCUS:
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001228 AutoFocusCallback cb = null;
1229 synchronized (mAutoFocusCallbackLock) {
1230 cb = mAutoFocusCallback;
1231 }
1232 if (cb != null) {
1233 boolean success = msg.arg1 == 0 ? false : true;
1234 cb.onAutoFocus(success, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001235 }
1236 return;
1237
1238 case CAMERA_MSG_ZOOM:
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001239 if (mZoomListener != null) {
1240 mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 return;
1243
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001244 case CAMERA_MSG_PREVIEW_METADATA:
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001245 if (mFaceListener != null) {
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001246 mFaceListener.onFaceDetection((Face[])msg.obj, mCamera);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001247 }
1248 return;
1249
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001250 case CAMERA_MSG_ERROR :
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 Log.e(TAG, "Error " + msg.arg1);
Yin-Chia Yeha07cd322018-05-31 12:11:42 -07001252 if (mDetailedErrorCallback != null) {
1253 mDetailedErrorCallback.onError(msg.arg1, mCamera);
1254 } else if (mErrorCallback != null) {
1255 if (msg.arg1 == CAMERA_ERROR_DISABLED) {
1256 mErrorCallback.onError(CAMERA_ERROR_EVICTED, mCamera);
1257 } else {
1258 mErrorCallback.onError(msg.arg1, mCamera);
1259 }
Dave Sparkse8b26e12009-07-14 10:35:40 -07001260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 return;
1262
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001263 case CAMERA_MSG_FOCUS_MOVE:
1264 if (mAutoFocusMoveCallback != null) {
1265 mAutoFocusMoveCallback.onAutoFocusMoving(msg.arg1 == 0 ? false : true, mCamera);
1266 }
1267 return;
1268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 default:
1270 Log.e(TAG, "Unknown message type " + msg.what);
1271 return;
1272 }
1273 }
1274 }
1275
Mathew Inwoodbcbe4402018-08-08 15:42:59 +01001276 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 private static void postEventFromNative(Object camera_ref,
1278 int what, int arg1, int arg2, Object obj)
1279 {
1280 Camera c = (Camera)((WeakReference)camera_ref).get();
1281 if (c == null)
1282 return;
1283
1284 if (c.mEventHandler != null) {
1285 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj);
1286 c.mEventHandler.sendMessage(m);
1287 }
1288 }
1289
1290 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001291 * Callback interface used to notify on completion of camera auto focus.
1292 *
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001293 * <p>Devices that do not support auto-focus will receive a "fake"
1294 * callback to this interface. If your application needs auto-focus and
Scott Maindf4578e2009-09-10 12:22:07 -07001295 * should not be installed on devices <em>without</em> auto-focus, you must
1296 * declare that your app uses the
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001297 * {@code android.hardware.camera.autofocus} feature, in the
Scott Maindf4578e2009-09-10 12:22:07 -07001298 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
1299 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001300 *
1301 * @see #autoFocus(AutoFocusCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001302 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1303 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001305 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 public interface AutoFocusCallback
1307 {
1308 /**
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001309 * Called when the camera auto focus completes. If the camera
1310 * does not support auto-focus and autoFocus is called,
1311 * onAutoFocus will be called immediately with a fake value of
1312 * <code>success</code> set to <code>true</code>.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001313 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08001314 * The auto-focus routine does not lock auto-exposure and auto-white
1315 * balance after it completes.
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001316 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 * @param success true if focus was successful, false if otherwise
1318 * @param camera the Camera service object
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001319 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
1320 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 */
1322 void onAutoFocus(boolean success, Camera camera);
Wu-cheng Li53b30912011-10-12 19:43:51 +08001323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324
1325 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001326 * Starts camera auto-focus and registers a callback function to run when
1327 * the camera is focused. This method is only valid when preview is active
1328 * (between {@link #startPreview()} and before {@link #stopPreview()}).
1329 *
1330 * <p>Callers should check
1331 * {@link android.hardware.Camera.Parameters#getFocusMode()} to determine if
1332 * this method should be called. If the camera does not support auto-focus,
1333 * it is a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)}
Wu-cheng Li36322db2009-09-18 18:59:21 +08001334 * callback will be called immediately.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001335 *
Scott Mainda0a56d2009-09-10 18:08:37 -07001336 * <p>If your application should not be installed
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001337 * on devices without auto-focus, you must declare that your application
1338 * uses auto-focus with the
Scott Maindf4578e2009-09-10 12:22:07 -07001339 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
1340 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001341 *
Wu-cheng Li068ef422009-09-27 13:19:36 -07001342 * <p>If the current flash mode is not
1343 * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001344 * fired during auto-focus, depending on the driver and camera hardware.<p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001345 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001346 * <p>Auto-exposure lock {@link android.hardware.Camera.Parameters#getAutoExposureLock()}
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08001347 * and auto-white balance locks {@link android.hardware.Camera.Parameters#getAutoWhiteBalanceLock()}
1348 * do not change during and after autofocus. But auto-focus routine may stop
1349 * auto-exposure and auto-white balance transiently during focusing.
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001350 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001351 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
1352 * image capture with {@link #takePicture(Camera.ShutterCallback,
1353 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
1354 * the focus position. Applications must call cancelAutoFocus to reset the
1355 * focus.</p>
1356 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -08001357 * <p>If autofocus is successful, consider using
1358 * {@link android.media.MediaActionSound} to properly play back an autofocus
1359 * success sound to the user.</p>
1360 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 * @param cb the callback to run
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001362 * @throws RuntimeException if starting autofocus fails; usually this would
1363 * be because of a hardware or other low-level error, or because
1364 * release() has been called on this Camera instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001365 * @see #cancelAutoFocus()
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001366 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
1367 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
Eino-Ville Talvala108708b2012-03-05 11:00:43 -08001368 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 */
1370 public final void autoFocus(AutoFocusCallback cb)
1371 {
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001372 synchronized (mAutoFocusCallbackLock) {
James Dong248ba232012-04-28 21:30:46 -07001373 mAutoFocusCallback = cb;
James Dong248ba232012-04-28 21:30:46 -07001374 }
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001375 native_autoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 }
1377 private native final void native_autoFocus();
1378
1379 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001380 * Cancels any auto-focus function in progress.
1381 * Whether or not auto-focus is currently in progress,
1382 * this function will return the focus position to the default.
Chih-Chung Chang244f8c22009-09-15 14:51:56 +08001383 * If the camera does not support auto-focus, this is a no-op.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001384 *
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001385 * @throws RuntimeException if canceling autofocus fails; usually this would
1386 * be because of a hardware or other low-level error, or because
1387 * release() has been called on this Camera instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001388 * @see #autoFocus(Camera.AutoFocusCallback)
Chih-Chung Chang244f8c22009-09-15 14:51:56 +08001389 */
1390 public final void cancelAutoFocus()
1391 {
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001392 synchronized (mAutoFocusCallbackLock) {
James Dong248ba232012-04-28 21:30:46 -07001393 mAutoFocusCallback = null;
James Dong248ba232012-04-28 21:30:46 -07001394 }
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001395 native_cancelAutoFocus();
1396 // CAMERA_MSG_FOCUS should be removed here because the following
1397 // scenario can happen:
1398 // - An application uses the same thread for autoFocus, cancelAutoFocus
1399 // and looper thread.
1400 // - The application calls autoFocus.
1401 // - HAL sends CAMERA_MSG_FOCUS, which enters the looper message queue.
1402 // Before event handler's handleMessage() is invoked, the application
1403 // calls cancelAutoFocus and autoFocus.
1404 // - The application gets the old CAMERA_MSG_FOCUS and thinks autofocus
1405 // has been completed. But in fact it is not.
1406 //
1407 // As documented in the beginning of the file, apps should not use
1408 // multiple threads to call autoFocus and cancelAutoFocus at the same
1409 // time. It is HAL's responsibility not to send a CAMERA_MSG_FOCUS
1410 // message after native_cancelAutoFocus is called.
1411 mEventHandler.removeMessages(CAMERA_MSG_FOCUS);
Chih-Chung Chang244f8c22009-09-15 14:51:56 +08001412 }
1413 private native final void native_cancelAutoFocus();
1414
1415 /**
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001416 * Callback interface used to notify on auto focus start and stop.
1417 *
Wu-cheng Li65745392012-04-12 18:07:16 +08001418 * <p>This is only supported in continuous autofocus modes -- {@link
1419 * Parameters#FOCUS_MODE_CONTINUOUS_VIDEO} and {@link
1420 * Parameters#FOCUS_MODE_CONTINUOUS_PICTURE}. Applications can show
1421 * autofocus animation based on this.</p>
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001422 *
1423 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1424 * applications.
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001425 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001426 @Deprecated
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001427 public interface AutoFocusMoveCallback
1428 {
1429 /**
1430 * Called when the camera auto focus starts or stops.
1431 *
1432 * @param start true if focus starts to move, false if focus stops to move
Wu-cheng Li65745392012-04-12 18:07:16 +08001433 * @param camera the Camera service object
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001434 */
1435 void onAutoFocusMoving(boolean start, Camera camera);
1436 }
1437
1438 /**
1439 * Sets camera auto-focus move callback.
1440 *
1441 * @param cb the callback to run
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001442 * @throws RuntimeException if enabling the focus move callback fails;
1443 * usually this would be because of a hardware or other low-level error,
1444 * or because release() has been called on this Camera instance.
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001445 */
1446 public void setAutoFocusMoveCallback(AutoFocusMoveCallback cb) {
1447 mAutoFocusMoveCallback = cb;
1448 enableFocusMoveCallback((mAutoFocusMoveCallback != null) ? 1 : 0);
1449 }
1450
1451 private native void enableFocusMoveCallback(int enable);
1452
1453 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001454 * Callback interface used to signal the moment of actual image capture.
1455 *
1456 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001457 *
1458 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1459 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 public interface ShutterCallback
1463 {
1464 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001465 * Called as near as possible to the moment when a photo is captured
1466 * from the sensor. This is a good opportunity to play a shutter sound
1467 * or give other feedback of camera operation. This may be some time
1468 * after the photo was triggered, but some time before the actual data
1469 * is available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 */
1471 void onShutter();
1472 }
1473
1474 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001475 * Callback interface used to supply image data from a photo capture.
1476 *
1477 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001478 *
1479 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1480 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001482 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 public interface PictureCallback {
1484 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001485 * Called when image data is available after a picture is taken.
1486 * The format of the data depends on the context of the callback
1487 * and {@link Camera.Parameters} settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001488 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 * @param data a byte array of the picture data
1490 * @param camera the Camera service object
1491 */
1492 void onPictureTaken(byte[] data, Camera camera);
1493 };
1494
1495 /**
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001496 * Equivalent to <pre>takePicture(Shutter, raw, null, jpeg)</pre>.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001497 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001498 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 */
1500 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
1501 PictureCallback jpeg) {
Dave Sparkse8b26e12009-07-14 10:35:40 -07001502 takePicture(shutter, raw, null, jpeg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 }
James Donge00cab72011-02-17 16:38:06 -08001504 private native final void native_takePicture(int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505
Dave Sparkse8b26e12009-07-14 10:35:40 -07001506 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001507 * Triggers an asynchronous image capture. The camera service will initiate
1508 * a series of callbacks to the application as the image capture progresses.
1509 * The shutter callback occurs after the image is captured. This can be used
1510 * to trigger a sound to let the user know that image has been captured. The
1511 * raw callback occurs when the raw image data is available (NOTE: the data
James Donge00cab72011-02-17 16:38:06 -08001512 * will be null if there is no raw image callback buffer available or the
1513 * raw image callback buffer is not large enough to hold the raw image).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001514 * The postview callback occurs when a scaled, fully processed postview
1515 * image is available (NOTE: not all hardware supports this). The jpeg
1516 * callback occurs when the compressed image is available. If the
1517 * application does not need a particular callback, a null can be passed
1518 * instead of a callback method.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001519 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001520 * <p>This method is only valid when preview is active (after
1521 * {@link #startPreview()}). Preview will be stopped after the image is
1522 * taken; callers must call {@link #startPreview()} again if they want to
Wu-cheng Li42419ce2011-06-01 17:22:24 +08001523 * re-start preview or take more pictures. This should not be called between
1524 * {@link android.media.MediaRecorder#start()} and
1525 * {@link android.media.MediaRecorder#stop()}.
Wu-cheng Li40057ce2009-12-02 18:57:29 +08001526 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001527 * <p>After calling this method, you must not call {@link #startPreview()}
1528 * or take another picture until the JPEG callback has returned.
1529 *
1530 * @param shutter the callback for image capture moment, or null
1531 * @param raw the callback for raw (uncompressed) image data, or null
Dave Sparkse8b26e12009-07-14 10:35:40 -07001532 * @param postview callback with postview image data, may be null
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001533 * @param jpeg the callback for JPEG image data, or null
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001534 * @throws RuntimeException if starting picture capture fails; usually this
1535 * would be because of a hardware or other low-level error, or because
1536 * release() has been called on this Camera instance.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001537 */
1538 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
1539 PictureCallback postview, PictureCallback jpeg) {
1540 mShutterCallback = shutter;
1541 mRawImageCallback = raw;
1542 mPostviewCallback = postview;
1543 mJpegCallback = jpeg;
James Donge00cab72011-02-17 16:38:06 -08001544
1545 // If callback is not set, do not send me callbacks.
1546 int msgType = 0;
1547 if (mShutterCallback != null) {
1548 msgType |= CAMERA_MSG_SHUTTER;
1549 }
1550 if (mRawImageCallback != null) {
1551 msgType |= CAMERA_MSG_RAW_IMAGE;
1552 }
1553 if (mPostviewCallback != null) {
1554 msgType |= CAMERA_MSG_POSTVIEW_FRAME;
1555 }
1556 if (mJpegCallback != null) {
1557 msgType |= CAMERA_MSG_COMPRESSED_IMAGE;
1558 }
1559
1560 native_takePicture(msgType);
Wu-cheng Lie9c6c9c2012-05-29 16:47:44 +08001561 mFaceDetectionRunning = false;
Dave Sparkse8b26e12009-07-14 10:35:40 -07001562 }
1563
1564 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001565 * Zooms to the requested value smoothly. The driver will notify {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001566 * OnZoomChangeListener} of the zoom value and whether zoom is stopped at
1567 * the time. For example, suppose the current zoom is 0 and startSmoothZoom
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001568 * is called with value 3. The
1569 * {@link Camera.OnZoomChangeListener#onZoomChange(int, boolean, Camera)}
1570 * method will be called three times with zoom values 1, 2, and 3.
1571 * Applications can call {@link #stopSmoothZoom} to stop the zoom earlier.
1572 * Applications should not call startSmoothZoom again or change the zoom
1573 * value before zoom stops. If the supplied zoom value equals to the current
1574 * zoom value, no zoom callback will be generated. This method is supported
1575 * if {@link android.hardware.Camera.Parameters#isSmoothZoomSupported}
1576 * returns true.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001577 *
1578 * @param value zoom value. The valid range is 0 to {@link
1579 * android.hardware.Camera.Parameters#getMaxZoom}.
Wu-cheng Li0ca25192010-03-29 16:21:12 +08001580 * @throws IllegalArgumentException if the zoom value is invalid.
1581 * @throws RuntimeException if the method fails.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001582 * @see #setZoomChangeListener(OnZoomChangeListener)
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001583 */
1584 public native final void startSmoothZoom(int value);
1585
1586 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001587 * Stops the smooth zoom. Applications should wait for the {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001588 * OnZoomChangeListener} to know when the zoom is actually stopped. This
1589 * method is supported if {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001590 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true.
Wu-cheng Li0ca25192010-03-29 16:21:12 +08001591 *
1592 * @throws RuntimeException if the method fails.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001593 */
1594 public native final void stopSmoothZoom();
1595
1596 /**
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001597 * Set the clockwise rotation of preview display in degrees. This affects
1598 * the preview frames and the picture displayed after snapshot. This method
1599 * is useful for portrait mode applications. Note that preview display of
Wu-cheng Lib982fb42010-10-19 17:19:09 +08001600 * front-facing cameras is flipped horizontally before the rotation, that
1601 * is, the image is reflected along the central vertical axis of the camera
1602 * sensor. So the users can see themselves as looking into a mirror.
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001603 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001604 * <p>This does not affect the order of byte array passed in {@link
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001605 * PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This
1606 * method is not allowed to be called during preview.
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001607 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001608 * <p>If you want to make the camera image show in the same orientation as
1609 * the display, you can use the following code.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001610 * <pre>
Chih-Chung Chang724c5222010-06-14 18:57:15 +08001611 * public static void setCameraDisplayOrientation(Activity activity,
1612 * int cameraId, android.hardware.Camera camera) {
1613 * android.hardware.Camera.CameraInfo info =
1614 * new android.hardware.Camera.CameraInfo();
1615 * android.hardware.Camera.getCameraInfo(cameraId, info);
1616 * int rotation = activity.getWindowManager().getDefaultDisplay()
1617 * .getRotation();
1618 * int degrees = 0;
1619 * switch (rotation) {
1620 * case Surface.ROTATION_0: degrees = 0; break;
1621 * case Surface.ROTATION_90: degrees = 90; break;
1622 * case Surface.ROTATION_180: degrees = 180; break;
1623 * case Surface.ROTATION_270: degrees = 270; break;
1624 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001625 *
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001626 * int result;
1627 * if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
1628 * result = (info.orientation + degrees) % 360;
1629 * result = (360 - result) % 360; // compensate the mirror
1630 * } else { // back-facing
1631 * result = (info.orientation - degrees + 360) % 360;
1632 * }
Chih-Chung Chang724c5222010-06-14 18:57:15 +08001633 * camera.setDisplayOrientation(result);
1634 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001635 * </pre>
Wu-cheng Lid3033622011-10-07 13:13:54 +08001636 *
1637 * <p>Starting from API level 14, this method can be called when preview is
1638 * active.
1639 *
Eino-Ville Talvala6c91e2c2016-03-25 11:54:39 -07001640 * <p><b>Note: </b>Before API level 24, the default value for orientation is 0. Starting in
1641 * API level 24, the default orientation will be such that applications in forced-landscape mode
1642 * will have correct preview orientation, which may be either a default of 0 or
1643 * 180. Applications that operate in portrait mode or allow for changing orientation must still
1644 * call this method after each orientation change to ensure correct preview display in all
1645 * cases.</p>
1646 *
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001647 * @param degrees the angle that the picture will be rotated clockwise.
Eino-Ville Talvala6c91e2c2016-03-25 11:54:39 -07001648 * Valid values are 0, 90, 180, and 270.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001649 * @throws RuntimeException if setting orientation fails; usually this would
1650 * be because of a hardware or other low-level error, or because
1651 * release() has been called on this Camera instance.
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001652 * @see #setPreviewDisplay(SurfaceHolder)
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001653 */
1654 public native final void setDisplayOrientation(int degrees);
1655
1656 /**
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001657 * <p>Enable or disable the default shutter sound when taking a picture.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001658 *
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001659 * <p>By default, the camera plays the system-defined camera shutter sound
1660 * when {@link #takePicture} is called. Using this method, the shutter sound
1661 * can be disabled. It is strongly recommended that an alternative shutter
1662 * sound is played in the {@link ShutterCallback} when the system shutter
1663 * sound is disabled.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001664 *
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001665 * <p>Note that devices may not always allow disabling the camera shutter
1666 * sound. If the shutter sound state cannot be set to the desired value,
1667 * this method will return false. {@link CameraInfo#canDisableShutterSound}
1668 * can be used to determine whether the device will allow the shutter sound
1669 * to be disabled.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001670 *
1671 * @param enabled whether the camera should play the system shutter sound
1672 * when {@link #takePicture takePicture} is called.
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001673 * @return {@code true} if the shutter sound state was successfully
1674 * changed. {@code false} if the shutter sound state could not be
1675 * changed. {@code true} is also returned if shutter sound playback
1676 * is already set to the requested state.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001677 * @throws RuntimeException if the call fails; usually this would be because
1678 * of a hardware or other low-level error, or because release() has been
1679 * called on this Camera instance.
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001680 * @see #takePicture
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001681 * @see CameraInfo#canDisableShutterSound
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001682 * @see ShutterCallback
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001683 */
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001684 public final boolean enableShutterSound(boolean enabled) {
jiabine059e452018-05-07 15:16:44 -07001685 boolean canDisableShutterSound = true;
1686 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
1687 IAudioService audioService = IAudioService.Stub.asInterface(b);
1688 try {
1689 if (audioService.isCameraSoundForced()) {
1690 canDisableShutterSound = false;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001691 }
jiabine059e452018-05-07 15:16:44 -07001692 } catch (RemoteException e) {
1693 Log.e(TAG, "Audio service is unavailable for queries");
1694 }
1695 if (!enabled && !canDisableShutterSound) {
1696 return false;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001697 }
jiabin503e1fd2018-04-19 16:39:44 -07001698 synchronized (mShutterSoundLock) {
jiabine059e452018-05-07 15:16:44 -07001699 mShutterSoundEnabledFromApp = enabled;
1700 // Return the result of _enableShutterSound(enabled) in all cases.
1701 // If the shutter sound can be disabled, disable it when the device is in DnD mode.
jiabin503e1fd2018-04-19 16:39:44 -07001702 boolean ret = _enableShutterSound(enabled);
jiabine059e452018-05-07 15:16:44 -07001703 if (enabled && !mHasAppOpsPlayAudio) {
1704 Log.i(TAG, "Shutter sound is not allowed by AppOpsManager");
1705 if (canDisableShutterSound) {
1706 _enableShutterSound(false);
1707 }
jiabin503e1fd2018-04-19 16:39:44 -07001708 }
1709 return ret;
1710 }
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001711 }
1712
Igor Murashkin66533622014-08-19 14:51:47 -07001713 /**
1714 * Disable the shutter sound unconditionally.
1715 *
1716 * <p>
1717 * This is only guaranteed to work for legacy cameras
1718 * (i.e. initialized with {@link #cameraInitUnspecified}). Trying to call this on
1719 * a regular camera will force a conditional check in the camera service.
1720 * </p>
1721 *
1722 * @return {@code true} if the shutter sound state was successfully
1723 * changed. {@code false} if the shutter sound state could not be
1724 * changed. {@code true} is also returned if shutter sound playback
1725 * is already set to the requested state.
1726 *
1727 * @hide
1728 */
1729 public final boolean disableShutterSound() {
1730 return _enableShutterSound(/*enabled*/false);
1731 }
1732
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001733 private native final boolean _enableShutterSound(boolean enabled);
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001734
jiabin503e1fd2018-04-19 16:39:44 -07001735 private static class IAppOpsCallbackWrapper extends IAppOpsCallback.Stub {
1736 private final WeakReference<Camera> mWeakCamera;
1737
1738 IAppOpsCallbackWrapper(Camera camera) {
1739 mWeakCamera = new WeakReference<Camera>(camera);
1740 }
1741
1742 @Override
1743 public void opChanged(int op, int uid, String packageName) {
1744 if (op == AppOpsManager.OP_PLAY_AUDIO) {
1745 final Camera camera = mWeakCamera.get();
1746 if (camera != null) {
1747 camera.updateAppOpsPlayAudio();
1748 }
1749 }
1750 }
1751 }
1752
1753 private void updateAppOpsPlayAudio() {
1754 synchronized (mShutterSoundLock) {
1755 boolean oldHasAppOpsPlayAudio = mHasAppOpsPlayAudio;
1756 try {
1757 int mode = AppOpsManager.MODE_IGNORED;
1758 if (mAppOps != null) {
1759 mode = mAppOps.checkAudioOperation(AppOpsManager.OP_PLAY_AUDIO,
1760 AudioAttributes.USAGE_ASSISTANCE_SONIFICATION,
1761 Process.myUid(), ActivityThread.currentPackageName());
1762 }
1763 mHasAppOpsPlayAudio = mode == AppOpsManager.MODE_ALLOWED;
1764 } catch (RemoteException e) {
1765 Log.e(TAG, "AppOpsService check audio operation failed");
1766 mHasAppOpsPlayAudio = false;
1767 }
1768 if (oldHasAppOpsPlayAudio != mHasAppOpsPlayAudio) {
1769 if (!mHasAppOpsPlayAudio) {
jiabine059e452018-05-07 15:16:44 -07001770 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
1771 IAudioService audioService = IAudioService.Stub.asInterface(b);
1772 try {
1773 if (audioService.isCameraSoundForced()) {
1774 return;
1775 }
1776 } catch (RemoteException e) {
1777 Log.e(TAG, "Audio service is unavailable for queries");
1778 }
jiabin503e1fd2018-04-19 16:39:44 -07001779 _enableShutterSound(false);
1780 } else {
jiabine059e452018-05-07 15:16:44 -07001781 enableShutterSound(mShutterSoundEnabledFromApp);
jiabin503e1fd2018-04-19 16:39:44 -07001782 }
1783 }
1784 }
1785 }
1786
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001787 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001788 * Callback interface for zoom changes during a smooth zoom operation.
1789 *
1790 * @see #setZoomChangeListener(OnZoomChangeListener)
1791 * @see #startSmoothZoom(int)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001792 *
1793 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1794 * applications.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001795 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001796 @Deprecated
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001797 public interface OnZoomChangeListener
Dave Sparkse8b26e12009-07-14 10:35:40 -07001798 {
1799 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001800 * Called when the zoom value has changed during a smooth zoom.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001801 *
1802 * @param zoomValue the current zoom value. In smooth zoom mode, camera
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001803 * calls this for every new zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001804 * @param stopped whether smooth zoom is stopped. If the value is true,
1805 * this is the last zoom update for the application.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001806 * @param camera the Camera service object
1807 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001808 void onZoomChange(int zoomValue, boolean stopped, Camera camera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001809 };
1810
1811 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001812 * Registers a listener to be notified when the zoom value is updated by the
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001813 * camera driver during smooth zoom.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001814 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001815 * @param listener the listener to notify
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001816 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001817 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001818 public final void setZoomChangeListener(OnZoomChangeListener listener)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001819 {
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001820 mZoomListener = listener;
Dave Sparkse8b26e12009-07-14 10:35:40 -07001821 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001822
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001823 /**
1824 * Callback interface for face detected in the preview frame.
1825 *
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001826 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1827 * applications.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001828 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001829 @Deprecated
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001830 public interface FaceDetectionListener
1831 {
1832 /**
1833 * Notify the listener of the detected faces in the preview frame.
1834 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001835 * @param faces The detected faces in a list
Joe Fernandez464cb212011-10-04 16:56:47 -07001836 * @param camera The {@link Camera} service object
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001837 */
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001838 void onFaceDetection(Face[] faces, Camera camera);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001839 }
1840
1841 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001842 * Registers a listener to be notified about the faces detected in the
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001843 * preview frame.
1844 *
1845 * @param listener the listener to notify
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001846 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001847 */
1848 public final void setFaceDetectionListener(FaceDetectionListener listener)
1849 {
1850 mFaceListener = listener;
1851 }
1852
1853 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001854 * Starts the face detection. This should be called after preview is started.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001855 * The camera will notify {@link FaceDetectionListener} of the detected
1856 * faces in the preview frame. The detected faces may be the same as the
1857 * previous ones. Applications should call {@link #stopFaceDetection} to
1858 * stop the face detection. This method is supported if {@link
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001859 * Parameters#getMaxNumDetectedFaces()} returns a number larger than 0.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001860 * If the face detection has started, apps should not call this again.
1861 *
Wu-cheng Li8c136702011-08-12 20:25:00 +08001862 * <p>When the face detection is running, {@link Parameters#setWhiteBalance(String)},
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001863 * {@link Parameters#setFocusAreas(List)}, and {@link Parameters#setMeteringAreas(List)}
Wu-cheng Li8c136702011-08-12 20:25:00 +08001864 * have no effect. The camera uses the detected faces to do auto-white balance,
1865 * auto exposure, and autofocus.
1866 *
1867 * <p>If the apps call {@link #autoFocus(AutoFocusCallback)}, the camera
1868 * will stop sending face callbacks. The last face callback indicates the
1869 * areas used to do autofocus. After focus completes, face detection will
1870 * resume sending face callbacks. If the apps call {@link
1871 * #cancelAutoFocus()}, the face callbacks will also resume.</p>
1872 *
1873 * <p>After calling {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
1874 * Camera.PictureCallback)} or {@link #stopPreview()}, and then resuming
1875 * preview with {@link #startPreview()}, the apps should call this method
1876 * again to resume face detection.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001877 *
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001878 * @throws IllegalArgumentException if the face detection is unsupported.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001879 * @throws RuntimeException if the method fails or the face detection is
1880 * already running.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001881 * @see FaceDetectionListener
1882 * @see #stopFaceDetection()
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001883 * @see Parameters#getMaxNumDetectedFaces()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001884 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001885 public final void startFaceDetection() {
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001886 if (mFaceDetectionRunning) {
1887 throw new RuntimeException("Face detection is already running");
1888 }
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001889 _startFaceDetection(CAMERA_FACE_DETECTION_HW);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001890 mFaceDetectionRunning = true;
1891 }
1892
1893 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001894 * Stops the face detection.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001895 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001896 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001897 */
1898 public final void stopFaceDetection() {
1899 _stopFaceDetection();
1900 mFaceDetectionRunning = false;
1901 }
1902
1903 private native final void _startFaceDetection(int type);
1904 private native final void _stopFaceDetection();
1905
1906 /**
Joe Fernandez464cb212011-10-04 16:56:47 -07001907 * Information about a face identified through camera face detection.
Wu-cheng Li53b30912011-10-12 19:43:51 +08001908 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001909 * <p>When face detection is used with a camera, the {@link FaceDetectionListener} returns a
1910 * list of face objects for use in focusing and metering.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001911 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001912 * @see FaceDetectionListener
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001913 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1914 * applications.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001915 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001916 @Deprecated
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001917 public static class Face {
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001918 /**
1919 * Create an empty face.
1920 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001921 public Face() {
1922 }
1923
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001924 /**
1925 * Bounds of the face. (-1000, -1000) represents the top-left of the
1926 * camera field of view, and (1000, 1000) represents the bottom-right of
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001927 * the field of view. For example, suppose the size of the viewfinder UI
1928 * is 800x480. The rect passed from the driver is (-1000, -1000, 0, 0).
Wu-cheng Li8c136702011-08-12 20:25:00 +08001929 * The corresponding viewfinder rect should be (0, 0, 400, 240). It is
1930 * guaranteed left < right and top < bottom. The coordinates can be
1931 * smaller than -1000 or bigger than 1000. But at least one vertex will
1932 * be within (-1000, -1000) and (1000, 1000).
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001933 *
1934 * <p>The direction is relative to the sensor orientation, that is, what
1935 * the sensor sees. The direction is not affected by the rotation or
Wu-cheng Li8c136702011-08-12 20:25:00 +08001936 * mirroring of {@link #setDisplayOrientation(int)}. The face bounding
1937 * rectangle does not provide any information about face orientation.</p>
1938 *
1939 * <p>Here is the matrix to convert driver coordinates to View coordinates
1940 * in pixels.</p>
1941 * <pre>
1942 * Matrix matrix = new Matrix();
1943 * CameraInfo info = CameraHolder.instance().getCameraInfo()[cameraId];
1944 * // Need mirror for front camera.
1945 * boolean mirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
1946 * matrix.setScale(mirror ? -1 : 1, 1);
1947 * // This is the value for android.hardware.Camera.setDisplayOrientation.
1948 * matrix.postRotate(displayOrientation);
1949 * // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
1950 * // UI coordinates range from (0, 0) to (width, height).
1951 * matrix.postScale(view.getWidth() / 2000f, view.getHeight() / 2000f);
1952 * matrix.postTranslate(view.getWidth() / 2f, view.getHeight() / 2f);
1953 * </pre>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001954 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001955 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001956 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001957 public Rect rect;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001958
1959 /**
Eino-Ville Talvala8df3b2b2012-09-15 14:30:12 -07001960 * <p>The confidence level for the detection of the face. The range is 1 to
1961 * 100. 100 is the highest confidence.</p>
1962 *
1963 * <p>Depending on the device, even very low-confidence faces may be
1964 * listed, so applications should filter out faces with low confidence,
1965 * depending on the use case. For a typical point-and-shoot camera
1966 * application that wishes to display rectangles around detected faces,
1967 * filtering out faces with confidence less than 50 is recommended.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001968 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001969 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001970 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001971 public int score;
Wei Huad52b3082011-08-19 13:01:51 -07001972
1973 /**
1974 * An unique id per face while the face is visible to the tracker. If
1975 * the face leaves the field-of-view and comes back, it will get a new
1976 * id. This is an optional field, may not be supported on all devices.
1977 * If not supported, id will always be set to -1. The optional fields
1978 * are supported as a set. Either they are all valid, or none of them
1979 * are.
1980 */
1981 public int id = -1;
1982
1983 /**
1984 * The coordinates of the center of the left eye. The coordinates are in
1985 * the same space as the ones for {@link #rect}. This is an optional
1986 * field, may not be supported on all devices. If not supported, the
1987 * value will always be set to null. The optional fields are supported
1988 * as a set. Either they are all valid, or none of them are.
1989 */
1990 public Point leftEye = null;
1991
1992 /**
1993 * The coordinates of the center of the right eye. The coordinates are
1994 * in the same space as the ones for {@link #rect}.This is an optional
1995 * field, may not be supported on all devices. If not supported, the
1996 * value will always be set to null. The optional fields are supported
1997 * as a set. Either they are all valid, or none of them are.
1998 */
1999 public Point rightEye = null;
2000
2001 /**
2002 * The coordinates of the center of the mouth. The coordinates are in
2003 * the same space as the ones for {@link #rect}. This is an optional
2004 * field, may not be supported on all devices. If not supported, the
2005 * value will always be set to null. The optional fields are supported
2006 * as a set. Either they are all valid, or none of them are.
2007 */
2008 public Point mouth = null;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08002009 }
2010
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002011 /**
2012 * Unspecified camera error.
2013 * @see Camera.ErrorCallback
2014 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 public static final int CAMERA_ERROR_UNKNOWN = 1;
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002016
2017 /**
Ruben Brunke91b9252015-04-14 13:38:06 -07002018 * Camera was disconnected due to use by higher priority user.
2019 * @see Camera.ErrorCallback
2020 */
2021 public static final int CAMERA_ERROR_EVICTED = 2;
2022
2023 /**
Yin-Chia Yeha07cd322018-05-31 12:11:42 -07002024 * Camera was disconnected due to device policy change or client
2025 * application going to background.
2026 * @see Camera.ErrorCallback
2027 *
2028 * @hide
2029 */
2030 public static final int CAMERA_ERROR_DISABLED = 3;
2031
2032 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002033 * Media server died. In this case, the application must release the
2034 * Camera object and instantiate a new one.
2035 * @see Camera.ErrorCallback
2036 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 public static final int CAMERA_ERROR_SERVER_DIED = 100;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002040 * Callback interface for camera error notification.
2041 *
2042 * @see #setErrorCallback(ErrorCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002043 *
2044 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2045 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002047 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 public interface ErrorCallback
2049 {
2050 /**
2051 * Callback for camera errors.
2052 * @param error error code:
2053 * <ul>
2054 * <li>{@link #CAMERA_ERROR_UNKNOWN}
2055 * <li>{@link #CAMERA_ERROR_SERVER_DIED}
2056 * </ul>
2057 * @param camera the Camera service object
2058 */
2059 void onError(int error, Camera camera);
2060 };
2061
2062 /**
2063 * Registers a callback to be invoked when an error occurs.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002064 * @param cb The callback to run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 */
2066 public final void setErrorCallback(ErrorCallback cb)
2067 {
2068 mErrorCallback = cb;
2069 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002070
Yin-Chia Yeha07cd322018-05-31 12:11:42 -07002071 /**
2072 * Registers a callback to be invoked when an error occurs.
2073 * The detailed error callback may contain error code that
2074 * gives more detailed information about the error.
2075 *
2076 * When a detailed callback is set, the callback set via
2077 * #setErrorCallback(ErrorCallback) will stop receiving
2078 * onError call.
2079 *
2080 * @param cb The callback to run
2081 *
2082 * @hide
2083 */
2084 public final void setDetailedErrorCallback(ErrorCallback cb)
2085 {
2086 mDetailedErrorCallback = cb;
2087 }
2088
Mathew Inwoodbcbe4402018-08-08 15:42:59 +01002089 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 private native final void native_setParameters(String params);
Mathew Inwoodbcbe4402018-08-08 15:42:59 +01002091 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 private native final String native_getParameters();
2093
2094 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002095 * Changes the settings for this Camera service.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002096 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 * @param params the Parameters to use for this Camera service
Wu-cheng Li99a3f3e2010-11-19 15:56:16 +08002098 * @throws RuntimeException if any parameter is invalid or not supported.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002099 * @see #getParameters()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 */
2101 public void setParameters(Parameters params) {
Eino-Ville Talvala7005b672013-04-02 15:46:38 -07002102 // If using preview allocations, don't allow preview size changes
2103 if (mUsingPreviewAllocation) {
2104 Size newPreviewSize = params.getPreviewSize();
2105 Size currentPreviewSize = getParameters().getPreviewSize();
2106 if (newPreviewSize.width != currentPreviewSize.width ||
2107 newPreviewSize.height != currentPreviewSize.height) {
2108 throw new IllegalStateException("Cannot change preview size" +
2109 " while a preview allocation is configured.");
2110 }
2111 }
2112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 native_setParameters(params.flatten());
2114 }
2115
2116 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002117 * Returns the current settings for this Camera service.
2118 * If modifications are made to the returned Parameters, they must be passed
2119 * to {@link #setParameters(Camera.Parameters)} to take effect.
2120 *
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07002121 * @throws RuntimeException if reading parameters fails; usually this would
2122 * be because of a hardware or other low-level error, or because
2123 * release() has been called on this Camera instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002124 * @see #setParameters(Camera.Parameters)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 */
2126 public Parameters getParameters() {
2127 Parameters p = new Parameters();
2128 String s = native_getParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 p.unflatten(s);
2130 return p;
2131 }
2132
2133 /**
Wu-cheng Li1c04a332011-11-22 18:21:18 +08002134 * Returns an empty {@link Parameters} for testing purpose.
2135 *
Ken Wakasaf76a50c2012-03-09 19:56:35 +09002136 * @return a Parameter object.
Wu-cheng Li1c04a332011-11-22 18:21:18 +08002137 *
2138 * @hide
2139 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +01002140 @UnsupportedAppUsage
Wu-cheng Li1c04a332011-11-22 18:21:18 +08002141 public static Parameters getEmptyParameters() {
2142 Camera camera = new Camera();
2143 return camera.new Parameters();
2144 }
2145
2146 /**
Igor Murashkindf6242e2014-07-01 18:06:13 -07002147 * Returns a copied {@link Parameters}; for shim use only.
2148 *
2149 * @param parameters a non-{@code null} parameters
2150 * @return a Parameter object, with all the parameters copied from {@code parameters}.
2151 *
2152 * @throws NullPointerException if {@code parameters} was {@code null}
2153 * @hide
2154 */
2155 public static Parameters getParametersCopy(Camera.Parameters parameters) {
2156 if (parameters == null) {
2157 throw new NullPointerException("parameters must not be null");
2158 }
2159
2160 Camera camera = parameters.getOuter();
2161 Parameters p = camera.new Parameters();
2162 p.copyFrom(parameters);
2163
2164 return p;
2165 }
2166
2167 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002168 * Image size (width and height dimensions).
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002169 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2170 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002172 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 public class Size {
2174 /**
2175 * Sets the dimensions for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 * @param w the photo width (pixels)
2178 * @param h the photo height (pixels)
2179 */
2180 public Size(int w, int h) {
2181 width = w;
2182 height = h;
2183 }
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002184 /**
2185 * Compares {@code obj} to this size.
2186 *
2187 * @param obj the object to compare this size with.
2188 * @return {@code true} if the width and height of {@code obj} is the
2189 * same as those of this size. {@code false} otherwise.
2190 */
2191 @Override
2192 public boolean equals(Object obj) {
2193 if (!(obj instanceof Size)) {
2194 return false;
2195 }
2196 Size s = (Size) obj;
2197 return width == s.width && height == s.height;
2198 }
2199 @Override
2200 public int hashCode() {
2201 return width * 32713 + height;
2202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 /** width of the picture */
2204 public int width;
2205 /** height of the picture */
2206 public int height;
2207 };
2208
2209 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002210 * <p>The Area class is used for choosing specific metering and focus areas for
2211 * the camera to use when calculating auto-exposure, auto-white balance, and
2212 * auto-focus.</p>
2213 *
2214 * <p>To find out how many simultaneous areas a given camera supports, use
2215 * {@link Parameters#getMaxNumMeteringAreas()} and
2216 * {@link Parameters#getMaxNumFocusAreas()}. If metering or focusing area
2217 * selection is unsupported, these methods will return 0.</p>
2218 *
2219 * <p>Each Area consists of a rectangle specifying its bounds, and a weight
2220 * that determines its importance. The bounds are relative to the camera's
2221 * current field of view. The coordinates are mapped so that (-1000, -1000)
2222 * is always the top-left corner of the current field of view, and (1000,
2223 * 1000) is always the bottom-right corner of the current field of
2224 * view. Setting Areas with bounds outside that range is not allowed. Areas
2225 * with zero or negative width or height are not allowed.</p>
2226 *
2227 * <p>The weight must range from 1 to 1000, and represents a weight for
2228 * every pixel in the area. This means that a large metering area with
2229 * the same weight as a smaller area will have more effect in the
2230 * metering result. Metering areas can overlap and the driver
2231 * will add the weights in the overlap region.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08002232 *
Wu-cheng Libde61a52011-06-07 18:23:14 +08002233 * @see Parameters#setFocusAreas(List)
2234 * @see Parameters#getFocusAreas()
2235 * @see Parameters#getMaxNumFocusAreas()
2236 * @see Parameters#setMeteringAreas(List)
2237 * @see Parameters#getMeteringAreas()
2238 * @see Parameters#getMaxNumMeteringAreas()
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002239 *
2240 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2241 * applications.
Wu-cheng Li30771b72011-04-02 06:19:46 +08002242 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002243 @Deprecated
Wu-cheng Li30771b72011-04-02 06:19:46 +08002244 public static class Area {
2245 /**
2246 * Create an area with specified rectangle and weight.
2247 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002248 * @param rect the bounds of the area.
2249 * @param weight the weight of the area.
Wu-cheng Li30771b72011-04-02 06:19:46 +08002250 */
2251 public Area(Rect rect, int weight) {
2252 this.rect = rect;
2253 this.weight = weight;
2254 }
2255 /**
2256 * Compares {@code obj} to this area.
2257 *
2258 * @param obj the object to compare this area with.
2259 * @return {@code true} if the rectangle and weight of {@code obj} is
2260 * the same as those of this area. {@code false} otherwise.
2261 */
2262 @Override
2263 public boolean equals(Object obj) {
2264 if (!(obj instanceof Area)) {
2265 return false;
2266 }
2267 Area a = (Area) obj;
2268 if (rect == null) {
2269 if (a.rect != null) return false;
2270 } else {
2271 if (!rect.equals(a.rect)) return false;
2272 }
2273 return weight == a.weight;
2274 }
2275
Wu-cheng Libde61a52011-06-07 18:23:14 +08002276 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002277 * Bounds of the area. (-1000, -1000) represents the top-left of the
2278 * camera field of view, and (1000, 1000) represents the bottom-right of
2279 * the field of view. Setting bounds outside that range is not
2280 * allowed. Bounds with zero or negative width or height are not
2281 * allowed.
Wu-cheng Libde61a52011-06-07 18:23:14 +08002282 *
2283 * @see Parameters#getFocusAreas()
2284 * @see Parameters#getMeteringAreas()
2285 */
Wu-cheng Li30771b72011-04-02 06:19:46 +08002286 public Rect rect;
2287
Wu-cheng Libde61a52011-06-07 18:23:14 +08002288 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002289 * Weight of the area. The weight must range from 1 to 1000, and
2290 * represents a weight for every pixel in the area. This means that a
2291 * large metering area with the same weight as a smaller area will have
2292 * more effect in the metering result. Metering areas can overlap and
2293 * the driver will add the weights in the overlap region.
Wu-cheng Libde61a52011-06-07 18:23:14 +08002294 *
2295 * @see Parameters#getFocusAreas()
2296 * @see Parameters#getMeteringAreas()
2297 */
Wu-cheng Li30771b72011-04-02 06:19:46 +08002298 public int weight;
Wu-cheng Libde61a52011-06-07 18:23:14 +08002299 }
Wu-cheng Li30771b72011-04-02 06:19:46 +08002300
2301 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002302 * Camera service settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002303 *
2304 * <p>To make camera parameters take effect, applications have to call
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002305 * {@link Camera#setParameters(Camera.Parameters)}. For example, after
2306 * {@link Camera.Parameters#setWhiteBalance} is called, white balance is not
2307 * actually changed until {@link Camera#setParameters(Camera.Parameters)}
2308 * is called with the changed parameters object.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002309 *
2310 * <p>Different devices may have different camera capabilities, such as
2311 * picture size or flash modes. The application should query the camera
2312 * capabilities before setting parameters. For example, the application
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002313 * should call {@link Camera.Parameters#getSupportedColorEffects()} before
2314 * calling {@link Camera.Parameters#setColorEffect(String)}. If the
2315 * camera does not support color effects,
2316 * {@link Camera.Parameters#getSupportedColorEffects()} will return null.
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002317 *
2318 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2319 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002321 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 public class Parameters {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002323 // Parameter keys to communicate with the camera driver.
2324 private static final String KEY_PREVIEW_SIZE = "preview-size";
2325 private static final String KEY_PREVIEW_FORMAT = "preview-format";
2326 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate";
Wu-cheng Li454630f2010-08-11 16:48:05 -07002327 private static final String KEY_PREVIEW_FPS_RANGE = "preview-fps-range";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002328 private static final String KEY_PICTURE_SIZE = "picture-size";
2329 private static final String KEY_PICTURE_FORMAT = "picture-format";
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002330 private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002331 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width";
2332 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height";
2333 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality";
2334 private static final String KEY_JPEG_QUALITY = "jpeg-quality";
2335 private static final String KEY_ROTATION = "rotation";
2336 private static final String KEY_GPS_LATITUDE = "gps-latitude";
2337 private static final String KEY_GPS_LONGITUDE = "gps-longitude";
2338 private static final String KEY_GPS_ALTITUDE = "gps-altitude";
2339 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp";
Ray Chen055c9862010-02-23 10:45:42 +08002340 private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002341 private static final String KEY_WHITE_BALANCE = "whitebalance";
2342 private static final String KEY_EFFECT = "effect";
2343 private static final String KEY_ANTIBANDING = "antibanding";
2344 private static final String KEY_SCENE_MODE = "scene-mode";
2345 private static final String KEY_FLASH_MODE = "flash-mode";
Wu-cheng Li36322db2009-09-18 18:59:21 +08002346 private static final String KEY_FOCUS_MODE = "focus-mode";
Wu-cheng Li30771b72011-04-02 06:19:46 +08002347 private static final String KEY_FOCUS_AREAS = "focus-areas";
2348 private static final String KEY_MAX_NUM_FOCUS_AREAS = "max-num-focus-areas";
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08002349 private static final String KEY_FOCAL_LENGTH = "focal-length";
2350 private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle";
2351 private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle";
Wu-cheng Liff723b62010-02-09 13:38:19 +08002352 private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation";
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002353 private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation";
2354 private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation";
2355 private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step";
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002356 private static final String KEY_AUTO_EXPOSURE_LOCK = "auto-exposure-lock";
2357 private static final String KEY_AUTO_EXPOSURE_LOCK_SUPPORTED = "auto-exposure-lock-supported";
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002358 private static final String KEY_AUTO_WHITEBALANCE_LOCK = "auto-whitebalance-lock";
2359 private static final String KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED = "auto-whitebalance-lock-supported";
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002360 private static final String KEY_METERING_AREAS = "metering-areas";
2361 private static final String KEY_MAX_NUM_METERING_AREAS = "max-num-metering-areas";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002362 private static final String KEY_ZOOM = "zoom";
2363 private static final String KEY_MAX_ZOOM = "max-zoom";
2364 private static final String KEY_ZOOM_RATIOS = "zoom-ratios";
2365 private static final String KEY_ZOOM_SUPPORTED = "zoom-supported";
2366 private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002367 private static final String KEY_FOCUS_DISTANCES = "focus-distances";
James Dongdd0b16c2010-09-21 16:23:48 -07002368 private static final String KEY_VIDEO_SIZE = "video-size";
2369 private static final String KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO =
2370 "preferred-preview-size-for-video";
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08002371 private static final String KEY_MAX_NUM_DETECTED_FACES_HW = "max-num-detected-faces-hw";
2372 private static final String KEY_MAX_NUM_DETECTED_FACES_SW = "max-num-detected-faces-sw";
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08002373 private static final String KEY_RECORDING_HINT = "recording-hint";
Wu-cheng Li98bb2512011-08-30 21:33:10 +08002374 private static final String KEY_VIDEO_SNAPSHOT_SUPPORTED = "video-snapshot-supported";
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07002375 private static final String KEY_VIDEO_STABILIZATION = "video-stabilization";
2376 private static final String KEY_VIDEO_STABILIZATION_SUPPORTED = "video-stabilization-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002377
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002378 // Parameter key suffix for supported values.
2379 private static final String SUPPORTED_VALUES_SUFFIX = "-values";
2380
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002381 private static final String TRUE = "true";
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002382 private static final String FALSE = "false";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002383
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002384 // Values for white balance settings.
2385 public static final String WHITE_BALANCE_AUTO = "auto";
2386 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent";
2387 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent";
2388 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent";
2389 public static final String WHITE_BALANCE_DAYLIGHT = "daylight";
2390 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight";
2391 public static final String WHITE_BALANCE_TWILIGHT = "twilight";
2392 public static final String WHITE_BALANCE_SHADE = "shade";
2393
2394 // Values for color effect settings.
2395 public static final String EFFECT_NONE = "none";
2396 public static final String EFFECT_MONO = "mono";
2397 public static final String EFFECT_NEGATIVE = "negative";
2398 public static final String EFFECT_SOLARIZE = "solarize";
2399 public static final String EFFECT_SEPIA = "sepia";
2400 public static final String EFFECT_POSTERIZE = "posterize";
2401 public static final String EFFECT_WHITEBOARD = "whiteboard";
2402 public static final String EFFECT_BLACKBOARD = "blackboard";
2403 public static final String EFFECT_AQUA = "aqua";
2404
2405 // Values for antibanding settings.
2406 public static final String ANTIBANDING_AUTO = "auto";
2407 public static final String ANTIBANDING_50HZ = "50hz";
2408 public static final String ANTIBANDING_60HZ = "60hz";
2409 public static final String ANTIBANDING_OFF = "off";
2410
2411 // Values for flash mode settings.
2412 /**
2413 * Flash will not be fired.
2414 */
2415 public static final String FLASH_MODE_OFF = "off";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002416
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002417 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07002418 * Flash will be fired automatically when required. The flash may be fired
2419 * during preview, auto-focus, or snapshot depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002420 */
2421 public static final String FLASH_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002422
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002423 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07002424 * Flash will always be fired during snapshot. The flash may also be
2425 * fired during preview or auto-focus depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002426 */
2427 public static final String FLASH_MODE_ON = "on";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002428
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002429 /**
2430 * Flash will be fired in red-eye reduction mode.
2431 */
2432 public static final String FLASH_MODE_RED_EYE = "red-eye";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002433
Wu-cheng Li36322db2009-09-18 18:59:21 +08002434 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07002435 * Constant emission of light during preview, auto-focus and snapshot.
2436 * This can also be used for video recording.
Wu-cheng Li36322db2009-09-18 18:59:21 +08002437 */
Wu-cheng Li068ef422009-09-27 13:19:36 -07002438 public static final String FLASH_MODE_TORCH = "torch";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002439
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002440 /**
2441 * Scene mode is off.
2442 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002443 public static final String SCENE_MODE_AUTO = "auto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002444
2445 /**
2446 * Take photos of fast moving objects. Same as {@link
2447 * #SCENE_MODE_SPORTS}.
2448 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002449 public static final String SCENE_MODE_ACTION = "action";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002450
2451 /**
2452 * Take people pictures.
2453 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002454 public static final String SCENE_MODE_PORTRAIT = "portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002455
2456 /**
2457 * Take pictures on distant objects.
2458 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002459 public static final String SCENE_MODE_LANDSCAPE = "landscape";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002460
2461 /**
2462 * Take photos at night.
2463 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002464 public static final String SCENE_MODE_NIGHT = "night";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002465
2466 /**
2467 * Take people pictures at night.
2468 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002469 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002470
2471 /**
2472 * Take photos in a theater. Flash light is off.
2473 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002474 public static final String SCENE_MODE_THEATRE = "theatre";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002475
2476 /**
2477 * Take pictures on the beach.
2478 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002479 public static final String SCENE_MODE_BEACH = "beach";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002480
2481 /**
2482 * Take pictures on the snow.
2483 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002484 public static final String SCENE_MODE_SNOW = "snow";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002485
2486 /**
2487 * Take sunset photos.
2488 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002489 public static final String SCENE_MODE_SUNSET = "sunset";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002490
2491 /**
2492 * Avoid blurry pictures (for example, due to hand shake).
2493 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002494 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002495
2496 /**
2497 * For shooting firework displays.
2498 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002499 public static final String SCENE_MODE_FIREWORKS = "fireworks";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002500
2501 /**
2502 * Take photos of fast moving objects. Same as {@link
2503 * #SCENE_MODE_ACTION}.
2504 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002505 public static final String SCENE_MODE_SPORTS = "sports";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002506
2507 /**
2508 * Take indoor low-light shot.
2509 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002510 public static final String SCENE_MODE_PARTY = "party";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002511
2512 /**
2513 * Capture the naturally warm color of scenes lit by candles.
2514 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002515 public static final String SCENE_MODE_CANDLELIGHT = "candlelight";
2516
Wu-cheng Lic58b4232010-03-29 17:21:28 +08002517 /**
2518 * Applications are looking for a barcode. Camera driver will be
2519 * optimized for barcode reading.
2520 */
2521 public static final String SCENE_MODE_BARCODE = "barcode";
2522
Wu-cheng Li36322db2009-09-18 18:59:21 +08002523 /**
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07002524 * Capture a scene using high dynamic range imaging techniques. The
2525 * camera will return an image that has an extended dynamic range
2526 * compared to a regular capture. Capturing such an image may take
2527 * longer than a regular capture.
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07002528 */
2529 public static final String SCENE_MODE_HDR = "hdr";
2530
2531 /**
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07002532 * Auto-focus mode. Applications should call {@link
2533 * #autoFocus(AutoFocusCallback)} to start the focus in this mode.
Wu-cheng Li36322db2009-09-18 18:59:21 +08002534 */
2535 public static final String FOCUS_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002536
Wu-cheng Li36322db2009-09-18 18:59:21 +08002537 /**
2538 * Focus is set at infinity. Applications should not call
2539 * {@link #autoFocus(AutoFocusCallback)} in this mode.
2540 */
2541 public static final String FOCUS_MODE_INFINITY = "infinity";
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07002542
2543 /**
2544 * Macro (close-up) focus mode. Applications should call
2545 * {@link #autoFocus(AutoFocusCallback)} to start the focus in this
2546 * mode.
2547 */
Wu-cheng Li36322db2009-09-18 18:59:21 +08002548 public static final String FOCUS_MODE_MACRO = "macro";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002549
Wu-cheng Li36322db2009-09-18 18:59:21 +08002550 /**
2551 * Focus is fixed. The camera is always in this mode if the focus is not
2552 * adjustable. If the camera has auto-focus, this mode can fix the
2553 * focus, which is usually at hyperfocal distance. Applications should
2554 * not call {@link #autoFocus(AutoFocusCallback)} in this mode.
2555 */
2556 public static final String FOCUS_MODE_FIXED = "fixed";
2557
Wu-cheng Lic58b4232010-03-29 17:21:28 +08002558 /**
2559 * Extended depth of field (EDOF). Focusing is done digitally and
2560 * continuously. Applications should not call {@link
2561 * #autoFocus(AutoFocusCallback)} in this mode.
2562 */
2563 public static final String FOCUS_MODE_EDOF = "edof";
2564
Wu-cheng Li699fe932010-08-05 11:50:25 -07002565 /**
Wu-cheng Lid45cb722010-09-20 16:15:32 -07002566 * Continuous auto focus mode intended for video recording. The camera
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002567 * continuously tries to focus. This is the best choice for video
2568 * recording because the focus changes smoothly . Applications still can
2569 * call {@link #takePicture(Camera.ShutterCallback,
2570 * Camera.PictureCallback, Camera.PictureCallback)} in this mode but the
2571 * subject may not be in focus. Auto focus starts when the parameter is
Wu-cheng Li53b30912011-10-12 19:43:51 +08002572 * set.
2573 *
2574 * <p>Since API level 14, applications can call {@link
2575 * #autoFocus(AutoFocusCallback)} in this mode. The focus callback will
2576 * immediately return with a boolean that indicates whether the focus is
2577 * sharp or not. The focus position is locked after autoFocus call. If
2578 * applications want to resume the continuous focus, cancelAutoFocus
2579 * must be called. Restarting the preview will not resume the continuous
2580 * autofocus. To stop continuous focus, applications should change the
2581 * focus mode to other modes.
2582 *
2583 * @see #FOCUS_MODE_CONTINUOUS_PICTURE
Wu-cheng Li699fe932010-08-05 11:50:25 -07002584 */
Wu-cheng Lid45cb722010-09-20 16:15:32 -07002585 public static final String FOCUS_MODE_CONTINUOUS_VIDEO = "continuous-video";
Wu-cheng Li699fe932010-08-05 11:50:25 -07002586
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002587 /**
2588 * Continuous auto focus mode intended for taking pictures. The camera
2589 * continuously tries to focus. The speed of focus change is more
2590 * aggressive than {@link #FOCUS_MODE_CONTINUOUS_VIDEO}. Auto focus
Wu-cheng Li53b30912011-10-12 19:43:51 +08002591 * starts when the parameter is set.
2592 *
Wu-cheng Li0f4f97b2011-10-27 18:07:01 +08002593 * <p>Applications can call {@link #autoFocus(AutoFocusCallback)} in
2594 * this mode. If the autofocus is in the middle of scanning, the focus
2595 * callback will return when it completes. If the autofocus is not
2596 * scanning, the focus callback will immediately return with a boolean
2597 * that indicates whether the focus is sharp or not. The apps can then
2598 * decide if they want to take a picture immediately or to change the
2599 * focus mode to auto, and run a full autofocus cycle. The focus
2600 * position is locked after autoFocus call. If applications want to
2601 * resume the continuous focus, cancelAutoFocus must be called.
2602 * Restarting the preview will not resume the continuous autofocus. To
2603 * stop continuous focus, applications should change the focus mode to
2604 * other modes.
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002605 *
2606 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002607 */
2608 public static final String FOCUS_MODE_CONTINUOUS_PICTURE = "continuous-picture";
2609
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002610 // Indices for focus distance array.
2611 /**
2612 * The array index of near focus distance for use with
2613 * {@link #getFocusDistances(float[])}.
2614 */
2615 public static final int FOCUS_DISTANCE_NEAR_INDEX = 0;
2616
2617 /**
2618 * The array index of optimal focus distance for use with
2619 * {@link #getFocusDistances(float[])}.
2620 */
2621 public static final int FOCUS_DISTANCE_OPTIMAL_INDEX = 1;
2622
2623 /**
2624 * The array index of far focus distance for use with
2625 * {@link #getFocusDistances(float[])}.
2626 */
2627 public static final int FOCUS_DISTANCE_FAR_INDEX = 2;
2628
Wu-cheng Lica099612010-05-06 16:47:30 +08002629 /**
Wu-cheng Li454630f2010-08-11 16:48:05 -07002630 * The array index of minimum preview fps for use with {@link
2631 * #getPreviewFpsRange(int[])} or {@link
2632 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07002633 */
2634 public static final int PREVIEW_FPS_MIN_INDEX = 0;
2635
2636 /**
2637 * The array index of maximum preview fps for use with {@link
2638 * #getPreviewFpsRange(int[])} or {@link
2639 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07002640 */
2641 public static final int PREVIEW_FPS_MAX_INDEX = 1;
2642
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002643 // Formats for setPreviewFormat and setPictureFormat.
2644 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp";
2645 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp";
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002646 private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv";
Wu-cheng Li10a1b302011-02-22 15:49:25 +08002647 private static final String PIXEL_FORMAT_YUV420P = "yuv420p";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002648 private static final String PIXEL_FORMAT_RGB565 = "rgb565";
2649 private static final String PIXEL_FORMAT_JPEG = "jpeg";
Wu-cheng Li70fb9082011-08-02 17:49:50 +08002650 private static final String PIXEL_FORMAT_BAYER_RGGB = "bayer-rggb";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002651
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002652 /**
2653 * Order matters: Keys that are {@link #set(String, String) set} later
2654 * will take precedence over keys that are set earlier (if the two keys
2655 * conflict with each other).
2656 *
2657 * <p>One example is {@link #setPreviewFpsRange(int, int)} , since it
2658 * conflicts with {@link #setPreviewFrameRate(int)} whichever key is set later
2659 * is the one that will take precedence.
2660 * </p>
2661 */
2662 private final LinkedHashMap<String, String> mMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663
2664 private Parameters() {
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002665 mMap = new LinkedHashMap<String, String>(/*initialCapacity*/64);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 }
2667
2668 /**
Igor Murashkindf6242e2014-07-01 18:06:13 -07002669 * Overwrite existing parameters with a copy of the ones from {@code other}.
2670 *
2671 * <b>For use by the legacy shim only.</b>
2672 *
2673 * @hide
2674 */
Mathew Inwoodbcbe4402018-08-08 15:42:59 +01002675 @UnsupportedAppUsage
Igor Murashkindf6242e2014-07-01 18:06:13 -07002676 public void copyFrom(Parameters other) {
2677 if (other == null) {
2678 throw new NullPointerException("other must not be null");
2679 }
2680
2681 mMap.putAll(other.mMap);
2682 }
2683
2684 private Camera getOuter() {
2685 return Camera.this;
2686 }
2687
Ruben Brunkd1f113d2014-07-11 11:46:20 -07002688
2689 /**
2690 * Value equality check.
2691 *
2692 * @hide
2693 */
2694 public boolean same(Parameters other) {
2695 if (this == other) {
2696 return true;
2697 }
2698 return other != null && Parameters.this.mMap.equals(other.mMap);
2699 }
2700
Igor Murashkindf6242e2014-07-01 18:06:13 -07002701 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 * Writes the current Parameters to the log.
2703 * @hide
2704 * @deprecated
2705 */
Igor Murashkina1d66272014-06-20 11:22:11 -07002706 @Deprecated
Mathew Inwoodbcbe4402018-08-08 15:42:59 +01002707 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 public void dump() {
2709 Log.e(TAG, "dump: size=" + mMap.size());
2710 for (String k : mMap.keySet()) {
2711 Log.e(TAG, "dump: " + k + "=" + mMap.get(k));
2712 }
2713 }
2714
2715 /**
2716 * Creates a single string with all the parameters set in
2717 * this Parameters object.
2718 * <p>The {@link #unflatten(String)} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002719 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 * @return a String with all values from this Parameters object, in
2721 * semi-colon delimited key-value pairs
2722 */
2723 public String flatten() {
Ali Utku Selen0a120182011-02-09 14:11:22 +01002724 StringBuilder flattened = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 for (String k : mMap.keySet()) {
2726 flattened.append(k);
2727 flattened.append("=");
2728 flattened.append(mMap.get(k));
2729 flattened.append(";");
2730 }
2731 // chop off the extra semicolon at the end
2732 flattened.deleteCharAt(flattened.length()-1);
2733 return flattened.toString();
2734 }
2735
2736 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002737 * Takes a flattened string of parameters and adds each one to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 * this Parameters object.
2739 * <p>The {@link #flatten()} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002740 *
2741 * @param flattened a String of parameters (key-value paired) that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 * are semi-colon delimited
2743 */
2744 public void unflatten(String flattened) {
2745 mMap.clear();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002746
Ali Utku Selen0a120182011-02-09 14:11:22 +01002747 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(';');
2748 splitter.setString(flattened);
2749 for (String kv : splitter) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 int pos = kv.indexOf('=');
2751 if (pos == -1) {
2752 continue;
2753 }
2754 String k = kv.substring(0, pos);
2755 String v = kv.substring(pos + 1);
2756 mMap.put(k, v);
2757 }
2758 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 public void remove(String key) {
2761 mMap.remove(key);
2762 }
2763
2764 /**
2765 * Sets a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002766 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 * @param key the key name for the parameter
2768 * @param value the String value of the parameter
2769 */
2770 public void set(String key, String value) {
Eino-Ville Talvalacb569232012-03-12 11:36:58 -07002771 if (key.indexOf('=') != -1 || key.indexOf(';') != -1 || key.indexOf(0) != -1) {
2772 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ; or \\0)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 return;
2774 }
Eino-Ville Talvalacb569232012-03-12 11:36:58 -07002775 if (value.indexOf('=') != -1 || value.indexOf(';') != -1 || value.indexOf(0) != -1) {
2776 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ; or \\0)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 return;
2778 }
2779
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002780 put(key, value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 }
2782
2783 /**
2784 * Sets an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002785 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 * @param key the key name for the parameter
2787 * @param value the int value of the parameter
2788 */
2789 public void set(String key, int value) {
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002790 put(key, Integer.toString(value));
2791 }
2792
2793 private void put(String key, String value) {
2794 /*
2795 * Remove the key if it already exists.
2796 *
2797 * This way setting a new value for an already existing key will always move
2798 * that key to be ordered the latest in the map.
2799 */
2800 mMap.remove(key);
2801 mMap.put(key, value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 }
2803
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002804 private void set(String key, List<Area> areas) {
Wu-cheng Lif715bf92011-04-14 14:04:18 +08002805 if (areas == null) {
2806 set(key, "(0,0,0,0,0)");
2807 } else {
2808 StringBuilder buffer = new StringBuilder();
2809 for (int i = 0; i < areas.size(); i++) {
2810 Area area = areas.get(i);
2811 Rect rect = area.rect;
2812 buffer.append('(');
2813 buffer.append(rect.left);
2814 buffer.append(',');
2815 buffer.append(rect.top);
2816 buffer.append(',');
2817 buffer.append(rect.right);
2818 buffer.append(',');
2819 buffer.append(rect.bottom);
2820 buffer.append(',');
2821 buffer.append(area.weight);
2822 buffer.append(')');
2823 if (i != areas.size() - 1) buffer.append(',');
2824 }
2825 set(key, buffer.toString());
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002826 }
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002827 }
2828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 /**
2830 * Returns the value of a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002831 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 * @param key the key name for the parameter
2833 * @return the String value of the parameter
2834 */
2835 public String get(String key) {
2836 return mMap.get(key);
2837 }
2838
2839 /**
2840 * Returns the value of an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002841 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 * @param key the key name for the parameter
2843 * @return the int value of the parameter
2844 */
2845 public int getInt(String key) {
2846 return Integer.parseInt(mMap.get(key));
2847 }
2848
2849 /**
Wu-cheng Li26274fa2011-05-05 14:36:28 +08002850 * Sets the dimensions for preview pictures. If the preview has already
2851 * started, applications should stop the preview first before changing
2852 * preview size.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002853 *
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002854 * The sides of width and height are based on camera orientation. That
2855 * is, the preview size is the size before it is rotated by display
2856 * orientation. So applications need to consider the display orientation
2857 * while setting preview size. For example, suppose the camera supports
2858 * both 480x320 and 320x480 preview sizes. The application wants a 3:2
2859 * preview ratio. If the display orientation is set to 0 or 180, preview
2860 * size should be set to 480x320. If the display orientation is set to
2861 * 90 or 270, preview size should be set to 320x480. The display
2862 * orientation should also be considered while setting picture size and
2863 * thumbnail size.
2864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 * @param width the width of the pictures, in pixels
2866 * @param height the height of the pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002867 * @see #setDisplayOrientation(int)
2868 * @see #getCameraInfo(int, CameraInfo)
2869 * @see #setPictureSize(int, int)
2870 * @see #setJpegThumbnailSize(int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 */
2872 public void setPreviewSize(int width, int height) {
2873 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002874 set(KEY_PREVIEW_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 }
2876
2877 /**
2878 * Returns the dimensions setting for preview pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002879 *
James Dongdd0b16c2010-09-21 16:23:48 -07002880 * @return a Size object with the width and height setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 * for the preview picture
2882 */
2883 public Size getPreviewSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002884 String pair = get(KEY_PREVIEW_SIZE);
2885 return strToSize(pair);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 }
2887
2888 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002889 * Gets the supported preview sizes.
2890 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002891 * @return a list of Size object. This method will always return a list
Wu-cheng Li9c799382009-12-04 19:59:18 +08002892 * with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002893 */
2894 public List<Size> getSupportedPreviewSizes() {
2895 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX);
2896 return splitSize(str);
2897 }
2898
2899 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002900 * <p>Gets the supported video frame sizes that can be used by
2901 * MediaRecorder.</p>
James Dongdd0b16c2010-09-21 16:23:48 -07002902 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002903 * <p>If the returned list is not null, the returned list will contain at
James Dongdd0b16c2010-09-21 16:23:48 -07002904 * least one Size and one of the sizes in the returned list must be
2905 * passed to MediaRecorder.setVideoSize() for camcorder application if
2906 * camera is used as the video source. In this case, the size of the
2907 * preview can be different from the resolution of the recorded video
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002908 * during video recording.</p>
James Dongdd0b16c2010-09-21 16:23:48 -07002909 *
2910 * @return a list of Size object if camera has separate preview and
2911 * video output; otherwise, null is returned.
2912 * @see #getPreferredPreviewSizeForVideo()
2913 */
2914 public List<Size> getSupportedVideoSizes() {
2915 String str = get(KEY_VIDEO_SIZE + SUPPORTED_VALUES_SUFFIX);
2916 return splitSize(str);
2917 }
2918
2919 /**
2920 * Returns the preferred or recommended preview size (width and height)
2921 * in pixels for video recording. Camcorder applications should
2922 * set the preview size to a value that is not larger than the
2923 * preferred preview size. In other words, the product of the width
2924 * and height of the preview size should not be larger than that of
2925 * the preferred preview size. In addition, we recommend to choose a
2926 * preview size that has the same aspect ratio as the resolution of
2927 * video to be recorded.
2928 *
2929 * @return the preferred preview size (width and height) in pixels for
2930 * video recording if getSupportedVideoSizes() does not return
2931 * null; otherwise, null is returned.
2932 * @see #getSupportedVideoSizes()
2933 */
2934 public Size getPreferredPreviewSizeForVideo() {
2935 String pair = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
2936 return strToSize(pair);
2937 }
2938
2939 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002940 * <p>Sets the dimensions for EXIF thumbnail in Jpeg picture. If
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002941 * applications set both width and height to 0, EXIF will not contain
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002942 * thumbnail.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002943 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002944 * <p>Applications need to consider the display orientation. See {@link
2945 * #setPreviewSize(int,int)} for reference.</p>
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002946 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 * @param width the width of the thumbnail, in pixels
2948 * @param height the height of the thumbnail, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002949 * @see #setPreviewSize(int,int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002951 public void setJpegThumbnailSize(int width, int height) {
2952 set(KEY_JPEG_THUMBNAIL_WIDTH, width);
2953 set(KEY_JPEG_THUMBNAIL_HEIGHT, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 }
2955
2956 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002957 * Returns the dimensions for EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002959 * @return a Size object with the height and width setting for the EXIF
2960 * thumbnails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002962 public Size getJpegThumbnailSize() {
2963 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH),
2964 getInt(KEY_JPEG_THUMBNAIL_HEIGHT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 }
2966
2967 /**
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002968 * Gets the supported jpeg thumbnail sizes.
2969 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002970 * @return a list of Size object. This method will always return a list
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002971 * with at least two elements. Size 0,0 (no thumbnail) is always
2972 * supported.
2973 */
2974 public List<Size> getSupportedJpegThumbnailSizes() {
2975 String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX);
2976 return splitSize(str);
2977 }
2978
2979 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002980 * Sets the quality of the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002982 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1
2983 * to 100, with 100 being the best.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002985 public void setJpegThumbnailQuality(int quality) {
2986 set(KEY_JPEG_THUMBNAIL_QUALITY, quality);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 }
2988
2989 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002990 * Returns the quality setting for the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002992 * @return the JPEG quality setting of the EXIF thumbnail.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002994 public int getJpegThumbnailQuality() {
2995 return getInt(KEY_JPEG_THUMBNAIL_QUALITY);
2996 }
2997
2998 /**
2999 * Sets Jpeg quality of captured picture.
3000 *
3001 * @param quality the JPEG quality of captured picture. The range is 1
3002 * to 100, with 100 being the best.
3003 */
3004 public void setJpegQuality(int quality) {
3005 set(KEY_JPEG_QUALITY, quality);
3006 }
3007
3008 /**
3009 * Returns the quality setting for the JPEG picture.
3010 *
3011 * @return the JPEG picture quality setting.
3012 */
3013 public int getJpegQuality() {
3014 return getInt(KEY_JPEG_QUALITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 }
3016
3017 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08003018 * Sets the rate at which preview frames are received. This is the
3019 * target frame rate. The actual frame rate depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003020 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 * @param fps the frame rate (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003022 * @deprecated replaced by {@link #setPreviewFpsRange(int,int)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003024 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 public void setPreviewFrameRate(int fps) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003026 set(KEY_PREVIEW_FRAME_RATE, fps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 }
3028
3029 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08003030 * Returns the setting for the rate at which preview frames are
3031 * received. This is the target frame rate. The actual frame rate
3032 * depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003033 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 * @return the frame rate setting (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003035 * @deprecated replaced by {@link #getPreviewFpsRange(int[])}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003037 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 public int getPreviewFrameRate() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003039 return getInt(KEY_PREVIEW_FRAME_RATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 }
3041
3042 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003043 * Gets the supported preview frame rates.
3044 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003045 * @return a list of supported preview frame rates. null if preview
3046 * frame rate setting is not supported.
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003047 * @deprecated replaced by {@link #getSupportedPreviewFpsRange()}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003048 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003049 @Deprecated
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003050 public List<Integer> getSupportedPreviewFrameRates() {
3051 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX);
3052 return splitInt(str);
3053 }
3054
3055 /**
Hai Guo4a68e3c2012-12-05 21:50:28 +08003056 * Sets the minimum and maximum preview fps. This controls the rate of
Wu-cheng Li1620d112010-08-27 15:09:20 -07003057 * preview frames received in {@link PreviewCallback}. The minimum and
Wu-cheng Li454630f2010-08-11 16:48:05 -07003058 * maximum preview fps must be one of the elements from {@link
3059 * #getSupportedPreviewFpsRange}.
3060 *
3061 * @param min the minimum preview fps (scaled by 1000).
3062 * @param max the maximum preview fps (scaled by 1000).
3063 * @throws RuntimeException if fps range is invalid.
3064 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
3065 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07003066 */
3067 public void setPreviewFpsRange(int min, int max) {
3068 set(KEY_PREVIEW_FPS_RANGE, "" + min + "," + max);
3069 }
3070
3071 /**
3072 * Returns the current minimum and maximum preview fps. The values are
3073 * one of the elements returned by {@link #getSupportedPreviewFpsRange}.
3074 *
3075 * @return range the minimum and maximum preview fps (scaled by 1000).
3076 * @see #PREVIEW_FPS_MIN_INDEX
3077 * @see #PREVIEW_FPS_MAX_INDEX
3078 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07003079 */
3080 public void getPreviewFpsRange(int[] range) {
3081 if (range == null || range.length != 2) {
3082 throw new IllegalArgumentException(
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003083 "range must be an array with two elements.");
Wu-cheng Li454630f2010-08-11 16:48:05 -07003084 }
3085 splitInt(get(KEY_PREVIEW_FPS_RANGE), range);
3086 }
3087
3088 /**
3089 * Gets the supported preview fps (frame-per-second) ranges. Each range
3090 * contains a minimum fps and maximum fps. If minimum fps equals to
3091 * maximum fps, the camera outputs frames in fixed frame rate. If not,
3092 * the camera outputs frames in auto frame rate. The actual frame rate
3093 * fluctuates between the minimum and the maximum. The values are
3094 * multiplied by 1000 and represented in integers. For example, if frame
3095 * rate is 26.623 frames per second, the value is 26623.
3096 *
3097 * @return a list of supported preview fps ranges. This method returns a
3098 * list with at least one element. Every element is an int array
3099 * of two values - minimum fps and maximum fps. The list is
3100 * sorted from small to large (first by maximum fps and then
3101 * minimum fps).
3102 * @see #PREVIEW_FPS_MIN_INDEX
3103 * @see #PREVIEW_FPS_MAX_INDEX
Wu-cheng Li454630f2010-08-11 16:48:05 -07003104 */
3105 public List<int[]> getSupportedPreviewFpsRange() {
3106 String str = get(KEY_PREVIEW_FPS_RANGE + SUPPORTED_VALUES_SUFFIX);
3107 return splitRange(str);
3108 }
3109
3110 /**
Wu-cheng Li7478ea62009-09-16 18:52:55 +08003111 * Sets the image format for preview pictures.
Scott Mainda0a56d2009-09-10 18:08:37 -07003112 * <p>If this is never called, the default format will be
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003113 * {@link android.graphics.ImageFormat#NV21}, which
Scott Maindf4578e2009-09-10 12:22:07 -07003114 * uses the NV21 encoding format.</p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +08003115 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003116 * <p>Use {@link Parameters#getSupportedPreviewFormats} to get a list of
3117 * the available preview formats.
3118 *
3119 * <p>It is strongly recommended that either
3120 * {@link android.graphics.ImageFormat#NV21} or
3121 * {@link android.graphics.ImageFormat#YV12} is used, since
3122 * they are supported by all camera devices.</p>
3123 *
3124 * <p>For YV12, the image buffer that is received is not necessarily
3125 * tightly packed, as there may be padding at the end of each row of
3126 * pixel data, as described in
3127 * {@link android.graphics.ImageFormat#YV12}. For camera callback data,
3128 * it can be assumed that the stride of the Y and UV data is the
3129 * smallest possible that meets the alignment requirements. That is, if
3130 * the preview size is <var>width x height</var>, then the following
3131 * equations describe the buffer index for the beginning of row
3132 * <var>y</var> for the Y plane and row <var>c</var> for the U and V
3133 * planes:
3134 *
Neil Fuller71fbb812015-11-30 09:51:33 +00003135 * <pre>{@code
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003136 * yStride = (int) ceil(width / 16.0) * 16;
3137 * uvStride = (int) ceil( (yStride / 2) / 16.0) * 16;
3138 * ySize = yStride * height;
3139 * uvSize = uvStride * height / 2;
3140 * yRowIndex = yStride * y;
3141 * uRowIndex = ySize + uvSize + uvStride * c;
3142 * vRowIndex = ySize + uvStride * c;
Neil Fuller71fbb812015-11-30 09:51:33 +00003143 * size = ySize + uvSize * 2;
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003144 * }
Neil Fuller71fbb812015-11-30 09:51:33 +00003145 *</pre>
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003146 *
3147 * @param pixel_format the desired preview picture format, defined by
3148 * one of the {@link android.graphics.ImageFormat} constants. (E.g.,
3149 * <var>ImageFormat.NV21</var> (default), or
3150 * <var>ImageFormat.YV12</var>)
3151 *
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003152 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003153 * @see android.hardware.Camera.Parameters#getSupportedPreviewFormats
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 */
3155 public void setPreviewFormat(int pixel_format) {
3156 String s = cameraFormatForPixelFormat(pixel_format);
3157 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003158 throw new IllegalArgumentException(
3159 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 }
3161
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003162 set(KEY_PREVIEW_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 }
3164
3165 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003166 * Returns the image format for preview frames got from
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003167 * {@link PreviewCallback}.
Wu-cheng Li7478ea62009-09-16 18:52:55 +08003168 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003169 * @return the preview format.
3170 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003171 * @see #setPreviewFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 */
3173 public int getPreviewFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003174 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT));
3175 }
3176
3177 /**
Wu-cheng Lif9293e72011-02-25 13:35:10 +08003178 * Gets the supported preview formats. {@link android.graphics.ImageFormat#NV21}
3179 * is always supported. {@link android.graphics.ImageFormat#YV12}
3180 * is always supported since API level 12.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003181 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003182 * @return a list of supported preview formats. This method will always
3183 * return a list with at least one element.
3184 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003185 * @see #setPreviewFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003186 */
3187 public List<Integer> getSupportedPreviewFormats() {
3188 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX);
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003189 ArrayList<Integer> formats = new ArrayList<Integer>();
3190 for (String s : split(str)) {
3191 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003192 if (f == ImageFormat.UNKNOWN) continue;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003193 formats.add(f);
3194 }
3195 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 }
3197
3198 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003199 * <p>Sets the dimensions for pictures.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003200 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003201 * <p>Applications need to consider the display orientation. See {@link
3202 * #setPreviewSize(int,int)} for reference.</p>
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08003203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 * @param width the width for pictures, in pixels
3205 * @param height the height for pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08003206 * @see #setPreviewSize(int,int)
3207 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 */
3209 public void setPictureSize(int width, int height) {
3210 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003211 set(KEY_PICTURE_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 }
3213
3214 /**
3215 * Returns the dimension setting for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003216 *
3217 * @return a Size object with the height and width setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 * for pictures
3219 */
3220 public Size getPictureSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003221 String pair = get(KEY_PICTURE_SIZE);
3222 return strToSize(pair);
3223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003225 /**
3226 * Gets the supported picture sizes.
3227 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003228 * @return a list of supported picture sizes. This method will always
3229 * return a list with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003230 */
3231 public List<Size> getSupportedPictureSizes() {
3232 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX);
3233 return splitSize(str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 }
3235
3236 /**
3237 * Sets the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003238 *
3239 * @param pixel_format the desired picture format
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003240 * (<var>ImageFormat.NV21</var>,
3241 * <var>ImageFormat.RGB_565</var>, or
3242 * <var>ImageFormat.JPEG</var>)
3243 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 */
3245 public void setPictureFormat(int pixel_format) {
3246 String s = cameraFormatForPixelFormat(pixel_format);
3247 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003248 throw new IllegalArgumentException(
3249 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 }
3251
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003252 set(KEY_PICTURE_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 }
3254
3255 /**
3256 * Returns the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003257 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003258 * @return the picture format
3259 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 */
3261 public int getPictureFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003262 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT));
3263 }
3264
3265 /**
3266 * Gets the supported picture formats.
3267 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003268 * @return supported picture formats. This method will always return a
3269 * list with at least one element.
3270 * @see android.graphics.ImageFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003271 */
3272 public List<Integer> getSupportedPictureFormats() {
Wu-cheng Li9c799382009-12-04 19:59:18 +08003273 String str = get(KEY_PICTURE_FORMAT + SUPPORTED_VALUES_SUFFIX);
3274 ArrayList<Integer> formats = new ArrayList<Integer>();
3275 for (String s : split(str)) {
3276 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003277 if (f == ImageFormat.UNKNOWN) continue;
Wu-cheng Li9c799382009-12-04 19:59:18 +08003278 formats.add(f);
3279 }
3280 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 }
3282
3283 private String cameraFormatForPixelFormat(int pixel_format) {
3284 switch(pixel_format) {
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003285 case ImageFormat.NV16: return PIXEL_FORMAT_YUV422SP;
3286 case ImageFormat.NV21: return PIXEL_FORMAT_YUV420SP;
3287 case ImageFormat.YUY2: return PIXEL_FORMAT_YUV422I;
Wu-cheng Li10a1b302011-02-22 15:49:25 +08003288 case ImageFormat.YV12: return PIXEL_FORMAT_YUV420P;
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003289 case ImageFormat.RGB_565: return PIXEL_FORMAT_RGB565;
3290 case ImageFormat.JPEG: return PIXEL_FORMAT_JPEG;
3291 default: return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 }
3293 }
3294
3295 private int pixelFormatForCameraFormat(String format) {
3296 if (format == null)
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003297 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003299 if (format.equals(PIXEL_FORMAT_YUV422SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003300 return ImageFormat.NV16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003302 if (format.equals(PIXEL_FORMAT_YUV420SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003303 return ImageFormat.NV21;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003305 if (format.equals(PIXEL_FORMAT_YUV422I))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003306 return ImageFormat.YUY2;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003307
Wu-cheng Li10a1b302011-02-22 15:49:25 +08003308 if (format.equals(PIXEL_FORMAT_YUV420P))
3309 return ImageFormat.YV12;
3310
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003311 if (format.equals(PIXEL_FORMAT_RGB565))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003312 return ImageFormat.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003314 if (format.equals(PIXEL_FORMAT_JPEG))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003315 return ImageFormat.JPEG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003317 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 }
3319
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003320 /**
Wu-cheng Li8969ea12012-04-20 17:38:09 +08003321 * Sets the clockwise rotation angle in degrees relative to the
3322 * orientation of the camera. This affects the pictures returned from
3323 * JPEG {@link PictureCallback}. The camera driver may set orientation
3324 * in the EXIF header without rotating the picture. Or the driver may
3325 * rotate the picture and the EXIF thumbnail. If the Jpeg picture is
Igor Murashkina1b02db2013-06-11 15:25:20 -07003326 * rotated, the orientation in the EXIF header will be missing or 1 (row
3327 * #0 is top and column #0 is left side).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003328 *
Igor Murashkina1b02db2013-06-11 15:25:20 -07003329 * <p>
3330 * If applications want to rotate the picture to match the orientation
3331 * of what users see, apps should use
3332 * {@link android.view.OrientationEventListener} and
3333 * {@link android.hardware.Camera.CameraInfo}. The value from
3334 * OrientationEventListener is relative to the natural orientation of
3335 * the device. CameraInfo.orientation is the angle between camera
3336 * orientation and natural device orientation. The sum of the two is the
3337 * rotation angle for back-facing camera. The difference of the two is
3338 * the rotation angle for front-facing camera. Note that the JPEG
3339 * pictures of front-facing cameras are not mirrored as in preview
3340 * display.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003341 *
Igor Murashkina1b02db2013-06-11 15:25:20 -07003342 * <p>
3343 * For example, suppose the natural orientation of the device is
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003344 * portrait. The device is rotated 270 degrees clockwise, so the device
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08003345 * orientation is 270. Suppose a back-facing camera sensor is mounted in
3346 * landscape and the top side of the camera sensor is aligned with the
3347 * right edge of the display in natural orientation. So the camera
3348 * orientation is 90. The rotation should be set to 0 (270 + 90).
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003349 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08003350 * <p>The reference code is as follows.
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003351 *
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -08003352 * <pre>
Scott Main9a10bf02011-08-24 19:09:48 -07003353 * public void onOrientationChanged(int orientation) {
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003354 * if (orientation == ORIENTATION_UNKNOWN) return;
3355 * android.hardware.Camera.CameraInfo info =
3356 * new android.hardware.Camera.CameraInfo();
3357 * android.hardware.Camera.getCameraInfo(cameraId, info);
3358 * orientation = (orientation + 45) / 90 * 90;
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08003359 * int rotation = 0;
3360 * if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
3361 * rotation = (info.orientation - orientation + 360) % 360;
3362 * } else { // back-facing camera
3363 * rotation = (info.orientation + orientation) % 360;
3364 * }
3365 * mParameters.setRotation(rotation);
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003366 * }
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -08003367 * </pre>
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003368 *
3369 * @param rotation The rotation angle in degrees relative to the
3370 * orientation of the camera. Rotation can only be 0,
3371 * 90, 180 or 270.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003372 * @throws IllegalArgumentException if rotation value is invalid.
3373 * @see android.view.OrientationEventListener
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003374 * @see #getCameraInfo(int, CameraInfo)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003375 */
3376 public void setRotation(int rotation) {
3377 if (rotation == 0 || rotation == 90 || rotation == 180
3378 || rotation == 270) {
3379 set(KEY_ROTATION, Integer.toString(rotation));
3380 } else {
3381 throw new IllegalArgumentException(
3382 "Invalid rotation=" + rotation);
3383 }
3384 }
3385
3386 /**
3387 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF
3388 * header.
3389 *
3390 * @param latitude GPS latitude coordinate.
3391 */
3392 public void setGpsLatitude(double latitude) {
3393 set(KEY_GPS_LATITUDE, Double.toString(latitude));
3394 }
3395
3396 /**
3397 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF
3398 * header.
3399 *
3400 * @param longitude GPS longitude coordinate.
3401 */
3402 public void setGpsLongitude(double longitude) {
3403 set(KEY_GPS_LONGITUDE, Double.toString(longitude));
3404 }
3405
3406 /**
3407 * Sets GPS altitude. This will be stored in JPEG EXIF header.
3408 *
3409 * @param altitude GPS altitude in meters.
3410 */
3411 public void setGpsAltitude(double altitude) {
3412 set(KEY_GPS_ALTITUDE, Double.toString(altitude));
3413 }
3414
3415 /**
3416 * Sets GPS timestamp. This will be stored in JPEG EXIF header.
3417 *
3418 * @param timestamp GPS timestamp (UTC in seconds since January 1,
3419 * 1970).
3420 */
3421 public void setGpsTimestamp(long timestamp) {
3422 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp));
3423 }
3424
3425 /**
Eino-Ville Talvalad8407272015-11-08 18:27:20 -08003426 * Sets GPS processing method. The method will be stored in a UTF-8 string up to 31 bytes
3427 * long, in the JPEG EXIF header.
Ray Chen055c9862010-02-23 10:45:42 +08003428 *
3429 * @param processing_method The processing method to get this location.
3430 */
3431 public void setGpsProcessingMethod(String processing_method) {
3432 set(KEY_GPS_PROCESSING_METHOD, processing_method);
3433 }
3434
3435 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003436 * Removes GPS latitude, longitude, altitude, and timestamp from the
3437 * parameters.
3438 */
3439 public void removeGpsData() {
3440 remove(KEY_GPS_LATITUDE);
3441 remove(KEY_GPS_LONGITUDE);
3442 remove(KEY_GPS_ALTITUDE);
3443 remove(KEY_GPS_TIMESTAMP);
Ray Chen055c9862010-02-23 10:45:42 +08003444 remove(KEY_GPS_PROCESSING_METHOD);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003445 }
3446
3447 /**
3448 * Gets the current white balance setting.
3449 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003450 * @return current white balance. null if white balance setting is not
3451 * supported.
3452 * @see #WHITE_BALANCE_AUTO
3453 * @see #WHITE_BALANCE_INCANDESCENT
3454 * @see #WHITE_BALANCE_FLUORESCENT
3455 * @see #WHITE_BALANCE_WARM_FLUORESCENT
3456 * @see #WHITE_BALANCE_DAYLIGHT
3457 * @see #WHITE_BALANCE_CLOUDY_DAYLIGHT
3458 * @see #WHITE_BALANCE_TWILIGHT
3459 * @see #WHITE_BALANCE_SHADE
3460 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003461 */
3462 public String getWhiteBalance() {
3463 return get(KEY_WHITE_BALANCE);
3464 }
3465
3466 /**
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003467 * Sets the white balance. Changing the setting will release the
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08003468 * auto-white balance lock. It is recommended not to change white
3469 * balance and AWB lock at the same time.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003470 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003471 * @param value new white balance.
3472 * @see #getWhiteBalance()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003473 * @see #setAutoWhiteBalanceLock(boolean)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003474 */
3475 public void setWhiteBalance(String value) {
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08003476 String oldValue = get(KEY_WHITE_BALANCE);
3477 if (same(value, oldValue)) return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003478 set(KEY_WHITE_BALANCE, value);
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003479 set(KEY_AUTO_WHITEBALANCE_LOCK, FALSE);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003480 }
3481
3482 /**
3483 * Gets the supported white balance.
3484 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003485 * @return a list of supported white balance. null if white balance
3486 * setting is not supported.
3487 * @see #getWhiteBalance()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003488 */
3489 public List<String> getSupportedWhiteBalance() {
3490 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX);
3491 return split(str);
3492 }
3493
3494 /**
3495 * Gets the current color effect setting.
3496 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003497 * @return current color effect. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003498 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003499 * @see #EFFECT_NONE
3500 * @see #EFFECT_MONO
3501 * @see #EFFECT_NEGATIVE
3502 * @see #EFFECT_SOLARIZE
3503 * @see #EFFECT_SEPIA
3504 * @see #EFFECT_POSTERIZE
3505 * @see #EFFECT_WHITEBOARD
3506 * @see #EFFECT_BLACKBOARD
3507 * @see #EFFECT_AQUA
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003508 */
3509 public String getColorEffect() {
3510 return get(KEY_EFFECT);
3511 }
3512
3513 /**
3514 * Sets the current color effect setting.
3515 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003516 * @param value new color effect.
3517 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003518 */
3519 public void setColorEffect(String value) {
3520 set(KEY_EFFECT, value);
3521 }
3522
3523 /**
3524 * Gets the supported color effects.
3525 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003526 * @return a list of supported color effects. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003527 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003528 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003529 */
3530 public List<String> getSupportedColorEffects() {
3531 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX);
3532 return split(str);
3533 }
3534
3535
3536 /**
3537 * Gets the current antibanding setting.
3538 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003539 * @return current antibanding. null if antibanding setting is not
3540 * supported.
3541 * @see #ANTIBANDING_AUTO
3542 * @see #ANTIBANDING_50HZ
3543 * @see #ANTIBANDING_60HZ
3544 * @see #ANTIBANDING_OFF
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003545 */
3546 public String getAntibanding() {
3547 return get(KEY_ANTIBANDING);
3548 }
3549
3550 /**
3551 * Sets the antibanding.
3552 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003553 * @param antibanding new antibanding value.
3554 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003555 */
3556 public void setAntibanding(String antibanding) {
3557 set(KEY_ANTIBANDING, antibanding);
3558 }
3559
3560 /**
3561 * Gets the supported antibanding values.
3562 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003563 * @return a list of supported antibanding values. null if antibanding
3564 * setting is not supported.
3565 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003566 */
3567 public List<String> getSupportedAntibanding() {
3568 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX);
3569 return split(str);
3570 }
3571
3572 /**
3573 * Gets the current scene mode setting.
3574 *
3575 * @return one of SCENE_MODE_XXX string constant. null if scene mode
3576 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003577 * @see #SCENE_MODE_AUTO
3578 * @see #SCENE_MODE_ACTION
3579 * @see #SCENE_MODE_PORTRAIT
3580 * @see #SCENE_MODE_LANDSCAPE
3581 * @see #SCENE_MODE_NIGHT
3582 * @see #SCENE_MODE_NIGHT_PORTRAIT
3583 * @see #SCENE_MODE_THEATRE
3584 * @see #SCENE_MODE_BEACH
3585 * @see #SCENE_MODE_SNOW
3586 * @see #SCENE_MODE_SUNSET
3587 * @see #SCENE_MODE_STEADYPHOTO
3588 * @see #SCENE_MODE_FIREWORKS
3589 * @see #SCENE_MODE_SPORTS
3590 * @see #SCENE_MODE_PARTY
3591 * @see #SCENE_MODE_CANDLELIGHT
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07003592 * @see #SCENE_MODE_BARCODE
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003593 */
3594 public String getSceneMode() {
3595 return get(KEY_SCENE_MODE);
3596 }
3597
3598 /**
Wu-cheng Lic58b4232010-03-29 17:21:28 +08003599 * Sets the scene mode. Changing scene mode may override other
3600 * parameters (such as flash mode, focus mode, white balance). For
3601 * example, suppose originally flash mode is on and supported flash
3602 * modes are on/off. In night scene mode, both flash mode and supported
3603 * flash mode may be changed to off. After setting scene mode,
Wu-cheng Li2988ab72009-09-30 17:08:19 -07003604 * applications should call getParameters to know if some parameters are
3605 * changed.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003606 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003607 * @param value scene mode.
3608 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003609 */
3610 public void setSceneMode(String value) {
3611 set(KEY_SCENE_MODE, value);
3612 }
3613
3614 /**
3615 * Gets the supported scene modes.
3616 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003617 * @return a list of supported scene modes. null if scene mode setting
3618 * is not supported.
3619 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003620 */
3621 public List<String> getSupportedSceneModes() {
3622 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX);
3623 return split(str);
3624 }
3625
3626 /**
3627 * Gets the current flash mode setting.
3628 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003629 * @return current flash mode. null if flash mode setting is not
3630 * supported.
3631 * @see #FLASH_MODE_OFF
3632 * @see #FLASH_MODE_AUTO
3633 * @see #FLASH_MODE_ON
3634 * @see #FLASH_MODE_RED_EYE
3635 * @see #FLASH_MODE_TORCH
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003636 */
3637 public String getFlashMode() {
3638 return get(KEY_FLASH_MODE);
3639 }
3640
3641 /**
3642 * Sets the flash mode.
3643 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003644 * @param value flash mode.
3645 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003646 */
3647 public void setFlashMode(String value) {
3648 set(KEY_FLASH_MODE, value);
3649 }
3650
3651 /**
3652 * Gets the supported flash modes.
3653 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003654 * @return a list of supported flash modes. null if flash mode setting
3655 * is not supported.
3656 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003657 */
3658 public List<String> getSupportedFlashModes() {
3659 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX);
3660 return split(str);
3661 }
3662
Wu-cheng Li36322db2009-09-18 18:59:21 +08003663 /**
3664 * Gets the current focus mode setting.
3665 *
Wu-cheng Li699fe932010-08-05 11:50:25 -07003666 * @return current focus mode. This method will always return a non-null
3667 * value. Applications should call {@link
3668 * #autoFocus(AutoFocusCallback)} to start the focus if focus
3669 * mode is FOCUS_MODE_AUTO or FOCUS_MODE_MACRO.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003670 * @see #FOCUS_MODE_AUTO
3671 * @see #FOCUS_MODE_INFINITY
3672 * @see #FOCUS_MODE_MACRO
3673 * @see #FOCUS_MODE_FIXED
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07003674 * @see #FOCUS_MODE_EDOF
Wu-cheng Lid45cb722010-09-20 16:15:32 -07003675 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Li36322db2009-09-18 18:59:21 +08003676 */
3677 public String getFocusMode() {
3678 return get(KEY_FOCUS_MODE);
3679 }
3680
3681 /**
3682 * Sets the focus mode.
3683 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003684 * @param value focus mode.
3685 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08003686 */
3687 public void setFocusMode(String value) {
3688 set(KEY_FOCUS_MODE, value);
3689 }
3690
3691 /**
3692 * Gets the supported focus modes.
3693 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003694 * @return a list of supported focus modes. This method will always
3695 * return a list with at least one element.
3696 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08003697 */
3698 public List<String> getSupportedFocusModes() {
3699 String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX);
3700 return split(str);
3701 }
3702
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003703 /**
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003704 * Gets the focal length (in millimeter) of the camera.
3705 *
Yin-Chia Yehbb85e1c2018-02-27 11:50:48 -08003706 * @return the focal length. Returns -1.0 when the device
3707 * doesn't report focal length information.
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003708 */
3709 public float getFocalLength() {
3710 return Float.parseFloat(get(KEY_FOCAL_LENGTH));
3711 }
3712
3713 /**
3714 * Gets the horizontal angle of view in degrees.
3715 *
Yin-Chia Yeh564ae822018-02-08 16:24:27 -08003716 * @return horizontal angle of view. Returns -1.0 when the device
3717 * doesn't report view angle information.
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003718 */
3719 public float getHorizontalViewAngle() {
3720 return Float.parseFloat(get(KEY_HORIZONTAL_VIEW_ANGLE));
3721 }
3722
3723 /**
3724 * Gets the vertical angle of view in degrees.
3725 *
Yin-Chia Yeh564ae822018-02-08 16:24:27 -08003726 * @return vertical angle of view. Returns -1.0 when the device
3727 * doesn't report view angle information.
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003728 */
3729 public float getVerticalViewAngle() {
3730 return Float.parseFloat(get(KEY_VERTICAL_VIEW_ANGLE));
3731 }
3732
3733 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003734 * Gets the current exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003735 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003736 * @return current exposure compensation index. The range is {@link
3737 * #getMinExposureCompensation} to {@link
3738 * #getMaxExposureCompensation}. 0 means exposure is not
3739 * adjusted.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003740 */
3741 public int getExposureCompensation() {
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003742 return getInt(KEY_EXPOSURE_COMPENSATION, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08003743 }
3744
3745 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003746 * Sets the exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003747 *
Wu-cheng Li0402e7d2010-02-26 15:04:55 +08003748 * @param value exposure compensation index. The valid value range is
3749 * from {@link #getMinExposureCompensation} (inclusive) to {@link
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003750 * #getMaxExposureCompensation} (inclusive). 0 means exposure is
3751 * not adjusted. Application should call
3752 * getMinExposureCompensation and getMaxExposureCompensation to
3753 * know if exposure compensation is supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003754 */
3755 public void setExposureCompensation(int value) {
3756 set(KEY_EXPOSURE_COMPENSATION, value);
3757 }
3758
3759 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003760 * Gets the maximum exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003761 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003762 * @return maximum exposure compensation index (>=0). If both this
3763 * method and {@link #getMinExposureCompensation} return 0,
3764 * exposure compensation is not supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003765 */
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003766 public int getMaxExposureCompensation() {
3767 return getInt(KEY_MAX_EXPOSURE_COMPENSATION, 0);
3768 }
3769
3770 /**
3771 * Gets the minimum exposure compensation index.
3772 *
3773 * @return minimum exposure compensation index (<=0). If both this
3774 * method and {@link #getMaxExposureCompensation} return 0,
3775 * exposure compensation is not supported.
3776 */
3777 public int getMinExposureCompensation() {
3778 return getInt(KEY_MIN_EXPOSURE_COMPENSATION, 0);
3779 }
3780
3781 /**
3782 * Gets the exposure compensation step.
3783 *
3784 * @return exposure compensation step. Applications can get EV by
3785 * multiplying the exposure compensation index and step. Ex: if
3786 * exposure compensation index is -6 and step is 0.333333333, EV
3787 * is -2.
3788 */
3789 public float getExposureCompensationStep() {
3790 return getFloat(KEY_EXPOSURE_COMPENSATION_STEP, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08003791 }
3792
3793 /**
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003794 * <p>Sets the auto-exposure lock state. Applications should check
3795 * {@link #isAutoExposureLockSupported} before using this method.</p>
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003796 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003797 * <p>If set to true, the camera auto-exposure routine will immediately
3798 * pause until the lock is set to false. Exposure compensation settings
3799 * changes will still take effect while auto-exposure is locked.</p>
3800 *
3801 * <p>If auto-exposure is already locked, setting this to true again has
3802 * no effect (the driver will not recalculate exposure values).</p>
3803 *
3804 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
3805 * image capture with {@link #takePicture(Camera.ShutterCallback,
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003806 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
3807 * lock.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003808 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003809 * <p>Exposure compensation, auto-exposure lock, and auto-white balance
3810 * lock can be used to capture an exposure-bracketed burst of images,
3811 * for example.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003812 *
3813 * <p>Auto-exposure state, including the lock state, will not be
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003814 * maintained after camera {@link #release()} is called. Locking
3815 * auto-exposure after {@link #open()} but before the first call to
3816 * {@link #startPreview()} will not allow the auto-exposure routine to
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003817 * run at all, and may result in severely over- or under-exposed
3818 * images.</p>
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003819 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003820 * @param toggle new state of the auto-exposure lock. True means that
3821 * auto-exposure is locked, false means that the auto-exposure
3822 * routine is free to run normally.
3823 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003824 * @see #getAutoExposureLock()
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003825 */
3826 public void setAutoExposureLock(boolean toggle) {
3827 set(KEY_AUTO_EXPOSURE_LOCK, toggle ? TRUE : FALSE);
3828 }
3829
3830 /**
3831 * Gets the state of the auto-exposure lock. Applications should check
3832 * {@link #isAutoExposureLockSupported} before using this method. See
3833 * {@link #setAutoExposureLock} for details about the lock.
3834 *
3835 * @return State of the auto-exposure lock. Returns true if
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003836 * auto-exposure is currently locked, and false otherwise.
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003837 *
3838 * @see #setAutoExposureLock(boolean)
3839 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003840 */
3841 public boolean getAutoExposureLock() {
3842 String str = get(KEY_AUTO_EXPOSURE_LOCK);
3843 return TRUE.equals(str);
3844 }
3845
3846 /**
3847 * Returns true if auto-exposure locking is supported. Applications
3848 * should call this before trying to lock auto-exposure. See
3849 * {@link #setAutoExposureLock} for details about the lock.
3850 *
3851 * @return true if auto-exposure lock is supported.
3852 * @see #setAutoExposureLock(boolean)
3853 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003854 */
3855 public boolean isAutoExposureLockSupported() {
3856 String str = get(KEY_AUTO_EXPOSURE_LOCK_SUPPORTED);
3857 return TRUE.equals(str);
3858 }
3859
3860 /**
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003861 * <p>Sets the auto-white balance lock state. Applications should check
3862 * {@link #isAutoWhiteBalanceLockSupported} before using this
3863 * method.</p>
3864 *
3865 * <p>If set to true, the camera auto-white balance routine will
3866 * immediately pause until the lock is set to false.</p>
3867 *
3868 * <p>If auto-white balance is already locked, setting this to true
3869 * again has no effect (the driver will not recalculate white balance
3870 * values).</p>
3871 *
3872 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
3873 * image capture with {@link #takePicture(Camera.ShutterCallback,
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003874 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
3875 * the lock.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003876 *
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003877 * <p> Changing the white balance mode with {@link #setWhiteBalance}
3878 * will release the auto-white balance lock if it is set.</p>
3879 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003880 * <p>Exposure compensation, AE lock, and AWB lock can be used to
3881 * capture an exposure-bracketed burst of images, for example.
3882 * Auto-white balance state, including the lock state, will not be
3883 * maintained after camera {@link #release()} is called. Locking
3884 * auto-white balance after {@link #open()} but before the first call to
3885 * {@link #startPreview()} will not allow the auto-white balance routine
3886 * to run at all, and may result in severely incorrect color in captured
3887 * images.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003888 *
3889 * @param toggle new state of the auto-white balance lock. True means
3890 * that auto-white balance is locked, false means that the
3891 * auto-white balance routine is free to run normally.
3892 *
3893 * @see #getAutoWhiteBalanceLock()
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003894 * @see #setWhiteBalance(String)
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003895 */
3896 public void setAutoWhiteBalanceLock(boolean toggle) {
3897 set(KEY_AUTO_WHITEBALANCE_LOCK, toggle ? TRUE : FALSE);
3898 }
3899
3900 /**
3901 * Gets the state of the auto-white balance lock. Applications should
3902 * check {@link #isAutoWhiteBalanceLockSupported} before using this
3903 * method. See {@link #setAutoWhiteBalanceLock} for details about the
3904 * lock.
3905 *
3906 * @return State of the auto-white balance lock. Returns true if
3907 * auto-white balance is currently locked, and false
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003908 * otherwise.
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003909 *
3910 * @see #setAutoWhiteBalanceLock(boolean)
3911 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003912 */
3913 public boolean getAutoWhiteBalanceLock() {
3914 String str = get(KEY_AUTO_WHITEBALANCE_LOCK);
3915 return TRUE.equals(str);
3916 }
3917
3918 /**
3919 * Returns true if auto-white balance locking is supported. Applications
3920 * should call this before trying to lock auto-white balance. See
3921 * {@link #setAutoWhiteBalanceLock} for details about the lock.
3922 *
3923 * @return true if auto-white balance lock is supported.
3924 * @see #setAutoWhiteBalanceLock(boolean)
3925 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003926 */
3927 public boolean isAutoWhiteBalanceLockSupported() {
3928 String str = get(KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED);
3929 return TRUE.equals(str);
3930 }
3931
3932 /**
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003933 * Gets current zoom value. This also works when smooth zoom is in
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003934 * progress. Applications should check {@link #isZoomSupported} before
3935 * using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003936 *
3937 * @return the current zoom value. The range is 0 to {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003938 * #getMaxZoom}. 0 means the camera is not zoomed.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003939 */
3940 public int getZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003941 return getInt(KEY_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003942 }
3943
3944 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003945 * Sets current zoom value. If the camera is zoomed (value > 0), the
3946 * actual picture size may be smaller than picture size setting.
3947 * Applications can check the actual picture size after picture is
3948 * returned from {@link PictureCallback}. The preview size remains the
3949 * same in zoom. Applications should check {@link #isZoomSupported}
3950 * before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003951 *
3952 * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003953 */
3954 public void setZoom(int value) {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003955 set(KEY_ZOOM, value);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003956 }
3957
3958 /**
3959 * Returns true if zoom is supported. Applications should call this
3960 * before using other zoom methods.
3961 *
3962 * @return true if zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003963 */
3964 public boolean isZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003965 String str = get(KEY_ZOOM_SUPPORTED);
3966 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003967 }
3968
3969 /**
3970 * Gets the maximum zoom value allowed for snapshot. This is the maximum
3971 * value that applications can set to {@link #setZoom(int)}.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003972 * Applications should call {@link #isZoomSupported} before using this
3973 * method. This value may change in different preview size. Applications
3974 * should call this again after setting preview size.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003975 *
3976 * @return the maximum zoom value supported by the camera.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003977 */
3978 public int getMaxZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003979 return getInt(KEY_MAX_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003980 }
3981
3982 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003983 * Gets the zoom ratios of all zoom values. Applications should check
3984 * {@link #isZoomSupported} before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003985 *
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003986 * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is
3987 * returned as 320. The number of elements is {@link
3988 * #getMaxZoom} + 1. The list is sorted from small to large. The
3989 * first element is always 100. The last element is the zoom
3990 * ratio of the maximum zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003991 */
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003992 public List<Integer> getZoomRatios() {
3993 return splitInt(get(KEY_ZOOM_RATIOS));
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003994 }
3995
3996 /**
3997 * Returns true if smooth zoom is supported. Applications should call
3998 * this before using other smooth zoom methods.
3999 *
4000 * @return true if smooth zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07004001 */
4002 public boolean isSmoothZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08004003 String str = get(KEY_SMOOTH_ZOOM_SUPPORTED);
4004 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07004005 }
4006
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004007 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004008 * <p>Gets the distances from the camera to where an object appears to be
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004009 * in focus. The object is sharpest at the optimal focus distance. The
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004010 * depth of field is the far focus distance minus near focus distance.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004011 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004012 * <p>Focus distances may change after calling {@link
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004013 * #autoFocus(AutoFocusCallback)}, {@link #cancelAutoFocus}, or {@link
4014 * #startPreview()}. Applications can call {@link #getParameters()}
4015 * and this method anytime to get the latest focus distances. If the
Wu-cheng Lid45cb722010-09-20 16:15:32 -07004016 * focus mode is FOCUS_MODE_CONTINUOUS_VIDEO, focus distances may change
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004017 * from time to time.</p>
Wu-cheng Li699fe932010-08-05 11:50:25 -07004018 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004019 * <p>This method is intended to estimate the distance between the camera
Wu-cheng Li699fe932010-08-05 11:50:25 -07004020 * and the subject. After autofocus, the subject distance may be within
4021 * near and far focus distance. However, the precision depends on the
4022 * camera hardware, autofocus algorithm, the focus area, and the scene.
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004023 * The error can be large and it should be only used as a reference.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004024 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004025 * <p>Far focus distance >= optimal focus distance >= near focus distance.
Wu-cheng Li185cc452010-05-20 15:36:13 +08004026 * If the focus distance is infinity, the value will be
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004027 * {@code Float.POSITIVE_INFINITY}.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004028 *
4029 * @param output focus distances in meters. output must be a float
4030 * array with three elements. Near focus distance, optimal focus
4031 * distance, and far focus distance will be filled in the array.
Wu-cheng Li185cc452010-05-20 15:36:13 +08004032 * @see #FOCUS_DISTANCE_NEAR_INDEX
4033 * @see #FOCUS_DISTANCE_OPTIMAL_INDEX
4034 * @see #FOCUS_DISTANCE_FAR_INDEX
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004035 */
4036 public void getFocusDistances(float[] output) {
4037 if (output == null || output.length != 3) {
4038 throw new IllegalArgumentException(
Ken Wakasaf76a50c2012-03-09 19:56:35 +09004039 "output must be a float array with three elements.");
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004040 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07004041 splitFloat(get(KEY_FOCUS_DISTANCES), output);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004042 }
4043
Wu-cheng Li30771b72011-04-02 06:19:46 +08004044 /**
4045 * Gets the maximum number of focus areas supported. This is the maximum
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004046 * length of the list in {@link #setFocusAreas(List)} and
4047 * {@link #getFocusAreas()}.
Wu-cheng Li30771b72011-04-02 06:19:46 +08004048 *
4049 * @return the maximum number of focus areas supported by the camera.
4050 * @see #getFocusAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08004051 */
4052 public int getMaxNumFocusAreas() {
4053 return getInt(KEY_MAX_NUM_FOCUS_AREAS, 0);
4054 }
4055
4056 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004057 * <p>Gets the current focus areas. Camera driver uses the areas to decide
4058 * focus.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004059 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004060 * <p>Before using this API or {@link #setFocusAreas(List)}, apps should
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004061 * call {@link #getMaxNumFocusAreas()} to know the maximum number of
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004062 * focus areas first. If the value is 0, focus area is not supported.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004063 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004064 * <p>Each focus area is a rectangle with specified weight. The direction
Wu-cheng Li30771b72011-04-02 06:19:46 +08004065 * is relative to the sensor orientation, that is, what the sensor sees.
4066 * The direction is not affected by the rotation or mirroring of
4067 * {@link #setDisplayOrientation(int)}. Coordinates of the rectangle
4068 * range from -1000 to 1000. (-1000, -1000) is the upper left point.
Wu-cheng Libde61a52011-06-07 18:23:14 +08004069 * (1000, 1000) is the lower right point. The width and height of focus
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004070 * areas cannot be 0 or negative.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004071 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004072 * <p>The weight must range from 1 to 1000. The weight should be
Eino-Ville Talvala4e396e02011-04-21 09:23:15 -07004073 * interpreted as a per-pixel weight - all pixels in the area have the
4074 * specified weight. This means a small area with the same weight as a
4075 * larger area will have less influence on the focusing than the larger
4076 * area. Focus areas can partially overlap and the driver will add the
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004077 * weights in the overlap region.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004078 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004079 * <p>A special case of a {@code null} focus area list means the driver is
4080 * free to select focus targets as it wants. For example, the driver may
4081 * use more signals to select focus areas and change them
4082 * dynamically. Apps can set the focus area list to {@code null} if they
4083 * want the driver to completely control focusing.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004084 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004085 * <p>Focus areas are relative to the current field of view
Wu-cheng Li30771b72011-04-02 06:19:46 +08004086 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000)
4087 * represents the top of the currently visible camera frame. The focus
4088 * area cannot be set to be outside the current field of view, even
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004089 * when using zoom.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004090 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004091 * <p>Focus area only has effect if the current focus mode is
Wu-cheng Li53b30912011-10-12 19:43:51 +08004092 * {@link #FOCUS_MODE_AUTO}, {@link #FOCUS_MODE_MACRO},
4093 * {@link #FOCUS_MODE_CONTINUOUS_VIDEO}, or
4094 * {@link #FOCUS_MODE_CONTINUOUS_PICTURE}.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004095 *
4096 * @return a list of current focus areas
Wu-cheng Li30771b72011-04-02 06:19:46 +08004097 */
4098 public List<Area> getFocusAreas() {
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004099 return splitArea(get(KEY_FOCUS_AREAS));
Wu-cheng Li30771b72011-04-02 06:19:46 +08004100 }
4101
4102 /**
4103 * Sets focus areas. See {@link #getFocusAreas()} for documentation.
4104 *
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004105 * @param focusAreas the focus areas
Wu-cheng Li30771b72011-04-02 06:19:46 +08004106 * @see #getFocusAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08004107 */
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004108 public void setFocusAreas(List<Area> focusAreas) {
4109 set(KEY_FOCUS_AREAS, focusAreas);
4110 }
4111
4112 /**
4113 * Gets the maximum number of metering areas supported. This is the
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004114 * maximum length of the list in {@link #setMeteringAreas(List)} and
4115 * {@link #getMeteringAreas()}.
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004116 *
4117 * @return the maximum number of metering areas supported by the camera.
4118 * @see #getMeteringAreas()
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004119 */
4120 public int getMaxNumMeteringAreas() {
4121 return getInt(KEY_MAX_NUM_METERING_AREAS, 0);
4122 }
4123
4124 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004125 * <p>Gets the current metering areas. Camera driver uses these areas to
4126 * decide exposure.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004127 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004128 * <p>Before using this API or {@link #setMeteringAreas(List)}, apps should
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004129 * call {@link #getMaxNumMeteringAreas()} to know the maximum number of
4130 * metering areas first. If the value is 0, metering area is not
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004131 * supported.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004132 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004133 * <p>Each metering area is a rectangle with specified weight. The
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004134 * direction is relative to the sensor orientation, that is, what the
4135 * sensor sees. The direction is not affected by the rotation or
4136 * mirroring of {@link #setDisplayOrientation(int)}. Coordinates of the
4137 * rectangle range from -1000 to 1000. (-1000, -1000) is the upper left
Wu-cheng Libde61a52011-06-07 18:23:14 +08004138 * point. (1000, 1000) is the lower right point. The width and height of
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004139 * metering areas cannot be 0 or negative.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004140 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004141 * <p>The weight must range from 1 to 1000, and represents a weight for
Eino-Ville Talvala4e396e02011-04-21 09:23:15 -07004142 * every pixel in the area. This means that a large metering area with
4143 * the same weight as a smaller area will have more effect in the
4144 * metering result. Metering areas can partially overlap and the driver
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004145 * will add the weights in the overlap region.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004146 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004147 * <p>A special case of a {@code null} metering area list means the driver
4148 * is free to meter as it chooses. For example, the driver may use more
4149 * signals to select metering areas and change them dynamically. Apps
4150 * can set the metering area list to {@code null} if they want the
4151 * driver to completely control metering.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004152 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004153 * <p>Metering areas are relative to the current field of view
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004154 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000)
4155 * represents the top of the currently visible camera frame. The
4156 * metering area cannot be set to be outside the current field of view,
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004157 * even when using zoom.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004158 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004159 * <p>No matter what metering areas are, the final exposure are compensated
4160 * by {@link #setExposureCompensation(int)}.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004161 *
4162 * @return a list of current metering areas
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004163 */
4164 public List<Area> getMeteringAreas() {
Wu-cheng Lid8aab932011-06-21 11:50:43 +08004165 return splitArea(get(KEY_METERING_AREAS));
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004166 }
4167
4168 /**
4169 * Sets metering areas. See {@link #getMeteringAreas()} for
4170 * documentation.
4171 *
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004172 * @param meteringAreas the metering areas
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004173 * @see #getMeteringAreas()
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004174 */
4175 public void setMeteringAreas(List<Area> meteringAreas) {
4176 set(KEY_METERING_AREAS, meteringAreas);
Wu-cheng Li30771b72011-04-02 06:19:46 +08004177 }
4178
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08004179 /**
4180 * Gets the maximum number of detected faces supported. This is the
4181 * maximum length of the list returned from {@link FaceDetectionListener}.
4182 * If the return value is 0, face detection of the specified type is not
4183 * supported.
4184 *
4185 * @return the maximum number of detected face supported by the camera.
Joe Fernandez464cb212011-10-04 16:56:47 -07004186 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08004187 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08004188 public int getMaxNumDetectedFaces() {
4189 return getInt(KEY_MAX_NUM_DETECTED_FACES_HW, 0);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08004190 }
4191
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004192 /**
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08004193 * Sets recording mode hint. This tells the camera that the intent of
4194 * the application is to record videos {@link
4195 * android.media.MediaRecorder#start()}, not to take still pictures
4196 * {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
4197 * Camera.PictureCallback, Camera.PictureCallback)}. Using this hint can
4198 * allow MediaRecorder.start() to start faster or with fewer glitches on
4199 * output. This should be called before starting preview for the best
4200 * result, but can be changed while the preview is active. The default
4201 * value is false.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004202 *
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08004203 * The app can still call takePicture() when the hint is true or call
4204 * MediaRecorder.start() when the hint is false. But the performance may
4205 * be worse.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004206 *
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08004207 * @param hint true if the apps intend to record videos using
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004208 * {@link android.media.MediaRecorder}.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004209 */
4210 public void setRecordingHint(boolean hint) {
4211 set(KEY_RECORDING_HINT, hint ? TRUE : FALSE);
4212 }
4213
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004214 /**
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004215 * <p>Returns true if video snapshot is supported. That is, applications
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004216 * can call {@link #takePicture(Camera.ShutterCallback,
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004217 * Camera.PictureCallback, Camera.PictureCallback,
4218 * Camera.PictureCallback)} during recording. Applications do not need
4219 * to call {@link #startPreview()} after taking a picture. The preview
4220 * will be still active. Other than that, taking a picture during
4221 * recording is identical to taking a picture normally. All settings and
4222 * methods related to takePicture work identically. Ex:
4223 * {@link #getPictureSize()}, {@link #getSupportedPictureSizes()},
4224 * {@link #setJpegQuality(int)}, {@link #setRotation(int)}, and etc. The
4225 * picture will have an EXIF header. {@link #FLASH_MODE_AUTO} and
4226 * {@link #FLASH_MODE_ON} also still work, but the video will record the
4227 * flash.</p>
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004228 *
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004229 * <p>Applications can set shutter callback as null to avoid the shutter
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004230 * sound. It is also recommended to set raw picture and post view
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004231 * callbacks to null to avoid the interrupt of preview display.</p>
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004232 *
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004233 * <p>Field-of-view of the recorded video may be different from that of the
4234 * captured pictures. The maximum size of a video snapshot may be
4235 * smaller than that for regular still captures. If the current picture
4236 * size is set higher than can be supported by video snapshot, the
4237 * picture will be captured at the maximum supported size instead.</p>
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004238 *
4239 * @return true if video snapshot is supported.
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004240 */
4241 public boolean isVideoSnapshotSupported() {
4242 String str = get(KEY_VIDEO_SNAPSHOT_SUPPORTED);
4243 return TRUE.equals(str);
4244 }
4245
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004246 /**
4247 * <p>Enables and disables video stabilization. Use
4248 * {@link #isVideoStabilizationSupported} to determine if calling this
4249 * method is valid.</p>
4250 *
4251 * <p>Video stabilization reduces the shaking due to the motion of the
4252 * camera in both the preview stream and in recorded videos, including
4253 * data received from the preview callback. It does not reduce motion
4254 * blur in images captured with
4255 * {@link Camera#takePicture takePicture}.</p>
4256 *
4257 * <p>Video stabilization can be enabled and disabled while preview or
4258 * recording is active, but toggling it may cause a jump in the video
4259 * stream that may be undesirable in a recorded video.</p>
4260 *
4261 * @param toggle Set to true to enable video stabilization, and false to
4262 * disable video stabilization.
4263 * @see #isVideoStabilizationSupported()
4264 * @see #getVideoStabilization()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004265 */
4266 public void setVideoStabilization(boolean toggle) {
4267 set(KEY_VIDEO_STABILIZATION, toggle ? TRUE : FALSE);
4268 }
4269
4270 /**
4271 * Get the current state of video stabilization. See
4272 * {@link #setVideoStabilization} for details of video stabilization.
4273 *
4274 * @return true if video stabilization is enabled
4275 * @see #isVideoStabilizationSupported()
4276 * @see #setVideoStabilization(boolean)
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004277 */
4278 public boolean getVideoStabilization() {
4279 String str = get(KEY_VIDEO_STABILIZATION);
4280 return TRUE.equals(str);
4281 }
4282
4283 /**
4284 * Returns true if video stabilization is supported. See
4285 * {@link #setVideoStabilization} for details of video stabilization.
4286 *
4287 * @return true if video stabilization is supported
4288 * @see #setVideoStabilization(boolean)
4289 * @see #getVideoStabilization()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004290 */
4291 public boolean isVideoStabilizationSupported() {
4292 String str = get(KEY_VIDEO_STABILIZATION_SUPPORTED);
4293 return TRUE.equals(str);
4294 }
4295
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004296 // Splits a comma delimited string to an ArrayList of String.
4297 // Return null if the passing string is null or the size is 0.
4298 private ArrayList<String> split(String str) {
4299 if (str == null) return null;
4300
Ali Utku Selen0a120182011-02-09 14:11:22 +01004301 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4302 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004303 ArrayList<String> substrings = new ArrayList<String>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01004304 for (String s : splitter) {
4305 substrings.add(s);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004306 }
4307 return substrings;
4308 }
4309
4310 // Splits a comma delimited string to an ArrayList of Integer.
4311 // Return null if the passing string is null or the size is 0.
4312 private ArrayList<Integer> splitInt(String str) {
4313 if (str == null) return null;
4314
Ali Utku Selen0a120182011-02-09 14:11:22 +01004315 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4316 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004317 ArrayList<Integer> substrings = new ArrayList<Integer>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01004318 for (String s : splitter) {
4319 substrings.add(Integer.parseInt(s));
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004320 }
4321 if (substrings.size() == 0) return null;
4322 return substrings;
4323 }
4324
Wu-cheng Li454630f2010-08-11 16:48:05 -07004325 private void splitInt(String str, int[] output) {
4326 if (str == null) return;
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004327
Ali Utku Selen0a120182011-02-09 14:11:22 +01004328 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4329 splitter.setString(str);
Wu-cheng Li454630f2010-08-11 16:48:05 -07004330 int index = 0;
Ali Utku Selen0a120182011-02-09 14:11:22 +01004331 for (String s : splitter) {
4332 output[index++] = Integer.parseInt(s);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004333 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07004334 }
4335
4336 // Splits a comma delimited string to an ArrayList of Float.
4337 private void splitFloat(String str, float[] output) {
4338 if (str == null) return;
4339
Ali Utku Selen0a120182011-02-09 14:11:22 +01004340 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4341 splitter.setString(str);
Wu-cheng Li454630f2010-08-11 16:48:05 -07004342 int index = 0;
Ali Utku Selen0a120182011-02-09 14:11:22 +01004343 for (String s : splitter) {
4344 output[index++] = Float.parseFloat(s);
Wu-cheng Li454630f2010-08-11 16:48:05 -07004345 }
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004346 }
4347
Wu-cheng Li24b326a2010-02-20 17:47:04 +08004348 // Returns the value of a float parameter.
4349 private float getFloat(String key, float defaultValue) {
4350 try {
4351 return Float.parseFloat(mMap.get(key));
4352 } catch (NumberFormatException ex) {
4353 return defaultValue;
4354 }
4355 }
4356
4357 // Returns the value of a integer parameter.
4358 private int getInt(String key, int defaultValue) {
4359 try {
4360 return Integer.parseInt(mMap.get(key));
4361 } catch (NumberFormatException ex) {
4362 return defaultValue;
4363 }
4364 }
4365
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004366 // Splits a comma delimited string to an ArrayList of Size.
4367 // Return null if the passing string is null or the size is 0.
4368 private ArrayList<Size> splitSize(String str) {
4369 if (str == null) return null;
4370
Ali Utku Selen0a120182011-02-09 14:11:22 +01004371 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4372 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004373 ArrayList<Size> sizeList = new ArrayList<Size>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01004374 for (String s : splitter) {
4375 Size size = strToSize(s);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004376 if (size != null) sizeList.add(size);
4377 }
4378 if (sizeList.size() == 0) return null;
4379 return sizeList;
4380 }
4381
4382 // Parses a string (ex: "480x320") to Size object.
4383 // Return null if the passing string is null.
4384 private Size strToSize(String str) {
4385 if (str == null) return null;
4386
4387 int pos = str.indexOf('x');
4388 if (pos != -1) {
4389 String width = str.substring(0, pos);
4390 String height = str.substring(pos + 1);
4391 return new Size(Integer.parseInt(width),
4392 Integer.parseInt(height));
4393 }
4394 Log.e(TAG, "Invalid size parameter string=" + str);
4395 return null;
4396 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07004397
4398 // Splits a comma delimited string to an ArrayList of int array.
4399 // Example string: "(10000,26623),(10000,30000)". Return null if the
4400 // passing string is null or the size is 0.
4401 private ArrayList<int[]> splitRange(String str) {
4402 if (str == null || str.charAt(0) != '('
4403 || str.charAt(str.length() - 1) != ')') {
4404 Log.e(TAG, "Invalid range list string=" + str);
4405 return null;
4406 }
4407
4408 ArrayList<int[]> rangeList = new ArrayList<int[]>();
4409 int endIndex, fromIndex = 1;
4410 do {
4411 int[] range = new int[2];
4412 endIndex = str.indexOf("),(", fromIndex);
4413 if (endIndex == -1) endIndex = str.length() - 1;
4414 splitInt(str.substring(fromIndex, endIndex), range);
4415 rangeList.add(range);
4416 fromIndex = endIndex + 3;
4417 } while (endIndex != str.length() - 1);
4418
4419 if (rangeList.size() == 0) return null;
4420 return rangeList;
4421 }
Wu-cheng Li30771b72011-04-02 06:19:46 +08004422
4423 // Splits a comma delimited string to an ArrayList of Area objects.
4424 // Example string: "(-10,-10,0,0,300),(0,0,10,10,700)". Return null if
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004425 // the passing string is null or the size is 0 or (0,0,0,0,0).
Mathew Inwoodbcbe4402018-08-08 15:42:59 +01004426 @UnsupportedAppUsage
Wu-cheng Li30771b72011-04-02 06:19:46 +08004427 private ArrayList<Area> splitArea(String str) {
4428 if (str == null || str.charAt(0) != '('
4429 || str.charAt(str.length() - 1) != ')') {
4430 Log.e(TAG, "Invalid area string=" + str);
4431 return null;
4432 }
4433
4434 ArrayList<Area> result = new ArrayList<Area>();
4435 int endIndex, fromIndex = 1;
4436 int[] array = new int[5];
4437 do {
4438 endIndex = str.indexOf("),(", fromIndex);
4439 if (endIndex == -1) endIndex = str.length() - 1;
4440 splitInt(str.substring(fromIndex, endIndex), array);
4441 Rect rect = new Rect(array[0], array[1], array[2], array[3]);
4442 result.add(new Area(rect, array[4]));
4443 fromIndex = endIndex + 3;
4444 } while (endIndex != str.length() - 1);
4445
4446 if (result.size() == 0) return null;
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004447
4448 if (result.size() == 1) {
Wu-cheng Libde61a52011-06-07 18:23:14 +08004449 Area area = result.get(0);
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004450 Rect rect = area.rect;
4451 if (rect.left == 0 && rect.top == 0 && rect.right == 0
4452 && rect.bottom == 0 && area.weight == 0) {
4453 return null;
4454 }
4455 }
4456
Wu-cheng Li30771b72011-04-02 06:19:46 +08004457 return result;
4458 }
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08004459
4460 private boolean same(String s1, String s2) {
4461 if (s1 == null && s2 == null) return true;
4462 if (s1 != null && s1.equals(s2)) return true;
4463 return false;
4464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 };
4466}