blob: 8e96f56e8cd35f30b08e3b4bba26fd749090a19e [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 Inwood5132cc12018-08-08 15:50:55 +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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Handler;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -070035import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Looper;
37import android.os.Message;
jiabin503e1fd2018-04-19 16:39:44 -070038import android.os.Process;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -070039import android.os.RemoteException;
40import android.os.ServiceManager;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -070041import android.renderscript.Allocation;
42import android.renderscript.Element;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -070043import android.renderscript.RSIllegalArgumentException;
Eino-Ville Talvala06a77182017-09-15 12:43:24 -070044import android.renderscript.RenderScript;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -070045import android.renderscript.Type;
Ali Utku Selen0a120182011-02-09 14:11:22 +010046import android.text.TextUtils;
Eino-Ville Talvala06a77182017-09-15 12:43:24 -070047import android.util.Log;
Wu-cheng Libde61a52011-06-07 18:23:14 +080048import android.view.Surface;
49import android.view.SurfaceHolder;
50
jiabin503e1fd2018-04-19 16:39:44 -070051import com.android.internal.annotations.GuardedBy;
52import com.android.internal.app.IAppOpsCallback;
53import com.android.internal.app.IAppOpsService;
54
Wu-cheng Libde61a52011-06-07 18:23:14 +080055import java.io.IOException;
56import java.lang.ref.WeakReference;
57import java.util.ArrayList;
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -070058import java.util.LinkedHashMap;
Wu-cheng Libde61a52011-06-07 18:23:14 +080059import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61/**
Dan Egnorbfcbeff2010-07-12 15:12:54 -070062 * The Camera class is used to set image capture settings, start/stop preview,
63 * snap pictures, and retrieve frames for encoding for video. This class is a
64 * client for the Camera service, which manages the actual camera hardware.
Scott Maindf4578e2009-09-10 12:22:07 -070065 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070066 * <p>To access the device camera, you must declare the
Wu-cheng Li7478ea62009-09-16 18:52:55 +080067 * {@link android.Manifest.permission#CAMERA} permission in your Android
Scott Maindf4578e2009-09-10 12:22:07 -070068 * Manifest. Also be sure to include the
69 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
Dan Egnorbfcbeff2010-07-12 15:12:54 -070070 * manifest element to declare camera features used by your application.
Wu-cheng Li7478ea62009-09-16 18:52:55 +080071 * For example, if you use the camera and auto-focus feature, your Manifest
Scott Maindf4578e2009-09-10 12:22:07 -070072 * should include the following:</p>
73 * <pre> &lt;uses-permission android:name="android.permission.CAMERA" />
74 * &lt;uses-feature android:name="android.hardware.camera" />
75 * &lt;uses-feature android:name="android.hardware.camera.autofocus" /></pre>
76 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -070077 * <p>To take pictures with this class, use the following steps:</p>
78 *
79 * <ol>
Dan Egnor341ff132010-07-20 11:30:17 -070080 * <li>Obtain an instance of Camera from {@link #open(int)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -070081 *
82 * <li>Get existing (default) settings with {@link #getParameters()}.
83 *
84 * <li>If necessary, modify the returned {@link Camera.Parameters} object and call
85 * {@link #setParameters(Camera.Parameters)}.
86 *
Eino-Ville Talvala6c91e2c2016-03-25 11:54:39 -070087 * <li>Call {@link #setDisplayOrientation(int)} to ensure correct orientation of preview.
Dan Egnorbfcbeff2010-07-12 15:12:54 -070088 *
89 * <li><b>Important</b>: Pass a fully initialized {@link SurfaceHolder} to
90 * {@link #setPreviewDisplay(SurfaceHolder)}. Without a surface, the camera
91 * will be unable to start the preview.
92 *
93 * <li><b>Important</b>: Call {@link #startPreview()} to start updating the
94 * preview surface. Preview must be started before you can take a picture.
95 *
96 * <li>When you want, call {@link #takePicture(Camera.ShutterCallback,
97 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)} to
98 * capture a photo. Wait for the callbacks to provide the actual image data.
99 *
100 * <li>After taking a picture, preview display will have stopped. To take more
101 * photos, call {@link #startPreview()} again first.
102 *
103 * <li>Call {@link #stopPreview()} to stop updating the preview surface.
104 *
105 * <li><b>Important:</b> Call {@link #release()} to release the camera for
106 * use by other applications. Applications should release the camera
107 * immediately in {@link android.app.Activity#onPause()} (and re-{@link #open()}
108 * it in {@link android.app.Activity#onResume()}).
109 * </ol>
110 *
111 * <p>To quickly switch to video recording mode, use these steps:</p>
112 *
113 * <ol>
114 * <li>Obtain and initialize a Camera and start preview as described above.
115 *
116 * <li>Call {@link #unlock()} to allow the media process to access the camera.
117 *
118 * <li>Pass the camera to {@link android.media.MediaRecorder#setCamera(Camera)}.
119 * See {@link android.media.MediaRecorder} information about video recording.
120 *
121 * <li>When finished recording, call {@link #reconnect()} to re-acquire
122 * and re-lock the camera.
123 *
124 * <li>If desired, restart preview and take more photos or videos.
125 *
126 * <li>Call {@link #stopPreview()} and {@link #release()} as described above.
127 * </ol>
128 *
129 * <p>This class is not thread-safe, and is meant for use from one event thread.
130 * Most long-running operations (preview, focus, photo capture, etc) happen
131 * asynchronously and invoke callbacks as necessary. Callbacks will be invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700132 * on the event thread {@link #open(int)} was called from. This class's methods
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700133 * must never be called from multiple threads at once.</p>
134 *
Scott Maindf4578e2009-09-10 12:22:07 -0700135 * <p class="caution"><strong>Caution:</strong> Different Android-powered devices
136 * may have different hardware specifications, such as megapixel ratings and
137 * auto-focus capabilities. In order for your application to be compatible with
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800138 * more devices, you should not make assumptions about the device camera
Scott Maindf4578e2009-09-10 12:22:07 -0700139 * specifications.</p>
Joe Fernandez6c5c3c32011-10-18 14:57:05 -0700140 *
141 * <div class="special reference">
142 * <h3>Developer Guides</h3>
143 * <p>For more information about using cameras, read the
144 * <a href="{@docRoot}guide/topics/media/camera.html">Camera</a> developer guide.</p>
145 * </div>
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700146 *
147 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
148 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700150@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151public class Camera {
152 private static final String TAG = "Camera";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800153
Wu-cheng Lid2c29292010-05-28 17:32:41 +0800154 // These match the enums in frameworks/base/include/camera/Camera.h
Benny Wongda83f462009-08-12 12:01:27 -0500155 private static final int CAMERA_MSG_ERROR = 0x001;
156 private static final int CAMERA_MSG_SHUTTER = 0x002;
157 private static final int CAMERA_MSG_FOCUS = 0x004;
158 private static final int CAMERA_MSG_ZOOM = 0x008;
159 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010;
160 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020;
161 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040;
162 private static final int CAMERA_MSG_RAW_IMAGE = 0x080;
163 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800164 private static final int CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x200;
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800165 private static final int CAMERA_MSG_PREVIEW_METADATA = 0x400;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800166 private static final int CAMERA_MSG_FOCUS_MOVE = 0x800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
Mathew Inwood5132cc12018-08-08 15:50:55 +0100168 @UnsupportedAppUsage
Ashok Bhat4838e332014-01-03 14:37:19 +0000169 private long mNativeContext; // accessed by native methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 private EventHandler mEventHandler;
171 private ShutterCallback mShutterCallback;
172 private PictureCallback mRawImageCallback;
173 private PictureCallback mJpegCallback;
174 private PreviewCallback mPreviewCallback;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700175 private boolean mUsingPreviewAllocation;
Dave Sparkse8b26e12009-07-14 10:35:40 -0700176 private PictureCallback mPostviewCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 private AutoFocusCallback mAutoFocusCallback;
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800178 private AutoFocusMoveCallback mAutoFocusMoveCallback;
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800179 private OnZoomChangeListener mZoomListener;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800180 private FaceDetectionListener mFaceListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 private ErrorCallback mErrorCallback;
Yin-Chia Yeha07cd322018-05-31 12:11:42 -0700182 private ErrorCallback mDetailedErrorCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 private boolean mOneShot;
Andrew Harp94927df2009-10-20 01:47:05 -0400184 private boolean mWithBuffer;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800185 private boolean mFaceDetectionRunning = false;
Igor Murashkina1d66272014-06-20 11:22:11 -0700186 private final Object mAutoFocusCallbackLock = new Object();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800187
jiabin503e1fd2018-04-19 16:39:44 -0700188 private final Object mShutterSoundLock = new Object();
189 // for AppOps
190 private @Nullable IAppOpsService mAppOps;
191 private IAppOpsCallback mAppOpsCallback;
192 @GuardedBy("mShutterSoundLock")
193 private boolean mHasAppOpsPlayAudio = true;
194 @GuardedBy("mShutterSoundLock")
195 private boolean mShutterSoundEnabledFromApp = true;
196
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700197 private static final int NO_ERROR = 0;
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 /**
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800200 * Broadcast Action: A new picture is taken by the camera, and the entry of
201 * the picture has been added to the media store.
202 * {@link android.content.Intent#getData} is URI of the picture.
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800203 *
204 * <p>In {@link android.os.Build.VERSION_CODES#N Android N} this broadcast was removed, and
205 * applications are recommended to use
206 * {@link android.app.job.JobInfo.Builder JobInfo.Builder}.{@link android.app.job.JobInfo.Builder#addTriggerContentUri}
207 * instead.</p>
208 *
209 * <p>In {@link android.os.Build.VERSION_CODES#O Android O} this broadcast has been brought
210 * back, but only for <em>registered</em> receivers. Apps that are actively running can
Dianne Hackbornc1541952017-04-27 10:55:49 -0700211 * again listen to the broadcast if they want an immediate clear signal about a picture
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800212 * being taken, however anything doing heavy work (or needing to be launched) as a result of
213 * this should still use JobScheduler.</p>
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800214 */
215 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
216 public static final String ACTION_NEW_PICTURE = "android.hardware.action.NEW_PICTURE";
217
218 /**
219 * Broadcast Action: A new video is recorded by the camera, and the entry
220 * of the video has been added to the media store.
221 * {@link android.content.Intent#getData} is URI of the video.
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800222 *
223 * <p>In {@link android.os.Build.VERSION_CODES#N Android N} this broadcast was removed, and
224 * applications are recommended to use
225 * {@link android.app.job.JobInfo.Builder JobInfo.Builder}.{@link android.app.job.JobInfo.Builder#addTriggerContentUri}
226 * instead.</p>
227 *
228 * <p>In {@link android.os.Build.VERSION_CODES#O Android O} this broadcast has been brought
229 * back, but only for <em>registered</em> receivers. Apps that are actively running can
Dianne Hackbornc1541952017-04-27 10:55:49 -0700230 * again listen to the broadcast if they want an immediate clear signal about a video
Dianne Hackborn5da9e942017-03-10 13:30:06 -0800231 * being taken, however anything doing heavy work (or needing to be launched) as a result of
232 * this should still use JobScheduler.</p>
Wu-cheng Li10e09c62011-07-18 09:09:41 +0800233 */
234 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
235 public static final String ACTION_NEW_VIDEO = "android.hardware.action.NEW_VIDEO";
236
237 /**
Zhijun He4c913802014-06-16 16:42:35 -0700238 * Camera HAL device API version 1.0
239 * @hide
240 */
Mathew Inwood5132cc12018-08-08 15:50:55 +0100241 @UnsupportedAppUsage
Zhijun He4c913802014-06-16 16:42:35 -0700242 public static final int CAMERA_HAL_API_VERSION_1_0 = 0x100;
243
244 /**
245 * A constant meaning the normal camera connect/open will be used.
Zhijun He4c913802014-06-16 16:42:35 -0700246 */
Igor Murashkina1d66272014-06-20 11:22:11 -0700247 private static final int CAMERA_HAL_API_VERSION_NORMAL_CONNECT = -2;
Zhijun He4c913802014-06-16 16:42:35 -0700248
249 /**
250 * Used to indicate HAL version un-specified.
251 */
252 private static final int CAMERA_HAL_API_VERSION_UNSPECIFIED = -1;
Igor Murashkina1d66272014-06-20 11:22:11 -0700253
Zhijun He4c913802014-06-16 16:42:35 -0700254 /**
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800255 * Hardware face detection. It does not use much CPU.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800256 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800257 private static final int CAMERA_FACE_DETECTION_HW = 0;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800258
259 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800260 * Software face detection. It uses some CPU.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800261 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +0800262 private static final int CAMERA_FACE_DETECTION_SW = 1;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800263
264 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700265 * Returns the number of physical cameras available on this device.
Yin-Chia Yeh564ae822018-02-08 16:24:27 -0800266 * The return value of this method might change dynamically if the device
267 * supports external cameras and an external camera is connected or
268 * disconnected.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700269 *
Shuzhen Wange7d3cd02018-05-31 11:41:39 -0700270 * If there is a
271 * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA
272 * logical multi-camera} in the system, to maintain app backward compatibility, this method will
273 * only expose one camera for every logical camera and underlying physical cameras group.
274 * Use camera2 API to see all cameras.
275 *
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700276 * @return total number of accessible camera devices, or 0 if there are no
277 * cameras or an error was encountered enumerating them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 */
Chih-Chung Change25cc652010-05-06 16:36:58 +0800279 public native static int getNumberOfCameras();
280
281 /**
Dan Egnor341ff132010-07-20 11:30:17 -0700282 * Returns the information about a particular camera.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800283 * If {@link #getNumberOfCameras()} returns N, the valid id is 0 to N-1.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700284 *
285 * @throws RuntimeException if an invalid ID is provided, or if there is an
286 * error retrieving the information (generally due to a hardware or other
287 * low-level failure).
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800288 */
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -0700289 public static void getCameraInfo(int cameraId, CameraInfo cameraInfo) {
290 _getCameraInfo(cameraId, cameraInfo);
291 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
292 IAudioService audioService = IAudioService.Stub.asInterface(b);
293 try {
294 if (audioService.isCameraSoundForced()) {
295 // Only set this when sound is forced; otherwise let native code
296 // decide.
297 cameraInfo.canDisableShutterSound = false;
298 }
299 } catch (RemoteException e) {
300 Log.e(TAG, "Audio service is unavailable for queries");
301 }
302 }
303 private native static void _getCameraInfo(int cameraId, CameraInfo cameraInfo);
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800304
305 /**
306 * Information about a camera
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700307 *
308 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
309 * applications.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800310 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700311 @Deprecated
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800312 public static class CameraInfo {
Wu-cheng Li78366602010-09-15 14:08:15 -0700313 /**
314 * The facing of the camera is opposite to that of the screen.
315 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800316 public static final int CAMERA_FACING_BACK = 0;
Wu-cheng Li78366602010-09-15 14:08:15 -0700317
318 /**
319 * The facing of the camera is the same as that of the screen.
320 */
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800321 public static final int CAMERA_FACING_FRONT = 1;
322
323 /**
Joe Fernandez464cb212011-10-04 16:56:47 -0700324 * The direction that the camera faces. It should be
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800325 * CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
326 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700327 public int facing;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800328
329 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700330 * <p>The orientation of the camera image. The value is the angle that the
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800331 * camera image needs to be rotated clockwise so it shows correctly on
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700332 * the display in its natural orientation. It should be 0, 90, 180, or 270.</p>
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800333 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700334 * <p>For example, suppose a device has a naturally tall screen. The
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +0800335 * back-facing camera sensor is mounted in landscape. You are looking at
336 * the screen. If the top side of the camera sensor is aligned with the
337 * right edge of the screen in natural orientation, the value should be
338 * 90. If the top side of a front-facing camera sensor is aligned with
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700339 * the right of the screen, the value should be 270.</p>
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800340 *
341 * @see #setDisplayOrientation(int)
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800342 * @see Parameters#setRotation(int)
343 * @see Parameters#setPreviewSize(int, int)
344 * @see Parameters#setPictureSize(int, int)
345 * @see Parameters#setJpegThumbnailSize(int, int)
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800346 */
Wu-cheng Li78366602010-09-15 14:08:15 -0700347 public int orientation;
Eino-Ville Talvalaf7c6c5a2012-09-19 11:46:11 -0700348
349 /**
350 * <p>Whether the shutter sound can be disabled.</p>
351 *
352 * <p>On some devices, the camera shutter sound cannot be turned off
353 * through {@link #enableShutterSound enableShutterSound}. This field
354 * can be used to determine whether a call to disable the shutter sound
355 * will succeed.</p>
356 *
357 * <p>If this field is set to true, then a call of
358 * {@code enableShutterSound(false)} will be successful. If set to
359 * false, then that call will fail, and the shutter sound will be played
360 * when {@link Camera#takePicture takePicture} is called.</p>
361 */
362 public boolean canDisableShutterSound;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800363 };
364
365 /**
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800366 * Creates a new Camera object to access a particular hardware camera. If
367 * the same camera is opened by other applications, this will throw a
368 * RuntimeException.
369 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700370 * <p>You must call {@link #release()} when you are done using the camera,
371 * otherwise it will remain locked and be unavailable to other applications.
372 *
Dan Egnor341ff132010-07-20 11:30:17 -0700373 * <p>Your application should only have one Camera object active at a time
374 * for a particular hardware camera.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700375 *
376 * <p>Callbacks from other methods are delivered to the event loop of the
377 * thread which called open(). If this thread has no event loop, then
378 * callbacks are delivered to the main application event loop. If there
379 * is no main application event loop, callbacks are not delivered.
380 *
381 * <p class="caution"><b>Caution:</b> On some devices, this method may
382 * take a long time to complete. It is best to call this method from a
383 * worker thread (possibly using {@link android.os.AsyncTask}) to avoid
384 * blocking the main application UI thread.
385 *
Dan Egnor341ff132010-07-20 11:30:17 -0700386 * @param cameraId the hardware camera to access, between 0 and
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800387 * {@link #getNumberOfCameras()}-1.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700388 * @return a new Camera object, connected, locked and ready for use.
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800389 * @throws RuntimeException if opening the camera fails (for example, if the
390 * camera is in use by another process or device policy manager has
391 * disabled the camera).
Wu-cheng Lifacc8ce2011-06-17 13:09:40 +0800392 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800393 */
394 public static Camera open(int cameraId) {
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800395 return new Camera(cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
397
Chih-Chung Change25cc652010-05-06 16:36:58 +0800398 /**
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800399 * Creates a new Camera object to access the first back-facing camera on the
400 * device. If the device does not have a back-facing camera, this returns
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700401 * null. Otherwise acts like the {@link #open(int)} call.
402 *
403 * @return a new Camera object for the first back-facing camera, or null if there is no
404 * backfacing camera
Wu-cheng Li78366602010-09-15 14:08:15 -0700405 * @see #open(int)
Chih-Chung Change25cc652010-05-06 16:36:58 +0800406 */
407 public static Camera open() {
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800408 int numberOfCameras = getNumberOfCameras();
409 CameraInfo cameraInfo = new CameraInfo();
410 for (int i = 0; i < numberOfCameras; i++) {
411 getCameraInfo(i, cameraInfo);
412 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800413 return new Camera(i);
Wu-cheng Lia48b70f2010-11-09 01:55:44 +0800414 }
415 }
416 return null;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800417 }
418
Zhijun He4c913802014-06-16 16:42:35 -0700419 /**
420 * Creates a new Camera object to access a particular hardware camera with
421 * given hal API version. If the same camera is opened by other applications
422 * or the hal API version is not supported by this device, this will throw a
423 * RuntimeException.
424 * <p>
425 * You must call {@link #release()} when you are done using the camera,
426 * otherwise it will remain locked and be unavailable to other applications.
427 * <p>
428 * Your application should only have one Camera object active at a time for
429 * a particular hardware camera.
430 * <p>
431 * Callbacks from other methods are delivered to the event loop of the
432 * thread which called open(). If this thread has no event loop, then
433 * callbacks are delivered to the main application event loop. If there is
434 * no main application event loop, callbacks are not delivered.
435 * <p class="caution">
436 * <b>Caution:</b> On some devices, this method may take a long time to
437 * complete. It is best to call this method from a worker thread (possibly
438 * using {@link android.os.AsyncTask}) to avoid blocking the main
439 * application UI thread.
440 *
441 * @param cameraId The hardware camera to access, between 0 and
442 * {@link #getNumberOfCameras()}-1.
Igor Murashkina1d66272014-06-20 11:22:11 -0700443 * @param halVersion The HAL API version this camera device to be opened as.
Zhijun He4c913802014-06-16 16:42:35 -0700444 * @return a new Camera object, connected, locked and ready for use.
Igor Murashkina1d66272014-06-20 11:22:11 -0700445 *
446 * @throws IllegalArgumentException if the {@code halVersion} is invalid
447 *
Zhijun He4c913802014-06-16 16:42:35 -0700448 * @throws RuntimeException if opening the camera fails (for example, if the
449 * camera is in use by another process or device policy manager has disabled
450 * the camera).
Igor Murashkina1d66272014-06-20 11:22:11 -0700451 *
Zhijun He4c913802014-06-16 16:42:35 -0700452 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName)
Igor Murashkina1d66272014-06-20 11:22:11 -0700453 * @see #CAMERA_HAL_API_VERSION_1_0
Zhijun He4c913802014-06-16 16:42:35 -0700454 *
455 * @hide
456 */
Mathew Inwood5132cc12018-08-08 15:50:55 +0100457 @UnsupportedAppUsage
Zhijun He4c913802014-06-16 16:42:35 -0700458 public static Camera openLegacy(int cameraId, int halVersion) {
Igor Murashkina1d66272014-06-20 11:22:11 -0700459 if (halVersion < CAMERA_HAL_API_VERSION_1_0) {
460 throw new IllegalArgumentException("Invalid HAL version " + halVersion);
461 }
462
Zhijun He4c913802014-06-16 16:42:35 -0700463 return new Camera(cameraId, halVersion);
464 }
465
466 /**
467 * Create a legacy camera object.
468 *
469 * @param cameraId The hardware camera to access, between 0 and
470 * {@link #getNumberOfCameras()}-1.
471 * @param halVersion The HAL API version this camera device to be opened as.
472 */
473 private Camera(int cameraId, int halVersion) {
Igor Murashkina1d66272014-06-20 11:22:11 -0700474 int err = cameraInitVersion(cameraId, halVersion);
Zhijun He4c913802014-06-16 16:42:35 -0700475 if (checkInitErrors(err)) {
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200476 if (err == -EACCES) {
477 throw new RuntimeException("Fail to connect to camera service");
478 } else if (err == -ENODEV) {
479 throw new RuntimeException("Camera initialization failed");
480 } else if (err == -ENOSYS) {
481 throw new RuntimeException("Camera initialization failed because some methods"
482 + " are not implemented");
483 } else if (err == -EOPNOTSUPP) {
484 throw new RuntimeException("Camera initialization failed because the hal"
485 + " version is not supported by this device");
486 } else if (err == -EINVAL) {
487 throw new RuntimeException("Camera initialization failed because the input"
488 + " arugments are invalid");
489 } else if (err == -EBUSY) {
490 throw new RuntimeException("Camera initialization failed because the camera"
491 + " device was already opened");
492 } else if (err == -EUSERS) {
493 throw new RuntimeException("Camera initialization failed because the max"
494 + " number of camera devices were already opened");
Zhijun He4c913802014-06-16 16:42:35 -0700495 }
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200496 // Should never hit this.
497 throw new RuntimeException("Unknown camera error");
Zhijun He4c913802014-06-16 16:42:35 -0700498 }
499 }
500
Igor Murashkina1d66272014-06-20 11:22:11 -0700501 private int cameraInitVersion(int cameraId, int halVersion) {
Zhijun He4c913802014-06-16 16:42:35 -0700502 mShutterCallback = null;
503 mRawImageCallback = null;
504 mJpegCallback = null;
505 mPreviewCallback = null;
506 mPostviewCallback = null;
507 mUsingPreviewAllocation = false;
508 mZoomListener = null;
509
510 Looper looper;
511 if ((looper = Looper.myLooper()) != null) {
512 mEventHandler = new EventHandler(this, looper);
513 } else if ((looper = Looper.getMainLooper()) != null) {
514 mEventHandler = new EventHandler(this, looper);
515 } else {
516 mEventHandler = null;
517 }
518
Svetoslavfbf0eca2015-05-01 16:52:41 -0700519 return native_setup(new WeakReference<Camera>(this), cameraId, halVersion,
520 ActivityThread.currentOpPackageName());
Zhijun He4c913802014-06-16 16:42:35 -0700521 }
522
Igor Murashkina1d66272014-06-20 11:22:11 -0700523 private int cameraInitNormal(int cameraId) {
524 return cameraInitVersion(cameraId, CAMERA_HAL_API_VERSION_NORMAL_CONNECT);
525 }
526
527 /**
528 * Connect to the camera service using #connectLegacy
529 *
530 * <p>
531 * This acts the same as normal except that it will return
532 * the detailed error code if open fails instead of
533 * converting everything into {@code NO_INIT}.</p>
534 *
535 * <p>Intended to use by the camera2 shim only, do <i>not</i> use this for other code.</p>
536 *
537 * @return a detailed errno error code, or {@code NO_ERROR} on success
538 *
539 * @hide
540 */
541 public int cameraInitUnspecified(int cameraId) {
542 return cameraInitVersion(cameraId, CAMERA_HAL_API_VERSION_UNSPECIFIED);
543 }
544
545 /** used by Camera#open, Camera#open(int) */
Wu-cheng Li7bc1b212012-04-19 12:23:31 +0800546 Camera(int cameraId) {
Igor Murashkina1d66272014-06-20 11:22:11 -0700547 int err = cameraInitNormal(cameraId);
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700548 if (checkInitErrors(err)) {
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200549 if (err == -EACCES) {
550 throw new RuntimeException("Fail to connect to camera service");
551 } else if (err == -ENODEV) {
552 throw new RuntimeException("Camera initialization failed");
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700553 }
Lazar Trsic8ea56f62015-07-07 17:31:20 +0200554 // Should never hit this.
555 throw new RuntimeException("Unknown camera error");
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700556 }
jiabin503e1fd2018-04-19 16:39:44 -0700557 initAppOps();
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700558 }
559
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700560
561 /**
562 * @hide
563 */
564 public static boolean checkInitErrors(int err) {
565 return err != NO_ERROR;
566 }
567
568 /**
569 * @hide
570 */
571 public static Camera openUninitialized() {
572 return new Camera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800574
Wu-cheng Li1c04a332011-11-22 18:21:18 +0800575 /**
576 * An empty Camera for testing purpose.
577 */
Daichi Hirono2dba5c82018-06-25 11:16:41 +0900578 Camera() {}
jiabin503e1fd2018-04-19 16:39:44 -0700579
580 private void initAppOps() {
581 IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE);
582 mAppOps = IAppOpsService.Stub.asInterface(b);
583 // initialize mHasAppOpsPlayAudio
584 updateAppOpsPlayAudio();
585 // register a callback to monitor whether the OP_PLAY_AUDIO is still allowed
586 mAppOpsCallback = new IAppOpsCallbackWrapper(this);
587 try {
588 mAppOps.startWatchingMode(AppOpsManager.OP_PLAY_AUDIO,
589 ActivityThread.currentPackageName(), mAppOpsCallback);
590 } catch (RemoteException e) {
591 Log.e(TAG, "Error registering appOps callback", e);
592 mHasAppOpsPlayAudio = false;
593 }
594 }
595
596 private void releaseAppOps() {
597 try {
598 if (mAppOps != null) {
599 mAppOps.stopWatchingMode(mAppOpsCallback);
600 }
601 } catch (Exception e) {
602 // nothing to do here, the object is supposed to be released anyway
603 }
Wu-cheng Li1c04a332011-11-22 18:21:18 +0800604 }
605
Igor Murashkina1d66272014-06-20 11:22:11 -0700606 @Override
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800607 protected void finalize() {
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -0800608 release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800610
Mathew Inwood5132cc12018-08-08 15:50:55 +0100611 @UnsupportedAppUsage
Zhijun He4c913802014-06-16 16:42:35 -0700612 private native final int native_setup(Object camera_this, int cameraId, int halVersion,
Eino-Ville Talvala788717c2013-02-15 18:30:15 -0800613 String packageName);
614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 private native final void native_release();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617
618 /**
619 * Disconnects and releases the Camera object resources.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700620 *
621 * <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 -0800622 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800623 public final void release() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 native_release();
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800625 mFaceDetectionRunning = false;
jiabin503e1fd2018-04-19 16:39:44 -0700626 releaseAppOps();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 }
628
629 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700630 * Unlocks the camera to allow another process to access it.
631 * Normally, the camera is locked to the process with an active Camera
632 * object until {@link #release()} is called. To allow rapid handoff
633 * between processes, you can call this method to release the camera
634 * temporarily for another process to use; once the other process is done
635 * you can call {@link #reconnect()} to reclaim the camera.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700637 * <p>This must be done before calling
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800638 * {@link android.media.MediaRecorder#setCamera(Camera)}. This cannot be
639 * called after recording starts.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700641 * <p>If you are not recording video, you probably do not need this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700643 * @throws RuntimeException if the camera cannot be unlocked.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 */
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800645 public native final void unlock();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700648 * Re-locks the camera to prevent other processes from accessing it.
649 * Camera objects are locked by default unless {@link #unlock()} is
650 * called. Normally {@link #reconnect()} is used instead.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800651 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800652 * <p>Since API level 14, camera is automatically locked for applications in
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800653 * {@link android.media.MediaRecorder#start()}. Applications can use the
654 * camera (ex: zoom) after recording starts. There is no need to call this
655 * after recording starts or stops.
656 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700657 * <p>If you are not recording video, you probably do not need this method.
658 *
659 * @throws RuntimeException if the camera cannot be re-locked (for
660 * example, if the camera is still in use by another process).
661 */
662 public native final void lock();
663
664 /**
665 * Reconnects to the camera service after another process used it.
666 * After {@link #unlock()} is called, another process may use the
667 * camera; when the process is done, you must reconnect to the camera,
668 * which will re-acquire the lock and allow you to continue using the
669 * camera.
670 *
Wu-cheng Li53b30912011-10-12 19:43:51 +0800671 * <p>Since API level 14, camera is automatically locked for applications in
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800672 * {@link android.media.MediaRecorder#start()}. Applications can use the
673 * camera (ex: zoom) after recording starts. There is no need to call this
674 * after recording starts or stops.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700675 *
676 * <p>If you are not recording video, you probably do not need this method.
677 *
678 * @throws IOException if a connection cannot be re-established (for
679 * example, if the camera is still in use by another process).
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700680 * @throws RuntimeException if release() has been called on this Camera
681 * instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700682 */
683 public native final void reconnect() throws IOException;
684
685 /**
686 * Sets the {@link Surface} to be used for live preview.
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800687 * Either a surface or surface texture is necessary for preview, and
688 * preview is necessary to take pictures. The same surface can be re-set
689 * without harm. Setting a preview surface will un-set any preview surface
690 * texture that was set via {@link #setPreviewTexture}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700691 *
692 * <p>The {@link SurfaceHolder} must already contain a surface when this
693 * method is called. If you are using {@link android.view.SurfaceView},
694 * you will need to register a {@link SurfaceHolder.Callback} with
695 * {@link SurfaceHolder#addCallback(SurfaceHolder.Callback)} and wait for
696 * {@link SurfaceHolder.Callback#surfaceCreated(SurfaceHolder)} before
697 * calling setPreviewDisplay() or starting preview.
698 *
699 * <p>This method must be called before {@link #startPreview()}. The
700 * one exception is that if the preview surface is not set (or set to null)
701 * before startPreview() is called, then this method may be called once
702 * with a non-null parameter to set the preview surface. (This allows
703 * camera setup and surface creation to happen in parallel, saving time.)
704 * The preview surface may not otherwise change while preview is running.
705 *
706 * @param holder containing the Surface on which to place the preview,
707 * or null to remove the preview surface
708 * @throws IOException if the method fails (for example, if the surface
709 * is unavailable or unsuitable).
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700710 * @throws RuntimeException if release() has been called on this Camera
711 * instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 */
713 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException {
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800714 if (holder != null) {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700715 setPreviewSurface(holder.getSurface());
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800716 } else {
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700717 setPreviewSurface((Surface)null);
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700721 /**
722 * @hide
723 */
Mathew Inwood5132cc12018-08-08 15:50:55 +0100724 @UnsupportedAppUsage
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700725 public native final void setPreviewSurface(Surface surface) throws IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
727 /**
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800728 * Sets the {@link SurfaceTexture} to be used for live preview.
729 * Either a surface or surface texture is necessary for preview, and
730 * preview is necessary to take pictures. The same surface texture can be
731 * re-set without harm. Setting a preview surface texture will un-set any
732 * preview surface that was set via {@link #setPreviewDisplay}.
733 *
734 * <p>This method must be called before {@link #startPreview()}. The
735 * one exception is that if the preview surface texture is not set (or set
736 * to null) before startPreview() is called, then this method may be called
737 * once with a non-null parameter to set the preview surface. (This allows
738 * camera setup and surface creation to happen in parallel, saving time.)
739 * The preview surface texture may not otherwise change while preview is
740 * running.
741 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -0700742 * <p>The timestamps provided by {@link SurfaceTexture#getTimestamp()} for a
Eino-Ville Talvalae309a0f2011-03-21 11:04:34 -0700743 * SurfaceTexture set as the preview texture have an unspecified zero point,
744 * and cannot be directly compared between different cameras or different
745 * instances of the same camera, or across multiple runs of the same
746 * program.
747 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800748 * <p>If you are using the preview data to create video or still images,
749 * strongly consider using {@link android.media.MediaActionSound} to
750 * properly indicate image capture or recording start/stop to the user.</p>
751 *
752 * @see android.media.MediaActionSound
753 * @see android.graphics.SurfaceTexture
754 * @see android.view.TextureView
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800755 * @param surfaceTexture the {@link SurfaceTexture} to which the preview
756 * images are to be sent or null to remove the current preview surface
757 * texture
758 * @throws IOException if the method fails (for example, if the surface
759 * texture is unavailable or unsuitable).
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700760 * @throws RuntimeException if release() has been called on this Camera
761 * instance.
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800762 */
Glenn Kastendbc289d2011-02-09 10:15:44 -0800763 public native final void setPreviewTexture(SurfaceTexture surfaceTexture) throws IOException;
Jamie Gennisfd6f39e2010-12-20 12:15:00 -0800764
765 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700766 * Callback interface used to deliver copies of preview frames as
767 * they are displayed.
768 *
769 * @see #setPreviewCallback(Camera.PreviewCallback)
770 * @see #setOneShotPreviewCallback(Camera.PreviewCallback)
771 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
772 * @see #startPreview()
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700773 *
774 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
775 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -0700777 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 public interface PreviewCallback
779 {
780 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700781 * Called as preview frames are displayed. This callback is invoked
Dan Egnor341ff132010-07-20 11:30:17 -0700782 * on the event thread {@link #open(int)} was called from.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700784 * <p>If using the {@link android.graphics.ImageFormat#YV12} format,
785 * refer to the equations in {@link Camera.Parameters#setPreviewFormat}
786 * for the arrangement of the pixel data in the preview callback
787 * buffers.
788 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700789 * @param data the contents of the preview frame in the format defined
Mathias Agopiana696f5d2010-02-17 17:53:09 -0800790 * by {@link android.graphics.ImageFormat}, which can be queried
Scott Maindf4578e2009-09-10 12:22:07 -0700791 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
Scott Mainda0a56d2009-09-10 18:08:37 -0700792 * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800793 * is never called, the default will be the YCbCr_420_SP
794 * (NV21) format.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700795 * @param camera the Camera service object.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 */
797 void onPreviewFrame(byte[] data, Camera camera);
798 };
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700801 * Starts capturing and drawing preview frames to the screen.
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800802 * Preview will not actually start until a surface is supplied
803 * with {@link #setPreviewDisplay(SurfaceHolder)} or
804 * {@link #setPreviewTexture(SurfaceTexture)}.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700805 *
806 * <p>If {@link #setPreviewCallback(Camera.PreviewCallback)},
807 * {@link #setOneShotPreviewCallback(Camera.PreviewCallback)}, or
808 * {@link #setPreviewCallbackWithBuffer(Camera.PreviewCallback)} were
809 * called, {@link Camera.PreviewCallback#onPreviewFrame(byte[], Camera)}
810 * will be called when preview data becomes available.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700811 *
812 * @throws RuntimeException if starting preview fails; usually this would be
813 * because of a hardware or other low-level error, or because release()
814 * has been called on this Camera instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 */
816 public native final void startPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700819 * Stops capturing and drawing preview frames to the surface, and
820 * resets the camera for a future call to {@link #startPreview()}.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700821 *
822 * @throws RuntimeException if stopping preview fails; usually this would be
823 * because of a hardware or other low-level error, or because release()
824 * has been called on this Camera instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 */
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800826 public final void stopPreview() {
827 _stopPreview();
828 mFaceDetectionRunning = false;
Chih-yu Huang664d72e2011-09-23 18:59:38 +0800829
830 mShutterCallback = null;
831 mRawImageCallback = null;
832 mPostviewCallback = null;
833 mJpegCallback = null;
Wu-cheng Lif05c1d62012-05-02 23:29:47 +0800834 synchronized (mAutoFocusCallbackLock) {
835 mAutoFocusCallback = null;
836 }
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800837 mAutoFocusMoveCallback = null;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +0800838 }
839
840 private native final void _stopPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 /**
843 * Return current preview state.
844 *
845 * FIXME: Unhide before release
846 * @hide
847 */
Mathew Inwood5132cc12018-08-08 15:50:55 +0100848 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 public native final boolean previewEnabled();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800852 * <p>Installs a callback to be invoked for every preview frame in addition
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700853 * to displaying them on the screen. The callback will be repeatedly called
854 * for as long as preview is active. This method can be called at any time,
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800855 * even while preview is live. Any other preview callbacks are
856 * overridden.</p>
857 *
858 * <p>If you are using the preview data to create video or still images,
859 * strongly consider using {@link android.media.MediaActionSound} to
860 * properly indicate image capture or recording start/stop to the user.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700862 * @param cb a callback object that receives a copy of each preview frame,
863 * or null to stop receiving callbacks.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700864 * @throws RuntimeException if release() has been called on this Camera
865 * instance.
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800866 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 */
868 public final void setPreviewCallback(PreviewCallback cb) {
869 mPreviewCallback = cb;
870 mOneShot = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400871 mWithBuffer = false;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700872 if (cb != null) {
873 mUsingPreviewAllocation = false;
874 }
Dave Sparksa6118c62009-10-13 02:28:54 -0700875 // Always use one-shot mode. We fake camera preview mode by
876 // doing one-shot preview continuously.
Andrew Harp94927df2009-10-20 01:47:05 -0400877 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879
880 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800881 * <p>Installs a callback to be invoked for the next preview frame in
882 * addition to displaying it on the screen. After one invocation, the
883 * callback is cleared. This method can be called any time, even when
884 * preview is live. Any other preview callbacks are overridden.</p>
885 *
886 * <p>If you are using the preview data to create video or still images,
887 * strongly consider using {@link android.media.MediaActionSound} to
888 * properly indicate image capture or recording start/stop to the user.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700890 * @param cb a callback object that receives a copy of the next preview frame,
891 * or null to stop receiving callbacks.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700892 * @throws RuntimeException if release() has been called on this Camera
893 * instance.
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800894 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 */
896 public final void setOneShotPreviewCallback(PreviewCallback cb) {
Andrew Harp94927df2009-10-20 01:47:05 -0400897 mPreviewCallback = cb;
898 mOneShot = true;
899 mWithBuffer = false;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700900 if (cb != null) {
901 mUsingPreviewAllocation = false;
902 }
Andrew Harp94927df2009-10-20 01:47:05 -0400903 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 }
905
Andrew Harp94927df2009-10-20 01:47:05 -0400906 private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer);
907
908 /**
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800909 * <p>Installs a callback to be invoked for every preview frame, using
910 * buffers supplied with {@link #addCallbackBuffer(byte[])}, in addition to
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700911 * displaying them on the screen. The callback will be repeatedly called
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800912 * for as long as preview is active and buffers are available. Any other
913 * preview callbacks are overridden.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400914 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700915 * <p>The purpose of this method is to improve preview efficiency and frame
916 * rate by allowing preview frame memory reuse. You must call
917 * {@link #addCallbackBuffer(byte[])} at some point -- before or after
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800918 * calling this method -- or no callbacks will received.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400919 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800920 * <p>The buffer queue will be cleared if this method is called with a null
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700921 * callback, {@link #setPreviewCallback(Camera.PreviewCallback)} is called,
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800922 * or {@link #setOneShotPreviewCallback(Camera.PreviewCallback)} is
923 * called.</p>
924 *
925 * <p>If you are using the preview data to create video or still images,
926 * strongly consider using {@link android.media.MediaActionSound} to
927 * properly indicate image capture or recording start/stop to the user.</p>
Andrew Harp94927df2009-10-20 01:47:05 -0400928 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700929 * @param cb a callback object that receives a copy of the preview frame,
930 * or null to stop receiving callbacks and clear the buffer queue.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -0700931 * @throws RuntimeException if release() has been called on this Camera
932 * instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700933 * @see #addCallbackBuffer(byte[])
Eino-Ville Talvala108708b2012-03-05 11:00:43 -0800934 * @see android.media.MediaActionSound
Andrew Harp94927df2009-10-20 01:47:05 -0400935 */
936 public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
937 mPreviewCallback = cb;
938 mOneShot = false;
939 mWithBuffer = true;
Eino-Ville Talvala7005b672013-04-02 15:46:38 -0700940 if (cb != null) {
941 mUsingPreviewAllocation = false;
942 }
Andrew Harp94927df2009-10-20 01:47:05 -0400943 setHasPreviewCallback(cb != null, true);
944 }
945
946 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +0800947 * Adds a pre-allocated buffer to the preview callback buffer queue.
948 * Applications can add one or more buffers to the queue. When a preview
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700949 * frame arrives and there is still at least one available buffer, the
950 * buffer will be used and removed from the queue. Then preview callback is
951 * invoked with the buffer. If a frame arrives and there is no buffer left,
952 * the frame is discarded. Applications should add buffers back when they
953 * finish processing the data in them.
Wu-cheng Lic10275a2010-03-09 13:49:21 -0800954 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700955 * <p>For formats besides YV12, the size of the buffer is determined by
956 * multiplying the preview image width, height, and bytes per pixel. The
957 * width and height can be read from
958 * {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel can be
959 * computed from {@link android.graphics.ImageFormat#getBitsPerPixel(int)} /
960 * 8, using the image format from
961 * {@link Camera.Parameters#getPreviewFormat()}.
962 *
963 * <p>If using the {@link android.graphics.ImageFormat#YV12} format, the
964 * size can be calculated using the equations listed in
965 * {@link Camera.Parameters#setPreviewFormat}.
Andrew Harp94927df2009-10-20 01:47:05 -0400966 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700967 * <p>This method is only necessary when
James Donge00cab72011-02-17 16:38:06 -0800968 * {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When
Dan Egnorbfcbeff2010-07-12 15:12:54 -0700969 * {@link #setPreviewCallback(PreviewCallback)} or
970 * {@link #setOneShotPreviewCallback(PreviewCallback)} are used, buffers
James Donge00cab72011-02-17 16:38:06 -0800971 * are automatically allocated. When a supplied buffer is too small to
972 * hold the preview frame data, preview callback will return null and
973 * the buffer will be removed from the buffer queue.
Andrew Harp94927df2009-10-20 01:47:05 -0400974 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -0700975 * @param callbackBuffer the buffer to add to the queue. The size of the
976 * buffer must match the values described above.
Wu-cheng Li5b9bcda2010-03-07 14:59:28 -0800977 * @see #setPreviewCallbackWithBuffer(PreviewCallback)
Andrew Harp94927df2009-10-20 01:47:05 -0400978 */
James Donge00cab72011-02-17 16:38:06 -0800979 public final void addCallbackBuffer(byte[] callbackBuffer)
980 {
981 _addCallbackBuffer(callbackBuffer, CAMERA_MSG_PREVIEW_FRAME);
982 }
983
984 /**
985 * Adds a pre-allocated buffer to the raw image callback buffer queue.
986 * Applications can add one or more buffers to the queue. When a raw image
987 * frame arrives and there is still at least one available buffer, the
988 * buffer will be used to hold the raw image data and removed from the
989 * queue. Then raw image callback is invoked with the buffer. If a raw
990 * image frame arrives but there is no buffer left, the frame is
991 * discarded. Applications should add buffers back when they finish
992 * processing the data in them by calling this method again in order
993 * to avoid running out of raw image callback buffers.
994 *
995 * <p>The size of the buffer is determined by multiplying the raw image
996 * width, height, and bytes per pixel. The width and height can be
997 * read from {@link Camera.Parameters#getPictureSize()}. Bytes per pixel
998 * can be computed from
999 * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
1000 * using the image format from {@link Camera.Parameters#getPreviewFormat()}.
1001 *
1002 * <p>This method is only necessary when the PictureCallbck for raw image
1003 * is used while calling {@link #takePicture(Camera.ShutterCallback,
1004 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
1005 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07001006 * <p>Please note that by calling this method, the mode for
1007 * application-managed callback buffers is triggered. If this method has
1008 * never been called, null will be returned by the raw image callback since
1009 * there is no image callback buffer available. Furthermore, When a supplied
1010 * buffer is too small to hold the raw image data, raw image callback will
1011 * return null and the buffer will be removed from the buffer queue.
James Donge00cab72011-02-17 16:38:06 -08001012 *
1013 * @param callbackBuffer the buffer to add to the raw image callback buffer
1014 * queue. The size should be width * height * (bits per pixel) / 8. An
1015 * null callbackBuffer will be ignored and won't be added to the queue.
1016 *
1017 * @see #takePicture(Camera.ShutterCallback,
1018 * Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
1019 *
1020 * {@hide}
1021 */
Mathew Inwood5132cc12018-08-08 15:50:55 +01001022 @UnsupportedAppUsage
James Donge00cab72011-02-17 16:38:06 -08001023 public final void addRawImageCallbackBuffer(byte[] callbackBuffer)
1024 {
1025 addCallbackBuffer(callbackBuffer, CAMERA_MSG_RAW_IMAGE);
1026 }
1027
Mathew Inwood5132cc12018-08-08 15:50:55 +01001028 @UnsupportedAppUsage
James Donge00cab72011-02-17 16:38:06 -08001029 private final void addCallbackBuffer(byte[] callbackBuffer, int msgType)
1030 {
1031 // CAMERA_MSG_VIDEO_FRAME may be allowed in the future.
1032 if (msgType != CAMERA_MSG_PREVIEW_FRAME &&
1033 msgType != CAMERA_MSG_RAW_IMAGE) {
1034 throw new IllegalArgumentException(
1035 "Unsupported message type: " + msgType);
1036 }
1037
1038 _addCallbackBuffer(callbackBuffer, msgType);
1039 }
1040
1041 private native final void _addCallbackBuffer(
1042 byte[] callbackBuffer, int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043
Eino-Ville Talvala7005b672013-04-02 15:46:38 -07001044 /**
1045 * <p>Create a {@link android.renderscript RenderScript}
1046 * {@link android.renderscript.Allocation Allocation} to use as a
1047 * destination of preview callback frames. Use
1048 * {@link #setPreviewCallbackAllocation setPreviewCallbackAllocation} to use
1049 * the created Allocation as a destination for camera preview frames.</p>
1050 *
1051 * <p>The Allocation will be created with a YUV type, and its contents must
1052 * be accessed within Renderscript with the {@code rsGetElementAtYuv_*}
1053 * accessor methods. Its size will be based on the current
1054 * {@link Parameters#getPreviewSize preview size} configured for this
1055 * camera.</p>
1056 *
1057 * @param rs the RenderScript context for this Allocation.
1058 * @param usage additional usage flags to set for the Allocation. The usage
1059 * flag {@link android.renderscript.Allocation#USAGE_IO_INPUT} will always
1060 * be set on the created Allocation, but additional flags may be provided
1061 * here.
1062 * @return a new YUV-type Allocation with dimensions equal to the current
1063 * preview size.
1064 * @throws RSIllegalArgumentException if the usage flags are not compatible
1065 * with an YUV Allocation.
1066 * @see #setPreviewCallbackAllocation
1067 * @hide
1068 */
1069 public final Allocation createPreviewAllocation(RenderScript rs, int usage)
1070 throws RSIllegalArgumentException {
1071 Parameters p = getParameters();
1072 Size previewSize = p.getPreviewSize();
1073 Type.Builder yuvBuilder = new Type.Builder(rs,
1074 Element.createPixel(rs,
1075 Element.DataType.UNSIGNED_8,
1076 Element.DataKind.PIXEL_YUV));
1077 // Use YV12 for wide compatibility. Changing this requires also
1078 // adjusting camera service's format selection.
1079 yuvBuilder.setYuvFormat(ImageFormat.YV12);
1080 yuvBuilder.setX(previewSize.width);
1081 yuvBuilder.setY(previewSize.height);
1082
1083 Allocation a = Allocation.createTyped(rs, yuvBuilder.create(),
1084 usage | Allocation.USAGE_IO_INPUT);
1085
1086 return a;
1087 }
1088
1089 /**
1090 * <p>Set an {@link android.renderscript.Allocation Allocation} as the
1091 * target of preview callback data. Use this method for efficient processing
1092 * of camera preview data with RenderScript. The Allocation must be created
1093 * with the {@link #createPreviewAllocation createPreviewAllocation }
1094 * method.</p>
1095 *
1096 * <p>Setting a preview allocation will disable any active preview callbacks
1097 * set by {@link #setPreviewCallback setPreviewCallback} or
1098 * {@link #setPreviewCallbackWithBuffer setPreviewCallbackWithBuffer}, and
1099 * vice versa. Using a preview allocation still requires an active standard
1100 * preview target to be set, either with
1101 * {@link #setPreviewTexture setPreviewTexture} or
1102 * {@link #setPreviewDisplay setPreviewDisplay}.</p>
1103 *
1104 * <p>To be notified when new frames are available to the Allocation, use
1105 * {@link android.renderscript.Allocation#setIoInputNotificationHandler Allocation.setIoInputNotificationHandler}. To
1106 * update the frame currently accessible from the Allocation to the latest
1107 * preview frame, call
1108 * {@link android.renderscript.Allocation#ioReceive Allocation.ioReceive}.</p>
1109 *
1110 * <p>To disable preview into the Allocation, call this method with a
1111 * {@code null} parameter.</p>
1112 *
1113 * <p>Once a preview allocation is set, the preview size set by
1114 * {@link Parameters#setPreviewSize setPreviewSize} cannot be changed. If
1115 * you wish to change the preview size, first remove the preview allocation
1116 * by calling {@code setPreviewCallbackAllocation(null)}, then change the
1117 * preview size, create a new preview Allocation with
1118 * {@link #createPreviewAllocation createPreviewAllocation}, and set it as
1119 * the new preview callback allocation target.</p>
1120 *
1121 * <p>If you are using the preview data to create video or still images,
1122 * strongly consider using {@link android.media.MediaActionSound} to
1123 * properly indicate image capture or recording start/stop to the user.</p>
1124 *
1125 * @param previewAllocation the allocation to use as destination for preview
1126 * @throws IOException if configuring the camera to use the Allocation for
1127 * preview fails.
1128 * @throws IllegalArgumentException if the Allocation's dimensions or other
1129 * parameters don't meet the requirements.
1130 * @see #createPreviewAllocation
1131 * @see #setPreviewCallback
1132 * @see #setPreviewCallbackWithBuffer
1133 * @hide
1134 */
1135 public final void setPreviewCallbackAllocation(Allocation previewAllocation)
1136 throws IOException {
1137 Surface previewSurface = null;
1138 if (previewAllocation != null) {
1139 Parameters p = getParameters();
1140 Size previewSize = p.getPreviewSize();
1141 if (previewSize.width != previewAllocation.getType().getX() ||
1142 previewSize.height != previewAllocation.getType().getY()) {
1143 throw new IllegalArgumentException(
1144 "Allocation dimensions don't match preview dimensions: " +
1145 "Allocation is " +
1146 previewAllocation.getType().getX() +
1147 ", " +
1148 previewAllocation.getType().getY() +
1149 ". Preview is " + previewSize.width + ", " +
1150 previewSize.height);
1151 }
1152 if ((previewAllocation.getUsage() &
1153 Allocation.USAGE_IO_INPUT) == 0) {
1154 throw new IllegalArgumentException(
1155 "Allocation usage does not include USAGE_IO_INPUT");
1156 }
1157 if (previewAllocation.getType().getElement().getDataKind() !=
1158 Element.DataKind.PIXEL_YUV) {
1159 throw new IllegalArgumentException(
1160 "Allocation is not of a YUV type");
1161 }
1162 previewSurface = previewAllocation.getSurface();
1163 mUsingPreviewAllocation = true;
1164 } else {
1165 mUsingPreviewAllocation = false;
1166 }
1167 setPreviewCallbackSurface(previewSurface);
1168 }
1169
1170 private native final void setPreviewCallbackSurface(Surface s);
1171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 private class EventHandler extends Handler
1173 {
Igor Murashkina1d66272014-06-20 11:22:11 -07001174 private final Camera mCamera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175
1176 public EventHandler(Camera c, Looper looper) {
1177 super(looper);
1178 mCamera = c;
1179 }
1180
1181 @Override
1182 public void handleMessage(Message msg) {
1183 switch(msg.what) {
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001184 case CAMERA_MSG_SHUTTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 if (mShutterCallback != null) {
1186 mShutterCallback.onShutter();
1187 }
1188 return;
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001189
1190 case CAMERA_MSG_RAW_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -07001191 if (mRawImageCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 return;
1195
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001196 case CAMERA_MSG_COMPRESSED_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -07001197 if (mJpegCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001201
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001202 case CAMERA_MSG_PREVIEW_FRAME:
Eino-Ville Talvalaca367b72012-05-30 16:01:33 -07001203 PreviewCallback pCb = mPreviewCallback;
1204 if (pCb != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 if (mOneShot) {
Dave Sparksa6118c62009-10-13 02:28:54 -07001206 // Clear the callback variable before the callback
1207 // in case the app calls setPreviewCallback from
1208 // the callback function
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 mPreviewCallback = null;
Andrew Harp94927df2009-10-20 01:47:05 -04001210 } else if (!mWithBuffer) {
Dave Sparksa6118c62009-10-13 02:28:54 -07001211 // We're faking the camera preview mode to prevent
1212 // the app from being flooded with preview frames.
1213 // Set to oneshot mode again.
Andrew Harp94927df2009-10-20 01:47:05 -04001214 setHasPreviewCallback(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 }
Eino-Ville Talvalaca367b72012-05-30 16:01:33 -07001216 pCb.onPreviewFrame((byte[])msg.obj, mCamera);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 }
1218 return;
1219
Dave Sparkse8b26e12009-07-14 10:35:40 -07001220 case CAMERA_MSG_POSTVIEW_FRAME:
1221 if (mPostviewCallback != null) {
1222 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera);
1223 }
1224 return;
1225
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001226 case CAMERA_MSG_FOCUS:
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001227 AutoFocusCallback cb = null;
1228 synchronized (mAutoFocusCallbackLock) {
1229 cb = mAutoFocusCallback;
1230 }
1231 if (cb != null) {
1232 boolean success = msg.arg1 == 0 ? false : true;
1233 cb.onAutoFocus(success, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001234 }
1235 return;
1236
1237 case CAMERA_MSG_ZOOM:
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001238 if (mZoomListener != null) {
1239 mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 return;
1242
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001243 case CAMERA_MSG_PREVIEW_METADATA:
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001244 if (mFaceListener != null) {
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001245 mFaceListener.onFaceDetection((Face[])msg.obj, mCamera);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001246 }
1247 return;
1248
Dave Sparksc62f9bd2009-06-26 13:33:32 -07001249 case CAMERA_MSG_ERROR :
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 Log.e(TAG, "Error " + msg.arg1);
Yin-Chia Yeha07cd322018-05-31 12:11:42 -07001251 if (mDetailedErrorCallback != null) {
1252 mDetailedErrorCallback.onError(msg.arg1, mCamera);
1253 } else if (mErrorCallback != null) {
1254 if (msg.arg1 == CAMERA_ERROR_DISABLED) {
1255 mErrorCallback.onError(CAMERA_ERROR_EVICTED, mCamera);
1256 } else {
1257 mErrorCallback.onError(msg.arg1, mCamera);
1258 }
Dave Sparkse8b26e12009-07-14 10:35:40 -07001259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 return;
1261
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001262 case CAMERA_MSG_FOCUS_MOVE:
1263 if (mAutoFocusMoveCallback != null) {
1264 mAutoFocusMoveCallback.onAutoFocusMoving(msg.arg1 == 0 ? false : true, mCamera);
1265 }
1266 return;
1267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 default:
1269 Log.e(TAG, "Unknown message type " + msg.what);
1270 return;
1271 }
1272 }
1273 }
1274
Mathew Inwood5132cc12018-08-08 15:50:55 +01001275 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 private static void postEventFromNative(Object camera_ref,
1277 int what, int arg1, int arg2, Object obj)
1278 {
1279 Camera c = (Camera)((WeakReference)camera_ref).get();
1280 if (c == null)
1281 return;
1282
1283 if (c.mEventHandler != null) {
1284 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj);
1285 c.mEventHandler.sendMessage(m);
1286 }
1287 }
1288
1289 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001290 * Callback interface used to notify on completion of camera auto focus.
1291 *
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001292 * <p>Devices that do not support auto-focus will receive a "fake"
1293 * callback to this interface. If your application needs auto-focus and
Scott Maindf4578e2009-09-10 12:22:07 -07001294 * should not be installed on devices <em>without</em> auto-focus, you must
1295 * declare that your app uses the
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001296 * {@code android.hardware.camera.autofocus} feature, in the
Scott Maindf4578e2009-09-10 12:22:07 -07001297 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
1298 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001299 *
1300 * @see #autoFocus(AutoFocusCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001301 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1302 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001304 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 public interface AutoFocusCallback
1306 {
1307 /**
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001308 * Called when the camera auto focus completes. If the camera
1309 * does not support auto-focus and autoFocus is called,
1310 * onAutoFocus will be called immediately with a fake value of
1311 * <code>success</code> set to <code>true</code>.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001312 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08001313 * The auto-focus routine does not lock auto-exposure and auto-white
1314 * balance after it completes.
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001315 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 * @param success true if focus was successful, false if otherwise
1317 * @param camera the Camera service object
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001318 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
1319 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 */
1321 void onAutoFocus(boolean success, Camera camera);
Wu-cheng Li53b30912011-10-12 19:43:51 +08001322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323
1324 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001325 * Starts camera auto-focus and registers a callback function to run when
1326 * the camera is focused. This method is only valid when preview is active
1327 * (between {@link #startPreview()} and before {@link #stopPreview()}).
1328 *
1329 * <p>Callers should check
1330 * {@link android.hardware.Camera.Parameters#getFocusMode()} to determine if
1331 * this method should be called. If the camera does not support auto-focus,
1332 * it is a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)}
Wu-cheng Li36322db2009-09-18 18:59:21 +08001333 * callback will be called immediately.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001334 *
Scott Mainda0a56d2009-09-10 18:08:37 -07001335 * <p>If your application should not be installed
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001336 * on devices without auto-focus, you must declare that your application
1337 * uses auto-focus with the
Scott Maindf4578e2009-09-10 12:22:07 -07001338 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
1339 * manifest element.</p>
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001340 *
Wu-cheng Li068ef422009-09-27 13:19:36 -07001341 * <p>If the current flash mode is not
1342 * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001343 * fired during auto-focus, depending on the driver and camera hardware.<p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001344 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001345 * <p>Auto-exposure lock {@link android.hardware.Camera.Parameters#getAutoExposureLock()}
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08001346 * and auto-white balance locks {@link android.hardware.Camera.Parameters#getAutoWhiteBalanceLock()}
1347 * do not change during and after autofocus. But auto-focus routine may stop
1348 * auto-exposure and auto-white balance transiently during focusing.
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001349 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001350 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
1351 * image capture with {@link #takePicture(Camera.ShutterCallback,
1352 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
1353 * the focus position. Applications must call cancelAutoFocus to reset the
1354 * focus.</p>
1355 *
Eino-Ville Talvala108708b2012-03-05 11:00:43 -08001356 * <p>If autofocus is successful, consider using
1357 * {@link android.media.MediaActionSound} to properly play back an autofocus
1358 * success sound to the user.</p>
1359 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 * @param cb the callback to run
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001361 * @throws RuntimeException if starting autofocus fails; usually this would
1362 * be because of a hardware or other low-level error, or because
1363 * release() has been called on this Camera instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001364 * @see #cancelAutoFocus()
Eino-Ville Talvala83d33522011-05-13 10:25:00 -07001365 * @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
1366 * @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
Eino-Ville Talvala108708b2012-03-05 11:00:43 -08001367 * @see android.media.MediaActionSound
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 */
1369 public final void autoFocus(AutoFocusCallback cb)
1370 {
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001371 synchronized (mAutoFocusCallbackLock) {
James Dong248ba232012-04-28 21:30:46 -07001372 mAutoFocusCallback = cb;
James Dong248ba232012-04-28 21:30:46 -07001373 }
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001374 native_autoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 }
1376 private native final void native_autoFocus();
1377
1378 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001379 * Cancels any auto-focus function in progress.
1380 * Whether or not auto-focus is currently in progress,
1381 * this function will return the focus position to the default.
Chih-Chung Chang244f8c22009-09-15 14:51:56 +08001382 * If the camera does not support auto-focus, this is a no-op.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001383 *
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001384 * @throws RuntimeException if canceling autofocus fails; usually this would
1385 * be because of a hardware or other low-level error, or because
1386 * release() has been called on this Camera instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001387 * @see #autoFocus(Camera.AutoFocusCallback)
Chih-Chung Chang244f8c22009-09-15 14:51:56 +08001388 */
1389 public final void cancelAutoFocus()
1390 {
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001391 synchronized (mAutoFocusCallbackLock) {
James Dong248ba232012-04-28 21:30:46 -07001392 mAutoFocusCallback = null;
James Dong248ba232012-04-28 21:30:46 -07001393 }
Wu-cheng Lif05c1d62012-05-02 23:29:47 +08001394 native_cancelAutoFocus();
1395 // CAMERA_MSG_FOCUS should be removed here because the following
1396 // scenario can happen:
1397 // - An application uses the same thread for autoFocus, cancelAutoFocus
1398 // and looper thread.
1399 // - The application calls autoFocus.
1400 // - HAL sends CAMERA_MSG_FOCUS, which enters the looper message queue.
1401 // Before event handler's handleMessage() is invoked, the application
1402 // calls cancelAutoFocus and autoFocus.
1403 // - The application gets the old CAMERA_MSG_FOCUS and thinks autofocus
1404 // has been completed. But in fact it is not.
1405 //
1406 // As documented in the beginning of the file, apps should not use
1407 // multiple threads to call autoFocus and cancelAutoFocus at the same
1408 // time. It is HAL's responsibility not to send a CAMERA_MSG_FOCUS
1409 // message after native_cancelAutoFocus is called.
1410 mEventHandler.removeMessages(CAMERA_MSG_FOCUS);
Chih-Chung Chang244f8c22009-09-15 14:51:56 +08001411 }
1412 private native final void native_cancelAutoFocus();
1413
1414 /**
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001415 * Callback interface used to notify on auto focus start and stop.
1416 *
Wu-cheng Li65745392012-04-12 18:07:16 +08001417 * <p>This is only supported in continuous autofocus modes -- {@link
1418 * Parameters#FOCUS_MODE_CONTINUOUS_VIDEO} and {@link
1419 * Parameters#FOCUS_MODE_CONTINUOUS_PICTURE}. Applications can show
1420 * autofocus animation based on this.</p>
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001421 *
1422 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1423 * applications.
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001424 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001425 @Deprecated
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001426 public interface AutoFocusMoveCallback
1427 {
1428 /**
1429 * Called when the camera auto focus starts or stops.
1430 *
1431 * @param start true if focus starts to move, false if focus stops to move
Wu-cheng Li65745392012-04-12 18:07:16 +08001432 * @param camera the Camera service object
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001433 */
1434 void onAutoFocusMoving(boolean start, Camera camera);
1435 }
1436
1437 /**
1438 * Sets camera auto-focus move callback.
1439 *
1440 * @param cb the callback to run
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001441 * @throws RuntimeException if enabling the focus move callback fails;
1442 * usually this would be because of a hardware or other low-level error,
1443 * or because release() has been called on this Camera instance.
Wu-cheng Li9d062cf2011-11-14 20:30:14 +08001444 */
1445 public void setAutoFocusMoveCallback(AutoFocusMoveCallback cb) {
1446 mAutoFocusMoveCallback = cb;
1447 enableFocusMoveCallback((mAutoFocusMoveCallback != null) ? 1 : 0);
1448 }
1449
1450 private native void enableFocusMoveCallback(int enable);
1451
1452 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001453 * Callback interface used to signal the moment of actual image capture.
1454 *
1455 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001456 *
1457 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1458 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001460 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 public interface ShutterCallback
1462 {
1463 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001464 * Called as near as possible to the moment when a photo is captured
1465 * from the sensor. This is a good opportunity to play a shutter sound
1466 * or give other feedback of camera operation. This may be some time
1467 * after the photo was triggered, but some time before the actual data
1468 * is available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 */
1470 void onShutter();
1471 }
1472
1473 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001474 * Callback interface used to supply image data from a photo capture.
1475 *
1476 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001477 *
1478 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1479 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001481 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 public interface PictureCallback {
1483 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001484 * Called when image data is available after a picture is taken.
1485 * The format of the data depends on the context of the callback
1486 * and {@link Camera.Parameters} settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001487 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 * @param data a byte array of the picture data
1489 * @param camera the Camera service object
1490 */
1491 void onPictureTaken(byte[] data, Camera camera);
1492 };
1493
1494 /**
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001495 * Equivalent to <pre>takePicture(Shutter, raw, null, jpeg)</pre>.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001496 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001497 * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 */
1499 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
1500 PictureCallback jpeg) {
Dave Sparkse8b26e12009-07-14 10:35:40 -07001501 takePicture(shutter, raw, null, jpeg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 }
James Donge00cab72011-02-17 16:38:06 -08001503 private native final void native_takePicture(int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504
Dave Sparkse8b26e12009-07-14 10:35:40 -07001505 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001506 * Triggers an asynchronous image capture. The camera service will initiate
1507 * a series of callbacks to the application as the image capture progresses.
1508 * The shutter callback occurs after the image is captured. This can be used
1509 * to trigger a sound to let the user know that image has been captured. The
1510 * raw callback occurs when the raw image data is available (NOTE: the data
James Donge00cab72011-02-17 16:38:06 -08001511 * will be null if there is no raw image callback buffer available or the
1512 * raw image callback buffer is not large enough to hold the raw image).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001513 * The postview callback occurs when a scaled, fully processed postview
1514 * image is available (NOTE: not all hardware supports this). The jpeg
1515 * callback occurs when the compressed image is available. If the
1516 * application does not need a particular callback, a null can be passed
1517 * instead of a callback method.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001518 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001519 * <p>This method is only valid when preview is active (after
1520 * {@link #startPreview()}). Preview will be stopped after the image is
1521 * taken; callers must call {@link #startPreview()} again if they want to
Wu-cheng Li42419ce2011-06-01 17:22:24 +08001522 * re-start preview or take more pictures. This should not be called between
1523 * {@link android.media.MediaRecorder#start()} and
1524 * {@link android.media.MediaRecorder#stop()}.
Wu-cheng Li40057ce2009-12-02 18:57:29 +08001525 *
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001526 * <p>After calling this method, you must not call {@link #startPreview()}
1527 * or take another picture until the JPEG callback has returned.
1528 *
1529 * @param shutter the callback for image capture moment, or null
1530 * @param raw the callback for raw (uncompressed) image data, or null
Dave Sparkse8b26e12009-07-14 10:35:40 -07001531 * @param postview callback with postview image data, may be null
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001532 * @param jpeg the callback for JPEG image data, or null
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001533 * @throws RuntimeException if starting picture capture fails; usually this
1534 * would be because of a hardware or other low-level error, or because
1535 * release() has been called on this Camera instance.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001536 */
1537 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
1538 PictureCallback postview, PictureCallback jpeg) {
1539 mShutterCallback = shutter;
1540 mRawImageCallback = raw;
1541 mPostviewCallback = postview;
1542 mJpegCallback = jpeg;
James Donge00cab72011-02-17 16:38:06 -08001543
1544 // If callback is not set, do not send me callbacks.
1545 int msgType = 0;
1546 if (mShutterCallback != null) {
1547 msgType |= CAMERA_MSG_SHUTTER;
1548 }
1549 if (mRawImageCallback != null) {
1550 msgType |= CAMERA_MSG_RAW_IMAGE;
1551 }
1552 if (mPostviewCallback != null) {
1553 msgType |= CAMERA_MSG_POSTVIEW_FRAME;
1554 }
1555 if (mJpegCallback != null) {
1556 msgType |= CAMERA_MSG_COMPRESSED_IMAGE;
1557 }
1558
1559 native_takePicture(msgType);
Wu-cheng Lie9c6c9c2012-05-29 16:47:44 +08001560 mFaceDetectionRunning = false;
Dave Sparkse8b26e12009-07-14 10:35:40 -07001561 }
1562
1563 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001564 * Zooms to the requested value smoothly. The driver will notify {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001565 * OnZoomChangeListener} of the zoom value and whether zoom is stopped at
1566 * the time. For example, suppose the current zoom is 0 and startSmoothZoom
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001567 * is called with value 3. The
1568 * {@link Camera.OnZoomChangeListener#onZoomChange(int, boolean, Camera)}
1569 * method will be called three times with zoom values 1, 2, and 3.
1570 * Applications can call {@link #stopSmoothZoom} to stop the zoom earlier.
1571 * Applications should not call startSmoothZoom again or change the zoom
1572 * value before zoom stops. If the supplied zoom value equals to the current
1573 * zoom value, no zoom callback will be generated. This method is supported
1574 * if {@link android.hardware.Camera.Parameters#isSmoothZoomSupported}
1575 * returns true.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001576 *
1577 * @param value zoom value. The valid range is 0 to {@link
1578 * android.hardware.Camera.Parameters#getMaxZoom}.
Wu-cheng Li0ca25192010-03-29 16:21:12 +08001579 * @throws IllegalArgumentException if the zoom value is invalid.
1580 * @throws RuntimeException if the method fails.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001581 * @see #setZoomChangeListener(OnZoomChangeListener)
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001582 */
1583 public native final void startSmoothZoom(int value);
1584
1585 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001586 * Stops the smooth zoom. Applications should wait for the {@link
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001587 * OnZoomChangeListener} to know when the zoom is actually stopped. This
1588 * method is supported if {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001589 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true.
Wu-cheng Li0ca25192010-03-29 16:21:12 +08001590 *
1591 * @throws RuntimeException if the method fails.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001592 */
1593 public native final void stopSmoothZoom();
1594
1595 /**
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001596 * Set the clockwise rotation of preview display in degrees. This affects
1597 * the preview frames and the picture displayed after snapshot. This method
1598 * is useful for portrait mode applications. Note that preview display of
Wu-cheng Lib982fb42010-10-19 17:19:09 +08001599 * front-facing cameras is flipped horizontally before the rotation, that
1600 * is, the image is reflected along the central vertical axis of the camera
1601 * sensor. So the users can see themselves as looking into a mirror.
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001602 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001603 * <p>This does not affect the order of byte array passed in {@link
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001604 * PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This
1605 * method is not allowed to be called during preview.
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001606 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08001607 * <p>If you want to make the camera image show in the same orientation as
1608 * the display, you can use the following code.
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001609 * <pre>
Chih-Chung Chang724c5222010-06-14 18:57:15 +08001610 * public static void setCameraDisplayOrientation(Activity activity,
1611 * int cameraId, android.hardware.Camera camera) {
1612 * android.hardware.Camera.CameraInfo info =
1613 * new android.hardware.Camera.CameraInfo();
1614 * android.hardware.Camera.getCameraInfo(cameraId, info);
1615 * int rotation = activity.getWindowManager().getDefaultDisplay()
1616 * .getRotation();
1617 * int degrees = 0;
1618 * switch (rotation) {
1619 * case Surface.ROTATION_0: degrees = 0; break;
1620 * case Surface.ROTATION_90: degrees = 90; break;
1621 * case Surface.ROTATION_180: degrees = 180; break;
1622 * case Surface.ROTATION_270: degrees = 270; break;
1623 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001624 *
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001625 * int result;
1626 * if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
1627 * result = (info.orientation + degrees) % 360;
1628 * result = (360 - result) % 360; // compensate the mirror
1629 * } else { // back-facing
1630 * result = (info.orientation - degrees + 360) % 360;
1631 * }
Chih-Chung Chang724c5222010-06-14 18:57:15 +08001632 * camera.setDisplayOrientation(result);
1633 * }
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001634 * </pre>
Wu-cheng Lid3033622011-10-07 13:13:54 +08001635 *
1636 * <p>Starting from API level 14, this method can be called when preview is
1637 * active.
1638 *
Eino-Ville Talvala6c91e2c2016-03-25 11:54:39 -07001639 * <p><b>Note: </b>Before API level 24, the default value for orientation is 0. Starting in
1640 * API level 24, the default orientation will be such that applications in forced-landscape mode
1641 * will have correct preview orientation, which may be either a default of 0 or
1642 * 180. Applications that operate in portrait mode or allow for changing orientation must still
1643 * call this method after each orientation change to ensure correct preview display in all
1644 * cases.</p>
1645 *
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001646 * @param degrees the angle that the picture will be rotated clockwise.
Eino-Ville Talvala6c91e2c2016-03-25 11:54:39 -07001647 * Valid values are 0, 90, 180, and 270.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001648 * @throws RuntimeException if setting orientation fails; usually this would
1649 * be because of a hardware or other low-level error, or because
1650 * release() has been called on this Camera instance.
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08001651 * @see #setPreviewDisplay(SurfaceHolder)
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001652 */
1653 public native final void setDisplayOrientation(int degrees);
1654
1655 /**
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001656 * <p>Enable or disable the default shutter sound when taking a picture.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001657 *
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001658 * <p>By default, the camera plays the system-defined camera shutter sound
1659 * when {@link #takePicture} is called. Using this method, the shutter sound
1660 * can be disabled. It is strongly recommended that an alternative shutter
1661 * sound is played in the {@link ShutterCallback} when the system shutter
1662 * sound is disabled.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001663 *
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001664 * <p>Note that devices may not always allow disabling the camera shutter
1665 * sound. If the shutter sound state cannot be set to the desired value,
1666 * this method will return false. {@link CameraInfo#canDisableShutterSound}
1667 * can be used to determine whether the device will allow the shutter sound
1668 * to be disabled.</p>
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001669 *
1670 * @param enabled whether the camera should play the system shutter sound
1671 * when {@link #takePicture takePicture} is called.
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001672 * @return {@code true} if the shutter sound state was successfully
1673 * changed. {@code false} if the shutter sound state could not be
1674 * changed. {@code true} is also returned if shutter sound playback
1675 * is already set to the requested state.
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07001676 * @throws RuntimeException if the call fails; usually this would be because
1677 * of a hardware or other low-level error, or because release() has been
1678 * called on this Camera instance.
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001679 * @see #takePicture
Eino-Ville Talvala487acdf2012-09-24 11:30:24 -07001680 * @see CameraInfo#canDisableShutterSound
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001681 * @see ShutterCallback
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001682 */
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001683 public final boolean enableShutterSound(boolean enabled) {
jiabine059e452018-05-07 15:16:44 -07001684 boolean canDisableShutterSound = true;
1685 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
1686 IAudioService audioService = IAudioService.Stub.asInterface(b);
1687 try {
1688 if (audioService.isCameraSoundForced()) {
1689 canDisableShutterSound = false;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001690 }
jiabine059e452018-05-07 15:16:44 -07001691 } catch (RemoteException e) {
1692 Log.e(TAG, "Audio service is unavailable for queries");
1693 }
1694 if (!enabled && !canDisableShutterSound) {
1695 return false;
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001696 }
jiabin503e1fd2018-04-19 16:39:44 -07001697 synchronized (mShutterSoundLock) {
jiabine059e452018-05-07 15:16:44 -07001698 mShutterSoundEnabledFromApp = enabled;
1699 // Return the result of _enableShutterSound(enabled) in all cases.
1700 // If the shutter sound can be disabled, disable it when the device is in DnD mode.
jiabin503e1fd2018-04-19 16:39:44 -07001701 boolean ret = _enableShutterSound(enabled);
jiabine059e452018-05-07 15:16:44 -07001702 if (enabled && !mHasAppOpsPlayAudio) {
1703 Log.i(TAG, "Shutter sound is not allowed by AppOpsManager");
1704 if (canDisableShutterSound) {
1705 _enableShutterSound(false);
1706 }
jiabin503e1fd2018-04-19 16:39:44 -07001707 }
1708 return ret;
1709 }
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001710 }
1711
Igor Murashkin66533622014-08-19 14:51:47 -07001712 /**
1713 * Disable the shutter sound unconditionally.
1714 *
1715 * <p>
1716 * This is only guaranteed to work for legacy cameras
1717 * (i.e. initialized with {@link #cameraInitUnspecified}). Trying to call this on
1718 * a regular camera will force a conditional check in the camera service.
1719 * </p>
1720 *
1721 * @return {@code true} if the shutter sound state was successfully
1722 * changed. {@code false} if the shutter sound state could not be
1723 * changed. {@code true} is also returned if shutter sound playback
1724 * is already set to the requested state.
1725 *
1726 * @hide
1727 */
1728 public final boolean disableShutterSound() {
1729 return _enableShutterSound(/*enabled*/false);
1730 }
1731
Eino-Ville Talvala4f8e5ce2012-10-08 18:16:35 -07001732 private native final boolean _enableShutterSound(boolean enabled);
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001733
jiabin503e1fd2018-04-19 16:39:44 -07001734 private static class IAppOpsCallbackWrapper extends IAppOpsCallback.Stub {
1735 private final WeakReference<Camera> mWeakCamera;
1736
1737 IAppOpsCallbackWrapper(Camera camera) {
1738 mWeakCamera = new WeakReference<Camera>(camera);
1739 }
1740
1741 @Override
1742 public void opChanged(int op, int uid, String packageName) {
1743 if (op == AppOpsManager.OP_PLAY_AUDIO) {
1744 final Camera camera = mWeakCamera.get();
1745 if (camera != null) {
1746 camera.updateAppOpsPlayAudio();
1747 }
1748 }
1749 }
1750 }
1751
1752 private void updateAppOpsPlayAudio() {
1753 synchronized (mShutterSoundLock) {
1754 boolean oldHasAppOpsPlayAudio = mHasAppOpsPlayAudio;
1755 try {
1756 int mode = AppOpsManager.MODE_IGNORED;
1757 if (mAppOps != null) {
1758 mode = mAppOps.checkAudioOperation(AppOpsManager.OP_PLAY_AUDIO,
1759 AudioAttributes.USAGE_ASSISTANCE_SONIFICATION,
1760 Process.myUid(), ActivityThread.currentPackageName());
1761 }
1762 mHasAppOpsPlayAudio = mode == AppOpsManager.MODE_ALLOWED;
1763 } catch (RemoteException e) {
1764 Log.e(TAG, "AppOpsService check audio operation failed");
1765 mHasAppOpsPlayAudio = false;
1766 }
1767 if (oldHasAppOpsPlayAudio != mHasAppOpsPlayAudio) {
1768 if (!mHasAppOpsPlayAudio) {
jiabine059e452018-05-07 15:16:44 -07001769 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
1770 IAudioService audioService = IAudioService.Stub.asInterface(b);
1771 try {
1772 if (audioService.isCameraSoundForced()) {
1773 return;
1774 }
1775 } catch (RemoteException e) {
1776 Log.e(TAG, "Audio service is unavailable for queries");
1777 }
jiabin503e1fd2018-04-19 16:39:44 -07001778 _enableShutterSound(false);
1779 } else {
jiabine059e452018-05-07 15:16:44 -07001780 enableShutterSound(mShutterSoundEnabledFromApp);
jiabin503e1fd2018-04-19 16:39:44 -07001781 }
1782 }
1783 }
1784 }
1785
Eino-Ville Talvala69fe5272012-09-07 12:26:40 -07001786 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001787 * Callback interface for zoom changes during a smooth zoom operation.
1788 *
1789 * @see #setZoomChangeListener(OnZoomChangeListener)
1790 * @see #startSmoothZoom(int)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001791 *
1792 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1793 * applications.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001794 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001795 @Deprecated
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001796 public interface OnZoomChangeListener
Dave Sparkse8b26e12009-07-14 10:35:40 -07001797 {
1798 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07001799 * Called when the zoom value has changed during a smooth zoom.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001800 *
1801 * @param zoomValue the current zoom value. In smooth zoom mode, camera
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001802 * calls this for every new zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001803 * @param stopped whether smooth zoom is stopped. If the value is true,
1804 * this is the last zoom update for the application.
Dave Sparkse8b26e12009-07-14 10:35:40 -07001805 * @param camera the Camera service object
1806 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001807 void onZoomChange(int zoomValue, boolean stopped, Camera camera);
Dave Sparkse8b26e12009-07-14 10:35:40 -07001808 };
1809
1810 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001811 * Registers a listener to be notified when the zoom value is updated by the
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001812 * camera driver during smooth zoom.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001813 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001814 * @param listener the listener to notify
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001815 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001816 */
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001817 public final void setZoomChangeListener(OnZoomChangeListener listener)
Dave Sparkse8b26e12009-07-14 10:35:40 -07001818 {
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08001819 mZoomListener = listener;
Dave Sparkse8b26e12009-07-14 10:35:40 -07001820 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001821
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001822 /**
1823 * Callback interface for face detected in the preview frame.
1824 *
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001825 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1826 * applications.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001827 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001828 @Deprecated
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001829 public interface FaceDetectionListener
1830 {
1831 /**
1832 * Notify the listener of the detected faces in the preview frame.
1833 *
Wu-cheng Li53b30912011-10-12 19:43:51 +08001834 * @param faces The detected faces in a list
Joe Fernandez464cb212011-10-04 16:56:47 -07001835 * @param camera The {@link Camera} service object
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001836 */
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001837 void onFaceDetection(Face[] faces, Camera camera);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001838 }
1839
1840 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001841 * Registers a listener to be notified about the faces detected in the
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001842 * preview frame.
1843 *
1844 * @param listener the listener to notify
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001845 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001846 */
1847 public final void setFaceDetectionListener(FaceDetectionListener listener)
1848 {
1849 mFaceListener = listener;
1850 }
1851
1852 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001853 * Starts the face detection. This should be called after preview is started.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001854 * The camera will notify {@link FaceDetectionListener} of the detected
1855 * faces in the preview frame. The detected faces may be the same as the
1856 * previous ones. Applications should call {@link #stopFaceDetection} to
1857 * stop the face detection. This method is supported if {@link
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001858 * Parameters#getMaxNumDetectedFaces()} returns a number larger than 0.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001859 * If the face detection has started, apps should not call this again.
1860 *
Wu-cheng Li8c136702011-08-12 20:25:00 +08001861 * <p>When the face detection is running, {@link Parameters#setWhiteBalance(String)},
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001862 * {@link Parameters#setFocusAreas(List)}, and {@link Parameters#setMeteringAreas(List)}
Wu-cheng Li8c136702011-08-12 20:25:00 +08001863 * have no effect. The camera uses the detected faces to do auto-white balance,
1864 * auto exposure, and autofocus.
1865 *
1866 * <p>If the apps call {@link #autoFocus(AutoFocusCallback)}, the camera
1867 * will stop sending face callbacks. The last face callback indicates the
1868 * areas used to do autofocus. After focus completes, face detection will
1869 * resume sending face callbacks. If the apps call {@link
1870 * #cancelAutoFocus()}, the face callbacks will also resume.</p>
1871 *
1872 * <p>After calling {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
1873 * Camera.PictureCallback)} or {@link #stopPreview()}, and then resuming
1874 * preview with {@link #startPreview()}, the apps should call this method
1875 * again to resume face detection.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001876 *
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001877 * @throws IllegalArgumentException if the face detection is unsupported.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001878 * @throws RuntimeException if the method fails or the face detection is
1879 * already running.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001880 * @see FaceDetectionListener
1881 * @see #stopFaceDetection()
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001882 * @see Parameters#getMaxNumDetectedFaces()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001883 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001884 public final void startFaceDetection() {
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001885 if (mFaceDetectionRunning) {
1886 throw new RuntimeException("Face detection is already running");
1887 }
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001888 _startFaceDetection(CAMERA_FACE_DETECTION_HW);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001889 mFaceDetectionRunning = true;
1890 }
1891
1892 /**
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001893 * Stops the face detection.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001894 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001895 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001896 */
1897 public final void stopFaceDetection() {
1898 _stopFaceDetection();
1899 mFaceDetectionRunning = false;
1900 }
1901
1902 private native final void _startFaceDetection(int type);
1903 private native final void _stopFaceDetection();
1904
1905 /**
Joe Fernandez464cb212011-10-04 16:56:47 -07001906 * Information about a face identified through camera face detection.
Wu-cheng Li53b30912011-10-12 19:43:51 +08001907 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001908 * <p>When face detection is used with a camera, the {@link FaceDetectionListener} returns a
1909 * list of face objects for use in focusing and metering.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001910 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001911 * @see FaceDetectionListener
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001912 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
1913 * applications.
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001914 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07001915 @Deprecated
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001916 public static class Face {
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001917 /**
1918 * Create an empty face.
1919 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001920 public Face() {
1921 }
1922
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001923 /**
1924 * Bounds of the face. (-1000, -1000) represents the top-left of the
1925 * camera field of view, and (1000, 1000) represents the bottom-right of
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08001926 * the field of view. For example, suppose the size of the viewfinder UI
1927 * is 800x480. The rect passed from the driver is (-1000, -1000, 0, 0).
Wu-cheng Li8c136702011-08-12 20:25:00 +08001928 * The corresponding viewfinder rect should be (0, 0, 400, 240). It is
1929 * guaranteed left < right and top < bottom. The coordinates can be
1930 * smaller than -1000 or bigger than 1000. But at least one vertex will
1931 * be within (-1000, -1000) and (1000, 1000).
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001932 *
1933 * <p>The direction is relative to the sensor orientation, that is, what
1934 * the sensor sees. The direction is not affected by the rotation or
Wu-cheng Li8c136702011-08-12 20:25:00 +08001935 * mirroring of {@link #setDisplayOrientation(int)}. The face bounding
1936 * rectangle does not provide any information about face orientation.</p>
1937 *
1938 * <p>Here is the matrix to convert driver coordinates to View coordinates
1939 * in pixels.</p>
1940 * <pre>
1941 * Matrix matrix = new Matrix();
1942 * CameraInfo info = CameraHolder.instance().getCameraInfo()[cameraId];
1943 * // Need mirror for front camera.
1944 * boolean mirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
1945 * matrix.setScale(mirror ? -1 : 1, 1);
1946 * // This is the value for android.hardware.Camera.setDisplayOrientation.
1947 * matrix.postRotate(displayOrientation);
1948 * // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
1949 * // UI coordinates range from (0, 0) to (width, height).
1950 * matrix.postScale(view.getWidth() / 2000f, view.getHeight() / 2000f);
1951 * matrix.postTranslate(view.getWidth() / 2f, view.getHeight() / 2f);
1952 * </pre>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001953 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001954 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001955 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001956 public Rect rect;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001957
1958 /**
Eino-Ville Talvala8df3b2b2012-09-15 14:30:12 -07001959 * <p>The confidence level for the detection of the face. The range is 1 to
1960 * 100. 100 is the highest confidence.</p>
1961 *
1962 * <p>Depending on the device, even very low-confidence faces may be
1963 * listed, so applications should filter out faces with low confidence,
1964 * depending on the use case. For a typical point-and-shoot camera
1965 * application that wishes to display rectangles around detected faces,
1966 * filtering out faces with confidence less than 50 is recommended.</p>
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001967 *
Joe Fernandez464cb212011-10-04 16:56:47 -07001968 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08001969 */
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001970 public int score;
Wei Huad52b3082011-08-19 13:01:51 -07001971
1972 /**
1973 * An unique id per face while the face is visible to the tracker. If
1974 * the face leaves the field-of-view and comes back, it will get a new
1975 * id. This is an optional field, may not be supported on all devices.
1976 * If not supported, id will always be set to -1. The optional fields
1977 * are supported as a set. Either they are all valid, or none of them
1978 * are.
1979 */
1980 public int id = -1;
1981
1982 /**
1983 * The coordinates of the center of the left eye. The coordinates are in
1984 * the same space as the ones for {@link #rect}. This is an optional
1985 * field, may not be supported on all devices. If not supported, the
1986 * value will always be set to null. The optional fields are supported
1987 * as a set. Either they are all valid, or none of them are.
1988 */
1989 public Point leftEye = null;
1990
1991 /**
1992 * The coordinates of the center of the right eye. The coordinates are
1993 * in the same space as the ones for {@link #rect}.This is an optional
1994 * field, may not be supported on all devices. If not supported, the
1995 * value will always be set to null. The optional fields are supported
1996 * as a set. Either they are all valid, or none of them are.
1997 */
1998 public Point rightEye = null;
1999
2000 /**
2001 * The coordinates of the center of the mouth. The coordinates are in
2002 * the same space as the ones for {@link #rect}. This is an optional
2003 * field, may not be supported on all devices. If not supported, the
2004 * value will always be set to null. The optional fields are supported
2005 * as a set. Either they are all valid, or none of them are.
2006 */
2007 public Point mouth = null;
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08002008 }
2009
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002010 /**
2011 * Unspecified camera error.
2012 * @see Camera.ErrorCallback
2013 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 public static final int CAMERA_ERROR_UNKNOWN = 1;
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002015
2016 /**
Ruben Brunke91b9252015-04-14 13:38:06 -07002017 * Camera was disconnected due to use by higher priority user.
2018 * @see Camera.ErrorCallback
2019 */
2020 public static final int CAMERA_ERROR_EVICTED = 2;
2021
2022 /**
Yin-Chia Yeha07cd322018-05-31 12:11:42 -07002023 * Camera was disconnected due to device policy change or client
2024 * application going to background.
2025 * @see Camera.ErrorCallback
2026 *
2027 * @hide
2028 */
2029 public static final int CAMERA_ERROR_DISABLED = 3;
2030
2031 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002032 * Media server died. In this case, the application must release the
2033 * Camera object and instantiate a new one.
2034 * @see Camera.ErrorCallback
2035 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 public static final int CAMERA_ERROR_SERVER_DIED = 100;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002039 * Callback interface for camera error notification.
2040 *
2041 * @see #setErrorCallback(ErrorCallback)
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002042 *
2043 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2044 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002046 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 public interface ErrorCallback
2048 {
2049 /**
2050 * Callback for camera errors.
2051 * @param error error code:
2052 * <ul>
2053 * <li>{@link #CAMERA_ERROR_UNKNOWN}
2054 * <li>{@link #CAMERA_ERROR_SERVER_DIED}
2055 * </ul>
2056 * @param camera the Camera service object
2057 */
2058 void onError(int error, Camera camera);
2059 };
2060
2061 /**
2062 * Registers a callback to be invoked when an error occurs.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002063 * @param cb The callback to run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 */
2065 public final void setErrorCallback(ErrorCallback cb)
2066 {
2067 mErrorCallback = cb;
2068 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002069
Yin-Chia Yeha07cd322018-05-31 12:11:42 -07002070 /**
2071 * Registers a callback to be invoked when an error occurs.
2072 * The detailed error callback may contain error code that
2073 * gives more detailed information about the error.
2074 *
2075 * When a detailed callback is set, the callback set via
2076 * #setErrorCallback(ErrorCallback) will stop receiving
2077 * onError call.
2078 *
2079 * @param cb The callback to run
2080 *
2081 * @hide
2082 */
2083 public final void setDetailedErrorCallback(ErrorCallback cb)
2084 {
2085 mDetailedErrorCallback = cb;
2086 }
2087
Mathew Inwood5132cc12018-08-08 15:50:55 +01002088 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 private native final void native_setParameters(String params);
Mathew Inwood5132cc12018-08-08 15:50:55 +01002090 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 private native final String native_getParameters();
2092
2093 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002094 * Changes the settings for this Camera service.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002095 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 * @param params the Parameters to use for this Camera service
Wu-cheng Li99a3f3e2010-11-19 15:56:16 +08002097 * @throws RuntimeException if any parameter is invalid or not supported.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002098 * @see #getParameters()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 */
2100 public void setParameters(Parameters params) {
Eino-Ville Talvala7005b672013-04-02 15:46:38 -07002101 // If using preview allocations, don't allow preview size changes
2102 if (mUsingPreviewAllocation) {
2103 Size newPreviewSize = params.getPreviewSize();
2104 Size currentPreviewSize = getParameters().getPreviewSize();
2105 if (newPreviewSize.width != currentPreviewSize.width ||
2106 newPreviewSize.height != currentPreviewSize.height) {
2107 throw new IllegalStateException("Cannot change preview size" +
2108 " while a preview allocation is configured.");
2109 }
2110 }
2111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 native_setParameters(params.flatten());
2113 }
2114
2115 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002116 * Returns the current settings for this Camera service.
2117 * If modifications are made to the returned Parameters, they must be passed
2118 * to {@link #setParameters(Camera.Parameters)} to take effect.
2119 *
Eino-Ville Talvala06a77182017-09-15 12:43:24 -07002120 * @throws RuntimeException if reading parameters fails; usually this would
2121 * be because of a hardware or other low-level error, or because
2122 * release() has been called on this Camera instance.
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002123 * @see #setParameters(Camera.Parameters)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 */
2125 public Parameters getParameters() {
2126 Parameters p = new Parameters();
2127 String s = native_getParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 p.unflatten(s);
2129 return p;
2130 }
2131
2132 /**
Wu-cheng Li1c04a332011-11-22 18:21:18 +08002133 * Returns an empty {@link Parameters} for testing purpose.
2134 *
Ken Wakasaf76a50c2012-03-09 19:56:35 +09002135 * @return a Parameter object.
Wu-cheng Li1c04a332011-11-22 18:21:18 +08002136 *
2137 * @hide
2138 */
Mathew Inwood5132cc12018-08-08 15:50:55 +01002139 @UnsupportedAppUsage
Wu-cheng Li1c04a332011-11-22 18:21:18 +08002140 public static Parameters getEmptyParameters() {
2141 Camera camera = new Camera();
2142 return camera.new Parameters();
2143 }
2144
2145 /**
Igor Murashkindf6242e2014-07-01 18:06:13 -07002146 * Returns a copied {@link Parameters}; for shim use only.
2147 *
2148 * @param parameters a non-{@code null} parameters
2149 * @return a Parameter object, with all the parameters copied from {@code parameters}.
2150 *
2151 * @throws NullPointerException if {@code parameters} was {@code null}
2152 * @hide
2153 */
2154 public static Parameters getParametersCopy(Camera.Parameters parameters) {
2155 if (parameters == null) {
2156 throw new NullPointerException("parameters must not be null");
2157 }
2158
2159 Camera camera = parameters.getOuter();
2160 Parameters p = camera.new Parameters();
2161 p.copyFrom(parameters);
2162
2163 return p;
2164 }
2165
2166 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002167 * Image size (width and height dimensions).
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002168 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2169 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002171 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 public class Size {
2173 /**
2174 * Sets the dimensions for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002175 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 * @param w the photo width (pixels)
2177 * @param h the photo height (pixels)
2178 */
2179 public Size(int w, int h) {
2180 width = w;
2181 height = h;
2182 }
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002183 /**
2184 * Compares {@code obj} to this size.
2185 *
2186 * @param obj the object to compare this size with.
2187 * @return {@code true} if the width and height of {@code obj} is the
2188 * same as those of this size. {@code false} otherwise.
2189 */
2190 @Override
2191 public boolean equals(Object obj) {
2192 if (!(obj instanceof Size)) {
2193 return false;
2194 }
2195 Size s = (Size) obj;
2196 return width == s.width && height == s.height;
2197 }
2198 @Override
2199 public int hashCode() {
2200 return width * 32713 + height;
2201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 /** width of the picture */
2203 public int width;
2204 /** height of the picture */
2205 public int height;
2206 };
2207
2208 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002209 * <p>The Area class is used for choosing specific metering and focus areas for
2210 * the camera to use when calculating auto-exposure, auto-white balance, and
2211 * auto-focus.</p>
2212 *
2213 * <p>To find out how many simultaneous areas a given camera supports, use
2214 * {@link Parameters#getMaxNumMeteringAreas()} and
2215 * {@link Parameters#getMaxNumFocusAreas()}. If metering or focusing area
2216 * selection is unsupported, these methods will return 0.</p>
2217 *
2218 * <p>Each Area consists of a rectangle specifying its bounds, and a weight
2219 * that determines its importance. The bounds are relative to the camera's
2220 * current field of view. The coordinates are mapped so that (-1000, -1000)
2221 * is always the top-left corner of the current field of view, and (1000,
2222 * 1000) is always the bottom-right corner of the current field of
2223 * view. Setting Areas with bounds outside that range is not allowed. Areas
2224 * with zero or negative width or height are not allowed.</p>
2225 *
2226 * <p>The weight must range from 1 to 1000, and represents a weight for
2227 * every pixel in the area. This means that a large metering area with
2228 * the same weight as a smaller area will have more effect in the
2229 * metering result. Metering areas can overlap and the driver
2230 * will add the weights in the overlap region.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08002231 *
Wu-cheng Libde61a52011-06-07 18:23:14 +08002232 * @see Parameters#setFocusAreas(List)
2233 * @see Parameters#getFocusAreas()
2234 * @see Parameters#getMaxNumFocusAreas()
2235 * @see Parameters#setMeteringAreas(List)
2236 * @see Parameters#getMeteringAreas()
2237 * @see Parameters#getMaxNumMeteringAreas()
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002238 *
2239 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2240 * applications.
Wu-cheng Li30771b72011-04-02 06:19:46 +08002241 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002242 @Deprecated
Wu-cheng Li30771b72011-04-02 06:19:46 +08002243 public static class Area {
2244 /**
2245 * Create an area with specified rectangle and weight.
2246 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002247 * @param rect the bounds of the area.
2248 * @param weight the weight of the area.
Wu-cheng Li30771b72011-04-02 06:19:46 +08002249 */
2250 public Area(Rect rect, int weight) {
2251 this.rect = rect;
2252 this.weight = weight;
2253 }
2254 /**
2255 * Compares {@code obj} to this area.
2256 *
2257 * @param obj the object to compare this area with.
2258 * @return {@code true} if the rectangle and weight of {@code obj} is
2259 * the same as those of this area. {@code false} otherwise.
2260 */
2261 @Override
2262 public boolean equals(Object obj) {
2263 if (!(obj instanceof Area)) {
2264 return false;
2265 }
2266 Area a = (Area) obj;
2267 if (rect == null) {
2268 if (a.rect != null) return false;
2269 } else {
2270 if (!rect.equals(a.rect)) return false;
2271 }
2272 return weight == a.weight;
2273 }
2274
Wu-cheng Libde61a52011-06-07 18:23:14 +08002275 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002276 * Bounds of the area. (-1000, -1000) represents the top-left of the
2277 * camera field of view, and (1000, 1000) represents the bottom-right of
2278 * the field of view. Setting bounds outside that range is not
2279 * allowed. Bounds with zero or negative width or height are not
2280 * allowed.
Wu-cheng Libde61a52011-06-07 18:23:14 +08002281 *
2282 * @see Parameters#getFocusAreas()
2283 * @see Parameters#getMeteringAreas()
2284 */
Wu-cheng Li30771b72011-04-02 06:19:46 +08002285 public Rect rect;
2286
Wu-cheng Libde61a52011-06-07 18:23:14 +08002287 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002288 * Weight of the area. The weight must range from 1 to 1000, and
2289 * represents a weight for every pixel in the area. This means that a
2290 * large metering area with the same weight as a smaller area will have
2291 * more effect in the metering result. Metering areas can overlap and
2292 * the driver will add the weights in the overlap region.
Wu-cheng Libde61a52011-06-07 18:23:14 +08002293 *
2294 * @see Parameters#getFocusAreas()
2295 * @see Parameters#getMeteringAreas()
2296 */
Wu-cheng Li30771b72011-04-02 06:19:46 +08002297 public int weight;
Wu-cheng Libde61a52011-06-07 18:23:14 +08002298 }
Wu-cheng Li30771b72011-04-02 06:19:46 +08002299
2300 /**
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002301 * Camera service settings.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002302 *
2303 * <p>To make camera parameters take effect, applications have to call
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002304 * {@link Camera#setParameters(Camera.Parameters)}. For example, after
2305 * {@link Camera.Parameters#setWhiteBalance} is called, white balance is not
2306 * actually changed until {@link Camera#setParameters(Camera.Parameters)}
2307 * is called with the changed parameters object.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002308 *
2309 * <p>Different devices may have different camera capabilities, such as
2310 * picture size or flash modes. The application should query the camera
2311 * capabilities before setting parameters. For example, the application
Dan Egnorbfcbeff2010-07-12 15:12:54 -07002312 * should call {@link Camera.Parameters#getSupportedColorEffects()} before
2313 * calling {@link Camera.Parameters#setColorEffect(String)}. If the
2314 * camera does not support color effects,
2315 * {@link Camera.Parameters#getSupportedColorEffects()} will return null.
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002316 *
2317 * @deprecated We recommend using the new {@link android.hardware.camera2} API for new
2318 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 */
Eino-Ville Talvalab942b052014-07-10 17:45:03 -07002320 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 public class Parameters {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002322 // Parameter keys to communicate with the camera driver.
2323 private static final String KEY_PREVIEW_SIZE = "preview-size";
2324 private static final String KEY_PREVIEW_FORMAT = "preview-format";
2325 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate";
Wu-cheng Li454630f2010-08-11 16:48:05 -07002326 private static final String KEY_PREVIEW_FPS_RANGE = "preview-fps-range";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002327 private static final String KEY_PICTURE_SIZE = "picture-size";
2328 private static final String KEY_PICTURE_FORMAT = "picture-format";
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002329 private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002330 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width";
2331 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height";
2332 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality";
2333 private static final String KEY_JPEG_QUALITY = "jpeg-quality";
2334 private static final String KEY_ROTATION = "rotation";
2335 private static final String KEY_GPS_LATITUDE = "gps-latitude";
2336 private static final String KEY_GPS_LONGITUDE = "gps-longitude";
2337 private static final String KEY_GPS_ALTITUDE = "gps-altitude";
2338 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp";
Ray Chen055c9862010-02-23 10:45:42 +08002339 private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002340 private static final String KEY_WHITE_BALANCE = "whitebalance";
2341 private static final String KEY_EFFECT = "effect";
2342 private static final String KEY_ANTIBANDING = "antibanding";
2343 private static final String KEY_SCENE_MODE = "scene-mode";
2344 private static final String KEY_FLASH_MODE = "flash-mode";
Wu-cheng Li36322db2009-09-18 18:59:21 +08002345 private static final String KEY_FOCUS_MODE = "focus-mode";
Wu-cheng Li30771b72011-04-02 06:19:46 +08002346 private static final String KEY_FOCUS_AREAS = "focus-areas";
2347 private static final String KEY_MAX_NUM_FOCUS_AREAS = "max-num-focus-areas";
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08002348 private static final String KEY_FOCAL_LENGTH = "focal-length";
2349 private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle";
2350 private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle";
Wu-cheng Liff723b62010-02-09 13:38:19 +08002351 private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation";
Wu-cheng Li24b326a2010-02-20 17:47:04 +08002352 private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation";
2353 private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation";
2354 private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step";
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002355 private static final String KEY_AUTO_EXPOSURE_LOCK = "auto-exposure-lock";
2356 private static final String KEY_AUTO_EXPOSURE_LOCK_SUPPORTED = "auto-exposure-lock-supported";
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07002357 private static final String KEY_AUTO_WHITEBALANCE_LOCK = "auto-whitebalance-lock";
2358 private static final String KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED = "auto-whitebalance-lock-supported";
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002359 private static final String KEY_METERING_AREAS = "metering-areas";
2360 private static final String KEY_MAX_NUM_METERING_AREAS = "max-num-metering-areas";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002361 private static final String KEY_ZOOM = "zoom";
2362 private static final String KEY_MAX_ZOOM = "max-zoom";
2363 private static final String KEY_ZOOM_RATIOS = "zoom-ratios";
2364 private static final String KEY_ZOOM_SUPPORTED = "zoom-supported";
2365 private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002366 private static final String KEY_FOCUS_DISTANCES = "focus-distances";
James Dongdd0b16c2010-09-21 16:23:48 -07002367 private static final String KEY_VIDEO_SIZE = "video-size";
2368 private static final String KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO =
2369 "preferred-preview-size-for-video";
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08002370 private static final String KEY_MAX_NUM_DETECTED_FACES_HW = "max-num-detected-faces-hw";
2371 private static final String KEY_MAX_NUM_DETECTED_FACES_SW = "max-num-detected-faces-sw";
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08002372 private static final String KEY_RECORDING_HINT = "recording-hint";
Wu-cheng Li98bb2512011-08-30 21:33:10 +08002373 private static final String KEY_VIDEO_SNAPSHOT_SUPPORTED = "video-snapshot-supported";
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07002374 private static final String KEY_VIDEO_STABILIZATION = "video-stabilization";
2375 private static final String KEY_VIDEO_STABILIZATION_SUPPORTED = "video-stabilization-supported";
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002376
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002377 // Parameter key suffix for supported values.
2378 private static final String SUPPORTED_VALUES_SUFFIX = "-values";
2379
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002380 private static final String TRUE = "true";
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07002381 private static final String FALSE = "false";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08002382
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002383 // Values for white balance settings.
2384 public static final String WHITE_BALANCE_AUTO = "auto";
2385 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent";
2386 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent";
2387 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent";
2388 public static final String WHITE_BALANCE_DAYLIGHT = "daylight";
2389 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight";
2390 public static final String WHITE_BALANCE_TWILIGHT = "twilight";
2391 public static final String WHITE_BALANCE_SHADE = "shade";
2392
2393 // Values for color effect settings.
2394 public static final String EFFECT_NONE = "none";
2395 public static final String EFFECT_MONO = "mono";
2396 public static final String EFFECT_NEGATIVE = "negative";
2397 public static final String EFFECT_SOLARIZE = "solarize";
2398 public static final String EFFECT_SEPIA = "sepia";
2399 public static final String EFFECT_POSTERIZE = "posterize";
2400 public static final String EFFECT_WHITEBOARD = "whiteboard";
2401 public static final String EFFECT_BLACKBOARD = "blackboard";
2402 public static final String EFFECT_AQUA = "aqua";
2403
2404 // Values for antibanding settings.
2405 public static final String ANTIBANDING_AUTO = "auto";
2406 public static final String ANTIBANDING_50HZ = "50hz";
2407 public static final String ANTIBANDING_60HZ = "60hz";
2408 public static final String ANTIBANDING_OFF = "off";
2409
2410 // Values for flash mode settings.
2411 /**
2412 * Flash will not be fired.
2413 */
2414 public static final String FLASH_MODE_OFF = "off";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002415
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002416 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07002417 * Flash will be fired automatically when required. The flash may be fired
2418 * during preview, auto-focus, or snapshot depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002419 */
2420 public static final String FLASH_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002421
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002422 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07002423 * Flash will always be fired during snapshot. The flash may also be
2424 * fired during preview or auto-focus depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002425 */
2426 public static final String FLASH_MODE_ON = "on";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002427
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002428 /**
2429 * Flash will be fired in red-eye reduction mode.
2430 */
2431 public static final String FLASH_MODE_RED_EYE = "red-eye";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002432
Wu-cheng Li36322db2009-09-18 18:59:21 +08002433 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -07002434 * Constant emission of light during preview, auto-focus and snapshot.
2435 * This can also be used for video recording.
Wu-cheng Li36322db2009-09-18 18:59:21 +08002436 */
Wu-cheng Li068ef422009-09-27 13:19:36 -07002437 public static final String FLASH_MODE_TORCH = "torch";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002438
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002439 /**
2440 * Scene mode is off.
2441 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002442 public static final String SCENE_MODE_AUTO = "auto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002443
2444 /**
2445 * Take photos of fast moving objects. Same as {@link
2446 * #SCENE_MODE_SPORTS}.
2447 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002448 public static final String SCENE_MODE_ACTION = "action";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002449
2450 /**
2451 * Take people pictures.
2452 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002453 public static final String SCENE_MODE_PORTRAIT = "portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002454
2455 /**
2456 * Take pictures on distant objects.
2457 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002458 public static final String SCENE_MODE_LANDSCAPE = "landscape";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002459
2460 /**
2461 * Take photos at night.
2462 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002463 public static final String SCENE_MODE_NIGHT = "night";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002464
2465 /**
2466 * Take people pictures at night.
2467 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002468 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002469
2470 /**
2471 * Take photos in a theater. Flash light is off.
2472 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002473 public static final String SCENE_MODE_THEATRE = "theatre";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002474
2475 /**
2476 * Take pictures on the beach.
2477 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002478 public static final String SCENE_MODE_BEACH = "beach";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002479
2480 /**
2481 * Take pictures on the snow.
2482 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002483 public static final String SCENE_MODE_SNOW = "snow";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002484
2485 /**
2486 * Take sunset photos.
2487 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002488 public static final String SCENE_MODE_SUNSET = "sunset";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002489
2490 /**
2491 * Avoid blurry pictures (for example, due to hand shake).
2492 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002493 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002494
2495 /**
2496 * For shooting firework displays.
2497 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002498 public static final String SCENE_MODE_FIREWORKS = "fireworks";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002499
2500 /**
2501 * Take photos of fast moving objects. Same as {@link
2502 * #SCENE_MODE_ACTION}.
2503 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002504 public static final String SCENE_MODE_SPORTS = "sports";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002505
2506 /**
2507 * Take indoor low-light shot.
2508 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002509 public static final String SCENE_MODE_PARTY = "party";
Wu-cheng Li00e21f82010-05-28 16:43:38 +08002510
2511 /**
2512 * Capture the naturally warm color of scenes lit by candles.
2513 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002514 public static final String SCENE_MODE_CANDLELIGHT = "candlelight";
2515
Wu-cheng Lic58b4232010-03-29 17:21:28 +08002516 /**
2517 * Applications are looking for a barcode. Camera driver will be
2518 * optimized for barcode reading.
2519 */
2520 public static final String SCENE_MODE_BARCODE = "barcode";
2521
Wu-cheng Li36322db2009-09-18 18:59:21 +08002522 /**
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07002523 * Capture a scene using high dynamic range imaging techniques. The
2524 * camera will return an image that has an extended dynamic range
2525 * compared to a regular capture. Capturing such an image may take
2526 * longer than a regular capture.
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07002527 */
2528 public static final String SCENE_MODE_HDR = "hdr";
2529
2530 /**
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07002531 * Auto-focus mode. Applications should call {@link
2532 * #autoFocus(AutoFocusCallback)} to start the focus in this mode.
Wu-cheng Li36322db2009-09-18 18:59:21 +08002533 */
2534 public static final String FOCUS_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002535
Wu-cheng Li36322db2009-09-18 18:59:21 +08002536 /**
2537 * Focus is set at infinity. Applications should not call
2538 * {@link #autoFocus(AutoFocusCallback)} in this mode.
2539 */
2540 public static final String FOCUS_MODE_INFINITY = "infinity";
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07002541
2542 /**
2543 * Macro (close-up) focus mode. Applications should call
2544 * {@link #autoFocus(AutoFocusCallback)} to start the focus in this
2545 * mode.
2546 */
Wu-cheng Li36322db2009-09-18 18:59:21 +08002547 public static final String FOCUS_MODE_MACRO = "macro";
Wu-cheng Li36f68b82009-09-28 16:14:58 -07002548
Wu-cheng Li36322db2009-09-18 18:59:21 +08002549 /**
2550 * Focus is fixed. The camera is always in this mode if the focus is not
2551 * adjustable. If the camera has auto-focus, this mode can fix the
2552 * focus, which is usually at hyperfocal distance. Applications should
2553 * not call {@link #autoFocus(AutoFocusCallback)} in this mode.
2554 */
2555 public static final String FOCUS_MODE_FIXED = "fixed";
2556
Wu-cheng Lic58b4232010-03-29 17:21:28 +08002557 /**
2558 * Extended depth of field (EDOF). Focusing is done digitally and
2559 * continuously. Applications should not call {@link
2560 * #autoFocus(AutoFocusCallback)} in this mode.
2561 */
2562 public static final String FOCUS_MODE_EDOF = "edof";
2563
Wu-cheng Li699fe932010-08-05 11:50:25 -07002564 /**
Wu-cheng Lid45cb722010-09-20 16:15:32 -07002565 * Continuous auto focus mode intended for video recording. The camera
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002566 * continuously tries to focus. This is the best choice for video
2567 * recording because the focus changes smoothly . Applications still can
2568 * call {@link #takePicture(Camera.ShutterCallback,
2569 * Camera.PictureCallback, Camera.PictureCallback)} in this mode but the
2570 * subject may not be in focus. Auto focus starts when the parameter is
Wu-cheng Li53b30912011-10-12 19:43:51 +08002571 * set.
2572 *
2573 * <p>Since API level 14, applications can call {@link
2574 * #autoFocus(AutoFocusCallback)} in this mode. The focus callback will
2575 * immediately return with a boolean that indicates whether the focus is
2576 * sharp or not. The focus position is locked after autoFocus call. If
2577 * applications want to resume the continuous focus, cancelAutoFocus
2578 * must be called. Restarting the preview will not resume the continuous
2579 * autofocus. To stop continuous focus, applications should change the
2580 * focus mode to other modes.
2581 *
2582 * @see #FOCUS_MODE_CONTINUOUS_PICTURE
Wu-cheng Li699fe932010-08-05 11:50:25 -07002583 */
Wu-cheng Lid45cb722010-09-20 16:15:32 -07002584 public static final String FOCUS_MODE_CONTINUOUS_VIDEO = "continuous-video";
Wu-cheng Li699fe932010-08-05 11:50:25 -07002585
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002586 /**
2587 * Continuous auto focus mode intended for taking pictures. The camera
2588 * continuously tries to focus. The speed of focus change is more
2589 * aggressive than {@link #FOCUS_MODE_CONTINUOUS_VIDEO}. Auto focus
Wu-cheng Li53b30912011-10-12 19:43:51 +08002590 * starts when the parameter is set.
2591 *
Wu-cheng Li0f4f97b2011-10-27 18:07:01 +08002592 * <p>Applications can call {@link #autoFocus(AutoFocusCallback)} in
2593 * this mode. If the autofocus is in the middle of scanning, the focus
2594 * callback will return when it completes. If the autofocus is not
2595 * scanning, the focus callback will immediately return with a boolean
2596 * that indicates whether the focus is sharp or not. The apps can then
2597 * decide if they want to take a picture immediately or to change the
2598 * focus mode to auto, and run a full autofocus cycle. The focus
2599 * position is locked after autoFocus call. If applications want to
2600 * resume the continuous focus, cancelAutoFocus must be called.
2601 * Restarting the preview will not resume the continuous autofocus. To
2602 * stop continuous focus, applications should change the focus mode to
2603 * other modes.
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002604 *
2605 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Lib9ac75d2011-08-16 21:14:16 +08002606 */
2607 public static final String FOCUS_MODE_CONTINUOUS_PICTURE = "continuous-picture";
2608
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08002609 // Indices for focus distance array.
2610 /**
2611 * The array index of near focus distance for use with
2612 * {@link #getFocusDistances(float[])}.
2613 */
2614 public static final int FOCUS_DISTANCE_NEAR_INDEX = 0;
2615
2616 /**
2617 * The array index of optimal focus distance for use with
2618 * {@link #getFocusDistances(float[])}.
2619 */
2620 public static final int FOCUS_DISTANCE_OPTIMAL_INDEX = 1;
2621
2622 /**
2623 * The array index of far focus distance for use with
2624 * {@link #getFocusDistances(float[])}.
2625 */
2626 public static final int FOCUS_DISTANCE_FAR_INDEX = 2;
2627
Wu-cheng Lica099612010-05-06 16:47:30 +08002628 /**
Wu-cheng Li454630f2010-08-11 16:48:05 -07002629 * The array index of minimum preview fps for use with {@link
2630 * #getPreviewFpsRange(int[])} or {@link
2631 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07002632 */
2633 public static final int PREVIEW_FPS_MIN_INDEX = 0;
2634
2635 /**
2636 * The array index of maximum preview fps for use with {@link
2637 * #getPreviewFpsRange(int[])} or {@link
2638 * #getSupportedPreviewFpsRange()}.
Wu-cheng Li454630f2010-08-11 16:48:05 -07002639 */
2640 public static final int PREVIEW_FPS_MAX_INDEX = 1;
2641
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002642 // Formats for setPreviewFormat and setPictureFormat.
2643 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp";
2644 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp";
Chih-Chung Changeb68c462009-09-18 18:37:44 +08002645 private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv";
Wu-cheng Li10a1b302011-02-22 15:49:25 +08002646 private static final String PIXEL_FORMAT_YUV420P = "yuv420p";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002647 private static final String PIXEL_FORMAT_RGB565 = "rgb565";
2648 private static final String PIXEL_FORMAT_JPEG = "jpeg";
Wu-cheng Li70fb9082011-08-02 17:49:50 +08002649 private static final String PIXEL_FORMAT_BAYER_RGGB = "bayer-rggb";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002650
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002651 /**
2652 * Order matters: Keys that are {@link #set(String, String) set} later
2653 * will take precedence over keys that are set earlier (if the two keys
2654 * conflict with each other).
2655 *
2656 * <p>One example is {@link #setPreviewFpsRange(int, int)} , since it
2657 * conflicts with {@link #setPreviewFrameRate(int)} whichever key is set later
2658 * is the one that will take precedence.
2659 * </p>
2660 */
2661 private final LinkedHashMap<String, String> mMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662
2663 private Parameters() {
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002664 mMap = new LinkedHashMap<String, String>(/*initialCapacity*/64);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 }
2666
2667 /**
Igor Murashkindf6242e2014-07-01 18:06:13 -07002668 * Overwrite existing parameters with a copy of the ones from {@code other}.
2669 *
2670 * <b>For use by the legacy shim only.</b>
2671 *
2672 * @hide
2673 */
Mathew Inwood5132cc12018-08-08 15:50:55 +01002674 @UnsupportedAppUsage
Igor Murashkindf6242e2014-07-01 18:06:13 -07002675 public void copyFrom(Parameters other) {
2676 if (other == null) {
2677 throw new NullPointerException("other must not be null");
2678 }
2679
2680 mMap.putAll(other.mMap);
2681 }
2682
2683 private Camera getOuter() {
2684 return Camera.this;
2685 }
2686
Ruben Brunkd1f113d2014-07-11 11:46:20 -07002687
2688 /**
2689 * Value equality check.
2690 *
2691 * @hide
2692 */
2693 public boolean same(Parameters other) {
2694 if (this == other) {
2695 return true;
2696 }
2697 return other != null && Parameters.this.mMap.equals(other.mMap);
2698 }
2699
Igor Murashkindf6242e2014-07-01 18:06:13 -07002700 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 * Writes the current Parameters to the log.
2702 * @hide
2703 * @deprecated
2704 */
Igor Murashkina1d66272014-06-20 11:22:11 -07002705 @Deprecated
Mathew Inwood5132cc12018-08-08 15:50:55 +01002706 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 public void dump() {
2708 Log.e(TAG, "dump: size=" + mMap.size());
2709 for (String k : mMap.keySet()) {
2710 Log.e(TAG, "dump: " + k + "=" + mMap.get(k));
2711 }
2712 }
2713
2714 /**
2715 * Creates a single string with all the parameters set in
2716 * this Parameters object.
2717 * <p>The {@link #unflatten(String)} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002718 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 * @return a String with all values from this Parameters object, in
2720 * semi-colon delimited key-value pairs
2721 */
2722 public String flatten() {
Ali Utku Selen0a120182011-02-09 14:11:22 +01002723 StringBuilder flattened = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 for (String k : mMap.keySet()) {
2725 flattened.append(k);
2726 flattened.append("=");
2727 flattened.append(mMap.get(k));
2728 flattened.append(";");
2729 }
2730 // chop off the extra semicolon at the end
2731 flattened.deleteCharAt(flattened.length()-1);
2732 return flattened.toString();
2733 }
2734
2735 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002736 * Takes a flattened string of parameters and adds each one to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 * this Parameters object.
2738 * <p>The {@link #flatten()} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002739 *
2740 * @param flattened a String of parameters (key-value paired) that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 * are semi-colon delimited
2742 */
2743 public void unflatten(String flattened) {
2744 mMap.clear();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002745
Ali Utku Selen0a120182011-02-09 14:11:22 +01002746 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(';');
2747 splitter.setString(flattened);
2748 for (String kv : splitter) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 int pos = kv.indexOf('=');
2750 if (pos == -1) {
2751 continue;
2752 }
2753 String k = kv.substring(0, pos);
2754 String v = kv.substring(pos + 1);
2755 mMap.put(k, v);
2756 }
2757 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 public void remove(String key) {
2760 mMap.remove(key);
2761 }
2762
2763 /**
2764 * Sets a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002765 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 * @param key the key name for the parameter
2767 * @param value the String value of the parameter
2768 */
2769 public void set(String key, String value) {
Eino-Ville Talvalacb569232012-03-12 11:36:58 -07002770 if (key.indexOf('=') != -1 || key.indexOf(';') != -1 || key.indexOf(0) != -1) {
2771 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ; or \\0)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 return;
2773 }
Eino-Ville Talvalacb569232012-03-12 11:36:58 -07002774 if (value.indexOf('=') != -1 || value.indexOf(';') != -1 || value.indexOf(0) != -1) {
2775 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ; or \\0)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 return;
2777 }
2778
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002779 put(key, value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
2781
2782 /**
2783 * Sets an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002784 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 * @param key the key name for the parameter
2786 * @param value the int value of the parameter
2787 */
2788 public void set(String key, int value) {
Igor Murashkin7d9a8ff2014-03-18 18:14:41 -07002789 put(key, Integer.toString(value));
2790 }
2791
2792 private void put(String key, String value) {
2793 /*
2794 * Remove the key if it already exists.
2795 *
2796 * This way setting a new value for an already existing key will always move
2797 * that key to be ordered the latest in the map.
2798 */
2799 mMap.remove(key);
2800 mMap.put(key, value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 }
2802
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002803 private void set(String key, List<Area> areas) {
Wu-cheng Lif715bf92011-04-14 14:04:18 +08002804 if (areas == null) {
2805 set(key, "(0,0,0,0,0)");
2806 } else {
2807 StringBuilder buffer = new StringBuilder();
2808 for (int i = 0; i < areas.size(); i++) {
2809 Area area = areas.get(i);
2810 Rect rect = area.rect;
2811 buffer.append('(');
2812 buffer.append(rect.left);
2813 buffer.append(',');
2814 buffer.append(rect.top);
2815 buffer.append(',');
2816 buffer.append(rect.right);
2817 buffer.append(',');
2818 buffer.append(rect.bottom);
2819 buffer.append(',');
2820 buffer.append(area.weight);
2821 buffer.append(')');
2822 if (i != areas.size() - 1) buffer.append(',');
2823 }
2824 set(key, buffer.toString());
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002825 }
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08002826 }
2827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 /**
2829 * Returns the value of a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002830 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 * @param key the key name for the parameter
2832 * @return the String value of the parameter
2833 */
2834 public String get(String key) {
2835 return mMap.get(key);
2836 }
2837
2838 /**
2839 * Returns the value of an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002840 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 * @param key the key name for the parameter
2842 * @return the int value of the parameter
2843 */
2844 public int getInt(String key) {
2845 return Integer.parseInt(mMap.get(key));
2846 }
2847
2848 /**
Wu-cheng Li26274fa2011-05-05 14:36:28 +08002849 * Sets the dimensions for preview pictures. If the preview has already
2850 * started, applications should stop the preview first before changing
2851 * preview size.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002852 *
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002853 * The sides of width and height are based on camera orientation. That
2854 * is, the preview size is the size before it is rotated by display
2855 * orientation. So applications need to consider the display orientation
2856 * while setting preview size. For example, suppose the camera supports
2857 * both 480x320 and 320x480 preview sizes. The application wants a 3:2
2858 * preview ratio. If the display orientation is set to 0 or 180, preview
2859 * size should be set to 480x320. If the display orientation is set to
2860 * 90 or 270, preview size should be set to 320x480. The display
2861 * orientation should also be considered while setting picture size and
2862 * thumbnail size.
2863 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 * @param width the width of the pictures, in pixels
2865 * @param height the height of the pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002866 * @see #setDisplayOrientation(int)
2867 * @see #getCameraInfo(int, CameraInfo)
2868 * @see #setPictureSize(int, int)
2869 * @see #setJpegThumbnailSize(int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 */
2871 public void setPreviewSize(int width, int height) {
2872 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002873 set(KEY_PREVIEW_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 }
2875
2876 /**
2877 * Returns the dimensions setting for preview pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002878 *
James Dongdd0b16c2010-09-21 16:23:48 -07002879 * @return a Size object with the width and height setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 * for the preview picture
2881 */
2882 public Size getPreviewSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002883 String pair = get(KEY_PREVIEW_SIZE);
2884 return strToSize(pair);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 }
2886
2887 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002888 * Gets the supported preview sizes.
2889 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002890 * @return a list of Size object. This method will always return a list
Wu-cheng Li9c799382009-12-04 19:59:18 +08002891 * with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002892 */
2893 public List<Size> getSupportedPreviewSizes() {
2894 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX);
2895 return splitSize(str);
2896 }
2897
2898 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002899 * <p>Gets the supported video frame sizes that can be used by
2900 * MediaRecorder.</p>
James Dongdd0b16c2010-09-21 16:23:48 -07002901 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002902 * <p>If the returned list is not null, the returned list will contain at
James Dongdd0b16c2010-09-21 16:23:48 -07002903 * least one Size and one of the sizes in the returned list must be
2904 * passed to MediaRecorder.setVideoSize() for camcorder application if
2905 * camera is used as the video source. In this case, the size of the
2906 * preview can be different from the resolution of the recorded video
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002907 * during video recording.</p>
James Dongdd0b16c2010-09-21 16:23:48 -07002908 *
2909 * @return a list of Size object if camera has separate preview and
2910 * video output; otherwise, null is returned.
2911 * @see #getPreferredPreviewSizeForVideo()
2912 */
2913 public List<Size> getSupportedVideoSizes() {
2914 String str = get(KEY_VIDEO_SIZE + SUPPORTED_VALUES_SUFFIX);
2915 return splitSize(str);
2916 }
2917
2918 /**
2919 * Returns the preferred or recommended preview size (width and height)
2920 * in pixels for video recording. Camcorder applications should
2921 * set the preview size to a value that is not larger than the
2922 * preferred preview size. In other words, the product of the width
2923 * and height of the preview size should not be larger than that of
2924 * the preferred preview size. In addition, we recommend to choose a
2925 * preview size that has the same aspect ratio as the resolution of
2926 * video to be recorded.
2927 *
2928 * @return the preferred preview size (width and height) in pixels for
2929 * video recording if getSupportedVideoSizes() does not return
2930 * null; otherwise, null is returned.
2931 * @see #getSupportedVideoSizes()
2932 */
2933 public Size getPreferredPreviewSizeForVideo() {
2934 String pair = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
2935 return strToSize(pair);
2936 }
2937
2938 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002939 * <p>Sets the dimensions for EXIF thumbnail in Jpeg picture. If
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002940 * applications set both width and height to 0, EXIF will not contain
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002941 * thumbnail.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002942 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07002943 * <p>Applications need to consider the display orientation. See {@link
2944 * #setPreviewSize(int,int)} for reference.</p>
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002945 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 * @param width the width of the thumbnail, in pixels
2947 * @param height the height of the thumbnail, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08002948 * @see #setPreviewSize(int,int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002950 public void setJpegThumbnailSize(int width, int height) {
2951 set(KEY_JPEG_THUMBNAIL_WIDTH, width);
2952 set(KEY_JPEG_THUMBNAIL_HEIGHT, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
2954
2955 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002956 * Returns the dimensions for EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002958 * @return a Size object with the height and width setting for the EXIF
2959 * thumbnails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002961 public Size getJpegThumbnailSize() {
2962 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH),
2963 getInt(KEY_JPEG_THUMBNAIL_HEIGHT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 }
2965
2966 /**
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002967 * Gets the supported jpeg thumbnail sizes.
2968 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08002969 * @return a list of Size object. This method will always return a list
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08002970 * with at least two elements. Size 0,0 (no thumbnail) is always
2971 * supported.
2972 */
2973 public List<Size> getSupportedJpegThumbnailSizes() {
2974 String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX);
2975 return splitSize(str);
2976 }
2977
2978 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002979 * Sets the quality of the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002981 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1
2982 * to 100, with 100 being the best.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002984 public void setJpegThumbnailQuality(int quality) {
2985 set(KEY_JPEG_THUMBNAIL_QUALITY, quality);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 }
2987
2988 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002989 * Returns the quality setting for the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002991 * @return the JPEG quality setting of the EXIF thumbnail.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08002993 public int getJpegThumbnailQuality() {
2994 return getInt(KEY_JPEG_THUMBNAIL_QUALITY);
2995 }
2996
2997 /**
2998 * Sets Jpeg quality of captured picture.
2999 *
3000 * @param quality the JPEG quality of captured picture. The range is 1
3001 * to 100, with 100 being the best.
3002 */
3003 public void setJpegQuality(int quality) {
3004 set(KEY_JPEG_QUALITY, quality);
3005 }
3006
3007 /**
3008 * Returns the quality setting for the JPEG picture.
3009 *
3010 * @return the JPEG picture quality setting.
3011 */
3012 public int getJpegQuality() {
3013 return getInt(KEY_JPEG_QUALITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 }
3015
3016 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08003017 * Sets the rate at which preview frames are received. This is the
3018 * target frame rate. The actual frame rate depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003019 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 * @param fps the frame rate (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003021 * @deprecated replaced by {@link #setPreviewFpsRange(int,int)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003023 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 public void setPreviewFrameRate(int fps) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003025 set(KEY_PREVIEW_FRAME_RATE, fps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 }
3027
3028 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08003029 * Returns the setting for the rate at which preview frames are
3030 * received. This is the target frame rate. The actual frame rate
3031 * depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003032 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 * @return the frame rate setting (frames per second)
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003034 * @deprecated replaced by {@link #getPreviewFpsRange(int[])}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003036 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 public int getPreviewFrameRate() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003038 return getInt(KEY_PREVIEW_FRAME_RATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 }
3040
3041 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003042 * Gets the supported preview frame rates.
3043 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003044 * @return a list of supported preview frame rates. null if preview
3045 * frame rate setting is not supported.
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003046 * @deprecated replaced by {@link #getSupportedPreviewFpsRange()}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003047 */
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003048 @Deprecated
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003049 public List<Integer> getSupportedPreviewFrameRates() {
3050 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX);
3051 return splitInt(str);
3052 }
3053
3054 /**
Hai Guo4a68e3c2012-12-05 21:50:28 +08003055 * Sets the minimum and maximum preview fps. This controls the rate of
Wu-cheng Li1620d112010-08-27 15:09:20 -07003056 * preview frames received in {@link PreviewCallback}. The minimum and
Wu-cheng Li454630f2010-08-11 16:48:05 -07003057 * maximum preview fps must be one of the elements from {@link
3058 * #getSupportedPreviewFpsRange}.
3059 *
3060 * @param min the minimum preview fps (scaled by 1000).
3061 * @param max the maximum preview fps (scaled by 1000).
3062 * @throws RuntimeException if fps range is invalid.
3063 * @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
3064 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07003065 */
3066 public void setPreviewFpsRange(int min, int max) {
3067 set(KEY_PREVIEW_FPS_RANGE, "" + min + "," + max);
3068 }
3069
3070 /**
3071 * Returns the current minimum and maximum preview fps. The values are
3072 * one of the elements returned by {@link #getSupportedPreviewFpsRange}.
3073 *
3074 * @return range the minimum and maximum preview fps (scaled by 1000).
3075 * @see #PREVIEW_FPS_MIN_INDEX
3076 * @see #PREVIEW_FPS_MAX_INDEX
3077 * @see #getSupportedPreviewFpsRange()
Wu-cheng Li454630f2010-08-11 16:48:05 -07003078 */
3079 public void getPreviewFpsRange(int[] range) {
3080 if (range == null || range.length != 2) {
3081 throw new IllegalArgumentException(
Wu-cheng Li5f1e69c2010-08-18 11:39:12 -07003082 "range must be an array with two elements.");
Wu-cheng Li454630f2010-08-11 16:48:05 -07003083 }
3084 splitInt(get(KEY_PREVIEW_FPS_RANGE), range);
3085 }
3086
3087 /**
3088 * Gets the supported preview fps (frame-per-second) ranges. Each range
3089 * contains a minimum fps and maximum fps. If minimum fps equals to
3090 * maximum fps, the camera outputs frames in fixed frame rate. If not,
3091 * the camera outputs frames in auto frame rate. The actual frame rate
3092 * fluctuates between the minimum and the maximum. The values are
3093 * multiplied by 1000 and represented in integers. For example, if frame
3094 * rate is 26.623 frames per second, the value is 26623.
3095 *
3096 * @return a list of supported preview fps ranges. This method returns a
3097 * list with at least one element. Every element is an int array
3098 * of two values - minimum fps and maximum fps. The list is
3099 * sorted from small to large (first by maximum fps and then
3100 * minimum fps).
3101 * @see #PREVIEW_FPS_MIN_INDEX
3102 * @see #PREVIEW_FPS_MAX_INDEX
Wu-cheng Li454630f2010-08-11 16:48:05 -07003103 */
3104 public List<int[]> getSupportedPreviewFpsRange() {
3105 String str = get(KEY_PREVIEW_FPS_RANGE + SUPPORTED_VALUES_SUFFIX);
3106 return splitRange(str);
3107 }
3108
3109 /**
Wu-cheng Li7478ea62009-09-16 18:52:55 +08003110 * Sets the image format for preview pictures.
Scott Mainda0a56d2009-09-10 18:08:37 -07003111 * <p>If this is never called, the default format will be
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003112 * {@link android.graphics.ImageFormat#NV21}, which
Scott Maindf4578e2009-09-10 12:22:07 -07003113 * uses the NV21 encoding format.</p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +08003114 *
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003115 * <p>Use {@link Parameters#getSupportedPreviewFormats} to get a list of
3116 * the available preview formats.
3117 *
3118 * <p>It is strongly recommended that either
3119 * {@link android.graphics.ImageFormat#NV21} or
3120 * {@link android.graphics.ImageFormat#YV12} is used, since
3121 * they are supported by all camera devices.</p>
3122 *
3123 * <p>For YV12, the image buffer that is received is not necessarily
3124 * tightly packed, as there may be padding at the end of each row of
3125 * pixel data, as described in
3126 * {@link android.graphics.ImageFormat#YV12}. For camera callback data,
3127 * it can be assumed that the stride of the Y and UV data is the
3128 * smallest possible that meets the alignment requirements. That is, if
3129 * the preview size is <var>width x height</var>, then the following
3130 * equations describe the buffer index for the beginning of row
3131 * <var>y</var> for the Y plane and row <var>c</var> for the U and V
3132 * planes:
3133 *
Neil Fuller71fbb812015-11-30 09:51:33 +00003134 * <pre>{@code
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003135 * yStride = (int) ceil(width / 16.0) * 16;
3136 * uvStride = (int) ceil( (yStride / 2) / 16.0) * 16;
3137 * ySize = yStride * height;
3138 * uvSize = uvStride * height / 2;
3139 * yRowIndex = yStride * y;
3140 * uRowIndex = ySize + uvSize + uvStride * c;
3141 * vRowIndex = ySize + uvStride * c;
Neil Fuller71fbb812015-11-30 09:51:33 +00003142 * size = ySize + uvSize * 2;
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003143 * }
Neil Fuller71fbb812015-11-30 09:51:33 +00003144 *</pre>
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003145 *
3146 * @param pixel_format the desired preview picture format, defined by
3147 * one of the {@link android.graphics.ImageFormat} constants. (E.g.,
3148 * <var>ImageFormat.NV21</var> (default), or
3149 * <var>ImageFormat.YV12</var>)
3150 *
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003151 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003152 * @see android.hardware.Camera.Parameters#getSupportedPreviewFormats
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 */
3154 public void setPreviewFormat(int pixel_format) {
3155 String s = cameraFormatForPixelFormat(pixel_format);
3156 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003157 throw new IllegalArgumentException(
3158 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 }
3160
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003161 set(KEY_PREVIEW_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 }
3163
3164 /**
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003165 * Returns the image format for preview frames got from
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003166 * {@link PreviewCallback}.
Wu-cheng Li7478ea62009-09-16 18:52:55 +08003167 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003168 * @return the preview format.
3169 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003170 * @see #setPreviewFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 */
3172 public int getPreviewFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003173 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT));
3174 }
3175
3176 /**
Wu-cheng Lif9293e72011-02-25 13:35:10 +08003177 * Gets the supported preview formats. {@link android.graphics.ImageFormat#NV21}
3178 * is always supported. {@link android.graphics.ImageFormat#YV12}
3179 * is always supported since API level 12.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003180 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003181 * @return a list of supported preview formats. This method will always
3182 * return a list with at least one element.
3183 * @see android.graphics.ImageFormat
Eino-Ville Talvala95151632012-05-02 16:21:18 -07003184 * @see #setPreviewFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003185 */
3186 public List<Integer> getSupportedPreviewFormats() {
3187 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX);
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003188 ArrayList<Integer> formats = new ArrayList<Integer>();
3189 for (String s : split(str)) {
3190 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003191 if (f == ImageFormat.UNKNOWN) continue;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003192 formats.add(f);
3193 }
3194 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 }
3196
3197 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003198 * <p>Sets the dimensions for pictures.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003199 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07003200 * <p>Applications need to consider the display orientation. See {@link
3201 * #setPreviewSize(int,int)} for reference.</p>
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08003202 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 * @param width the width for pictures, in pixels
3204 * @param height the height for pictures, in pixels
Wu-cheng Lic157e0c2010-10-07 18:36:07 +08003205 * @see #setPreviewSize(int,int)
3206 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 */
3208 public void setPictureSize(int width, int height) {
3209 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003210 set(KEY_PICTURE_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 }
3212
3213 /**
3214 * Returns the dimension setting for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003215 *
3216 * @return a Size object with the height and width setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 * for pictures
3218 */
3219 public Size getPictureSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003220 String pair = get(KEY_PICTURE_SIZE);
3221 return strToSize(pair);
3222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003224 /**
3225 * Gets the supported picture sizes.
3226 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003227 * @return a list of supported picture sizes. This method will always
3228 * return a list with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003229 */
3230 public List<Size> getSupportedPictureSizes() {
3231 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX);
3232 return splitSize(str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 }
3234
3235 /**
3236 * Sets the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003237 *
3238 * @param pixel_format the desired picture format
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003239 * (<var>ImageFormat.NV21</var>,
3240 * <var>ImageFormat.RGB_565</var>, or
3241 * <var>ImageFormat.JPEG</var>)
3242 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 */
3244 public void setPictureFormat(int pixel_format) {
3245 String s = cameraFormatForPixelFormat(pixel_format);
3246 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003247 throw new IllegalArgumentException(
3248 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
3250
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003251 set(KEY_PICTURE_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 }
3253
3254 /**
3255 * Returns the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003256 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003257 * @return the picture format
3258 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 */
3260 public int getPictureFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003261 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT));
3262 }
3263
3264 /**
3265 * Gets the supported picture formats.
3266 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003267 * @return supported picture formats. This method will always return a
3268 * list with at least one element.
3269 * @see android.graphics.ImageFormat
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003270 */
3271 public List<Integer> getSupportedPictureFormats() {
Wu-cheng Li9c799382009-12-04 19:59:18 +08003272 String str = get(KEY_PICTURE_FORMAT + SUPPORTED_VALUES_SUFFIX);
3273 ArrayList<Integer> formats = new ArrayList<Integer>();
3274 for (String s : split(str)) {
3275 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003276 if (f == ImageFormat.UNKNOWN) continue;
Wu-cheng Li9c799382009-12-04 19:59:18 +08003277 formats.add(f);
3278 }
3279 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 }
3281
3282 private String cameraFormatForPixelFormat(int pixel_format) {
3283 switch(pixel_format) {
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003284 case ImageFormat.NV16: return PIXEL_FORMAT_YUV422SP;
3285 case ImageFormat.NV21: return PIXEL_FORMAT_YUV420SP;
3286 case ImageFormat.YUY2: return PIXEL_FORMAT_YUV422I;
Wu-cheng Li10a1b302011-02-22 15:49:25 +08003287 case ImageFormat.YV12: return PIXEL_FORMAT_YUV420P;
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003288 case ImageFormat.RGB_565: return PIXEL_FORMAT_RGB565;
3289 case ImageFormat.JPEG: return PIXEL_FORMAT_JPEG;
3290 default: return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 }
3292 }
3293
3294 private int pixelFormatForCameraFormat(String format) {
3295 if (format == null)
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003296 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003298 if (format.equals(PIXEL_FORMAT_YUV422SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003299 return ImageFormat.NV16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003301 if (format.equals(PIXEL_FORMAT_YUV420SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003302 return ImageFormat.NV21;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003304 if (format.equals(PIXEL_FORMAT_YUV422I))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003305 return ImageFormat.YUY2;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08003306
Wu-cheng Li10a1b302011-02-22 15:49:25 +08003307 if (format.equals(PIXEL_FORMAT_YUV420P))
3308 return ImageFormat.YV12;
3309
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003310 if (format.equals(PIXEL_FORMAT_RGB565))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003311 return ImageFormat.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003313 if (format.equals(PIXEL_FORMAT_JPEG))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003314 return ImageFormat.JPEG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315
Mathias Agopiana696f5d2010-02-17 17:53:09 -08003316 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 }
3318
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003319 /**
Wu-cheng Li8969ea12012-04-20 17:38:09 +08003320 * Sets the clockwise rotation angle in degrees relative to the
3321 * orientation of the camera. This affects the pictures returned from
3322 * JPEG {@link PictureCallback}. The camera driver may set orientation
3323 * in the EXIF header without rotating the picture. Or the driver may
3324 * rotate the picture and the EXIF thumbnail. If the Jpeg picture is
Igor Murashkina1b02db2013-06-11 15:25:20 -07003325 * rotated, the orientation in the EXIF header will be missing or 1 (row
3326 * #0 is top and column #0 is left side).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003327 *
Igor Murashkina1b02db2013-06-11 15:25:20 -07003328 * <p>
3329 * If applications want to rotate the picture to match the orientation
3330 * of what users see, apps should use
3331 * {@link android.view.OrientationEventListener} and
3332 * {@link android.hardware.Camera.CameraInfo}. The value from
3333 * OrientationEventListener is relative to the natural orientation of
3334 * the device. CameraInfo.orientation is the angle between camera
3335 * orientation and natural device orientation. The sum of the two is the
3336 * rotation angle for back-facing camera. The difference of the two is
3337 * the rotation angle for front-facing camera. Note that the JPEG
3338 * pictures of front-facing cameras are not mirrored as in preview
3339 * display.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003340 *
Igor Murashkina1b02db2013-06-11 15:25:20 -07003341 * <p>
3342 * For example, suppose the natural orientation of the device is
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003343 * portrait. The device is rotated 270 degrees clockwise, so the device
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08003344 * orientation is 270. Suppose a back-facing camera sensor is mounted in
3345 * landscape and the top side of the camera sensor is aligned with the
3346 * right edge of the display in natural orientation. So the camera
3347 * orientation is 90. The rotation should be set to 0 (270 + 90).
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003348 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -08003349 * <p>The reference code is as follows.
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003350 *
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -08003351 * <pre>
Scott Main9a10bf02011-08-24 19:09:48 -07003352 * public void onOrientationChanged(int orientation) {
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003353 * if (orientation == ORIENTATION_UNKNOWN) return;
3354 * android.hardware.Camera.CameraInfo info =
3355 * new android.hardware.Camera.CameraInfo();
3356 * android.hardware.Camera.getCameraInfo(cameraId, info);
3357 * orientation = (orientation + 45) / 90 * 90;
Wu-cheng Li2fe6fca2010-10-15 14:42:23 +08003358 * int rotation = 0;
3359 * if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
3360 * rotation = (info.orientation - orientation + 360) % 360;
3361 * } else { // back-facing camera
3362 * rotation = (info.orientation + orientation) % 360;
3363 * }
3364 * mParameters.setRotation(rotation);
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003365 * }
Eino-Ville Talvalae0cc55a2011-11-08 10:12:09 -08003366 * </pre>
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003367 *
3368 * @param rotation The rotation angle in degrees relative to the
3369 * orientation of the camera. Rotation can only be 0,
3370 * 90, 180 or 270.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003371 * @throws IllegalArgumentException if rotation value is invalid.
3372 * @see android.view.OrientationEventListener
Wu-cheng Li2fb818c2010-09-13 20:02:01 -07003373 * @see #getCameraInfo(int, CameraInfo)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003374 */
3375 public void setRotation(int rotation) {
3376 if (rotation == 0 || rotation == 90 || rotation == 180
3377 || rotation == 270) {
3378 set(KEY_ROTATION, Integer.toString(rotation));
3379 } else {
3380 throw new IllegalArgumentException(
3381 "Invalid rotation=" + rotation);
3382 }
3383 }
3384
3385 /**
3386 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF
3387 * header.
3388 *
3389 * @param latitude GPS latitude coordinate.
3390 */
3391 public void setGpsLatitude(double latitude) {
3392 set(KEY_GPS_LATITUDE, Double.toString(latitude));
3393 }
3394
3395 /**
3396 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF
3397 * header.
3398 *
3399 * @param longitude GPS longitude coordinate.
3400 */
3401 public void setGpsLongitude(double longitude) {
3402 set(KEY_GPS_LONGITUDE, Double.toString(longitude));
3403 }
3404
3405 /**
3406 * Sets GPS altitude. This will be stored in JPEG EXIF header.
3407 *
3408 * @param altitude GPS altitude in meters.
3409 */
3410 public void setGpsAltitude(double altitude) {
3411 set(KEY_GPS_ALTITUDE, Double.toString(altitude));
3412 }
3413
3414 /**
3415 * Sets GPS timestamp. This will be stored in JPEG EXIF header.
3416 *
3417 * @param timestamp GPS timestamp (UTC in seconds since January 1,
3418 * 1970).
3419 */
3420 public void setGpsTimestamp(long timestamp) {
3421 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp));
3422 }
3423
3424 /**
Eino-Ville Talvalad8407272015-11-08 18:27:20 -08003425 * Sets GPS processing method. The method will be stored in a UTF-8 string up to 31 bytes
3426 * long, in the JPEG EXIF header.
Ray Chen055c9862010-02-23 10:45:42 +08003427 *
3428 * @param processing_method The processing method to get this location.
3429 */
3430 public void setGpsProcessingMethod(String processing_method) {
3431 set(KEY_GPS_PROCESSING_METHOD, processing_method);
3432 }
3433
3434 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003435 * Removes GPS latitude, longitude, altitude, and timestamp from the
3436 * parameters.
3437 */
3438 public void removeGpsData() {
3439 remove(KEY_GPS_LATITUDE);
3440 remove(KEY_GPS_LONGITUDE);
3441 remove(KEY_GPS_ALTITUDE);
3442 remove(KEY_GPS_TIMESTAMP);
Ray Chen055c9862010-02-23 10:45:42 +08003443 remove(KEY_GPS_PROCESSING_METHOD);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003444 }
3445
3446 /**
3447 * Gets the current white balance setting.
3448 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003449 * @return current white balance. null if white balance setting is not
3450 * supported.
3451 * @see #WHITE_BALANCE_AUTO
3452 * @see #WHITE_BALANCE_INCANDESCENT
3453 * @see #WHITE_BALANCE_FLUORESCENT
3454 * @see #WHITE_BALANCE_WARM_FLUORESCENT
3455 * @see #WHITE_BALANCE_DAYLIGHT
3456 * @see #WHITE_BALANCE_CLOUDY_DAYLIGHT
3457 * @see #WHITE_BALANCE_TWILIGHT
3458 * @see #WHITE_BALANCE_SHADE
3459 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003460 */
3461 public String getWhiteBalance() {
3462 return get(KEY_WHITE_BALANCE);
3463 }
3464
3465 /**
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003466 * Sets the white balance. Changing the setting will release the
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08003467 * auto-white balance lock. It is recommended not to change white
3468 * balance and AWB lock at the same time.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003469 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003470 * @param value new white balance.
3471 * @see #getWhiteBalance()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07003472 * @see #setAutoWhiteBalanceLock(boolean)
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003473 */
3474 public void setWhiteBalance(String value) {
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08003475 String oldValue = get(KEY_WHITE_BALANCE);
3476 if (same(value, oldValue)) return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003477 set(KEY_WHITE_BALANCE, value);
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003478 set(KEY_AUTO_WHITEBALANCE_LOCK, FALSE);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003479 }
3480
3481 /**
3482 * Gets the supported white balance.
3483 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003484 * @return a list of supported white balance. null if white balance
3485 * setting is not supported.
3486 * @see #getWhiteBalance()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003487 */
3488 public List<String> getSupportedWhiteBalance() {
3489 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX);
3490 return split(str);
3491 }
3492
3493 /**
3494 * Gets the current color effect setting.
3495 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003496 * @return current color effect. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003497 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003498 * @see #EFFECT_NONE
3499 * @see #EFFECT_MONO
3500 * @see #EFFECT_NEGATIVE
3501 * @see #EFFECT_SOLARIZE
3502 * @see #EFFECT_SEPIA
3503 * @see #EFFECT_POSTERIZE
3504 * @see #EFFECT_WHITEBOARD
3505 * @see #EFFECT_BLACKBOARD
3506 * @see #EFFECT_AQUA
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003507 */
3508 public String getColorEffect() {
3509 return get(KEY_EFFECT);
3510 }
3511
3512 /**
3513 * Sets the current color effect setting.
3514 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003515 * @param value new color effect.
3516 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003517 */
3518 public void setColorEffect(String value) {
3519 set(KEY_EFFECT, value);
3520 }
3521
3522 /**
3523 * Gets the supported color effects.
3524 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003525 * @return a list of supported color effects. null if color effect
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003526 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003527 * @see #getColorEffect()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003528 */
3529 public List<String> getSupportedColorEffects() {
3530 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX);
3531 return split(str);
3532 }
3533
3534
3535 /**
3536 * Gets the current antibanding setting.
3537 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003538 * @return current antibanding. null if antibanding setting is not
3539 * supported.
3540 * @see #ANTIBANDING_AUTO
3541 * @see #ANTIBANDING_50HZ
3542 * @see #ANTIBANDING_60HZ
3543 * @see #ANTIBANDING_OFF
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003544 */
3545 public String getAntibanding() {
3546 return get(KEY_ANTIBANDING);
3547 }
3548
3549 /**
3550 * Sets the antibanding.
3551 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003552 * @param antibanding new antibanding value.
3553 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003554 */
3555 public void setAntibanding(String antibanding) {
3556 set(KEY_ANTIBANDING, antibanding);
3557 }
3558
3559 /**
3560 * Gets the supported antibanding values.
3561 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003562 * @return a list of supported antibanding values. null if antibanding
3563 * setting is not supported.
3564 * @see #getAntibanding()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003565 */
3566 public List<String> getSupportedAntibanding() {
3567 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX);
3568 return split(str);
3569 }
3570
3571 /**
3572 * Gets the current scene mode setting.
3573 *
3574 * @return one of SCENE_MODE_XXX string constant. null if scene mode
3575 * setting is not supported.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003576 * @see #SCENE_MODE_AUTO
3577 * @see #SCENE_MODE_ACTION
3578 * @see #SCENE_MODE_PORTRAIT
3579 * @see #SCENE_MODE_LANDSCAPE
3580 * @see #SCENE_MODE_NIGHT
3581 * @see #SCENE_MODE_NIGHT_PORTRAIT
3582 * @see #SCENE_MODE_THEATRE
3583 * @see #SCENE_MODE_BEACH
3584 * @see #SCENE_MODE_SNOW
3585 * @see #SCENE_MODE_SUNSET
3586 * @see #SCENE_MODE_STEADYPHOTO
3587 * @see #SCENE_MODE_FIREWORKS
3588 * @see #SCENE_MODE_SPORTS
3589 * @see #SCENE_MODE_PARTY
3590 * @see #SCENE_MODE_CANDLELIGHT
Eino-Ville Talvalada2f0ea2012-09-10 12:03:35 -07003591 * @see #SCENE_MODE_BARCODE
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003592 */
3593 public String getSceneMode() {
3594 return get(KEY_SCENE_MODE);
3595 }
3596
3597 /**
Wu-cheng Lic58b4232010-03-29 17:21:28 +08003598 * Sets the scene mode. Changing scene mode may override other
3599 * parameters (such as flash mode, focus mode, white balance). For
3600 * example, suppose originally flash mode is on and supported flash
3601 * modes are on/off. In night scene mode, both flash mode and supported
3602 * flash mode may be changed to off. After setting scene mode,
Wu-cheng Li2988ab72009-09-30 17:08:19 -07003603 * applications should call getParameters to know if some parameters are
3604 * changed.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003605 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003606 * @param value scene mode.
3607 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003608 */
3609 public void setSceneMode(String value) {
3610 set(KEY_SCENE_MODE, value);
3611 }
3612
3613 /**
3614 * Gets the supported scene modes.
3615 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003616 * @return a list of supported scene modes. null if scene mode setting
3617 * is not supported.
3618 * @see #getSceneMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003619 */
3620 public List<String> getSupportedSceneModes() {
3621 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX);
3622 return split(str);
3623 }
3624
3625 /**
3626 * Gets the current flash mode setting.
3627 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003628 * @return current flash mode. null if flash mode setting is not
3629 * supported.
3630 * @see #FLASH_MODE_OFF
3631 * @see #FLASH_MODE_AUTO
3632 * @see #FLASH_MODE_ON
3633 * @see #FLASH_MODE_RED_EYE
3634 * @see #FLASH_MODE_TORCH
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003635 */
3636 public String getFlashMode() {
3637 return get(KEY_FLASH_MODE);
3638 }
3639
3640 /**
3641 * Sets the flash mode.
3642 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003643 * @param value flash mode.
3644 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003645 */
3646 public void setFlashMode(String value) {
3647 set(KEY_FLASH_MODE, value);
3648 }
3649
3650 /**
3651 * Gets the supported flash modes.
3652 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003653 * @return a list of supported flash modes. null if flash mode setting
3654 * is not supported.
3655 * @see #getFlashMode()
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08003656 */
3657 public List<String> getSupportedFlashModes() {
3658 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX);
3659 return split(str);
3660 }
3661
Wu-cheng Li36322db2009-09-18 18:59:21 +08003662 /**
3663 * Gets the current focus mode setting.
3664 *
Wu-cheng Li699fe932010-08-05 11:50:25 -07003665 * @return current focus mode. This method will always return a non-null
3666 * value. Applications should call {@link
3667 * #autoFocus(AutoFocusCallback)} to start the focus if focus
3668 * mode is FOCUS_MODE_AUTO or FOCUS_MODE_MACRO.
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003669 * @see #FOCUS_MODE_AUTO
3670 * @see #FOCUS_MODE_INFINITY
3671 * @see #FOCUS_MODE_MACRO
3672 * @see #FOCUS_MODE_FIXED
Wu-cheng Lif008f3e2010-08-17 13:44:35 -07003673 * @see #FOCUS_MODE_EDOF
Wu-cheng Lid45cb722010-09-20 16:15:32 -07003674 * @see #FOCUS_MODE_CONTINUOUS_VIDEO
Wu-cheng Li36322db2009-09-18 18:59:21 +08003675 */
3676 public String getFocusMode() {
3677 return get(KEY_FOCUS_MODE);
3678 }
3679
3680 /**
3681 * Sets the focus mode.
3682 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003683 * @param value focus mode.
3684 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08003685 */
3686 public void setFocusMode(String value) {
3687 set(KEY_FOCUS_MODE, value);
3688 }
3689
3690 /**
3691 * Gets the supported focus modes.
3692 *
Wu-cheng Li3f4639a2010-04-04 15:05:41 +08003693 * @return a list of supported focus modes. This method will always
3694 * return a list with at least one element.
3695 * @see #getFocusMode()
Wu-cheng Li36322db2009-09-18 18:59:21 +08003696 */
3697 public List<String> getSupportedFocusModes() {
3698 String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX);
3699 return split(str);
3700 }
3701
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003702 /**
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003703 * Gets the focal length (in millimeter) of the camera.
3704 *
Yin-Chia Yehbb85e1c2018-02-27 11:50:48 -08003705 * @return the focal length. Returns -1.0 when the device
3706 * doesn't report focal length information.
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003707 */
3708 public float getFocalLength() {
3709 return Float.parseFloat(get(KEY_FOCAL_LENGTH));
3710 }
3711
3712 /**
3713 * Gets the horizontal angle of view in degrees.
3714 *
Yin-Chia Yeh564ae822018-02-08 16:24:27 -08003715 * @return horizontal angle of view. Returns -1.0 when the device
3716 * doesn't report view angle information.
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003717 */
3718 public float getHorizontalViewAngle() {
3719 return Float.parseFloat(get(KEY_HORIZONTAL_VIEW_ANGLE));
3720 }
3721
3722 /**
3723 * Gets the vertical angle of view in degrees.
3724 *
Yin-Chia Yeh564ae822018-02-08 16:24:27 -08003725 * @return vertical angle of view. Returns -1.0 when the device
3726 * doesn't report view angle information.
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08003727 */
3728 public float getVerticalViewAngle() {
3729 return Float.parseFloat(get(KEY_VERTICAL_VIEW_ANGLE));
3730 }
3731
3732 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003733 * Gets the current exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003734 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003735 * @return current exposure compensation index. The range is {@link
3736 * #getMinExposureCompensation} to {@link
3737 * #getMaxExposureCompensation}. 0 means exposure is not
3738 * adjusted.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003739 */
3740 public int getExposureCompensation() {
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003741 return getInt(KEY_EXPOSURE_COMPENSATION, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08003742 }
3743
3744 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003745 * Sets the exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003746 *
Wu-cheng Li0402e7d2010-02-26 15:04:55 +08003747 * @param value exposure compensation index. The valid value range is
3748 * from {@link #getMinExposureCompensation} (inclusive) to {@link
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003749 * #getMaxExposureCompensation} (inclusive). 0 means exposure is
3750 * not adjusted. Application should call
3751 * getMinExposureCompensation and getMaxExposureCompensation to
3752 * know if exposure compensation is supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003753 */
3754 public void setExposureCompensation(int value) {
3755 set(KEY_EXPOSURE_COMPENSATION, value);
3756 }
3757
3758 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003759 * Gets the maximum exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003760 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003761 * @return maximum exposure compensation index (>=0). If both this
3762 * method and {@link #getMinExposureCompensation} return 0,
3763 * exposure compensation is not supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08003764 */
Wu-cheng Li24b326a2010-02-20 17:47:04 +08003765 public int getMaxExposureCompensation() {
3766 return getInt(KEY_MAX_EXPOSURE_COMPENSATION, 0);
3767 }
3768
3769 /**
3770 * Gets the minimum exposure compensation index.
3771 *
3772 * @return minimum exposure compensation index (<=0). If both this
3773 * method and {@link #getMaxExposureCompensation} return 0,
3774 * exposure compensation is not supported.
3775 */
3776 public int getMinExposureCompensation() {
3777 return getInt(KEY_MIN_EXPOSURE_COMPENSATION, 0);
3778 }
3779
3780 /**
3781 * Gets the exposure compensation step.
3782 *
3783 * @return exposure compensation step. Applications can get EV by
3784 * multiplying the exposure compensation index and step. Ex: if
3785 * exposure compensation index is -6 and step is 0.333333333, EV
3786 * is -2.
3787 */
3788 public float getExposureCompensationStep() {
3789 return getFloat(KEY_EXPOSURE_COMPENSATION_STEP, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08003790 }
3791
3792 /**
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003793 * <p>Sets the auto-exposure lock state. Applications should check
3794 * {@link #isAutoExposureLockSupported} before using this method.</p>
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003795 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003796 * <p>If set to true, the camera auto-exposure routine will immediately
3797 * pause until the lock is set to false. Exposure compensation settings
3798 * changes will still take effect while auto-exposure is locked.</p>
3799 *
3800 * <p>If auto-exposure is already locked, setting this to true again has
3801 * no effect (the driver will not recalculate exposure values).</p>
3802 *
3803 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
3804 * image capture with {@link #takePicture(Camera.ShutterCallback,
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003805 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
3806 * lock.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003807 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003808 * <p>Exposure compensation, auto-exposure lock, and auto-white balance
3809 * lock can be used to capture an exposure-bracketed burst of images,
3810 * for example.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003811 *
3812 * <p>Auto-exposure state, including the lock state, will not be
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003813 * maintained after camera {@link #release()} is called. Locking
3814 * auto-exposure after {@link #open()} but before the first call to
3815 * {@link #startPreview()} will not allow the auto-exposure routine to
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003816 * run at all, and may result in severely over- or under-exposed
3817 * images.</p>
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003818 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003819 * @param toggle new state of the auto-exposure lock. True means that
3820 * auto-exposure is locked, false means that the auto-exposure
3821 * routine is free to run normally.
3822 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003823 * @see #getAutoExposureLock()
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003824 */
3825 public void setAutoExposureLock(boolean toggle) {
3826 set(KEY_AUTO_EXPOSURE_LOCK, toggle ? TRUE : FALSE);
3827 }
3828
3829 /**
3830 * Gets the state of the auto-exposure lock. Applications should check
3831 * {@link #isAutoExposureLockSupported} before using this method. See
3832 * {@link #setAutoExposureLock} for details about the lock.
3833 *
3834 * @return State of the auto-exposure lock. Returns true if
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003835 * auto-exposure is currently locked, and false otherwise.
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003836 *
3837 * @see #setAutoExposureLock(boolean)
3838 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003839 */
3840 public boolean getAutoExposureLock() {
3841 String str = get(KEY_AUTO_EXPOSURE_LOCK);
3842 return TRUE.equals(str);
3843 }
3844
3845 /**
3846 * Returns true if auto-exposure locking is supported. Applications
3847 * should call this before trying to lock auto-exposure. See
3848 * {@link #setAutoExposureLock} for details about the lock.
3849 *
3850 * @return true if auto-exposure lock is supported.
3851 * @see #setAutoExposureLock(boolean)
3852 *
Eino-Ville Talvala3773eef2011-04-15 13:51:42 -07003853 */
3854 public boolean isAutoExposureLockSupported() {
3855 String str = get(KEY_AUTO_EXPOSURE_LOCK_SUPPORTED);
3856 return TRUE.equals(str);
3857 }
3858
3859 /**
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003860 * <p>Sets the auto-white balance lock state. Applications should check
3861 * {@link #isAutoWhiteBalanceLockSupported} before using this
3862 * method.</p>
3863 *
3864 * <p>If set to true, the camera auto-white balance routine will
3865 * immediately pause until the lock is set to false.</p>
3866 *
3867 * <p>If auto-white balance is already locked, setting this to true
3868 * again has no effect (the driver will not recalculate white balance
3869 * values).</p>
3870 *
3871 * <p>Stopping preview with {@link #stopPreview()}, or triggering still
3872 * image capture with {@link #takePicture(Camera.ShutterCallback,
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003873 * Camera.PictureCallback, Camera.PictureCallback)}, will not change the
3874 * the lock.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003875 *
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003876 * <p> Changing the white balance mode with {@link #setWhiteBalance}
3877 * will release the auto-white balance lock if it is set.</p>
3878 *
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003879 * <p>Exposure compensation, AE lock, and AWB lock can be used to
3880 * capture an exposure-bracketed burst of images, for example.
3881 * Auto-white balance state, including the lock state, will not be
3882 * maintained after camera {@link #release()} is called. Locking
3883 * auto-white balance after {@link #open()} but before the first call to
3884 * {@link #startPreview()} will not allow the auto-white balance routine
3885 * to run at all, and may result in severely incorrect color in captured
3886 * images.</p>
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003887 *
3888 * @param toggle new state of the auto-white balance lock. True means
3889 * that auto-white balance is locked, false means that the
3890 * auto-white balance routine is free to run normally.
3891 *
3892 * @see #getAutoWhiteBalanceLock()
Eino-Ville Talvala16b67132011-08-18 13:57:49 -07003893 * @see #setWhiteBalance(String)
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003894 */
3895 public void setAutoWhiteBalanceLock(boolean toggle) {
3896 set(KEY_AUTO_WHITEBALANCE_LOCK, toggle ? TRUE : FALSE);
3897 }
3898
3899 /**
3900 * Gets the state of the auto-white balance lock. Applications should
3901 * check {@link #isAutoWhiteBalanceLockSupported} before using this
3902 * method. See {@link #setAutoWhiteBalanceLock} for details about the
3903 * lock.
3904 *
3905 * @return State of the auto-white balance lock. Returns true if
3906 * auto-white balance is currently locked, and false
Wu-cheng Lib4f95be2011-09-22 11:43:28 +08003907 * otherwise.
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003908 *
3909 * @see #setAutoWhiteBalanceLock(boolean)
3910 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003911 */
3912 public boolean getAutoWhiteBalanceLock() {
3913 String str = get(KEY_AUTO_WHITEBALANCE_LOCK);
3914 return TRUE.equals(str);
3915 }
3916
3917 /**
3918 * Returns true if auto-white balance locking is supported. Applications
3919 * should call this before trying to lock auto-white balance. See
3920 * {@link #setAutoWhiteBalanceLock} for details about the lock.
3921 *
3922 * @return true if auto-white balance lock is supported.
3923 * @see #setAutoWhiteBalanceLock(boolean)
3924 *
Eino-Ville Talvalad9c2601a2011-05-13 10:19:59 -07003925 */
3926 public boolean isAutoWhiteBalanceLockSupported() {
3927 String str = get(KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED);
3928 return TRUE.equals(str);
3929 }
3930
3931 /**
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003932 * Gets current zoom value. This also works when smooth zoom is in
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003933 * progress. Applications should check {@link #isZoomSupported} before
3934 * using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003935 *
3936 * @return the current zoom value. The range is 0 to {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003937 * #getMaxZoom}. 0 means the camera is not zoomed.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003938 */
3939 public int getZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003940 return getInt(KEY_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003941 }
3942
3943 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003944 * Sets current zoom value. If the camera is zoomed (value > 0), the
3945 * actual picture size may be smaller than picture size setting.
3946 * Applications can check the actual picture size after picture is
3947 * returned from {@link PictureCallback}. The preview size remains the
3948 * same in zoom. Applications should check {@link #isZoomSupported}
3949 * before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003950 *
3951 * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003952 */
3953 public void setZoom(int value) {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003954 set(KEY_ZOOM, value);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003955 }
3956
3957 /**
3958 * Returns true if zoom is supported. Applications should call this
3959 * before using other zoom methods.
3960 *
3961 * @return true if zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003962 */
3963 public boolean isZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003964 String str = get(KEY_ZOOM_SUPPORTED);
3965 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003966 }
3967
3968 /**
3969 * Gets the maximum zoom value allowed for snapshot. This is the maximum
3970 * value that applications can set to {@link #setZoom(int)}.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003971 * Applications should call {@link #isZoomSupported} before using this
3972 * method. This value may change in different preview size. Applications
3973 * should call this again after setting preview size.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003974 *
3975 * @return the maximum zoom value supported by the camera.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003976 */
3977 public int getMaxZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003978 return getInt(KEY_MAX_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003979 }
3980
3981 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003982 * Gets the zoom ratios of all zoom values. Applications should check
3983 * {@link #isZoomSupported} before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003984 *
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003985 * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is
3986 * returned as 320. The number of elements is {@link
3987 * #getMaxZoom} + 1. The list is sorted from small to large. The
3988 * first element is always 100. The last element is the zoom
3989 * ratio of the maximum zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003990 */
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08003991 public List<Integer> getZoomRatios() {
3992 return splitInt(get(KEY_ZOOM_RATIOS));
Wu-cheng Li36f68b82009-09-28 16:14:58 -07003993 }
3994
3995 /**
3996 * Returns true if smooth zoom is supported. Applications should call
3997 * this before using other smooth zoom methods.
3998 *
3999 * @return true if smooth zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07004000 */
4001 public boolean isSmoothZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08004002 String str = get(KEY_SMOOTH_ZOOM_SUPPORTED);
4003 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07004004 }
4005
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004006 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004007 * <p>Gets the distances from the camera to where an object appears to be
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004008 * in focus. The object is sharpest at the optimal focus distance. The
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004009 * depth of field is the far focus distance minus near focus distance.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004010 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004011 * <p>Focus distances may change after calling {@link
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004012 * #autoFocus(AutoFocusCallback)}, {@link #cancelAutoFocus}, or {@link
4013 * #startPreview()}. Applications can call {@link #getParameters()}
4014 * and this method anytime to get the latest focus distances. If the
Wu-cheng Lid45cb722010-09-20 16:15:32 -07004015 * focus mode is FOCUS_MODE_CONTINUOUS_VIDEO, focus distances may change
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004016 * from time to time.</p>
Wu-cheng Li699fe932010-08-05 11:50:25 -07004017 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004018 * <p>This method is intended to estimate the distance between the camera
Wu-cheng Li699fe932010-08-05 11:50:25 -07004019 * and the subject. After autofocus, the subject distance may be within
4020 * near and far focus distance. However, the precision depends on the
4021 * camera hardware, autofocus algorithm, the focus area, and the scene.
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004022 * The error can be large and it should be only used as a reference.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004023 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004024 * <p>Far focus distance >= optimal focus distance >= near focus distance.
Wu-cheng Li185cc452010-05-20 15:36:13 +08004025 * If the focus distance is infinity, the value will be
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004026 * {@code Float.POSITIVE_INFINITY}.</p>
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004027 *
4028 * @param output focus distances in meters. output must be a float
4029 * array with three elements. Near focus distance, optimal focus
4030 * distance, and far focus distance will be filled in the array.
Wu-cheng Li185cc452010-05-20 15:36:13 +08004031 * @see #FOCUS_DISTANCE_NEAR_INDEX
4032 * @see #FOCUS_DISTANCE_OPTIMAL_INDEX
4033 * @see #FOCUS_DISTANCE_FAR_INDEX
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004034 */
4035 public void getFocusDistances(float[] output) {
4036 if (output == null || output.length != 3) {
4037 throw new IllegalArgumentException(
Ken Wakasaf76a50c2012-03-09 19:56:35 +09004038 "output must be a float array with three elements.");
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004039 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07004040 splitFloat(get(KEY_FOCUS_DISTANCES), output);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004041 }
4042
Wu-cheng Li30771b72011-04-02 06:19:46 +08004043 /**
4044 * Gets the maximum number of focus areas supported. This is the maximum
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004045 * length of the list in {@link #setFocusAreas(List)} and
4046 * {@link #getFocusAreas()}.
Wu-cheng Li30771b72011-04-02 06:19:46 +08004047 *
4048 * @return the maximum number of focus areas supported by the camera.
4049 * @see #getFocusAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08004050 */
4051 public int getMaxNumFocusAreas() {
4052 return getInt(KEY_MAX_NUM_FOCUS_AREAS, 0);
4053 }
4054
4055 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004056 * <p>Gets the current focus areas. Camera driver uses the areas to decide
4057 * focus.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004058 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004059 * <p>Before using this API or {@link #setFocusAreas(List)}, apps should
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004060 * call {@link #getMaxNumFocusAreas()} to know the maximum number of
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004061 * focus areas first. If the value is 0, focus area is not supported.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004062 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004063 * <p>Each focus area is a rectangle with specified weight. The direction
Wu-cheng Li30771b72011-04-02 06:19:46 +08004064 * is relative to the sensor orientation, that is, what the sensor sees.
4065 * The direction is not affected by the rotation or mirroring of
4066 * {@link #setDisplayOrientation(int)}. Coordinates of the rectangle
4067 * range from -1000 to 1000. (-1000, -1000) is the upper left point.
Wu-cheng Libde61a52011-06-07 18:23:14 +08004068 * (1000, 1000) is the lower right point. The width and height of focus
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004069 * areas cannot be 0 or negative.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004070 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004071 * <p>The weight must range from 1 to 1000. The weight should be
Eino-Ville Talvala4e396e02011-04-21 09:23:15 -07004072 * interpreted as a per-pixel weight - all pixels in the area have the
4073 * specified weight. This means a small area with the same weight as a
4074 * larger area will have less influence on the focusing than the larger
4075 * area. Focus areas can partially overlap and the driver will add the
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004076 * weights in the overlap region.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004077 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004078 * <p>A special case of a {@code null} focus area list means the driver is
4079 * free to select focus targets as it wants. For example, the driver may
4080 * use more signals to select focus areas and change them
4081 * dynamically. Apps can set the focus area list to {@code null} if they
4082 * want the driver to completely control focusing.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004083 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004084 * <p>Focus areas are relative to the current field of view
Wu-cheng Li30771b72011-04-02 06:19:46 +08004085 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000)
4086 * represents the top of the currently visible camera frame. The focus
4087 * area cannot be set to be outside the current field of view, even
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004088 * when using zoom.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004089 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004090 * <p>Focus area only has effect if the current focus mode is
Wu-cheng Li53b30912011-10-12 19:43:51 +08004091 * {@link #FOCUS_MODE_AUTO}, {@link #FOCUS_MODE_MACRO},
4092 * {@link #FOCUS_MODE_CONTINUOUS_VIDEO}, or
4093 * {@link #FOCUS_MODE_CONTINUOUS_PICTURE}.</p>
Wu-cheng Li30771b72011-04-02 06:19:46 +08004094 *
4095 * @return a list of current focus areas
Wu-cheng Li30771b72011-04-02 06:19:46 +08004096 */
4097 public List<Area> getFocusAreas() {
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004098 return splitArea(get(KEY_FOCUS_AREAS));
Wu-cheng Li30771b72011-04-02 06:19:46 +08004099 }
4100
4101 /**
4102 * Sets focus areas. See {@link #getFocusAreas()} for documentation.
4103 *
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004104 * @param focusAreas the focus areas
Wu-cheng Li30771b72011-04-02 06:19:46 +08004105 * @see #getFocusAreas()
Wu-cheng Li30771b72011-04-02 06:19:46 +08004106 */
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004107 public void setFocusAreas(List<Area> focusAreas) {
4108 set(KEY_FOCUS_AREAS, focusAreas);
4109 }
4110
4111 /**
4112 * Gets the maximum number of metering areas supported. This is the
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004113 * maximum length of the list in {@link #setMeteringAreas(List)} and
4114 * {@link #getMeteringAreas()}.
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004115 *
4116 * @return the maximum number of metering areas supported by the camera.
4117 * @see #getMeteringAreas()
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004118 */
4119 public int getMaxNumMeteringAreas() {
4120 return getInt(KEY_MAX_NUM_METERING_AREAS, 0);
4121 }
4122
4123 /**
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004124 * <p>Gets the current metering areas. Camera driver uses these areas to
4125 * decide exposure.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004126 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004127 * <p>Before using this API or {@link #setMeteringAreas(List)}, apps should
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004128 * call {@link #getMaxNumMeteringAreas()} to know the maximum number of
4129 * metering areas first. If the value is 0, metering area is not
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004130 * supported.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004131 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004132 * <p>Each metering area is a rectangle with specified weight. The
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004133 * direction is relative to the sensor orientation, that is, what the
4134 * sensor sees. The direction is not affected by the rotation or
4135 * mirroring of {@link #setDisplayOrientation(int)}. Coordinates of the
4136 * rectangle range from -1000 to 1000. (-1000, -1000) is the upper left
Wu-cheng Libde61a52011-06-07 18:23:14 +08004137 * point. (1000, 1000) is the lower right point. The width and height of
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004138 * metering areas cannot be 0 or negative.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004139 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004140 * <p>The weight must range from 1 to 1000, and represents a weight for
Eino-Ville Talvala4e396e02011-04-21 09:23:15 -07004141 * every pixel in the area. This means that a large metering area with
4142 * the same weight as a smaller area will have more effect in the
4143 * metering result. Metering areas can partially overlap and the driver
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004144 * will add the weights in the overlap region.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004145 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004146 * <p>A special case of a {@code null} metering area list means the driver
4147 * is free to meter as it chooses. For example, the driver may use more
4148 * signals to select metering areas and change them dynamically. Apps
4149 * can set the metering area list to {@code null} if they want the
4150 * driver to completely control metering.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004151 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004152 * <p>Metering areas are relative to the current field of view
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004153 * ({@link #getZoom()}). No matter what the zoom level is, (-1000,-1000)
4154 * represents the top of the currently visible camera frame. The
4155 * metering area cannot be set to be outside the current field of view,
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004156 * even when using zoom.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004157 *
Eino-Ville Talvala32a972c2011-06-07 10:34:56 -07004158 * <p>No matter what metering areas are, the final exposure are compensated
4159 * by {@link #setExposureCompensation(int)}.</p>
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004160 *
4161 * @return a list of current metering areas
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004162 */
4163 public List<Area> getMeteringAreas() {
Wu-cheng Lid8aab932011-06-21 11:50:43 +08004164 return splitArea(get(KEY_METERING_AREAS));
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004165 }
4166
4167 /**
4168 * Sets metering areas. See {@link #getMeteringAreas()} for
4169 * documentation.
4170 *
Wu-cheng Li7b1c5c82011-04-15 19:16:52 +08004171 * @param meteringAreas the metering areas
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004172 * @see #getMeteringAreas()
Wu-cheng Lie98e4c82011-04-12 19:34:29 +08004173 */
4174 public void setMeteringAreas(List<Area> meteringAreas) {
4175 set(KEY_METERING_AREAS, meteringAreas);
Wu-cheng Li30771b72011-04-02 06:19:46 +08004176 }
4177
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08004178 /**
4179 * Gets the maximum number of detected faces supported. This is the
4180 * maximum length of the list returned from {@link FaceDetectionListener}.
4181 * If the return value is 0, face detection of the specified type is not
4182 * supported.
4183 *
4184 * @return the maximum number of detected face supported by the camera.
Joe Fernandez464cb212011-10-04 16:56:47 -07004185 * @see #startFaceDetection()
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08004186 */
Wu-cheng Lic0c683b2011-08-04 00:11:00 +08004187 public int getMaxNumDetectedFaces() {
4188 return getInt(KEY_MAX_NUM_DETECTED_FACES_HW, 0);
Wu-cheng Li4c2292e2011-07-22 02:37:11 +08004189 }
4190
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004191 /**
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08004192 * Sets recording mode hint. This tells the camera that the intent of
4193 * the application is to record videos {@link
4194 * android.media.MediaRecorder#start()}, not to take still pictures
4195 * {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
4196 * Camera.PictureCallback, Camera.PictureCallback)}. Using this hint can
4197 * allow MediaRecorder.start() to start faster or with fewer glitches on
4198 * output. This should be called before starting preview for the best
4199 * result, but can be changed while the preview is active. The default
4200 * value is false.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004201 *
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08004202 * The app can still call takePicture() when the hint is true or call
4203 * MediaRecorder.start() when the hint is false. But the performance may
4204 * be worse.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004205 *
Wu-cheng Li9c53f1c2011-08-02 17:26:58 +08004206 * @param hint true if the apps intend to record videos using
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004207 * {@link android.media.MediaRecorder}.
Wu-cheng Li25d8fb52011-08-02 13:20:36 +08004208 */
4209 public void setRecordingHint(boolean hint) {
4210 set(KEY_RECORDING_HINT, hint ? TRUE : FALSE);
4211 }
4212
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004213 /**
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004214 * <p>Returns true if video snapshot is supported. That is, applications
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004215 * can call {@link #takePicture(Camera.ShutterCallback,
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004216 * Camera.PictureCallback, Camera.PictureCallback,
4217 * Camera.PictureCallback)} during recording. Applications do not need
4218 * to call {@link #startPreview()} after taking a picture. The preview
4219 * will be still active. Other than that, taking a picture during
4220 * recording is identical to taking a picture normally. All settings and
4221 * methods related to takePicture work identically. Ex:
4222 * {@link #getPictureSize()}, {@link #getSupportedPictureSizes()},
4223 * {@link #setJpegQuality(int)}, {@link #setRotation(int)}, and etc. The
4224 * picture will have an EXIF header. {@link #FLASH_MODE_AUTO} and
4225 * {@link #FLASH_MODE_ON} also still work, but the video will record the
4226 * flash.</p>
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004227 *
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004228 * <p>Applications can set shutter callback as null to avoid the shutter
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004229 * sound. It is also recommended to set raw picture and post view
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004230 * callbacks to null to avoid the interrupt of preview display.</p>
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004231 *
Eino-Ville Talvala1cab31a2012-11-05 10:33:55 -08004232 * <p>Field-of-view of the recorded video may be different from that of the
4233 * captured pictures. The maximum size of a video snapshot may be
4234 * smaller than that for regular still captures. If the current picture
4235 * size is set higher than can be supported by video snapshot, the
4236 * picture will be captured at the maximum supported size instead.</p>
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004237 *
4238 * @return true if video snapshot is supported.
Wu-cheng Li98bb2512011-08-30 21:33:10 +08004239 */
4240 public boolean isVideoSnapshotSupported() {
4241 String str = get(KEY_VIDEO_SNAPSHOT_SUPPORTED);
4242 return TRUE.equals(str);
4243 }
4244
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004245 /**
4246 * <p>Enables and disables video stabilization. Use
4247 * {@link #isVideoStabilizationSupported} to determine if calling this
4248 * method is valid.</p>
4249 *
4250 * <p>Video stabilization reduces the shaking due to the motion of the
4251 * camera in both the preview stream and in recorded videos, including
4252 * data received from the preview callback. It does not reduce motion
4253 * blur in images captured with
4254 * {@link Camera#takePicture takePicture}.</p>
4255 *
4256 * <p>Video stabilization can be enabled and disabled while preview or
4257 * recording is active, but toggling it may cause a jump in the video
4258 * stream that may be undesirable in a recorded video.</p>
4259 *
4260 * @param toggle Set to true to enable video stabilization, and false to
4261 * disable video stabilization.
4262 * @see #isVideoStabilizationSupported()
4263 * @see #getVideoStabilization()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004264 */
4265 public void setVideoStabilization(boolean toggle) {
4266 set(KEY_VIDEO_STABILIZATION, toggle ? TRUE : FALSE);
4267 }
4268
4269 /**
4270 * Get the current state of video stabilization. See
4271 * {@link #setVideoStabilization} for details of video stabilization.
4272 *
4273 * @return true if video stabilization is enabled
4274 * @see #isVideoStabilizationSupported()
4275 * @see #setVideoStabilization(boolean)
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004276 */
4277 public boolean getVideoStabilization() {
4278 String str = get(KEY_VIDEO_STABILIZATION);
4279 return TRUE.equals(str);
4280 }
4281
4282 /**
4283 * Returns true if video stabilization is supported. See
4284 * {@link #setVideoStabilization} for details of video stabilization.
4285 *
4286 * @return true if video stabilization is supported
4287 * @see #setVideoStabilization(boolean)
4288 * @see #getVideoStabilization()
Eino-Ville Talvala037abb82011-10-11 12:41:58 -07004289 */
4290 public boolean isVideoStabilizationSupported() {
4291 String str = get(KEY_VIDEO_STABILIZATION_SUPPORTED);
4292 return TRUE.equals(str);
4293 }
4294
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004295 // Splits a comma delimited string to an ArrayList of String.
4296 // Return null if the passing string is null or the size is 0.
4297 private ArrayList<String> split(String str) {
4298 if (str == null) return null;
4299
Ali Utku Selen0a120182011-02-09 14:11:22 +01004300 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4301 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004302 ArrayList<String> substrings = new ArrayList<String>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01004303 for (String s : splitter) {
4304 substrings.add(s);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004305 }
4306 return substrings;
4307 }
4308
4309 // Splits a comma delimited string to an ArrayList of Integer.
4310 // Return null if the passing string is null or the size is 0.
4311 private ArrayList<Integer> splitInt(String str) {
4312 if (str == null) return null;
4313
Ali Utku Selen0a120182011-02-09 14:11:22 +01004314 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4315 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004316 ArrayList<Integer> substrings = new ArrayList<Integer>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01004317 for (String s : splitter) {
4318 substrings.add(Integer.parseInt(s));
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004319 }
4320 if (substrings.size() == 0) return null;
4321 return substrings;
4322 }
4323
Wu-cheng Li454630f2010-08-11 16:48:05 -07004324 private void splitInt(String str, int[] output) {
4325 if (str == null) return;
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004326
Ali Utku Selen0a120182011-02-09 14:11:22 +01004327 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4328 splitter.setString(str);
Wu-cheng Li454630f2010-08-11 16:48:05 -07004329 int index = 0;
Ali Utku Selen0a120182011-02-09 14:11:22 +01004330 for (String s : splitter) {
4331 output[index++] = Integer.parseInt(s);
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004332 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07004333 }
4334
4335 // Splits a comma delimited string to an ArrayList of Float.
4336 private void splitFloat(String str, float[] output) {
4337 if (str == null) return;
4338
Ali Utku Selen0a120182011-02-09 14:11:22 +01004339 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4340 splitter.setString(str);
Wu-cheng Li454630f2010-08-11 16:48:05 -07004341 int index = 0;
Ali Utku Selen0a120182011-02-09 14:11:22 +01004342 for (String s : splitter) {
4343 output[index++] = Float.parseFloat(s);
Wu-cheng Li454630f2010-08-11 16:48:05 -07004344 }
Wu-cheng Lie339c5e2010-05-13 19:31:02 +08004345 }
4346
Wu-cheng Li24b326a2010-02-20 17:47:04 +08004347 // Returns the value of a float parameter.
4348 private float getFloat(String key, float defaultValue) {
4349 try {
4350 return Float.parseFloat(mMap.get(key));
4351 } catch (NumberFormatException ex) {
4352 return defaultValue;
4353 }
4354 }
4355
4356 // Returns the value of a integer parameter.
4357 private int getInt(String key, int defaultValue) {
4358 try {
4359 return Integer.parseInt(mMap.get(key));
4360 } catch (NumberFormatException ex) {
4361 return defaultValue;
4362 }
4363 }
4364
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004365 // Splits a comma delimited string to an ArrayList of Size.
4366 // Return null if the passing string is null or the size is 0.
4367 private ArrayList<Size> splitSize(String str) {
4368 if (str == null) return null;
4369
Ali Utku Selen0a120182011-02-09 14:11:22 +01004370 TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
4371 splitter.setString(str);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004372 ArrayList<Size> sizeList = new ArrayList<Size>();
Ali Utku Selen0a120182011-02-09 14:11:22 +01004373 for (String s : splitter) {
4374 Size size = strToSize(s);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08004375 if (size != null) sizeList.add(size);
4376 }
4377 if (sizeList.size() == 0) return null;
4378 return sizeList;
4379 }
4380
4381 // Parses a string (ex: "480x320") to Size object.
4382 // Return null if the passing string is null.
4383 private Size strToSize(String str) {
4384 if (str == null) return null;
4385
4386 int pos = str.indexOf('x');
4387 if (pos != -1) {
4388 String width = str.substring(0, pos);
4389 String height = str.substring(pos + 1);
4390 return new Size(Integer.parseInt(width),
4391 Integer.parseInt(height));
4392 }
4393 Log.e(TAG, "Invalid size parameter string=" + str);
4394 return null;
4395 }
Wu-cheng Li454630f2010-08-11 16:48:05 -07004396
4397 // Splits a comma delimited string to an ArrayList of int array.
4398 // Example string: "(10000,26623),(10000,30000)". Return null if the
4399 // passing string is null or the size is 0.
4400 private ArrayList<int[]> splitRange(String str) {
4401 if (str == null || str.charAt(0) != '('
4402 || str.charAt(str.length() - 1) != ')') {
4403 Log.e(TAG, "Invalid range list string=" + str);
4404 return null;
4405 }
4406
4407 ArrayList<int[]> rangeList = new ArrayList<int[]>();
4408 int endIndex, fromIndex = 1;
4409 do {
4410 int[] range = new int[2];
4411 endIndex = str.indexOf("),(", fromIndex);
4412 if (endIndex == -1) endIndex = str.length() - 1;
4413 splitInt(str.substring(fromIndex, endIndex), range);
4414 rangeList.add(range);
4415 fromIndex = endIndex + 3;
4416 } while (endIndex != str.length() - 1);
4417
4418 if (rangeList.size() == 0) return null;
4419 return rangeList;
4420 }
Wu-cheng Li30771b72011-04-02 06:19:46 +08004421
4422 // Splits a comma delimited string to an ArrayList of Area objects.
4423 // Example string: "(-10,-10,0,0,300),(0,0,10,10,700)". Return null if
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004424 // the passing string is null or the size is 0 or (0,0,0,0,0).
Mathew Inwood5132cc12018-08-08 15:50:55 +01004425 @UnsupportedAppUsage
Wu-cheng Li30771b72011-04-02 06:19:46 +08004426 private ArrayList<Area> splitArea(String str) {
4427 if (str == null || str.charAt(0) != '('
4428 || str.charAt(str.length() - 1) != ')') {
4429 Log.e(TAG, "Invalid area string=" + str);
4430 return null;
4431 }
4432
4433 ArrayList<Area> result = new ArrayList<Area>();
4434 int endIndex, fromIndex = 1;
4435 int[] array = new int[5];
4436 do {
4437 endIndex = str.indexOf("),(", fromIndex);
4438 if (endIndex == -1) endIndex = str.length() - 1;
4439 splitInt(str.substring(fromIndex, endIndex), array);
4440 Rect rect = new Rect(array[0], array[1], array[2], array[3]);
4441 result.add(new Area(rect, array[4]));
4442 fromIndex = endIndex + 3;
4443 } while (endIndex != str.length() - 1);
4444
4445 if (result.size() == 0) return null;
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004446
4447 if (result.size() == 1) {
Wu-cheng Libde61a52011-06-07 18:23:14 +08004448 Area area = result.get(0);
Wu-cheng Lif715bf92011-04-14 14:04:18 +08004449 Rect rect = area.rect;
4450 if (rect.left == 0 && rect.top == 0 && rect.right == 0
4451 && rect.bottom == 0 && area.weight == 0) {
4452 return null;
4453 }
4454 }
4455
Wu-cheng Li30771b72011-04-02 06:19:46 +08004456 return result;
4457 }
Wu-cheng Lib838d8d2011-11-17 20:12:23 +08004458
4459 private boolean same(String s1, String s2) {
4460 if (s1 == null && s2 == null) return true;
4461 if (s1 != null && s1.equals(s2)) return true;
4462 return false;
4463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 };
4465}