blob: abebdeb91489c7cddbc60c441c6d7ed4d066b6a0 [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
19import java.lang.ref.WeakReference;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +080020import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import java.util.HashMap;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +080022import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.StringTokenizer;
24import java.io.IOException;
25
26import android.util.Log;
27import android.view.Surface;
28import android.view.SurfaceHolder;
Mathias Agopiana696f5d2010-02-17 17:53:09 -080029import android.graphics.ImageFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33
34/**
35 * The Camera class is used to connect/disconnect with the camera service,
36 * set capture settings, start/stop preview, snap a picture, and retrieve
37 * frames for encoding for video.
38 * <p>There is no default constructor for this class. Use {@link #open()} to
39 * get a Camera object.</p>
Scott Maindf4578e2009-09-10 12:22:07 -070040 *
Wu-cheng Li7478ea62009-09-16 18:52:55 +080041 * <p>In order to use the device camera, you must declare the
42 * {@link android.Manifest.permission#CAMERA} permission in your Android
Scott Maindf4578e2009-09-10 12:22:07 -070043 * Manifest. Also be sure to include the
44 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
Wu-cheng Li7478ea62009-09-16 18:52:55 +080045 * manifest element in order to declare camera features used by your application.
46 * For example, if you use the camera and auto-focus feature, your Manifest
Scott Maindf4578e2009-09-10 12:22:07 -070047 * should include the following:</p>
48 * <pre> &lt;uses-permission android:name="android.permission.CAMERA" />
49 * &lt;uses-feature android:name="android.hardware.camera" />
50 * &lt;uses-feature android:name="android.hardware.camera.autofocus" /></pre>
51 *
52 * <p class="caution"><strong>Caution:</strong> Different Android-powered devices
53 * may have different hardware specifications, such as megapixel ratings and
54 * auto-focus capabilities. In order for your application to be compatible with
Wu-cheng Li7478ea62009-09-16 18:52:55 +080055 * more devices, you should not make assumptions about the device camera
Scott Maindf4578e2009-09-10 12:22:07 -070056 * specifications.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 */
58public class Camera {
59 private static final String TAG = "Camera";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +080060
Dave Sparksc62f9bd2009-06-26 13:33:32 -070061 // These match the enums in frameworks/base/include/ui/Camera.h
Benny Wongda83f462009-08-12 12:01:27 -050062 private static final int CAMERA_MSG_ERROR = 0x001;
63 private static final int CAMERA_MSG_SHUTTER = 0x002;
64 private static final int CAMERA_MSG_FOCUS = 0x004;
65 private static final int CAMERA_MSG_ZOOM = 0x008;
66 private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010;
67 private static final int CAMERA_MSG_VIDEO_FRAME = 0x020;
68 private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040;
69 private static final int CAMERA_MSG_RAW_IMAGE = 0x080;
70 private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100;
71 private static final int CAMERA_MSG_ALL_MSGS = 0x1FF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73 private int mNativeContext; // accessed by native methods
74 private EventHandler mEventHandler;
75 private ShutterCallback mShutterCallback;
76 private PictureCallback mRawImageCallback;
77 private PictureCallback mJpegCallback;
78 private PreviewCallback mPreviewCallback;
Dave Sparkse8b26e12009-07-14 10:35:40 -070079 private PictureCallback mPostviewCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 private AutoFocusCallback mAutoFocusCallback;
Dave Sparkse8b26e12009-07-14 10:35:40 -070081 private ZoomCallback mZoomCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 private ErrorCallback mErrorCallback;
83 private boolean mOneShot;
Andrew Harp94927df2009-10-20 01:47:05 -040084 private boolean mWithBuffer;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +080085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 /**
87 * Returns a new Camera object.
88 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +080089 public static Camera open() {
90 return new Camera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 }
92
93 Camera() {
94 mShutterCallback = null;
95 mRawImageCallback = null;
96 mJpegCallback = null;
97 mPreviewCallback = null;
Dave Sparkse8b26e12009-07-14 10:35:40 -070098 mPostviewCallback = null;
99 mZoomCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
101 Looper looper;
102 if ((looper = Looper.myLooper()) != null) {
103 mEventHandler = new EventHandler(this, looper);
104 } else if ((looper = Looper.getMainLooper()) != null) {
105 mEventHandler = new EventHandler(this, looper);
106 } else {
107 mEventHandler = null;
108 }
109
110 native_setup(new WeakReference<Camera>(this));
111 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800112
113 protected void finalize() {
114 native_release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 private native final void native_setup(Object camera_this);
118 private native final void native_release();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
121 /**
122 * Disconnects and releases the Camera object resources.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800123 * <p>It is recommended that you call this as soon as you're done with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 * Camera object.</p>
125 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800126 public final void release() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 native_release();
128 }
129
130 /**
131 * Reconnect to the camera after passing it to MediaRecorder. To save
132 * setup/teardown time, a client of Camera can pass an initialized Camera
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800133 * object to a MediaRecorder to use for video recording. Once the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * MediaRecorder is done with the Camera, this method can be used to
135 * re-establish a connection with the camera hardware. NOTE: The Camera
136 * object must first be unlocked by the process that owns it before it
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800137 * can be connected to another process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 *
139 * @throws IOException if the method fails.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 */
141 public native final void reconnect() throws IOException;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /**
144 * Lock the camera to prevent other processes from accessing it. To save
145 * setup/teardown time, a client of Camera can pass an initialized Camera
146 * object to another process. This method is used to re-lock the Camera
147 * object prevent other processes from accessing it. By default, the
148 * Camera object is locked. Locking it again from the same process will
149 * have no effect. Attempting to lock it from another process if it has
150 * not been unlocked will fail.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 *
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800152 * @throws RuntimeException if the method fails.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 */
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800154 public native final void lock();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800157 * Unlock the camera to allow another process to access it. To save
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 * setup/teardown time, a client of Camera can pass an initialized Camera
159 * object to another process. This method is used to unlock the Camera
160 * object before handing off the Camera object to the other process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 *
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800162 * @throws RuntimeException if the method fails.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 */
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800164 public native final void unlock();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 /**
167 * Sets the SurfaceHolder to be used for a picture preview. If the surface
168 * changed since the last call, the screen will blank. Nothing happens
169 * if the same surface is re-set.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800170 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 * @param holder the SurfaceHolder upon which to place the picture preview
172 * @throws IOException if the method fails.
173 */
174 public final void setPreviewDisplay(SurfaceHolder holder) throws IOException {
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800175 if (holder != null) {
176 setPreviewDisplay(holder.getSurface());
177 } else {
178 setPreviewDisplay((Surface)null);
179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 }
181
182 private native final void setPreviewDisplay(Surface surface);
183
184 /**
185 * Used to get a copy of each preview frame.
186 */
187 public interface PreviewCallback
188 {
189 /**
190 * The callback that delivers the preview frames.
191 *
Scott Maindf4578e2009-09-10 12:22:07 -0700192 * @param data The contents of the preview frame in the format defined
Mathias Agopiana696f5d2010-02-17 17:53:09 -0800193 * by {@link android.graphics.ImageFormat}, which can be queried
Scott Maindf4578e2009-09-10 12:22:07 -0700194 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
Scott Mainda0a56d2009-09-10 18:08:37 -0700195 * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800196 * is never called, the default will be the YCbCr_420_SP
197 * (NV21) format.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 * @param camera The Camera service object.
199 */
200 void onPreviewFrame(byte[] data, Camera camera);
201 };
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 /**
204 * Start drawing preview frames to the surface.
205 */
206 public native final void startPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 /**
209 * Stop drawing preview frames to the surface.
210 */
211 public native final void stopPreview();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 /**
214 * Return current preview state.
215 *
216 * FIXME: Unhide before release
217 * @hide
218 */
219 public native final boolean previewEnabled();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 /**
222 * Can be called at any time to instruct the camera to use a callback for
223 * each preview frame in addition to displaying it.
224 *
225 * @param cb A callback object that receives a copy of each preview frame.
226 * Pass null to stop receiving callbacks at any time.
227 */
228 public final void setPreviewCallback(PreviewCallback cb) {
229 mPreviewCallback = cb;
230 mOneShot = false;
Andrew Harp94927df2009-10-20 01:47:05 -0400231 mWithBuffer = false;
Dave Sparksa6118c62009-10-13 02:28:54 -0700232 // Always use one-shot mode. We fake camera preview mode by
233 // doing one-shot preview continuously.
Andrew Harp94927df2009-10-20 01:47:05 -0400234 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 }
236
237 /**
238 * Installs a callback to retrieve a single preview frame, after which the
239 * callback is cleared.
240 *
241 * @param cb A callback object that receives a copy of the preview frame.
242 */
243 public final void setOneShotPreviewCallback(PreviewCallback cb) {
Andrew Harp94927df2009-10-20 01:47:05 -0400244 mPreviewCallback = cb;
245 mOneShot = true;
246 mWithBuffer = false;
247 setHasPreviewCallback(cb != null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
249
Andrew Harp94927df2009-10-20 01:47:05 -0400250 private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer);
251
252 /**
253 * Installs a callback which will get called as long as there are buffers in the
254 * preview buffer queue, which minimizes dynamic allocation of preview buffers.
255 *
256 * Apps must call addCallbackBuffer to explicitly register the buffers to use, or no callbacks
257 * will be received. addCallbackBuffer may be safely called before or after
258 * a call to setPreviewCallbackWithBuffer with a non-null callback parameter.
259 *
260 * The buffer queue will be cleared upon any calls to setOneShotPreviewCallback,
261 * setPreviewCallback, or to this method with a null callback parameter.
262 *
263 * @param cb A callback object that receives a copy of the preview frame. A null value will clear the queue.
Andrew Harp94927df2009-10-20 01:47:05 -0400264 */
265 public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
266 mPreviewCallback = cb;
267 mOneShot = false;
268 mWithBuffer = true;
269 setHasPreviewCallback(cb != null, true);
270 }
271
272 /**
Wu-cheng Lic10275a2010-03-09 13:49:21 -0800273 * Adds a pre-allocated buffer to the callback buffer queue. Applications
274 * can add one or more buffers to the queue. When a preview frame arrives
275 * and there is still available buffer, buffer will be filled and it is
276 * removed from the queue. Then preview callback is invoked with the buffer.
277 * If a frame arrives and there is no buffer left, the frame is discarded.
278 * Applications should add the buffers back when they finish the processing.
279 *
Andrew Harp94927df2009-10-20 01:47:05 -0400280 * Preview width and height can be determined from getPreviewSize, and bitsPerPixel can be
Wu-cheng Li5b9bcda2010-03-07 14:59:28 -0800281 * found from {@link android.hardware.Camera.Parameters#getPreviewFormat()}
282 * and {@link android.graphics.ImageFormat#getBitsPerPixel(int)}.
Andrew Harp94927df2009-10-20 01:47:05 -0400283 *
284 * Alternatively, a buffer from a previous callback may be passed in or used
285 * to determine the size of new preview frame buffers.
286 *
287 * @param callbackBuffer The buffer to register. Size should be width * height * bitsPerPixel / 8.
Wu-cheng Li5b9bcda2010-03-07 14:59:28 -0800288 * @see #setPreviewCallbackWithBuffer(PreviewCallback)
Andrew Harp94927df2009-10-20 01:47:05 -0400289 */
290 public native final void addCallbackBuffer(byte[] callbackBuffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291
292 private class EventHandler extends Handler
293 {
294 private Camera mCamera;
295
296 public EventHandler(Camera c, Looper looper) {
297 super(looper);
298 mCamera = c;
299 }
300
301 @Override
302 public void handleMessage(Message msg) {
303 switch(msg.what) {
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700304 case CAMERA_MSG_SHUTTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 if (mShutterCallback != null) {
306 mShutterCallback.onShutter();
307 }
308 return;
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700309
310 case CAMERA_MSG_RAW_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700311 if (mRawImageCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 return;
315
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700316 case CAMERA_MSG_COMPRESSED_IMAGE:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700317 if (mJpegCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 return;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800321
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700322 case CAMERA_MSG_PREVIEW_FRAME:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 if (mPreviewCallback != null) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700324 PreviewCallback cb = mPreviewCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 if (mOneShot) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700326 // Clear the callback variable before the callback
327 // in case the app calls setPreviewCallback from
328 // the callback function
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 mPreviewCallback = null;
Andrew Harp94927df2009-10-20 01:47:05 -0400330 } else if (!mWithBuffer) {
Dave Sparksa6118c62009-10-13 02:28:54 -0700331 // We're faking the camera preview mode to prevent
332 // the app from being flooded with preview frames.
333 // Set to oneshot mode again.
Andrew Harp94927df2009-10-20 01:47:05 -0400334 setHasPreviewCallback(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 }
Dave Sparksa6118c62009-10-13 02:28:54 -0700336 cb.onPreviewFrame((byte[])msg.obj, mCamera);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
338 return;
339
Dave Sparkse8b26e12009-07-14 10:35:40 -0700340 case CAMERA_MSG_POSTVIEW_FRAME:
341 if (mPostviewCallback != null) {
342 mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera);
343 }
344 return;
345
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700346 case CAMERA_MSG_FOCUS:
Dave Sparkse8b26e12009-07-14 10:35:40 -0700347 if (mAutoFocusCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700349 }
350 return;
351
352 case CAMERA_MSG_ZOOM:
353 if (mZoomCallback != null) {
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700354 mZoomCallback.onZoomUpdate(msg.arg1, msg.arg2 != 0, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 return;
357
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700358 case CAMERA_MSG_ERROR :
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 Log.e(TAG, "Error " + msg.arg1);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700360 if (mErrorCallback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 mErrorCallback.onError(msg.arg1, mCamera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 return;
364
365 default:
366 Log.e(TAG, "Unknown message type " + msg.what);
367 return;
368 }
369 }
370 }
371
372 private static void postEventFromNative(Object camera_ref,
373 int what, int arg1, int arg2, Object obj)
374 {
375 Camera c = (Camera)((WeakReference)camera_ref).get();
376 if (c == null)
377 return;
378
379 if (c.mEventHandler != null) {
380 Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj);
381 c.mEventHandler.sendMessage(m);
382 }
383 }
384
385 /**
386 * Handles the callback for the camera auto focus.
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800387 * <p>Devices that do not support auto-focus will receive a "fake"
388 * callback to this interface. If your application needs auto-focus and
Scott Maindf4578e2009-09-10 12:22:07 -0700389 * should not be installed on devices <em>without</em> auto-focus, you must
390 * declare that your app uses the
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800391 * {@code android.hardware.camera.autofocus} feature, in the
Scott Maindf4578e2009-09-10 12:22:07 -0700392 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
393 * manifest element.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 */
395 public interface AutoFocusCallback
396 {
397 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800398 * Callback for the camera auto focus. If the camera does not support
399 * auto-focus and autoFocus is called, onAutoFocus will be called
400 * immediately with success.
401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 * @param success true if focus was successful, false if otherwise
403 * @param camera the Camera service object
404 */
405 void onAutoFocus(boolean success, Camera camera);
406 };
407
408 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800409 * Starts auto-focus function and registers a callback function to run when
Wu-cheng Li36322db2009-09-18 18:59:21 +0800410 * camera is focused. Only valid after startPreview() has been called.
411 * Applications should call {@link
412 * android.hardware.Camera.Parameters#getFocusMode()} to determine if this
413 * method should be called. If the camera does not support auto-focus, it is
414 * a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)}
415 * callback will be called immediately.
Scott Mainda0a56d2009-09-10 18:08:37 -0700416 * <p>If your application should not be installed
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800417 * on devices without auto-focus, you must declare that your application
418 * uses auto-focus with the
Scott Maindf4578e2009-09-10 12:22:07 -0700419 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
420 * manifest element.</p>
Wu-cheng Li068ef422009-09-27 13:19:36 -0700421 * <p>If the current flash mode is not
422 * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be
423 * fired during auto-focus depending on the driver.<p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +0800424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * @param cb the callback to run
426 */
427 public final void autoFocus(AutoFocusCallback cb)
428 {
429 mAutoFocusCallback = cb;
430 native_autoFocus();
431 }
432 private native final void native_autoFocus();
433
434 /**
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800435 * Cancels auto-focus function. If the auto-focus is still in progress,
436 * this function will cancel it. Whether the auto-focus is in progress
437 * or not, this function will return the focus position to the default.
438 * If the camera does not support auto-focus, this is a no-op.
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800439 */
440 public final void cancelAutoFocus()
441 {
442 mAutoFocusCallback = null;
443 native_cancelAutoFocus();
444 }
445 private native final void native_cancelAutoFocus();
446
447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * An interface which contains a callback for the shutter closing after taking a picture.
449 */
450 public interface ShutterCallback
451 {
452 /**
453 * Can be used to play a shutter sound as soon as the image has been captured, but before
454 * the data is available.
455 */
456 void onShutter();
457 }
458
459 /**
460 * Handles the callback for when a picture is taken.
461 */
462 public interface PictureCallback {
463 /**
464 * Callback for when a picture is taken.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800465 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 * @param data a byte array of the picture data
467 * @param camera the Camera service object
468 */
469 void onPictureTaken(byte[] data, Camera camera);
470 };
471
472 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800473 * Triggers an asynchronous image capture. The camera service will initiate
474 * a series of callbacks to the application as the image capture progresses.
475 * The shutter callback occurs after the image is captured. This can be used
476 * to trigger a sound to let the user know that image has been captured. The
477 * raw callback occurs when the raw image data is available (NOTE: the data
478 * may be null if the hardware does not have enough memory to make a copy).
479 * The jpeg callback occurs when the compressed image is available. If the
480 * application does not need a particular callback, a null can be passed
481 * instead of a callback method.
482 *
Wu-cheng Li40057ce2009-12-02 18:57:29 +0800483 * This method will stop the preview. Applications should not call {@link
484 * #stopPreview()} before this. After jpeg callback is received,
485 * applications can call {@link #startPreview()} to restart the preview.
486 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 * @param shutter callback after the image is captured, may be null
488 * @param raw callback with raw image data, may be null
489 * @param jpeg callback with jpeg image data, may be null
490 */
491 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
492 PictureCallback jpeg) {
Dave Sparkse8b26e12009-07-14 10:35:40 -0700493 takePicture(shutter, raw, null, jpeg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
495 private native final void native_takePicture();
496
Dave Sparkse8b26e12009-07-14 10:35:40 -0700497 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800498 * Triggers an asynchronous image capture. The camera service will initiate
499 * a series of callbacks to the application as the image capture progresses.
500 * The shutter callback occurs after the image is captured. This can be used
501 * to trigger a sound to let the user know that image has been captured. The
502 * raw callback occurs when the raw image data is available (NOTE: the data
503 * may be null if the hardware does not have enough memory to make a copy).
504 * The postview callback occurs when a scaled, fully processed postview
505 * image is available (NOTE: not all hardware supports this). The jpeg
506 * callback occurs when the compressed image is available. If the
507 * application does not need a particular callback, a null can be passed
508 * instead of a callback method.
Dave Sparkse8b26e12009-07-14 10:35:40 -0700509 *
Wu-cheng Li40057ce2009-12-02 18:57:29 +0800510 * This method will stop the preview. Applications should not call {@link
511 * #stopPreview()} before this. After jpeg callback is received,
512 * applications can call {@link #startPreview()} to restart the preview.
513 *
Dave Sparkse8b26e12009-07-14 10:35:40 -0700514 * @param shutter callback after the image is captured, may be null
515 * @param raw callback with raw image data, may be null
516 * @param postview callback with postview image data, may be null
517 * @param jpeg callback with jpeg image data, may be null
518 */
519 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
520 PictureCallback postview, PictureCallback jpeg) {
521 mShutterCallback = shutter;
522 mRawImageCallback = raw;
523 mPostviewCallback = postview;
524 mJpegCallback = jpeg;
525 native_takePicture();
526 }
527
528 /**
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700529 * Zooms to the requested value smoothly. Driver will generate {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -0800530 * ZoomCallback} for the zoom value and whether zoom is stopped at the
531 * time. For example, suppose the current zoom is 0 and startSmoothZoom is
532 * called with value 3. Three ZoomCallback will be generated with zoom value
533 * 1, 2, and 3. The applications can call {@link #stopSmoothZoom} to stop
534 * the zoom earlier. The applications should not call startSmoothZoom again
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800535 * or change the zoom value before zoom stops. If the passing zoom value
536 * equals to the current zoom value, no zoom callback will be generated.
537 * This method is supported if {@link
538 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true.
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700539 *
540 * @param value zoom value. The valid range is 0 to {@link
541 * android.hardware.Camera.Parameters#getMaxZoom}.
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800542 * @throws IllegalArgumentException if the zoom value is invalid.
543 * @throws RuntimeException if the method fails.
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700544 */
545 public native final void startSmoothZoom(int value);
546
547 /**
548 * Stops the smooth zoom. The applications should wait for the {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -0800549 * ZoomCallback} to know when the zoom is actually stopped. This method is
550 * supported if {@link
551 * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true.
Wu-cheng Li0ca25192010-03-29 16:21:12 +0800552 *
553 * @throws RuntimeException if the method fails.
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700554 */
555 public native final void stopSmoothZoom();
556
557 /**
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800558 * Set the display orientation. This affects the preview frames and the
559 * picture displayed after snapshot. This method is useful for portrait
560 * mode applications.
561 *
562 * This does not affect the order of byte array passed in
563 * {@link PreviewCallback#onPreviewFrame}. This method is not allowed to
564 * be called during preview.
565 *
566 * @param degrees the angle that the picture will be rotated clockwise.
Chih-Chung Change7bd22a2010-01-27 10:24:42 -0800567 * Valid values are 0, 90, 180, and 270. The starting
568 * position is 0 (landscape).
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800569 */
570 public native final void setDisplayOrientation(int degrees);
571
572 /**
Dave Sparkse8b26e12009-07-14 10:35:40 -0700573 * Handles the zoom callback.
Wu-cheng Li77153ee2009-09-29 16:04:21 -0700574 *
Dave Sparkse8b26e12009-07-14 10:35:40 -0700575 */
576 public interface ZoomCallback
577 {
578 /**
579 * Callback for zoom updates
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700580 *
581 * @param zoomValue the current zoom value. In smooth zoom mode, camera
582 * generates this callback for every new zoom value.
583 * @param stopped whether smooth zoom is stopped. If the value is true,
584 * this is the last zoom update for the application.
585 *
Dave Sparkse8b26e12009-07-14 10:35:40 -0700586 * @param camera the Camera service object
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -0800587 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -0700588 */
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700589 void onZoomUpdate(int zoomValue, boolean stopped, Camera camera);
Dave Sparkse8b26e12009-07-14 10:35:40 -0700590 };
591
592 /**
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700593 * Registers a callback to be invoked when the zoom value is updated by the
594 * camera driver during smooth zoom.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -0800595 *
Dave Sparkse8b26e12009-07-14 10:35:40 -0700596 * @param cb the callback to run
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -0800597 * @see #startSmoothZoom(int)
Dave Sparkse8b26e12009-07-14 10:35:40 -0700598 */
599 public final void setZoomCallback(ZoomCallback cb)
600 {
601 mZoomCallback = cb;
602 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800603
James Donga1b653d2009-07-02 10:04:20 -0700604 // These match the enum in include/ui/Camera.h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 /** Unspecified camerar error. @see #ErrorCallback */
606 public static final int CAMERA_ERROR_UNKNOWN = 1;
607 /** Media server died. In this case, the application must release the
608 * Camera object and instantiate a new one. @see #ErrorCallback */
609 public static final int CAMERA_ERROR_SERVER_DIED = 100;
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 /**
612 * Handles the camera error callback.
613 */
614 public interface ErrorCallback
615 {
616 /**
617 * Callback for camera errors.
618 * @param error error code:
619 * <ul>
620 * <li>{@link #CAMERA_ERROR_UNKNOWN}
621 * <li>{@link #CAMERA_ERROR_SERVER_DIED}
622 * </ul>
623 * @param camera the Camera service object
624 */
625 void onError(int error, Camera camera);
626 };
627
628 /**
629 * Registers a callback to be invoked when an error occurs.
630 * @param cb the callback to run
631 */
632 public final void setErrorCallback(ErrorCallback cb)
633 {
634 mErrorCallback = cb;
635 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 private native final void native_setParameters(String params);
638 private native final String native_getParameters();
639
640 /**
641 * Sets the Parameters for pictures from this Camera service.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800642 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * @param params the Parameters to use for this Camera service
644 */
645 public void setParameters(Parameters params) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 native_setParameters(params.flatten());
647 }
648
649 /**
650 * Returns the picture Parameters for this Camera service.
651 */
652 public Parameters getParameters() {
653 Parameters p = new Parameters();
654 String s = native_getParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 p.unflatten(s);
656 return p;
657 }
658
659 /**
660 * Handles the picture size (dimensions).
661 */
662 public class Size {
663 /**
664 * Sets the dimensions for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800665 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 * @param w the photo width (pixels)
667 * @param h the photo height (pixels)
668 */
669 public Size(int w, int h) {
670 width = w;
671 height = h;
672 }
Wu-cheng Li4c4300c2010-01-23 15:45:39 +0800673 /**
674 * Compares {@code obj} to this size.
675 *
676 * @param obj the object to compare this size with.
677 * @return {@code true} if the width and height of {@code obj} is the
678 * same as those of this size. {@code false} otherwise.
679 */
680 @Override
681 public boolean equals(Object obj) {
682 if (!(obj instanceof Size)) {
683 return false;
684 }
685 Size s = (Size) obj;
686 return width == s.width && height == s.height;
687 }
688 @Override
689 public int hashCode() {
690 return width * 32713 + height;
691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 /** width of the picture */
693 public int width;
694 /** height of the picture */
695 public int height;
696 };
697
698 /**
699 * Handles the parameters for pictures created by a Camera service.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800700 *
701 * <p>To make camera parameters take effect, applications have to call
702 * Camera.setParameters. For example, after setWhiteBalance is called, white
703 * balance is not changed until Camera.setParameters() is called.
704 *
705 * <p>Different devices may have different camera capabilities, such as
706 * picture size or flash modes. The application should query the camera
707 * capabilities before setting parameters. For example, the application
708 * should call getSupportedColorEffects before calling setEffect. If the
709 * camera does not support color effects, getSupportedColorEffects will
710 * return null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 */
712 public class Parameters {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800713 // Parameter keys to communicate with the camera driver.
714 private static final String KEY_PREVIEW_SIZE = "preview-size";
715 private static final String KEY_PREVIEW_FORMAT = "preview-format";
716 private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate";
717 private static final String KEY_PICTURE_SIZE = "picture-size";
718 private static final String KEY_PICTURE_FORMAT = "picture-format";
Wu-cheng Li4c4300c2010-01-23 15:45:39 +0800719 private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800720 private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width";
721 private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height";
722 private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality";
723 private static final String KEY_JPEG_QUALITY = "jpeg-quality";
724 private static final String KEY_ROTATION = "rotation";
725 private static final String KEY_GPS_LATITUDE = "gps-latitude";
726 private static final String KEY_GPS_LONGITUDE = "gps-longitude";
727 private static final String KEY_GPS_ALTITUDE = "gps-altitude";
728 private static final String KEY_GPS_TIMESTAMP = "gps-timestamp";
Ray Chen055c9862010-02-23 10:45:42 +0800729 private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800730 private static final String KEY_WHITE_BALANCE = "whitebalance";
731 private static final String KEY_EFFECT = "effect";
732 private static final String KEY_ANTIBANDING = "antibanding";
733 private static final String KEY_SCENE_MODE = "scene-mode";
734 private static final String KEY_FLASH_MODE = "flash-mode";
Wu-cheng Li36322db2009-09-18 18:59:21 +0800735 private static final String KEY_FOCUS_MODE = "focus-mode";
Wu-cheng Li6c8d2762010-01-27 22:55:14 +0800736 private static final String KEY_FOCAL_LENGTH = "focal-length";
737 private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle";
738 private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle";
Wu-cheng Liff723b62010-02-09 13:38:19 +0800739 private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation";
Wu-cheng Li24b326a2010-02-20 17:47:04 +0800740 private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation";
741 private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation";
742 private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step";
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -0800743 private static final String KEY_ZOOM = "zoom";
744 private static final String KEY_MAX_ZOOM = "max-zoom";
745 private static final String KEY_ZOOM_RATIOS = "zoom-ratios";
746 private static final String KEY_ZOOM_SUPPORTED = "zoom-supported";
747 private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800748 // Parameter key suffix for supported values.
749 private static final String SUPPORTED_VALUES_SUFFIX = "-values";
750
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -0800751 private static final String TRUE = "true";
752
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800753 // Values for white balance settings.
754 public static final String WHITE_BALANCE_AUTO = "auto";
755 public static final String WHITE_BALANCE_INCANDESCENT = "incandescent";
756 public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent";
757 public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent";
758 public static final String WHITE_BALANCE_DAYLIGHT = "daylight";
759 public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight";
760 public static final String WHITE_BALANCE_TWILIGHT = "twilight";
761 public static final String WHITE_BALANCE_SHADE = "shade";
762
763 // Values for color effect settings.
764 public static final String EFFECT_NONE = "none";
765 public static final String EFFECT_MONO = "mono";
766 public static final String EFFECT_NEGATIVE = "negative";
767 public static final String EFFECT_SOLARIZE = "solarize";
768 public static final String EFFECT_SEPIA = "sepia";
769 public static final String EFFECT_POSTERIZE = "posterize";
770 public static final String EFFECT_WHITEBOARD = "whiteboard";
771 public static final String EFFECT_BLACKBOARD = "blackboard";
772 public static final String EFFECT_AQUA = "aqua";
773
774 // Values for antibanding settings.
775 public static final String ANTIBANDING_AUTO = "auto";
776 public static final String ANTIBANDING_50HZ = "50hz";
777 public static final String ANTIBANDING_60HZ = "60hz";
778 public static final String ANTIBANDING_OFF = "off";
779
780 // Values for flash mode settings.
781 /**
782 * Flash will not be fired.
783 */
784 public static final String FLASH_MODE_OFF = "off";
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700785
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800786 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -0700787 * Flash will be fired automatically when required. The flash may be fired
788 * during preview, auto-focus, or snapshot depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800789 */
790 public static final String FLASH_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700791
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800792 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -0700793 * Flash will always be fired during snapshot. The flash may also be
794 * fired during preview or auto-focus depending on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800795 */
796 public static final String FLASH_MODE_ON = "on";
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700797
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800798 /**
799 * Flash will be fired in red-eye reduction mode.
800 */
801 public static final String FLASH_MODE_RED_EYE = "red-eye";
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700802
Wu-cheng Li36322db2009-09-18 18:59:21 +0800803 /**
Wu-cheng Li068ef422009-09-27 13:19:36 -0700804 * Constant emission of light during preview, auto-focus and snapshot.
805 * This can also be used for video recording.
Wu-cheng Li36322db2009-09-18 18:59:21 +0800806 */
Wu-cheng Li068ef422009-09-27 13:19:36 -0700807 public static final String FLASH_MODE_TORCH = "torch";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800808
809 // Values for scene mode settings.
810 public static final String SCENE_MODE_AUTO = "auto";
811 public static final String SCENE_MODE_ACTION = "action";
812 public static final String SCENE_MODE_PORTRAIT = "portrait";
813 public static final String SCENE_MODE_LANDSCAPE = "landscape";
814 public static final String SCENE_MODE_NIGHT = "night";
815 public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait";
816 public static final String SCENE_MODE_THEATRE = "theatre";
817 public static final String SCENE_MODE_BEACH = "beach";
818 public static final String SCENE_MODE_SNOW = "snow";
819 public static final String SCENE_MODE_SUNSET = "sunset";
820 public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto";
821 public static final String SCENE_MODE_FIREWORKS = "fireworks";
822 public static final String SCENE_MODE_SPORTS = "sports";
823 public static final String SCENE_MODE_PARTY = "party";
824 public static final String SCENE_MODE_CANDLELIGHT = "candlelight";
825
Wu-cheng Li36322db2009-09-18 18:59:21 +0800826 // Values for focus mode settings.
827 /**
828 * Auto-focus mode.
829 */
830 public static final String FOCUS_MODE_AUTO = "auto";
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700831
Wu-cheng Li36322db2009-09-18 18:59:21 +0800832 /**
833 * Focus is set at infinity. Applications should not call
834 * {@link #autoFocus(AutoFocusCallback)} in this mode.
835 */
836 public static final String FOCUS_MODE_INFINITY = "infinity";
837 public static final String FOCUS_MODE_MACRO = "macro";
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700838
Wu-cheng Li36322db2009-09-18 18:59:21 +0800839 /**
840 * Focus is fixed. The camera is always in this mode if the focus is not
841 * adjustable. If the camera has auto-focus, this mode can fix the
842 * focus, which is usually at hyperfocal distance. Applications should
843 * not call {@link #autoFocus(AutoFocusCallback)} in this mode.
844 */
845 public static final String FOCUS_MODE_FIXED = "fixed";
846
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800847 // Formats for setPreviewFormat and setPictureFormat.
848 private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp";
849 private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp";
Chih-Chung Changeb68c462009-09-18 18:37:44 +0800850 private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv";
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800851 private static final String PIXEL_FORMAT_RGB565 = "rgb565";
852 private static final String PIXEL_FORMAT_JPEG = "jpeg";
853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 private HashMap<String, String> mMap;
855
856 private Parameters() {
857 mMap = new HashMap<String, String>();
858 }
859
860 /**
861 * Writes the current Parameters to the log.
862 * @hide
863 * @deprecated
864 */
865 public void dump() {
866 Log.e(TAG, "dump: size=" + mMap.size());
867 for (String k : mMap.keySet()) {
868 Log.e(TAG, "dump: " + k + "=" + mMap.get(k));
869 }
870 }
871
872 /**
873 * Creates a single string with all the parameters set in
874 * this Parameters object.
875 * <p>The {@link #unflatten(String)} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 * @return a String with all values from this Parameters object, in
878 * semi-colon delimited key-value pairs
879 */
880 public String flatten() {
881 StringBuilder flattened = new StringBuilder();
882 for (String k : mMap.keySet()) {
883 flattened.append(k);
884 flattened.append("=");
885 flattened.append(mMap.get(k));
886 flattened.append(";");
887 }
888 // chop off the extra semicolon at the end
889 flattened.deleteCharAt(flattened.length()-1);
890 return flattened.toString();
891 }
892
893 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800894 * Takes a flattened string of parameters and adds each one to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 * this Parameters object.
896 * <p>The {@link #flatten()} method does the reverse.</p>
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800897 *
898 * @param flattened a String of parameters (key-value paired) that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 * are semi-colon delimited
900 */
901 public void unflatten(String flattened) {
902 mMap.clear();
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 StringTokenizer tokenizer = new StringTokenizer(flattened, ";");
905 while (tokenizer.hasMoreElements()) {
906 String kv = tokenizer.nextToken();
907 int pos = kv.indexOf('=');
908 if (pos == -1) {
909 continue;
910 }
911 String k = kv.substring(0, pos);
912 String v = kv.substring(pos + 1);
913 mMap.put(k, v);
914 }
915 }
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 public void remove(String key) {
918 mMap.remove(key);
919 }
920
921 /**
922 * Sets a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800923 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 * @param key the key name for the parameter
925 * @param value the String value of the parameter
926 */
927 public void set(String key, String value) {
928 if (key.indexOf('=') != -1 || key.indexOf(';') != -1) {
929 Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ;)");
930 return;
931 }
932 if (value.indexOf('=') != -1 || value.indexOf(';') != -1) {
933 Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ;)");
934 return;
935 }
936
937 mMap.put(key, value);
938 }
939
940 /**
941 * Sets an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800942 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 * @param key the key name for the parameter
944 * @param value the int value of the parameter
945 */
946 public void set(String key, int value) {
947 mMap.put(key, Integer.toString(value));
948 }
949
950 /**
951 * Returns the value of a String parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800952 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 * @param key the key name for the parameter
954 * @return the String value of the parameter
955 */
956 public String get(String key) {
957 return mMap.get(key);
958 }
959
960 /**
961 * Returns the value of an integer parameter.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800962 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 * @param key the key name for the parameter
964 * @return the int value of the parameter
965 */
966 public int getInt(String key) {
967 return Integer.parseInt(mMap.get(key));
968 }
969
970 /**
971 * Sets the dimensions for preview pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800972 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 * @param width the width of the pictures, in pixels
974 * @param height the height of the pictures, in pixels
975 */
976 public void setPreviewSize(int width, int height) {
977 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800978 set(KEY_PREVIEW_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980
981 /**
982 * Returns the dimensions setting for preview pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800983 *
984 * @return a Size object with the height and width setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 * for the preview picture
986 */
987 public Size getPreviewSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800988 String pair = get(KEY_PREVIEW_SIZE);
989 return strToSize(pair);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991
992 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800993 * Gets the supported preview sizes.
994 *
Wu-cheng Li9c799382009-12-04 19:59:18 +0800995 * @return a List of Size object. This method will always return a list
996 * with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +0800997 */
998 public List<Size> getSupportedPreviewSizes() {
999 String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX);
1000 return splitSize(str);
1001 }
1002
1003 /**
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08001004 * Sets the dimensions for EXIF thumbnail in Jpeg picture. If
1005 * applications set both width and height to 0, EXIF will not contain
1006 * thumbnail.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 * @param width the width of the thumbnail, in pixels
1009 * @param height the height of the thumbnail, in pixels
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001011 public void setJpegThumbnailSize(int width, int height) {
1012 set(KEY_JPEG_THUMBNAIL_WIDTH, width);
1013 set(KEY_JPEG_THUMBNAIL_HEIGHT, height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
1015
1016 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001017 * Returns the dimensions for EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001019 * @return a Size object with the height and width setting for the EXIF
1020 * thumbnails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001022 public Size getJpegThumbnailSize() {
1023 return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH),
1024 getInt(KEY_JPEG_THUMBNAIL_HEIGHT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026
1027 /**
Wu-cheng Li4c4300c2010-01-23 15:45:39 +08001028 * Gets the supported jpeg thumbnail sizes.
1029 *
1030 * @return a List of Size object. This method will always return a list
1031 * with at least two elements. Size 0,0 (no thumbnail) is always
1032 * supported.
1033 */
1034 public List<Size> getSupportedJpegThumbnailSizes() {
1035 String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX);
1036 return splitSize(str);
1037 }
1038
1039 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001040 * Sets the quality of the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001042 * @param quality the JPEG quality of the EXIF thumbnail. The range is 1
1043 * to 100, with 100 being the best.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001045 public void setJpegThumbnailQuality(int quality) {
1046 set(KEY_JPEG_THUMBNAIL_QUALITY, quality);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048
1049 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001050 * Returns the quality setting for the EXIF thumbnail in Jpeg picture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 *
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001052 * @return the JPEG quality setting of the EXIF thumbnail.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 */
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001054 public int getJpegThumbnailQuality() {
1055 return getInt(KEY_JPEG_THUMBNAIL_QUALITY);
1056 }
1057
1058 /**
1059 * Sets Jpeg quality of captured picture.
1060 *
1061 * @param quality the JPEG quality of captured picture. The range is 1
1062 * to 100, with 100 being the best.
1063 */
1064 public void setJpegQuality(int quality) {
1065 set(KEY_JPEG_QUALITY, quality);
1066 }
1067
1068 /**
1069 * Returns the quality setting for the JPEG picture.
1070 *
1071 * @return the JPEG picture quality setting.
1072 */
1073 public int getJpegQuality() {
1074 return getInt(KEY_JPEG_QUALITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076
1077 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08001078 * Sets the rate at which preview frames are received. This is the
1079 * target frame rate. The actual frame rate depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001080 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 * @param fps the frame rate (frames per second)
1082 */
1083 public void setPreviewFrameRate(int fps) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001084 set(KEY_PREVIEW_FRAME_RATE, fps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 }
1086
1087 /**
Wu-cheng Lia18e9012010-02-10 13:05:29 +08001088 * Returns the setting for the rate at which preview frames are
1089 * received. This is the target frame rate. The actual frame rate
1090 * depends on the driver.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001091 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 * @return the frame rate setting (frames per second)
1093 */
1094 public int getPreviewFrameRate() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001095 return getInt(KEY_PREVIEW_FRAME_RATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097
1098 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001099 * Gets the supported preview frame rates.
1100 *
1101 * @return a List of Integer objects (preview frame rates). null if
1102 * preview frame rate setting is not supported.
1103 */
1104 public List<Integer> getSupportedPreviewFrameRates() {
1105 String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX);
1106 return splitInt(str);
1107 }
1108
1109 /**
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001110 * Sets the image format for preview pictures.
Scott Mainda0a56d2009-09-10 18:08:37 -07001111 * <p>If this is never called, the default format will be
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001112 * {@link android.graphics.ImageFormat#NV21}, which
Scott Maindf4578e2009-09-10 12:22:07 -07001113 * uses the NV21 encoding format.</p>
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001114 *
Scott Maindf4578e2009-09-10 12:22:07 -07001115 * @param pixel_format the desired preview picture format, defined
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001116 * by one of the {@link android.graphics.ImageFormat} constants.
1117 * (E.g., <var>ImageFormat.NV21</var> (default),
1118 * <var>ImageFormat.RGB_565</var>, or
1119 * <var>ImageFormat.JPEG</var>)
1120 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 */
1122 public void setPreviewFormat(int pixel_format) {
1123 String s = cameraFormatForPixelFormat(pixel_format);
1124 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001125 throw new IllegalArgumentException(
1126 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001129 set(KEY_PREVIEW_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
1131
1132 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001133 * Returns the image format for preview pictures got from
1134 * {@link PreviewCallback}.
Wu-cheng Li7478ea62009-09-16 18:52:55 +08001135 *
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001136 * @return the {@link android.graphics.ImageFormat} int representing
Scott Maindf4578e2009-09-10 12:22:07 -07001137 * the preview picture format.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 */
1139 public int getPreviewFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001140 return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT));
1141 }
1142
1143 /**
1144 * Gets the supported preview formats.
1145 *
Wu-cheng Li9c799382009-12-04 19:59:18 +08001146 * @return a List of Integer objects. This method will always return a
1147 * list with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001148 */
1149 public List<Integer> getSupportedPreviewFormats() {
1150 String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX);
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001151 ArrayList<Integer> formats = new ArrayList<Integer>();
1152 for (String s : split(str)) {
1153 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001154 if (f == ImageFormat.UNKNOWN) continue;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001155 formats.add(f);
1156 }
1157 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
1159
1160 /**
1161 * Sets the dimensions for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001162 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 * @param width the width for pictures, in pixels
1164 * @param height the height for pictures, in pixels
1165 */
1166 public void setPictureSize(int width, int height) {
1167 String v = Integer.toString(width) + "x" + Integer.toString(height);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001168 set(KEY_PICTURE_SIZE, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170
1171 /**
1172 * Returns the dimension setting for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001173 *
1174 * @return a Size object with the height and width setting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 * for pictures
1176 */
1177 public Size getPictureSize() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001178 String pair = get(KEY_PICTURE_SIZE);
1179 return strToSize(pair);
1180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001182 /**
1183 * Gets the supported picture sizes.
1184 *
Wu-cheng Li9c799382009-12-04 19:59:18 +08001185 * @return a List of Size objects. This method will always return a list
1186 * with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001187 */
1188 public List<Size> getSupportedPictureSizes() {
1189 String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX);
1190 return splitSize(str);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192
1193 /**
1194 * Sets the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001195 *
1196 * @param pixel_format the desired picture format
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001197 * (<var>ImageFormat.NV21</var>,
1198 * <var>ImageFormat.RGB_565</var>, or
1199 * <var>ImageFormat.JPEG</var>)
1200 * @see android.graphics.ImageFormat
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 */
1202 public void setPictureFormat(int pixel_format) {
1203 String s = cameraFormatForPixelFormat(pixel_format);
1204 if (s == null) {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001205 throw new IllegalArgumentException(
1206 "Invalid pixel_format=" + pixel_format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 }
1208
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001209 set(KEY_PICTURE_FORMAT, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211
1212 /**
1213 * Returns the image format for pictures.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001214 *
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001215 * @return the ImageFormat int representing the picture format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 */
1217 public int getPictureFormat() {
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001218 return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT));
1219 }
1220
1221 /**
1222 * Gets the supported picture formats.
1223 *
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001224 * @return a List of Integer objects (values are ImageFormat.XXX). This
Wu-cheng Li9c799382009-12-04 19:59:18 +08001225 * method will always return a list with at least one element.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001226 */
1227 public List<Integer> getSupportedPictureFormats() {
Wu-cheng Li9c799382009-12-04 19:59:18 +08001228 String str = get(KEY_PICTURE_FORMAT + SUPPORTED_VALUES_SUFFIX);
1229 ArrayList<Integer> formats = new ArrayList<Integer>();
1230 for (String s : split(str)) {
1231 int f = pixelFormatForCameraFormat(s);
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001232 if (f == ImageFormat.UNKNOWN) continue;
Wu-cheng Li9c799382009-12-04 19:59:18 +08001233 formats.add(f);
1234 }
1235 return formats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
1237
1238 private String cameraFormatForPixelFormat(int pixel_format) {
1239 switch(pixel_format) {
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001240 case ImageFormat.NV16: return PIXEL_FORMAT_YUV422SP;
1241 case ImageFormat.NV21: return PIXEL_FORMAT_YUV420SP;
1242 case ImageFormat.YUY2: return PIXEL_FORMAT_YUV422I;
1243 case ImageFormat.RGB_565: return PIXEL_FORMAT_RGB565;
1244 case ImageFormat.JPEG: return PIXEL_FORMAT_JPEG;
1245 default: return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 }
1247 }
1248
1249 private int pixelFormatForCameraFormat(String format) {
1250 if (format == null)
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001251 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001253 if (format.equals(PIXEL_FORMAT_YUV422SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001254 return ImageFormat.NV16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001256 if (format.equals(PIXEL_FORMAT_YUV420SP))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001257 return ImageFormat.NV21;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001259 if (format.equals(PIXEL_FORMAT_YUV422I))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001260 return ImageFormat.YUY2;
Chih-Chung Changeb68c462009-09-18 18:37:44 +08001261
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001262 if (format.equals(PIXEL_FORMAT_RGB565))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001263 return ImageFormat.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001265 if (format.equals(PIXEL_FORMAT_JPEG))
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001266 return ImageFormat.JPEG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267
Mathias Agopiana696f5d2010-02-17 17:53:09 -08001268 return ImageFormat.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 }
1270
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001271 /**
Wu-cheng Lie6bea602009-09-23 15:45:27 -07001272 * Sets the orientation of the device in degrees. For example, suppose
1273 * the natural position of the device is landscape. If the user takes a
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001274 * picture in landscape mode in 2048x1536 resolution, the rotation
1275 * should be set to 0. If the user rotates the phone 90 degrees
1276 * clockwise, the rotation should be set to 90. Applications can use
1277 * {@link android.view.OrientationEventListener} to set this parameter.
1278 *
Wu-cheng Lie6bea602009-09-23 15:45:27 -07001279 * The camera driver may set orientation in the EXIF header without
1280 * rotating the picture. Or the driver may rotate the picture and
1281 * the EXIF thumbnail. If the Jpeg picture is rotated, the orientation
1282 * in the EXIF header will be missing or 1 (row #0 is top and column #0
1283 * is left side).
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001284 *
1285 * @param rotation The orientation of the device in degrees. Rotation
1286 * can only be 0, 90, 180 or 270.
1287 * @throws IllegalArgumentException if rotation value is invalid.
1288 * @see android.view.OrientationEventListener
1289 */
1290 public void setRotation(int rotation) {
1291 if (rotation == 0 || rotation == 90 || rotation == 180
1292 || rotation == 270) {
1293 set(KEY_ROTATION, Integer.toString(rotation));
1294 } else {
1295 throw new IllegalArgumentException(
1296 "Invalid rotation=" + rotation);
1297 }
1298 }
1299
1300 /**
1301 * Sets GPS latitude coordinate. This will be stored in JPEG EXIF
1302 * header.
1303 *
1304 * @param latitude GPS latitude coordinate.
1305 */
1306 public void setGpsLatitude(double latitude) {
1307 set(KEY_GPS_LATITUDE, Double.toString(latitude));
1308 }
1309
1310 /**
1311 * Sets GPS longitude coordinate. This will be stored in JPEG EXIF
1312 * header.
1313 *
1314 * @param longitude GPS longitude coordinate.
1315 */
1316 public void setGpsLongitude(double longitude) {
1317 set(KEY_GPS_LONGITUDE, Double.toString(longitude));
1318 }
1319
1320 /**
1321 * Sets GPS altitude. This will be stored in JPEG EXIF header.
1322 *
1323 * @param altitude GPS altitude in meters.
1324 */
1325 public void setGpsAltitude(double altitude) {
1326 set(KEY_GPS_ALTITUDE, Double.toString(altitude));
1327 }
1328
1329 /**
1330 * Sets GPS timestamp. This will be stored in JPEG EXIF header.
1331 *
1332 * @param timestamp GPS timestamp (UTC in seconds since January 1,
1333 * 1970).
1334 */
1335 public void setGpsTimestamp(long timestamp) {
1336 set(KEY_GPS_TIMESTAMP, Long.toString(timestamp));
1337 }
1338
1339 /**
Ray Chene2083772010-03-10 15:02:49 -08001340 * Sets GPS processing method. It will store up to 32 characters
Ray Chen055c9862010-02-23 10:45:42 +08001341 * in JPEG EXIF header.
1342 *
1343 * @param processing_method The processing method to get this location.
1344 */
1345 public void setGpsProcessingMethod(String processing_method) {
1346 set(KEY_GPS_PROCESSING_METHOD, processing_method);
1347 }
1348
1349 /**
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001350 * Removes GPS latitude, longitude, altitude, and timestamp from the
1351 * parameters.
1352 */
1353 public void removeGpsData() {
1354 remove(KEY_GPS_LATITUDE);
1355 remove(KEY_GPS_LONGITUDE);
1356 remove(KEY_GPS_ALTITUDE);
1357 remove(KEY_GPS_TIMESTAMP);
Ray Chen055c9862010-02-23 10:45:42 +08001358 remove(KEY_GPS_PROCESSING_METHOD);
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001359 }
1360
1361 /**
1362 * Gets the current white balance setting.
1363 *
1364 * @return one of WHITE_BALANCE_XXX string constant. null if white
1365 * balance setting is not supported.
1366 */
1367 public String getWhiteBalance() {
1368 return get(KEY_WHITE_BALANCE);
1369 }
1370
1371 /**
1372 * Sets the white balance.
1373 *
1374 * @param value WHITE_BALANCE_XXX string constant.
1375 */
1376 public void setWhiteBalance(String value) {
1377 set(KEY_WHITE_BALANCE, value);
1378 }
1379
1380 /**
1381 * Gets the supported white balance.
1382 *
1383 * @return a List of WHITE_BALANCE_XXX string constants. null if white
1384 * balance setting is not supported.
1385 */
1386 public List<String> getSupportedWhiteBalance() {
1387 String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX);
1388 return split(str);
1389 }
1390
1391 /**
1392 * Gets the current color effect setting.
1393 *
1394 * @return one of EFFECT_XXX string constant. null if color effect
1395 * setting is not supported.
1396 */
1397 public String getColorEffect() {
1398 return get(KEY_EFFECT);
1399 }
1400
1401 /**
1402 * Sets the current color effect setting.
1403 *
1404 * @param value EFFECT_XXX string constants.
1405 */
1406 public void setColorEffect(String value) {
1407 set(KEY_EFFECT, value);
1408 }
1409
1410 /**
1411 * Gets the supported color effects.
1412 *
1413 * @return a List of EFFECT_XXX string constants. null if color effect
1414 * setting is not supported.
1415 */
1416 public List<String> getSupportedColorEffects() {
1417 String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX);
1418 return split(str);
1419 }
1420
1421
1422 /**
1423 * Gets the current antibanding setting.
1424 *
1425 * @return one of ANTIBANDING_XXX string constant. null if antibanding
1426 * setting is not supported.
1427 */
1428 public String getAntibanding() {
1429 return get(KEY_ANTIBANDING);
1430 }
1431
1432 /**
1433 * Sets the antibanding.
1434 *
1435 * @param antibanding ANTIBANDING_XXX string constant.
1436 */
1437 public void setAntibanding(String antibanding) {
1438 set(KEY_ANTIBANDING, antibanding);
1439 }
1440
1441 /**
1442 * Gets the supported antibanding values.
1443 *
1444 * @return a List of ANTIBANDING_XXX string constants. null if
1445 * antibanding setting is not supported.
1446 */
1447 public List<String> getSupportedAntibanding() {
1448 String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX);
1449 return split(str);
1450 }
1451
1452 /**
1453 * Gets the current scene mode setting.
1454 *
1455 * @return one of SCENE_MODE_XXX string constant. null if scene mode
1456 * setting is not supported.
1457 */
1458 public String getSceneMode() {
1459 return get(KEY_SCENE_MODE);
1460 }
1461
1462 /**
Wu-cheng Li2988ab72009-09-30 17:08:19 -07001463 * Sets the scene mode. Other parameters may be changed after changing
1464 * scene mode. For example, flash and supported flash mode may be
1465 * changed to "off" in night scene mode. After setting scene mode,
1466 * applications should call getParameters to know if some parameters are
1467 * changed.
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001468 *
1469 * @param value SCENE_MODE_XXX string constants.
1470 */
1471 public void setSceneMode(String value) {
1472 set(KEY_SCENE_MODE, value);
1473 }
1474
1475 /**
1476 * Gets the supported scene modes.
1477 *
1478 * @return a List of SCENE_MODE_XXX string constant. null if scene mode
1479 * setting is not supported.
1480 */
1481 public List<String> getSupportedSceneModes() {
1482 String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX);
1483 return split(str);
1484 }
1485
1486 /**
1487 * Gets the current flash mode setting.
1488 *
1489 * @return one of FLASH_MODE_XXX string constant. null if flash mode
1490 * setting is not supported.
1491 */
1492 public String getFlashMode() {
1493 return get(KEY_FLASH_MODE);
1494 }
1495
1496 /**
1497 * Sets the flash mode.
1498 *
1499 * @param value FLASH_MODE_XXX string constants.
1500 */
1501 public void setFlashMode(String value) {
1502 set(KEY_FLASH_MODE, value);
1503 }
1504
1505 /**
1506 * Gets the supported flash modes.
1507 *
1508 * @return a List of FLASH_MODE_XXX string constants. null if flash mode
1509 * setting is not supported.
1510 */
1511 public List<String> getSupportedFlashModes() {
1512 String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX);
1513 return split(str);
1514 }
1515
Wu-cheng Li36322db2009-09-18 18:59:21 +08001516 /**
1517 * Gets the current focus mode setting.
1518 *
1519 * @return one of FOCUS_MODE_XXX string constant. If the camera does not
1520 * support auto-focus, this should return {@link
1521 * #FOCUS_MODE_FIXED}. If the focus mode is not FOCUS_MODE_FIXED
1522 * or {@link #FOCUS_MODE_INFINITY}, applications should call
1523 * {@link #autoFocus(AutoFocusCallback)} to start the focus.
1524 */
1525 public String getFocusMode() {
1526 return get(KEY_FOCUS_MODE);
1527 }
1528
1529 /**
1530 * Sets the focus mode.
1531 *
1532 * @param value FOCUS_MODE_XXX string constants.
1533 */
1534 public void setFocusMode(String value) {
1535 set(KEY_FOCUS_MODE, value);
1536 }
1537
1538 /**
1539 * Gets the supported focus modes.
1540 *
Wu-cheng Li9c799382009-12-04 19:59:18 +08001541 * @return a List of FOCUS_MODE_XXX string constants. This method will
1542 * always return a list with at least one element.
Wu-cheng Li36322db2009-09-18 18:59:21 +08001543 */
1544 public List<String> getSupportedFocusModes() {
1545 String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX);
1546 return split(str);
1547 }
1548
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001549 /**
Wu-cheng Li6c8d2762010-01-27 22:55:14 +08001550 * Gets the focal length (in millimeter) of the camera.
1551 *
1552 * @return the focal length. This method will always return a valid
1553 * value.
1554 */
1555 public float getFocalLength() {
1556 return Float.parseFloat(get(KEY_FOCAL_LENGTH));
1557 }
1558
1559 /**
1560 * Gets the horizontal angle of view in degrees.
1561 *
1562 * @return horizontal angle of view. This method will always return a
1563 * valid value.
1564 */
1565 public float getHorizontalViewAngle() {
1566 return Float.parseFloat(get(KEY_HORIZONTAL_VIEW_ANGLE));
1567 }
1568
1569 /**
1570 * Gets the vertical angle of view in degrees.
1571 *
1572 * @return vertical angle of view. This method will always return a
1573 * valid value.
1574 */
1575 public float getVerticalViewAngle() {
1576 return Float.parseFloat(get(KEY_VERTICAL_VIEW_ANGLE));
1577 }
1578
1579 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001580 * Gets the current exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08001581 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001582 * @return current exposure compensation index. The range is {@link
1583 * #getMinExposureCompensation} to {@link
1584 * #getMaxExposureCompensation}. 0 means exposure is not
1585 * adjusted.
Wu-cheng Liff723b62010-02-09 13:38:19 +08001586 */
1587 public int getExposureCompensation() {
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001588 return getInt(KEY_EXPOSURE_COMPENSATION, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08001589 }
1590
1591 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001592 * Sets the exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08001593 *
Wu-cheng Li0402e7d2010-02-26 15:04:55 +08001594 * @param value exposure compensation index. The valid value range is
1595 * from {@link #getMinExposureCompensation} (inclusive) to {@link
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001596 * #getMaxExposureCompensation} (inclusive). 0 means exposure is
1597 * not adjusted. Application should call
1598 * getMinExposureCompensation and getMaxExposureCompensation to
1599 * know if exposure compensation is supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08001600 */
1601 public void setExposureCompensation(int value) {
1602 set(KEY_EXPOSURE_COMPENSATION, value);
1603 }
1604
1605 /**
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001606 * Gets the maximum exposure compensation index.
Wu-cheng Liff723b62010-02-09 13:38:19 +08001607 *
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001608 * @return maximum exposure compensation index (>=0). If both this
1609 * method and {@link #getMinExposureCompensation} return 0,
1610 * exposure compensation is not supported.
Wu-cheng Liff723b62010-02-09 13:38:19 +08001611 */
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001612 public int getMaxExposureCompensation() {
1613 return getInt(KEY_MAX_EXPOSURE_COMPENSATION, 0);
1614 }
1615
1616 /**
1617 * Gets the minimum exposure compensation index.
1618 *
1619 * @return minimum exposure compensation index (<=0). If both this
1620 * method and {@link #getMaxExposureCompensation} return 0,
1621 * exposure compensation is not supported.
1622 */
1623 public int getMinExposureCompensation() {
1624 return getInt(KEY_MIN_EXPOSURE_COMPENSATION, 0);
1625 }
1626
1627 /**
1628 * Gets the exposure compensation step.
1629 *
1630 * @return exposure compensation step. Applications can get EV by
1631 * multiplying the exposure compensation index and step. Ex: if
1632 * exposure compensation index is -6 and step is 0.333333333, EV
1633 * is -2.
1634 */
1635 public float getExposureCompensationStep() {
1636 return getFloat(KEY_EXPOSURE_COMPENSATION_STEP, 0);
Wu-cheng Liff723b62010-02-09 13:38:19 +08001637 }
1638
1639 /**
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001640 * Gets current zoom value. This also works when smooth zoom is in
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001641 * progress. Applications should check {@link #isZoomSupported} before
1642 * using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001643 *
1644 * @return the current zoom value. The range is 0 to {@link
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001645 * #getMaxZoom}. 0 means the camera is not zoomed.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001646 */
1647 public int getZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001648 return getInt(KEY_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001649 }
1650
1651 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001652 * Sets current zoom value. If the camera is zoomed (value > 0), the
1653 * actual picture size may be smaller than picture size setting.
1654 * Applications can check the actual picture size after picture is
1655 * returned from {@link PictureCallback}. The preview size remains the
1656 * same in zoom. Applications should check {@link #isZoomSupported}
1657 * before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001658 *
1659 * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001660 */
1661 public void setZoom(int value) {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001662 set(KEY_ZOOM, value);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001663 }
1664
1665 /**
1666 * Returns true if zoom is supported. Applications should call this
1667 * before using other zoom methods.
1668 *
1669 * @return true if zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001670 */
1671 public boolean isZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001672 String str = get(KEY_ZOOM_SUPPORTED);
1673 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001674 }
1675
1676 /**
1677 * Gets the maximum zoom value allowed for snapshot. This is the maximum
1678 * value that applications can set to {@link #setZoom(int)}.
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001679 * Applications should call {@link #isZoomSupported} before using this
1680 * method. This value may change in different preview size. Applications
1681 * should call this again after setting preview size.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001682 *
1683 * @return the maximum zoom value supported by the camera.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001684 */
1685 public int getMaxZoom() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001686 return getInt(KEY_MAX_ZOOM, 0);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001687 }
1688
1689 /**
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001690 * Gets the zoom ratios of all zoom values. Applications should check
1691 * {@link #isZoomSupported} before using this method.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001692 *
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001693 * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is
1694 * returned as 320. The number of elements is {@link
1695 * #getMaxZoom} + 1. The list is sorted from small to large. The
1696 * first element is always 100. The last element is the zoom
1697 * ratio of the maximum zoom value.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001698 */
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001699 public List<Integer> getZoomRatios() {
1700 return splitInt(get(KEY_ZOOM_RATIOS));
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001701 }
1702
1703 /**
1704 * Returns true if smooth zoom is supported. Applications should call
1705 * this before using other smooth zoom methods.
1706 *
1707 * @return true if smooth zoom is supported.
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001708 */
1709 public boolean isSmoothZoomSupported() {
Wu-cheng Li8cbb8f52010-02-28 23:19:55 -08001710 String str = get(KEY_SMOOTH_ZOOM_SUPPORTED);
1711 return TRUE.equals(str);
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001712 }
1713
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001714 // Splits a comma delimited string to an ArrayList of String.
1715 // Return null if the passing string is null or the size is 0.
1716 private ArrayList<String> split(String str) {
1717 if (str == null) return null;
1718
1719 // Use StringTokenizer because it is faster than split.
1720 StringTokenizer tokenizer = new StringTokenizer(str, ",");
1721 ArrayList<String> substrings = new ArrayList<String>();
1722 while (tokenizer.hasMoreElements()) {
1723 substrings.add(tokenizer.nextToken());
1724 }
1725 return substrings;
1726 }
1727
1728 // Splits a comma delimited string to an ArrayList of Integer.
1729 // Return null if the passing string is null or the size is 0.
1730 private ArrayList<Integer> splitInt(String str) {
1731 if (str == null) return null;
1732
1733 StringTokenizer tokenizer = new StringTokenizer(str, ",");
1734 ArrayList<Integer> substrings = new ArrayList<Integer>();
1735 while (tokenizer.hasMoreElements()) {
1736 String token = tokenizer.nextToken();
1737 substrings.add(Integer.parseInt(token));
1738 }
1739 if (substrings.size() == 0) return null;
1740 return substrings;
1741 }
1742
Wu-cheng Li24b326a2010-02-20 17:47:04 +08001743 // Returns the value of a float parameter.
1744 private float getFloat(String key, float defaultValue) {
1745 try {
1746 return Float.parseFloat(mMap.get(key));
1747 } catch (NumberFormatException ex) {
1748 return defaultValue;
1749 }
1750 }
1751
1752 // Returns the value of a integer parameter.
1753 private int getInt(String key, int defaultValue) {
1754 try {
1755 return Integer.parseInt(mMap.get(key));
1756 } catch (NumberFormatException ex) {
1757 return defaultValue;
1758 }
1759 }
1760
Wu-cheng Li9b6a8ab2009-08-17 18:19:25 +08001761 // Splits a comma delimited string to an ArrayList of Size.
1762 // Return null if the passing string is null or the size is 0.
1763 private ArrayList<Size> splitSize(String str) {
1764 if (str == null) return null;
1765
1766 StringTokenizer tokenizer = new StringTokenizer(str, ",");
1767 ArrayList<Size> sizeList = new ArrayList<Size>();
1768 while (tokenizer.hasMoreElements()) {
1769 Size size = strToSize(tokenizer.nextToken());
1770 if (size != null) sizeList.add(size);
1771 }
1772 if (sizeList.size() == 0) return null;
1773 return sizeList;
1774 }
1775
1776 // Parses a string (ex: "480x320") to Size object.
1777 // Return null if the passing string is null.
1778 private Size strToSize(String str) {
1779 if (str == null) return null;
1780
1781 int pos = str.indexOf('x');
1782 if (pos != -1) {
1783 String width = str.substring(0, pos);
1784 String height = str.substring(pos + 1);
1785 return new Size(Integer.parseInt(width),
1786 Integer.parseInt(height));
1787 }
1788 Log.e(TAG, "Invalid size parameter string=" + str);
1789 return null;
1790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 };
1792}