Camera.java revision e6bea600fe5600017a4824adb14752a5b915d164
1/*
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;
20import java.util.ArrayList;
21import java.util.HashMap;
22import java.util.List;
23import java.util.StringTokenizer;
24import java.io.IOException;
25
26import android.util.Log;
27import android.view.Surface;
28import android.view.SurfaceHolder;
29import android.graphics.PixelFormat;
30import 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>
40 *
41 * <p>In order to use the device camera, you must declare the
42 * {@link android.Manifest.permission#CAMERA} permission in your Android
43 * Manifest. Also be sure to include the
44 * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
45 * 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
47 * 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
55 * more devices, you should not make assumptions about the device camera
56 * specifications.</p>
57 */
58public class Camera {
59    private static final String TAG = "Camera";
60
61    // These match the enums in frameworks/base/include/ui/Camera.h
62    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;
72
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;
79    private PictureCallback mPostviewCallback;
80    private AutoFocusCallback mAutoFocusCallback;
81    private ZoomCallback mZoomCallback;
82    private ErrorCallback mErrorCallback;
83    private boolean mOneShot;
84
85    /**
86     * Returns a new Camera object.
87     */
88    public static Camera open() {
89        return new Camera();
90    }
91
92    Camera() {
93        mShutterCallback = null;
94        mRawImageCallback = null;
95        mJpegCallback = null;
96        mPreviewCallback = null;
97        mPostviewCallback = null;
98        mZoomCallback = null;
99
100        Looper looper;
101        if ((looper = Looper.myLooper()) != null) {
102            mEventHandler = new EventHandler(this, looper);
103        } else if ((looper = Looper.getMainLooper()) != null) {
104            mEventHandler = new EventHandler(this, looper);
105        } else {
106            mEventHandler = null;
107        }
108
109        native_setup(new WeakReference<Camera>(this));
110    }
111
112    protected void finalize() {
113        native_release();
114    }
115
116    private native final void native_setup(Object camera_this);
117    private native final void native_release();
118
119
120    /**
121     * Disconnects and releases the Camera object resources.
122     * <p>It is recommended that you call this as soon as you're done with the
123     * Camera object.</p>
124     */
125    public final void release() {
126        native_release();
127    }
128
129    /**
130     * Reconnect to the camera after passing it to MediaRecorder. To save
131     * setup/teardown time, a client of Camera can pass an initialized Camera
132     * object to a MediaRecorder to use for video recording. Once the
133     * MediaRecorder is done with the Camera, this method can be used to
134     * re-establish a connection with the camera hardware. NOTE: The Camera
135     * object must first be unlocked by the process that owns it before it
136     * can be connected to another process.
137     *
138     * @throws IOException if the method fails.
139     *
140     * @hide
141     */
142    public native final void reconnect() throws IOException;
143
144    /**
145     * Lock the camera to prevent other processes from accessing it. To save
146     * setup/teardown time, a client of Camera can pass an initialized Camera
147     * object to another process. This method is used to re-lock the Camera
148     * object prevent other processes from accessing it. By default, the
149     * Camera object is locked. Locking it again from the same process will
150     * have no effect. Attempting to lock it from another process if it has
151     * not been unlocked will fail.
152     *
153     * @throws RuntimeException if the method fails.
154     */
155    public native final void lock();
156
157    /**
158     * Unlock the camera to allow another process to access it. To save
159     * setup/teardown time, a client of Camera can pass an initialized Camera
160     * object to another process. This method is used to unlock the Camera
161     * object before handing off the Camera object to the other process.
162     *
163     * @throws RuntimeException if the method fails.
164     */
165    public native final void unlock();
166
167    /**
168     * Sets the SurfaceHolder to be used for a picture preview. If the surface
169     * changed since the last call, the screen will blank. Nothing happens
170     * if the same surface is re-set.
171     *
172     * @param holder the SurfaceHolder upon which to place the picture preview
173     * @throws IOException if the method fails.
174     */
175    public final void setPreviewDisplay(SurfaceHolder holder) throws IOException {
176        if (holder != null) {
177            setPreviewDisplay(holder.getSurface());
178        } else {
179            setPreviewDisplay((Surface)null);
180        }
181    }
182
183    private native final void setPreviewDisplay(Surface surface);
184
185    /**
186     * Used to get a copy of each preview frame.
187     */
188    public interface PreviewCallback
189    {
190        /**
191         * The callback that delivers the preview frames.
192         *
193         * @param data The contents of the preview frame in the format defined
194         *  by {@link android.graphics.PixelFormat}, which can be queried
195         *  with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
196         *  If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}
197         *             is never called, the default will be the YCbCr_420_SP
198         *             (NV21) format.
199         * @param camera The Camera service object.
200         */
201        void onPreviewFrame(byte[] data, Camera camera);
202    };
203
204    /**
205     * Start drawing preview frames to the surface.
206     */
207    public native final void startPreview();
208
209    /**
210     * Stop drawing preview frames to the surface.
211     */
212    public native final void stopPreview();
213
214    /**
215     * Return current preview state.
216     *
217     * FIXME: Unhide before release
218     * @hide
219     */
220    public native final boolean previewEnabled();
221
222    /**
223     * Can be called at any time to instruct the camera to use a callback for
224     * each preview frame in addition to displaying it.
225     *
226     * @param cb A callback object that receives a copy of each preview frame.
227     *           Pass null to stop receiving callbacks at any time.
228     */
229    public final void setPreviewCallback(PreviewCallback cb) {
230        mPreviewCallback = cb;
231        mOneShot = false;
232        setHasPreviewCallback(cb != null, false);
233    }
234
235    /**
236     * Installs a callback to retrieve a single preview frame, after which the
237     * callback is cleared.
238     *
239     * @param cb A callback object that receives a copy of the preview frame.
240     */
241    public final void setOneShotPreviewCallback(PreviewCallback cb) {
242        if (cb != null) {
243            mPreviewCallback = cb;
244            mOneShot = true;
245            setHasPreviewCallback(true, true);
246        }
247    }
248
249    private native final void setHasPreviewCallback(boolean installed, boolean oneshot);
250
251    private class EventHandler extends Handler
252    {
253        private Camera mCamera;
254
255        public EventHandler(Camera c, Looper looper) {
256            super(looper);
257            mCamera = c;
258        }
259
260        @Override
261        public void handleMessage(Message msg) {
262            switch(msg.what) {
263            case CAMERA_MSG_SHUTTER:
264                if (mShutterCallback != null) {
265                    mShutterCallback.onShutter();
266                }
267                return;
268
269            case CAMERA_MSG_RAW_IMAGE:
270                if (mRawImageCallback != null) {
271                    mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera);
272                }
273                return;
274
275            case CAMERA_MSG_COMPRESSED_IMAGE:
276                if (mJpegCallback != null) {
277                    mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera);
278                }
279                return;
280
281            case CAMERA_MSG_PREVIEW_FRAME:
282                if (mPreviewCallback != null) {
283                    mPreviewCallback.onPreviewFrame((byte[])msg.obj, mCamera);
284                    if (mOneShot) {
285                        mPreviewCallback = null;
286                    }
287                }
288                return;
289
290            case CAMERA_MSG_POSTVIEW_FRAME:
291                if (mPostviewCallback != null) {
292                    mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera);
293                }
294                return;
295
296            case CAMERA_MSG_FOCUS:
297                if (mAutoFocusCallback != null) {
298                    mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera);
299                }
300                return;
301
302            case CAMERA_MSG_ZOOM:
303                if (mZoomCallback != null) {
304                    mZoomCallback.onZoomUpdate(msg.arg1, mCamera);
305                }
306                return;
307
308            case CAMERA_MSG_ERROR :
309                Log.e(TAG, "Error " + msg.arg1);
310                if (mErrorCallback != null) {
311                    mErrorCallback.onError(msg.arg1, mCamera);
312                }
313                return;
314
315            default:
316                Log.e(TAG, "Unknown message type " + msg.what);
317                return;
318            }
319        }
320    }
321
322    private static void postEventFromNative(Object camera_ref,
323                                            int what, int arg1, int arg2, Object obj)
324    {
325        Camera c = (Camera)((WeakReference)camera_ref).get();
326        if (c == null)
327            return;
328
329        if (c.mEventHandler != null) {
330            Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj);
331            c.mEventHandler.sendMessage(m);
332        }
333    }
334
335    /**
336     * Handles the callback for the camera auto focus.
337     * <p>Devices that do not support auto-focus will receive a "fake"
338     * callback to this interface. If your application needs auto-focus and
339     * should not be installed on devices <em>without</em> auto-focus, you must
340     * declare that your app uses the
341     * {@code android.hardware.camera.autofocus} feature, in the
342     * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
343     * manifest element.</p>
344     */
345    public interface AutoFocusCallback
346    {
347        /**
348         * Callback for the camera auto focus. If the camera does not support
349         * auto-focus and autoFocus is called, onAutoFocus will be called
350         * immediately with success.
351         *
352         * @param success true if focus was successful, false if otherwise
353         * @param camera  the Camera service object
354         */
355        void onAutoFocus(boolean success, Camera camera);
356    };
357
358    /**
359     * Starts auto-focus function and registers a callback function to run when
360     * camera is focused. Only valid after startPreview() has been called.
361     * Applications should call {@link
362     * android.hardware.Camera.Parameters#getFocusMode()} to determine if this
363     * method should be called. If the camera does not support auto-focus, it is
364     * a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)}
365     * callback will be called immediately.
366     * <p>If your application should not be installed
367     * on devices without auto-focus, you must declare that your application
368     * uses auto-focus with the
369     * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html">&lt;uses-feature></a>
370     * manifest element.</p>
371     *
372     * @param cb the callback to run
373     */
374    public final void autoFocus(AutoFocusCallback cb)
375    {
376        mAutoFocusCallback = cb;
377        native_autoFocus();
378    }
379    private native final void native_autoFocus();
380
381    /**
382     * Cancels auto-focus function. If the auto-focus is still in progress,
383     * this function will cancel it. Whether the auto-focus is in progress
384     * or not, this function will return the focus position to the default.
385     * If the camera does not support auto-focus, this is a no-op.
386     */
387    public final void cancelAutoFocus()
388    {
389        mAutoFocusCallback = null;
390        native_cancelAutoFocus();
391    }
392    private native final void native_cancelAutoFocus();
393
394    /**
395     * An interface which contains a callback for the shutter closing after taking a picture.
396     */
397    public interface ShutterCallback
398    {
399        /**
400         * Can be used to play a shutter sound as soon as the image has been captured, but before
401         * the data is available.
402         */
403        void onShutter();
404    }
405
406    /**
407     * Handles the callback for when a picture is taken.
408     */
409    public interface PictureCallback {
410        /**
411         * Callback for when a picture is taken.
412         *
413         * @param data   a byte array of the picture data
414         * @param camera the Camera service object
415         */
416        void onPictureTaken(byte[] data, Camera camera);
417    };
418
419    /**
420     * Triggers an asynchronous image capture. The camera service will initiate
421     * a series of callbacks to the application as the image capture progresses.
422     * The shutter callback occurs after the image is captured. This can be used
423     * to trigger a sound to let the user know that image has been captured. The
424     * raw callback occurs when the raw image data is available (NOTE: the data
425     * may be null if the hardware does not have enough memory to make a copy).
426     * The jpeg callback occurs when the compressed image is available. If the
427     * application does not need a particular callback, a null can be passed
428     * instead of a callback method.
429     *
430     * @param shutter   callback after the image is captured, may be null
431     * @param raw       callback with raw image data, may be null
432     * @param jpeg      callback with jpeg image data, may be null
433     */
434    public final void takePicture(ShutterCallback shutter, PictureCallback raw,
435            PictureCallback jpeg) {
436        takePicture(shutter, raw, null, jpeg);
437    }
438    private native final void native_takePicture();
439
440    /**
441     * Triggers an asynchronous image capture. The camera service will initiate
442     * a series of callbacks to the application as the image capture progresses.
443     * The shutter callback occurs after the image is captured. This can be used
444     * to trigger a sound to let the user know that image has been captured. The
445     * raw callback occurs when the raw image data is available (NOTE: the data
446     * may be null if the hardware does not have enough memory to make a copy).
447     * The postview callback occurs when a scaled, fully processed postview
448     * image is available (NOTE: not all hardware supports this). The jpeg
449     * callback occurs when the compressed image is available. If the
450     * application does not need a particular callback, a null can be passed
451     * instead of a callback method.
452     *
453     * @param shutter   callback after the image is captured, may be null
454     * @param raw       callback with raw image data, may be null
455     * @param postview  callback with postview image data, may be null
456     * @param jpeg      callback with jpeg image data, may be null
457     */
458    public final void takePicture(ShutterCallback shutter, PictureCallback raw,
459            PictureCallback postview, PictureCallback jpeg) {
460        mShutterCallback = shutter;
461        mRawImageCallback = raw;
462        mPostviewCallback = postview;
463        mJpegCallback = jpeg;
464        native_takePicture();
465    }
466
467    /**
468     * Handles the zoom callback.
469     */
470    public interface ZoomCallback
471    {
472        /**
473         * Callback for zoom updates
474         * @param zoomLevel   new zoom level in 1/1000 increments,
475         * e.g. a zoom of 3.2x is stored as 3200. Accuracy of the
476         * value is dependent on the hardware implementation. Not
477         * all devices will generate this callback.
478         * @param camera  the Camera service object
479         */
480        void onZoomUpdate(int zoomLevel, Camera camera);
481    };
482
483    /**
484     * Registers a callback to be invoked when the zoom
485     * level is updated by the camera driver.
486     * @param cb the callback to run
487     */
488    public final void setZoomCallback(ZoomCallback cb)
489    {
490        mZoomCallback = cb;
491    }
492
493    // These match the enum in include/ui/Camera.h
494    /** Unspecified camerar error.  @see #ErrorCallback */
495    public static final int CAMERA_ERROR_UNKNOWN = 1;
496    /** Media server died. In this case, the application must release the
497     * Camera object and instantiate a new one. @see #ErrorCallback */
498    public static final int CAMERA_ERROR_SERVER_DIED = 100;
499
500    /**
501     * Handles the camera error callback.
502     */
503    public interface ErrorCallback
504    {
505        /**
506         * Callback for camera errors.
507         * @param error   error code:
508         * <ul>
509         * <li>{@link #CAMERA_ERROR_UNKNOWN}
510         * <li>{@link #CAMERA_ERROR_SERVER_DIED}
511         * </ul>
512         * @param camera  the Camera service object
513         */
514        void onError(int error, Camera camera);
515    };
516
517    /**
518     * Registers a callback to be invoked when an error occurs.
519     * @param cb the callback to run
520     */
521    public final void setErrorCallback(ErrorCallback cb)
522    {
523        mErrorCallback = cb;
524    }
525
526    private native final void native_setParameters(String params);
527    private native final String native_getParameters();
528
529    /**
530     * Sets the Parameters for pictures from this Camera service.
531     *
532     * @param params the Parameters to use for this Camera service
533     */
534    public void setParameters(Parameters params) {
535        native_setParameters(params.flatten());
536    }
537
538    /**
539     * Returns the picture Parameters for this Camera service.
540     */
541    public Parameters getParameters() {
542        Parameters p = new Parameters();
543        String s = native_getParameters();
544        p.unflatten(s);
545        return p;
546    }
547
548    /**
549     * Handles the picture size (dimensions).
550     */
551    public class Size {
552        /**
553         * Sets the dimensions for pictures.
554         *
555         * @param w the photo width (pixels)
556         * @param h the photo height (pixels)
557         */
558        public Size(int w, int h) {
559            width = w;
560            height = h;
561        }
562        /** width of the picture */
563        public int width;
564        /** height of the picture */
565        public int height;
566    };
567
568    /**
569     * Handles the parameters for pictures created by a Camera service.
570     *
571     * <p>To make camera parameters take effect, applications have to call
572     * Camera.setParameters. For example, after setWhiteBalance is called, white
573     * balance is not changed until Camera.setParameters() is called.
574     *
575     * <p>Different devices may have different camera capabilities, such as
576     * picture size or flash modes. The application should query the camera
577     * capabilities before setting parameters. For example, the application
578     * should call getSupportedColorEffects before calling setEffect. If the
579     * camera does not support color effects, getSupportedColorEffects will
580     * return null.
581     */
582    public class Parameters {
583        // Parameter keys to communicate with the camera driver.
584        private static final String KEY_PREVIEW_SIZE = "preview-size";
585        private static final String KEY_PREVIEW_FORMAT = "preview-format";
586        private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate";
587        private static final String KEY_PICTURE_SIZE = "picture-size";
588        private static final String KEY_PICTURE_FORMAT = "picture-format";
589        private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width";
590        private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height";
591        private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality";
592        private static final String KEY_JPEG_QUALITY = "jpeg-quality";
593        private static final String KEY_ROTATION = "rotation";
594        private static final String KEY_GPS_LATITUDE = "gps-latitude";
595        private static final String KEY_GPS_LONGITUDE = "gps-longitude";
596        private static final String KEY_GPS_ALTITUDE = "gps-altitude";
597        private static final String KEY_GPS_TIMESTAMP = "gps-timestamp";
598        private static final String KEY_WHITE_BALANCE = "whitebalance";
599        private static final String KEY_EFFECT = "effect";
600        private static final String KEY_ANTIBANDING = "antibanding";
601        private static final String KEY_SCENE_MODE = "scene-mode";
602        private static final String KEY_FLASH_MODE = "flash-mode";
603        private static final String KEY_FOCUS_MODE = "focus-mode";
604        // Parameter key suffix for supported values.
605        private static final String SUPPORTED_VALUES_SUFFIX = "-values";
606
607        // Values for white balance settings.
608        public static final String WHITE_BALANCE_AUTO = "auto";
609        public static final String WHITE_BALANCE_INCANDESCENT = "incandescent";
610        public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent";
611        public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent";
612        public static final String WHITE_BALANCE_DAYLIGHT = "daylight";
613        public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight";
614        public static final String WHITE_BALANCE_TWILIGHT = "twilight";
615        public static final String WHITE_BALANCE_SHADE = "shade";
616
617        // Values for color effect settings.
618        public static final String EFFECT_NONE = "none";
619        public static final String EFFECT_MONO = "mono";
620        public static final String EFFECT_NEGATIVE = "negative";
621        public static final String EFFECT_SOLARIZE = "solarize";
622        public static final String EFFECT_SEPIA = "sepia";
623        public static final String EFFECT_POSTERIZE = "posterize";
624        public static final String EFFECT_WHITEBOARD = "whiteboard";
625        public static final String EFFECT_BLACKBOARD = "blackboard";
626        public static final String EFFECT_AQUA = "aqua";
627
628        // Values for antibanding settings.
629        public static final String ANTIBANDING_AUTO = "auto";
630        public static final String ANTIBANDING_50HZ = "50hz";
631        public static final String ANTIBANDING_60HZ = "60hz";
632        public static final String ANTIBANDING_OFF = "off";
633
634        // Values for flash mode settings.
635        /**
636         * Flash will not be fired.
637         */
638        public static final String FLASH_MODE_OFF = "off";
639        /**
640         * Flash will be fired automatically when required. The timing is
641         * decided by camera driver.
642         */
643        public static final String FLASH_MODE_AUTO = "auto";
644        /**
645         * Flash will always be fired. The timing is decided by camera driver.
646         */
647        public static final String FLASH_MODE_ON = "on";
648        /**
649         * Flash will be fired in red-eye reduction mode.
650         */
651        public static final String FLASH_MODE_RED_EYE = "red-eye";
652        /**
653         * Constant emission of light. This can be used for video recording.
654         */
655        public static final String FLASH_MODE_VIDEO_LIGHT = "video-light";
656
657        // Values for scene mode settings.
658        public static final String SCENE_MODE_AUTO = "auto";
659        public static final String SCENE_MODE_ACTION = "action";
660        public static final String SCENE_MODE_PORTRAIT = "portrait";
661        public static final String SCENE_MODE_LANDSCAPE = "landscape";
662        public static final String SCENE_MODE_NIGHT = "night";
663        public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait";
664        public static final String SCENE_MODE_THEATRE = "theatre";
665        public static final String SCENE_MODE_BEACH = "beach";
666        public static final String SCENE_MODE_SNOW = "snow";
667        public static final String SCENE_MODE_SUNSET = "sunset";
668        public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto";
669        public static final String SCENE_MODE_FIREWORKS = "fireworks";
670        public static final String SCENE_MODE_SPORTS = "sports";
671        public static final String SCENE_MODE_PARTY = "party";
672        public static final String SCENE_MODE_CANDLELIGHT = "candlelight";
673
674        // Values for focus mode settings.
675        /**
676         * Auto-focus mode.
677         */
678        public static final String FOCUS_MODE_AUTO = "auto";
679        /**
680         * Focus is set at infinity. Applications should not call
681         * {@link #autoFocus(AutoFocusCallback)} in this mode.
682         */
683        public static final String FOCUS_MODE_INFINITY = "infinity";
684        public static final String FOCUS_MODE_MACRO = "macro";
685        /**
686         * Focus is fixed. The camera is always in this mode if the focus is not
687         * adjustable. If the camera has auto-focus, this mode can fix the
688         * focus, which is usually at hyperfocal distance. Applications should
689         * not call {@link #autoFocus(AutoFocusCallback)} in this mode.
690         */
691        public static final String FOCUS_MODE_FIXED = "fixed";
692
693        // Formats for setPreviewFormat and setPictureFormat.
694        private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp";
695        private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp";
696        private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv";
697        private static final String PIXEL_FORMAT_RGB565 = "rgb565";
698        private static final String PIXEL_FORMAT_JPEG = "jpeg";
699
700        private HashMap<String, String> mMap;
701
702        private Parameters() {
703            mMap = new HashMap<String, String>();
704        }
705
706        /**
707         * Writes the current Parameters to the log.
708         * @hide
709         * @deprecated
710         */
711        public void dump() {
712            Log.e(TAG, "dump: size=" + mMap.size());
713            for (String k : mMap.keySet()) {
714                Log.e(TAG, "dump: " + k + "=" + mMap.get(k));
715            }
716        }
717
718        /**
719         * Creates a single string with all the parameters set in
720         * this Parameters object.
721         * <p>The {@link #unflatten(String)} method does the reverse.</p>
722         *
723         * @return a String with all values from this Parameters object, in
724         *         semi-colon delimited key-value pairs
725         */
726        public String flatten() {
727            StringBuilder flattened = new StringBuilder();
728            for (String k : mMap.keySet()) {
729                flattened.append(k);
730                flattened.append("=");
731                flattened.append(mMap.get(k));
732                flattened.append(";");
733            }
734            // chop off the extra semicolon at the end
735            flattened.deleteCharAt(flattened.length()-1);
736            return flattened.toString();
737        }
738
739        /**
740         * Takes a flattened string of parameters and adds each one to
741         * this Parameters object.
742         * <p>The {@link #flatten()} method does the reverse.</p>
743         *
744         * @param flattened a String of parameters (key-value paired) that
745         *                  are semi-colon delimited
746         */
747        public void unflatten(String flattened) {
748            mMap.clear();
749
750            StringTokenizer tokenizer = new StringTokenizer(flattened, ";");
751            while (tokenizer.hasMoreElements()) {
752                String kv = tokenizer.nextToken();
753                int pos = kv.indexOf('=');
754                if (pos == -1) {
755                    continue;
756                }
757                String k = kv.substring(0, pos);
758                String v = kv.substring(pos + 1);
759                mMap.put(k, v);
760            }
761        }
762
763        public void remove(String key) {
764            mMap.remove(key);
765        }
766
767        /**
768         * Sets a String parameter.
769         *
770         * @param key   the key name for the parameter
771         * @param value the String value of the parameter
772         */
773        public void set(String key, String value) {
774            if (key.indexOf('=') != -1 || key.indexOf(';') != -1) {
775                Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ;)");
776                return;
777            }
778            if (value.indexOf('=') != -1 || value.indexOf(';') != -1) {
779                Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ;)");
780                return;
781            }
782
783            mMap.put(key, value);
784        }
785
786        /**
787         * Sets an integer parameter.
788         *
789         * @param key   the key name for the parameter
790         * @param value the int value of the parameter
791         */
792        public void set(String key, int value) {
793            mMap.put(key, Integer.toString(value));
794        }
795
796        /**
797         * Returns the value of a String parameter.
798         *
799         * @param key the key name for the parameter
800         * @return the String value of the parameter
801         */
802        public String get(String key) {
803            return mMap.get(key);
804        }
805
806        /**
807         * Returns the value of an integer parameter.
808         *
809         * @param key the key name for the parameter
810         * @return the int value of the parameter
811         */
812        public int getInt(String key) {
813            return Integer.parseInt(mMap.get(key));
814        }
815
816        /**
817         * Sets the dimensions for preview pictures.
818         *
819         * @param width  the width of the pictures, in pixels
820         * @param height the height of the pictures, in pixels
821         */
822        public void setPreviewSize(int width, int height) {
823            String v = Integer.toString(width) + "x" + Integer.toString(height);
824            set(KEY_PREVIEW_SIZE, v);
825        }
826
827        /**
828         * Returns the dimensions setting for preview pictures.
829         *
830         * @return a Size object with the height and width setting
831         *          for the preview picture
832         */
833        public Size getPreviewSize() {
834            String pair = get(KEY_PREVIEW_SIZE);
835            return strToSize(pair);
836        }
837
838        /**
839         * Gets the supported preview sizes.
840         *
841         * @return a List of Size object. null if preview size setting is not
842         *         supported.
843         */
844        public List<Size> getSupportedPreviewSizes() {
845            String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX);
846            return splitSize(str);
847        }
848
849        /**
850         * Sets the dimensions for EXIF thumbnail in Jpeg picture.
851         *
852         * @param width  the width of the thumbnail, in pixels
853         * @param height the height of the thumbnail, in pixels
854         */
855        public void setJpegThumbnailSize(int width, int height) {
856            set(KEY_JPEG_THUMBNAIL_WIDTH, width);
857            set(KEY_JPEG_THUMBNAIL_HEIGHT, height);
858        }
859
860        /**
861         * Returns the dimensions for EXIF thumbnail in Jpeg picture.
862         *
863         * @return a Size object with the height and width setting for the EXIF
864         *         thumbnails
865         */
866        public Size getJpegThumbnailSize() {
867            return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH),
868                            getInt(KEY_JPEG_THUMBNAIL_HEIGHT));
869        }
870
871        /**
872         * Sets the quality of the EXIF thumbnail in Jpeg picture.
873         *
874         * @param quality the JPEG quality of the EXIF thumbnail. The range is 1
875         *                to 100, with 100 being the best.
876         */
877        public void setJpegThumbnailQuality(int quality) {
878            set(KEY_JPEG_THUMBNAIL_QUALITY, quality);
879        }
880
881        /**
882         * Returns the quality setting for the EXIF thumbnail in Jpeg picture.
883         *
884         * @return the JPEG quality setting of the EXIF thumbnail.
885         */
886        public int getJpegThumbnailQuality() {
887            return getInt(KEY_JPEG_THUMBNAIL_QUALITY);
888        }
889
890        /**
891         * Sets Jpeg quality of captured picture.
892         *
893         * @param quality the JPEG quality of captured picture. The range is 1
894         *                to 100, with 100 being the best.
895         */
896        public void setJpegQuality(int quality) {
897            set(KEY_JPEG_QUALITY, quality);
898        }
899
900        /**
901         * Returns the quality setting for the JPEG picture.
902         *
903         * @return the JPEG picture quality setting.
904         */
905        public int getJpegQuality() {
906            return getInt(KEY_JPEG_QUALITY);
907        }
908
909        /**
910         * Sets the rate at which preview frames are received.
911         *
912         * @param fps the frame rate (frames per second)
913         */
914        public void setPreviewFrameRate(int fps) {
915            set(KEY_PREVIEW_FRAME_RATE, fps);
916        }
917
918        /**
919         * Returns the setting for the rate at which preview frames
920         * are received.
921         *
922         * @return the frame rate setting (frames per second)
923         */
924        public int getPreviewFrameRate() {
925            return getInt(KEY_PREVIEW_FRAME_RATE);
926        }
927
928        /**
929         * Gets the supported preview frame rates.
930         *
931         * @return a List of Integer objects (preview frame rates). null if
932         *         preview frame rate setting is not supported.
933         */
934        public List<Integer> getSupportedPreviewFrameRates() {
935            String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX);
936            return splitInt(str);
937        }
938
939        /**
940         * Sets the image format for preview pictures.
941         * <p>If this is never called, the default format will be
942         * {@link android.graphics.PixelFormat#YCbCr_420_SP}, which
943         * uses the NV21 encoding format.</p>
944         *
945         * @param pixel_format the desired preview picture format, defined
946         *   by one of the {@link android.graphics.PixelFormat} constants.
947         *   (E.g., <var>PixelFormat.YCbCr_420_SP</var> (default),
948         *                      <var>PixelFormat.RGB_565</var>, or
949         *                      <var>PixelFormat.JPEG</var>)
950         * @see android.graphics.PixelFormat
951         */
952        public void setPreviewFormat(int pixel_format) {
953            String s = cameraFormatForPixelFormat(pixel_format);
954            if (s == null) {
955                throw new IllegalArgumentException(
956                        "Invalid pixel_format=" + pixel_format);
957            }
958
959            set(KEY_PREVIEW_FORMAT, s);
960        }
961
962        /**
963         * Returns the image format for preview pictures got from
964         * {@link PreviewCallback}.
965         *
966         * @return the {@link android.graphics.PixelFormat} int representing
967         *         the preview picture format.
968         */
969        public int getPreviewFormat() {
970            return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT));
971        }
972
973        /**
974         * Gets the supported preview formats.
975         *
976         * @return a List of Integer objects. null if preview format setting is
977         *         not supported.
978         */
979        public List<Integer> getSupportedPreviewFormats() {
980            String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX);
981            ArrayList<Integer> formats = new ArrayList<Integer>();
982            for (String s : split(str)) {
983                int f = pixelFormatForCameraFormat(s);
984                if (f == PixelFormat.UNKNOWN) continue;
985                formats.add(f);
986            }
987            return formats;
988        }
989
990        /**
991         * Sets the dimensions for pictures.
992         *
993         * @param width  the width for pictures, in pixels
994         * @param height the height for pictures, in pixels
995         */
996        public void setPictureSize(int width, int height) {
997            String v = Integer.toString(width) + "x" + Integer.toString(height);
998            set(KEY_PICTURE_SIZE, v);
999        }
1000
1001        /**
1002         * Returns the dimension setting for pictures.
1003         *
1004         * @return a Size object with the height and width setting
1005         *          for pictures
1006         */
1007        public Size getPictureSize() {
1008            String pair = get(KEY_PICTURE_SIZE);
1009            return strToSize(pair);
1010        }
1011
1012        /**
1013         * Gets the supported picture sizes.
1014         *
1015         * @return a List of Size objects. null if picture size setting is not
1016         *         supported.
1017         */
1018        public List<Size> getSupportedPictureSizes() {
1019            String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX);
1020            return splitSize(str);
1021        }
1022
1023        /**
1024         * Sets the image format for pictures.
1025         *
1026         * @param pixel_format the desired picture format
1027         *                     (<var>PixelFormat.YCbCr_420_SP (NV21)</var>,
1028         *                      <var>PixelFormat.RGB_565</var>, or
1029         *                      <var>PixelFormat.JPEG</var>)
1030         * @see android.graphics.PixelFormat
1031         */
1032        public void setPictureFormat(int pixel_format) {
1033            String s = cameraFormatForPixelFormat(pixel_format);
1034            if (s == null) {
1035                throw new IllegalArgumentException(
1036                        "Invalid pixel_format=" + pixel_format);
1037            }
1038
1039            set(KEY_PICTURE_FORMAT, s);
1040        }
1041
1042        /**
1043         * Returns the image format for pictures.
1044         *
1045         * @return the PixelFormat int representing the picture format
1046         */
1047        public int getPictureFormat() {
1048            return pixelFormatForCameraFormat(get(KEY_PICTURE_FORMAT));
1049        }
1050
1051        /**
1052         * Gets the supported picture formats.
1053         *
1054         * @return a List of Integer objects (values are PixelFormat.XXX). null
1055         *         if picture setting is not supported.
1056         */
1057        public List<Integer> getSupportedPictureFormats() {
1058            String str = get(KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX);
1059            return splitInt(str);
1060        }
1061
1062        private String cameraFormatForPixelFormat(int pixel_format) {
1063            switch(pixel_format) {
1064            case PixelFormat.YCbCr_422_SP: return PIXEL_FORMAT_YUV422SP;
1065            case PixelFormat.YCbCr_420_SP: return PIXEL_FORMAT_YUV420SP;
1066            case PixelFormat.YCbCr_422_I:  return PIXEL_FORMAT_YUV422I;
1067            case PixelFormat.RGB_565:      return PIXEL_FORMAT_RGB565;
1068            case PixelFormat.JPEG:         return PIXEL_FORMAT_JPEG;
1069            default:                       return null;
1070            }
1071        }
1072
1073        private int pixelFormatForCameraFormat(String format) {
1074            if (format == null)
1075                return PixelFormat.UNKNOWN;
1076
1077            if (format.equals(PIXEL_FORMAT_YUV422SP))
1078                return PixelFormat.YCbCr_422_SP;
1079
1080            if (format.equals(PIXEL_FORMAT_YUV420SP))
1081                return PixelFormat.YCbCr_420_SP;
1082
1083            if (format.equals(PIXEL_FORMAT_YUV422I))
1084                return PixelFormat.YCbCr_422_I;
1085
1086            if (format.equals(PIXEL_FORMAT_RGB565))
1087                return PixelFormat.RGB_565;
1088
1089            if (format.equals(PIXEL_FORMAT_JPEG))
1090                return PixelFormat.JPEG;
1091
1092            return PixelFormat.UNKNOWN;
1093        }
1094
1095        /**
1096         * Sets the orientation of the device in degrees. For example, suppose
1097         * the natural position of the device is landscape. If the user takes a
1098         * picture in landscape mode in 2048x1536 resolution, the rotation
1099         * should be set to 0. If the user rotates the phone 90 degrees
1100         * clockwise, the rotation should be set to 90. Applications can use
1101         * {@link android.view.OrientationEventListener} to set this parameter.
1102         *
1103         * The camera driver may set orientation in the EXIF header without
1104         * rotating the picture. Or the driver may rotate the picture and
1105         * the EXIF thumbnail. If the Jpeg picture is rotated, the orientation
1106         * in the EXIF header will be missing or 1 (row #0 is top and column #0
1107         * is left side).
1108         *
1109         * @param rotation The orientation of the device in degrees. Rotation
1110         *                 can only be 0, 90, 180 or 270.
1111         * @throws IllegalArgumentException if rotation value is invalid.
1112         * @see android.view.OrientationEventListener
1113         */
1114        public void setRotation(int rotation) {
1115            if (rotation == 0 || rotation == 90 || rotation == 180
1116                    || rotation == 270) {
1117                set(KEY_ROTATION, Integer.toString(rotation));
1118            } else {
1119                throw new IllegalArgumentException(
1120                        "Invalid rotation=" + rotation);
1121            }
1122        }
1123
1124        /**
1125         * Sets GPS latitude coordinate. This will be stored in JPEG EXIF
1126         * header.
1127         *
1128         * @param latitude GPS latitude coordinate.
1129         */
1130        public void setGpsLatitude(double latitude) {
1131            set(KEY_GPS_LATITUDE, Double.toString(latitude));
1132        }
1133
1134        /**
1135         * Sets GPS longitude coordinate. This will be stored in JPEG EXIF
1136         * header.
1137         *
1138         * @param longitude GPS longitude coordinate.
1139         */
1140        public void setGpsLongitude(double longitude) {
1141            set(KEY_GPS_LONGITUDE, Double.toString(longitude));
1142        }
1143
1144        /**
1145         * Sets GPS altitude. This will be stored in JPEG EXIF header.
1146         *
1147         * @param altitude GPS altitude in meters.
1148         */
1149        public void setGpsAltitude(double altitude) {
1150            set(KEY_GPS_ALTITUDE, Double.toString(altitude));
1151        }
1152
1153        /**
1154         * Sets GPS timestamp. This will be stored in JPEG EXIF header.
1155         *
1156         * @param timestamp GPS timestamp (UTC in seconds since January 1,
1157         *                  1970).
1158         */
1159        public void setGpsTimestamp(long timestamp) {
1160            set(KEY_GPS_TIMESTAMP, Long.toString(timestamp));
1161        }
1162
1163        /**
1164         * Removes GPS latitude, longitude, altitude, and timestamp from the
1165         * parameters.
1166         */
1167        public void removeGpsData() {
1168            remove(KEY_GPS_LATITUDE);
1169            remove(KEY_GPS_LONGITUDE);
1170            remove(KEY_GPS_ALTITUDE);
1171            remove(KEY_GPS_TIMESTAMP);
1172        }
1173
1174        /**
1175         * Gets the current white balance setting.
1176         *
1177         * @return one of WHITE_BALANCE_XXX string constant. null if white
1178         *         balance setting is not supported.
1179         */
1180        public String getWhiteBalance() {
1181            return get(KEY_WHITE_BALANCE);
1182        }
1183
1184        /**
1185         * Sets the white balance.
1186         *
1187         * @param value WHITE_BALANCE_XXX string constant.
1188         */
1189        public void setWhiteBalance(String value) {
1190            set(KEY_WHITE_BALANCE, value);
1191        }
1192
1193        /**
1194         * Gets the supported white balance.
1195         *
1196         * @return a List of WHITE_BALANCE_XXX string constants. null if white
1197         *         balance setting is not supported.
1198         */
1199        public List<String> getSupportedWhiteBalance() {
1200            String str = get(KEY_WHITE_BALANCE + SUPPORTED_VALUES_SUFFIX);
1201            return split(str);
1202        }
1203
1204        /**
1205         * Gets the current color effect setting.
1206         *
1207         * @return one of EFFECT_XXX string constant. null if color effect
1208         *         setting is not supported.
1209         */
1210        public String getColorEffect() {
1211            return get(KEY_EFFECT);
1212        }
1213
1214        /**
1215         * Sets the current color effect setting.
1216         *
1217         * @param value EFFECT_XXX string constants.
1218         */
1219        public void setColorEffect(String value) {
1220            set(KEY_EFFECT, value);
1221        }
1222
1223        /**
1224         * Gets the supported color effects.
1225         *
1226         * @return a List of EFFECT_XXX string constants. null if color effect
1227         *         setting is not supported.
1228         */
1229        public List<String> getSupportedColorEffects() {
1230            String str = get(KEY_EFFECT + SUPPORTED_VALUES_SUFFIX);
1231            return split(str);
1232        }
1233
1234
1235        /**
1236         * Gets the current antibanding setting.
1237         *
1238         * @return one of ANTIBANDING_XXX string constant. null if antibanding
1239         *         setting is not supported.
1240         */
1241        public String getAntibanding() {
1242            return get(KEY_ANTIBANDING);
1243        }
1244
1245        /**
1246         * Sets the antibanding.
1247         *
1248         * @param antibanding ANTIBANDING_XXX string constant.
1249         */
1250        public void setAntibanding(String antibanding) {
1251            set(KEY_ANTIBANDING, antibanding);
1252        }
1253
1254        /**
1255         * Gets the supported antibanding values.
1256         *
1257         * @return a List of ANTIBANDING_XXX string constants. null if
1258         *         antibanding setting is not supported.
1259         */
1260        public List<String> getSupportedAntibanding() {
1261            String str = get(KEY_ANTIBANDING + SUPPORTED_VALUES_SUFFIX);
1262            return split(str);
1263        }
1264
1265        /**
1266         * Gets the current scene mode setting.
1267         *
1268         * @return one of SCENE_MODE_XXX string constant. null if scene mode
1269         *         setting is not supported.
1270         */
1271        public String getSceneMode() {
1272            return get(KEY_SCENE_MODE);
1273        }
1274
1275        /**
1276         * Sets the scene mode.
1277         *
1278         * @param value SCENE_MODE_XXX string constants.
1279         */
1280        public void setSceneMode(String value) {
1281            set(KEY_SCENE_MODE, value);
1282        }
1283
1284        /**
1285         * Gets the supported scene modes.
1286         *
1287         * @return a List of SCENE_MODE_XXX string constant. null if scene mode
1288         *         setting is not supported.
1289         */
1290        public List<String> getSupportedSceneModes() {
1291            String str = get(KEY_SCENE_MODE + SUPPORTED_VALUES_SUFFIX);
1292            return split(str);
1293        }
1294
1295        /**
1296         * Gets the current flash mode setting.
1297         *
1298         * @return one of FLASH_MODE_XXX string constant. null if flash mode
1299         *         setting is not supported.
1300         */
1301        public String getFlashMode() {
1302            return get(KEY_FLASH_MODE);
1303        }
1304
1305        /**
1306         * Sets the flash mode.
1307         *
1308         * @param value FLASH_MODE_XXX string constants.
1309         */
1310        public void setFlashMode(String value) {
1311            set(KEY_FLASH_MODE, value);
1312        }
1313
1314        /**
1315         * Gets the supported flash modes.
1316         *
1317         * @return a List of FLASH_MODE_XXX string constants. null if flash mode
1318         *         setting is not supported.
1319         */
1320        public List<String> getSupportedFlashModes() {
1321            String str = get(KEY_FLASH_MODE + SUPPORTED_VALUES_SUFFIX);
1322            return split(str);
1323        }
1324
1325        /**
1326         * Gets the current focus mode setting.
1327         *
1328         * @return one of FOCUS_MODE_XXX string constant. If the camera does not
1329         *         support auto-focus, this should return {@link
1330         *         #FOCUS_MODE_FIXED}. If the focus mode is not FOCUS_MODE_FIXED
1331         *         or {@link #FOCUS_MODE_INFINITY}, applications should call
1332         *         {@link #autoFocus(AutoFocusCallback)} to start the focus.
1333         */
1334        public String getFocusMode() {
1335            return get(KEY_FOCUS_MODE);
1336        }
1337
1338        /**
1339         * Sets the focus mode.
1340         *
1341         * @param value FOCUS_MODE_XXX string constants.
1342         */
1343        public void setFocusMode(String value) {
1344            set(KEY_FOCUS_MODE, value);
1345        }
1346
1347        /**
1348         * Gets the supported focus modes.
1349         *
1350         * @return a List of FOCUS_MODE_XXX string constants. null if focus mode
1351         *         setting is not supported.
1352         */
1353        public List<String> getSupportedFocusModes() {
1354            String str = get(KEY_FOCUS_MODE + SUPPORTED_VALUES_SUFFIX);
1355            return split(str);
1356        }
1357
1358        // Splits a comma delimited string to an ArrayList of String.
1359        // Return null if the passing string is null or the size is 0.
1360        private ArrayList<String> split(String str) {
1361            if (str == null) return null;
1362
1363            // Use StringTokenizer because it is faster than split.
1364            StringTokenizer tokenizer = new StringTokenizer(str, ",");
1365            ArrayList<String> substrings = new ArrayList<String>();
1366            while (tokenizer.hasMoreElements()) {
1367                substrings.add(tokenizer.nextToken());
1368            }
1369            return substrings;
1370        }
1371
1372        // Splits a comma delimited string to an ArrayList of Integer.
1373        // Return null if the passing string is null or the size is 0.
1374        private ArrayList<Integer> splitInt(String str) {
1375            if (str == null) return null;
1376
1377            StringTokenizer tokenizer = new StringTokenizer(str, ",");
1378            ArrayList<Integer> substrings = new ArrayList<Integer>();
1379            while (tokenizer.hasMoreElements()) {
1380                String token = tokenizer.nextToken();
1381                substrings.add(Integer.parseInt(token));
1382            }
1383            if (substrings.size() == 0) return null;
1384            return substrings;
1385        }
1386
1387        // Splits a comma delimited string to an ArrayList of Size.
1388        // Return null if the passing string is null or the size is 0.
1389        private ArrayList<Size> splitSize(String str) {
1390            if (str == null) return null;
1391
1392            StringTokenizer tokenizer = new StringTokenizer(str, ",");
1393            ArrayList<Size> sizeList = new ArrayList<Size>();
1394            while (tokenizer.hasMoreElements()) {
1395                Size size = strToSize(tokenizer.nextToken());
1396                if (size != null) sizeList.add(size);
1397            }
1398            if (sizeList.size() == 0) return null;
1399            return sizeList;
1400        }
1401
1402        // Parses a string (ex: "480x320") to Size object.
1403        // Return null if the passing string is null.
1404        private Size strToSize(String str) {
1405            if (str == null) return null;
1406
1407            int pos = str.indexOf('x');
1408            if (pos != -1) {
1409                String width = str.substring(0, pos);
1410                String height = str.substring(pos + 1);
1411                return new Size(Integer.parseInt(width),
1412                                Integer.parseInt(height));
1413            }
1414            Log.e(TAG, "Invalid size parameter string=" + str);
1415            return null;
1416        }
1417    };
1418}
1419