|
Android example source code file (Camera.java)
The Camera.java Android example source code/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.hardware; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.StringTokenizer; import java.io.IOException; import android.util.Log; import android.view.Surface; import android.view.SurfaceHolder; import android.graphics.ImageFormat; import android.os.Handler; import android.os.Looper; import android.os.Message; /** * The Camera class is used to connect/disconnect with the camera service, * set capture settings, start/stop preview, snap a picture, and retrieve * frames for encoding for video. * <p>There is no default constructor for this class. Use {@link #open()} to * get a Camera object.</p> * * <p>In order to use the device camera, you must declare the * {@link android.Manifest.permission#CAMERA} permission in your Android * Manifest. Also be sure to include the * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature> * manifest element in order to declare camera features used by your application. * For example, if you use the camera and auto-focus feature, your Manifest * should include the following:</p> * <pre> <uses-permission android:name="android.permission.CAMERA" /> * <uses-feature android:name="android.hardware.camera" /> * <uses-feature android:name="android.hardware.camera.autofocus" /></pre> * * <p class="caution">Caution: Different Android-powered devices * may have different hardware specifications, such as megapixel ratings and * auto-focus capabilities. In order for your application to be compatible with * more devices, you should not make assumptions about the device camera * specifications.</p> */ public class Camera { private static final String TAG = "Camera"; // These match the enums in frameworks/base/include/ui/Camera.h private static final int CAMERA_MSG_ERROR = 0x001; private static final int CAMERA_MSG_SHUTTER = 0x002; private static final int CAMERA_MSG_FOCUS = 0x004; private static final int CAMERA_MSG_ZOOM = 0x008; private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010; private static final int CAMERA_MSG_VIDEO_FRAME = 0x020; private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040; private static final int CAMERA_MSG_RAW_IMAGE = 0x080; private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100; private static final int CAMERA_MSG_ALL_MSGS = 0x1FF; private int mNativeContext; // accessed by native methods private EventHandler mEventHandler; private ShutterCallback mShutterCallback; private PictureCallback mRawImageCallback; private PictureCallback mJpegCallback; private PreviewCallback mPreviewCallback; private PictureCallback mPostviewCallback; private AutoFocusCallback mAutoFocusCallback; private OnZoomChangeListener mZoomListener; private ErrorCallback mErrorCallback; private boolean mOneShot; private boolean mWithBuffer; /** * Returns a new Camera object. */ public static Camera open() { return new Camera(); } Camera() { mShutterCallback = null; mRawImageCallback = null; mJpegCallback = null; mPreviewCallback = null; mPostviewCallback = null; mZoomListener = null; Looper looper; if ((looper = Looper.myLooper()) != null) { mEventHandler = new EventHandler(this, looper); } else if ((looper = Looper.getMainLooper()) != null) { mEventHandler = new EventHandler(this, looper); } else { mEventHandler = null; } native_setup(new WeakReference<Camera>(this)); } protected void finalize() { native_release(); } private native final void native_setup(Object camera_this); private native final void native_release(); /** * Disconnects and releases the Camera object resources. * <p>It is recommended that you call this as soon as you're done with the * Camera object.</p> */ public final void release() { native_release(); } /** * Reconnect to the camera after passing it to MediaRecorder. To save * setup/teardown time, a client of Camera can pass an initialized Camera * object to a MediaRecorder to use for video recording. Once the * MediaRecorder is done with the Camera, this method can be used to * re-establish a connection with the camera hardware. NOTE: The Camera * object must first be unlocked by the process that owns it before it * can be connected to another process. * * @throws IOException if the method fails. */ public native final void reconnect() throws IOException; /** * Lock the camera to prevent other processes from accessing it. To save * setup/teardown time, a client of Camera can pass an initialized Camera * object to another process. This method is used to re-lock the Camera * object prevent other processes from accessing it. By default, the * Camera object is locked. Locking it again from the same process will * have no effect. Attempting to lock it from another process if it has * not been unlocked will fail. * * @throws RuntimeException if the method fails. */ public native final void lock(); /** * Unlock the camera to allow another process to access it. To save * setup/teardown time, a client of Camera can pass an initialized Camera * object to another process. This method is used to unlock the Camera * object before handing off the Camera object to the other process. * * @throws RuntimeException if the method fails. */ public native final void unlock(); /** * Sets the SurfaceHolder to be used for a picture preview. If the surface * changed since the last call, the screen will blank. Nothing happens * if the same surface is re-set. * * @param holder the SurfaceHolder upon which to place the picture preview * @throws IOException if the method fails. */ public final void setPreviewDisplay(SurfaceHolder holder) throws IOException { if (holder != null) { setPreviewDisplay(holder.getSurface()); } else { setPreviewDisplay((Surface)null); } } private native final void setPreviewDisplay(Surface surface); /** * Used to get a copy of each preview frame. */ public interface PreviewCallback { /** * The callback that delivers the preview frames. * * @param data The contents of the preview frame in the format defined * by {@link android.graphics.ImageFormat}, which can be queried * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}. * If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)} * is never called, the default will be the YCbCr_420_SP * (NV21) format. * @param camera The Camera service object. */ void onPreviewFrame(byte[] data, Camera camera); }; /** * Start drawing preview frames to the surface. */ public native final void startPreview(); /** * Stop drawing preview frames to the surface. */ public native final void stopPreview(); /** * Return current preview state. * * FIXME: Unhide before release * @hide */ public native final boolean previewEnabled(); /** * Can be called at any time to instruct the camera to use a callback for * each preview frame in addition to displaying it. * * @param cb A callback object that receives a copy of each preview frame. * Pass null to stop receiving callbacks at any time. */ public final void setPreviewCallback(PreviewCallback cb) { mPreviewCallback = cb; mOneShot = false; mWithBuffer = false; // Always use one-shot mode. We fake camera preview mode by // doing one-shot preview continuously. setHasPreviewCallback(cb != null, false); } /** * Installs a callback to retrieve a single preview frame, after which the * callback is cleared. * * @param cb A callback object that receives a copy of the preview frame. */ public final void setOneShotPreviewCallback(PreviewCallback cb) { mPreviewCallback = cb; mOneShot = true; mWithBuffer = false; setHasPreviewCallback(cb != null, false); } private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer); /** * Installs a callback which will get called as long as there are buffers in the * preview buffer queue, which minimizes dynamic allocation of preview buffers. * * Apps must call addCallbackBuffer to explicitly register the buffers to use, or no callbacks * will be received. addCallbackBuffer may be safely called before or after * a call to setPreviewCallbackWithBuffer with a non-null callback parameter. * * The buffer queue will be cleared upon any calls to setOneShotPreviewCallback, * setPreviewCallback, or to this method with a null callback parameter. * * @param cb A callback object that receives a copy of the preview frame. A null value will clear the queue. */ public final void setPreviewCallbackWithBuffer(PreviewCallback cb) { mPreviewCallback = cb; mOneShot = false; mWithBuffer = true; setHasPreviewCallback(cb != null, true); } /** * Adds a pre-allocated buffer to the preview callback buffer queue. * Applications can add one or more buffers to the queue. When a preview * frame arrives and there is still available buffer, buffer will be filled * and it is removed from the queue. Then preview callback is invoked with * the buffer. If a frame arrives and there is no buffer left, the frame is * discarded. Applications should add the buffers back when they finish the * processing. * * The image format of the callback buffer can be read from {@link * android.hardware.Camera.Parameters#getPreviewFormat()}. bitsPerPixel can * be read from {@link android.graphics.ImageFormat#getBitsPerPixel(int)}. * Preview width and height can be determined from getPreviewSize. * * Alternatively, a buffer from a previous callback may be passed in or used * to determine the size of new preview frame buffers. * * @param callbackBuffer The buffer to register. Size should be width * height * bitsPerPixel / 8. * @see #setPreviewCallbackWithBuffer(PreviewCallback) */ public native final void addCallbackBuffer(byte[] callbackBuffer); private class EventHandler extends Handler { private Camera mCamera; public EventHandler(Camera c, Looper looper) { super(looper); mCamera = c; } @Override public void handleMessage(Message msg) { switch(msg.what) { case CAMERA_MSG_SHUTTER: if (mShutterCallback != null) { mShutterCallback.onShutter(); } return; case CAMERA_MSG_RAW_IMAGE: if (mRawImageCallback != null) { mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera); } return; case CAMERA_MSG_COMPRESSED_IMAGE: if (mJpegCallback != null) { mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera); } return; case CAMERA_MSG_PREVIEW_FRAME: if (mPreviewCallback != null) { PreviewCallback cb = mPreviewCallback; if (mOneShot) { // Clear the callback variable before the callback // in case the app calls setPreviewCallback from // the callback function mPreviewCallback = null; } else if (!mWithBuffer) { // We're faking the camera preview mode to prevent // the app from being flooded with preview frames. // Set to oneshot mode again. setHasPreviewCallback(true, false); } cb.onPreviewFrame((byte[])msg.obj, mCamera); } return; case CAMERA_MSG_POSTVIEW_FRAME: if (mPostviewCallback != null) { mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera); } return; case CAMERA_MSG_FOCUS: if (mAutoFocusCallback != null) { mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera); } return; case CAMERA_MSG_ZOOM: if (mZoomListener != null) { mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera); } return; case CAMERA_MSG_ERROR : Log.e(TAG, "Error " + msg.arg1); if (mErrorCallback != null) { mErrorCallback.onError(msg.arg1, mCamera); } return; default: Log.e(TAG, "Unknown message type " + msg.what); return; } } } private static void postEventFromNative(Object camera_ref, int what, int arg1, int arg2, Object obj) { Camera c = (Camera)((WeakReference)camera_ref).get(); if (c == null) return; if (c.mEventHandler != null) { Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj); c.mEventHandler.sendMessage(m); } } /** * Handles the callback for the camera auto focus. * <p>Devices that do not support auto-focus will receive a "fake" * callback to this interface. If your application needs auto-focus and * should not be installed on devices <em>without auto-focus, you must * declare that your app uses the * {@code android.hardware.camera.autofocus} feature, in the * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature> * manifest element.</p> */ public interface AutoFocusCallback { /** * Callback for the camera auto focus. If the camera does not support * auto-focus and autoFocus is called, onAutoFocus will be called * immediately with success. * * @param success true if focus was successful, false if otherwise * @param camera the Camera service object */ void onAutoFocus(boolean success, Camera camera); }; /** * Starts auto-focus function and registers a callback function to run when * camera is focused. Only valid after startPreview() has been called. * Applications should call {@link * android.hardware.Camera.Parameters#getFocusMode()} to determine if this * method should be called. If the camera does not support auto-focus, it is * a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)} * callback will be called immediately. * <p>If your application should not be installed * on devices without auto-focus, you must declare that your application * uses auto-focus with the * <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature> * manifest element.</p> * <p>If the current flash mode is not * {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be * fired during auto-focus depending on the driver.<p> * * @param cb the callback to run */ public final void autoFocus(AutoFocusCallback cb) { mAutoFocusCallback = cb; native_autoFocus(); } private native final void native_autoFocus(); /** * Cancels auto-focus function. If the auto-focus is still in progress, * this function will cancel it. Whether the auto-focus is in progress * or not, this function will return the focus position to the default. * If the camera does not support auto-focus, this is a no-op. */ public final void cancelAutoFocus() { mAutoFocusCallback = null; native_cancelAutoFocus(); } private native final void native_cancelAutoFocus(); /** * An interface which contains a callback for the shutter closing after taking a picture. */ public interface ShutterCallback { /** * Can be used to play a shutter sound as soon as the image has been captured, but before * the data is available. */ void onShutter(); } /** * Handles the callback for when a picture is taken. */ public interface PictureCallback { /** * Callback for when a picture is taken. * * @param data a byte array of the picture data * @param camera the Camera service object */ void onPictureTaken(byte[] data, Camera camera); }; /** * Triggers an asynchronous image capture. The camera service will initiate * a series of callbacks to the application as the image capture progresses. * The shutter callback occurs after the image is captured. This can be used * to trigger a sound to let the user know that image has been captured. The * raw callback occurs when the raw image data is available (NOTE: the data * may be null if the hardware does not have enough memory to make a copy). * The jpeg callback occurs when the compressed image is available. If the * application does not need a particular callback, a null can be passed * instead of a callback method. * * This method will stop the preview. Applications should not call {@link * #stopPreview()} before this. After jpeg callback is received, * applications can call {@link #startPreview()} to restart the preview. * * @param shutter callback after the image is captured, may be null * @param raw callback with raw image data, may be null * @param jpeg callback with jpeg image data, may be null */ public final void takePicture(ShutterCallback shutter, PictureCallback raw, PictureCallback jpeg) { takePicture(shutter, raw, null, jpeg); } private native final void native_takePicture(); /** * Triggers an asynchronous image capture. The camera service will initiate * a series of callbacks to the application as the image capture progresses. * The shutter callback occurs after the image is captured. This can be used * to trigger a sound to let the user know that image has been captured. The * raw callback occurs when the raw image data is available (NOTE: the data * may be null if the hardware does not have enough memory to make a copy). * The postview callback occurs when a scaled, fully processed postview * image is available (NOTE: not all hardware supports this). The jpeg * callback occurs when the compressed image is available. If the * application does not need a particular callback, a null can be passed * instead of a callback method. * * This method will stop the preview. Applications should not call {@link * #stopPreview()} before this. After jpeg callback is received, * applications can call {@link #startPreview()} to restart the preview. * * @param shutter callback after the image is captured, may be null * @param raw callback with raw image data, may be null * @param postview callback with postview image data, may be null * @param jpeg callback with jpeg image data, may be null */ public final void takePicture(ShutterCallback shutter, PictureCallback raw, PictureCallback postview, PictureCallback jpeg) { mShutterCallback = shutter; mRawImageCallback = raw; mPostviewCallback = postview; mJpegCallback = jpeg; native_takePicture(); } /** * Zooms to the requested value smoothly. Driver will notify {@link * OnZoomChangeListener} of the zoom value and whether zoom is stopped at * the time. For example, suppose the current zoom is 0 and startSmoothZoom * is called with value 3. Method onZoomChange will be called three times * with zoom value 1, 2, and 3. The applications can call {@link * #stopSmoothZoom} to stop the zoom earlier. The applications should not * call startSmoothZoom again or change the zoom value before zoom stops. If * the passing zoom value equals to the current zoom value, no zoom callback * will be generated. This method is supported if {@link * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true. * * @param value zoom value. The valid range is 0 to {@link * android.hardware.Camera.Parameters#getMaxZoom}. * @throws IllegalArgumentException if the zoom value is invalid. * @throws RuntimeException if the method fails. */ public native final void startSmoothZoom(int value); /** * Stops the smooth zoom. The applications should wait for the {@link * OnZoomChangeListener} to know when the zoom is actually stopped. This * method is supported if {@link * android.hardware.Camera.Parameters#isSmoothZoomSupported} is true. * * @throws RuntimeException if the method fails. */ public native final void stopSmoothZoom(); /** * Set the display orientation. This affects the preview frames and the * picture displayed after snapshot. This method is useful for portrait * mode applications. * * This does not affect the order of byte array passed in * {@link PreviewCallback#onPreviewFrame}. This method is not allowed to * be called during preview. * * @param degrees the angle that the picture will be rotated clockwise. * Valid values are 0, 90, 180, and 270. The starting * position is 0 (landscape). */ public native final void setDisplayOrientation(int degrees); /** * Interface for a callback to be invoked when zoom value changes. */ public interface OnZoomChangeListener { /** * Called when the zoom value has changed. * * @param zoomValue the current zoom value. In smooth zoom mode, camera * calls this for every new zoom value. * @param stopped whether smooth zoom is stopped. If the value is true, * this is the last zoom update for the application. * * @param camera the Camera service object * @see #startSmoothZoom(int) */ void onZoomChange(int zoomValue, boolean stopped, Camera camera); }; /** * Registers a listener to be notified when the zoom value is updated by the * camera driver during smooth zoom. * * @param listener the listener to notify * @see #startSmoothZoom(int) */ public final void setZoomChangeListener(OnZoomChangeListener listener) { mZoomListener = listener; } // These match the enum in include/ui/Camera.h /** Unspecified camerar error. @see #ErrorCallback */ public static final int CAMERA_ERROR_UNKNOWN = 1; /** Media server died. In this case, the application must release the * Camera object and instantiate a new one. @see #ErrorCallback */ public static final int CAMERA_ERROR_SERVER_DIED = 100; /** * Handles the camera error callback. */ public interface ErrorCallback { /** * Callback for camera errors. * @param error error code: * <ul> * <li>{@link #CAMERA_ERROR_UNKNOWN} * <li>{@link #CAMERA_ERROR_SERVER_DIED} * </ul> * @param camera the Camera service object */ void onError(int error, Camera camera); }; /** * Registers a callback to be invoked when an error occurs. * @param cb the callback to run */ public final void setErrorCallback(ErrorCallback cb) { mErrorCallback = cb; } private native final void native_setParameters(String params); private native final String native_getParameters(); /** * Sets the Parameters for pictures from this Camera service. * * @param params the Parameters to use for this Camera service */ public void setParameters(Parameters params) { native_setParameters(params.flatten()); } /** * Returns the picture Parameters for this Camera service. */ public Parameters getParameters() { Parameters p = new Parameters(); String s = native_getParameters(); p.unflatten(s); return p; } /** * Handles the picture size (dimensions). */ public class Size { /** * Sets the dimensions for pictures. * * @param w the photo width (pixels) * @param h the photo height (pixels) */ public Size(int w, int h) { width = w; height = h; } /** * Compares {@code obj} to this size. * * @param obj the object to compare this size with. * @return {@code true} if the width and height of {@code obj} is the * same as those of this size. {@code false} otherwise. */ @Override public boolean equals(Object obj) { if (!(obj instanceof Size)) { return false; } Size s = (Size) obj; return width == s.width && height == s.height; } @Override public int hashCode() { return width * 32713 + height; } /** width of the picture */ public int width; /** height of the picture */ public int height; }; /** * Handles the parameters for pictures created by a Camera service. * * <p>To make camera parameters take effect, applications have to call * Camera.setParameters. For example, after setWhiteBalance is called, white * balance is not changed until Camera.setParameters() is called. * * <p>Different devices may have different camera capabilities, such as * picture size or flash modes. The application should query the camera * capabilities before setting parameters. For example, the application * should call getSupportedColorEffects before calling setEffect. If the * camera does not support color effects, getSupportedColorEffects will * return null. */ public class Parameters { // Parameter keys to communicate with the camera driver. private static final String KEY_PREVIEW_SIZE = "preview-size"; private static final String KEY_PREVIEW_FORMAT = "preview-format"; private static final String KEY_PREVIEW_FRAME_RATE = "preview-frame-rate"; private static final String KEY_PICTURE_SIZE = "picture-size"; private static final String KEY_PICTURE_FORMAT = "picture-format"; private static final String KEY_JPEG_THUMBNAIL_SIZE = "jpeg-thumbnail-size"; private static final String KEY_JPEG_THUMBNAIL_WIDTH = "jpeg-thumbnail-width"; private static final String KEY_JPEG_THUMBNAIL_HEIGHT = "jpeg-thumbnail-height"; private static final String KEY_JPEG_THUMBNAIL_QUALITY = "jpeg-thumbnail-quality"; private static final String KEY_JPEG_QUALITY = "jpeg-quality"; private static final String KEY_ROTATION = "rotation"; private static final String KEY_GPS_LATITUDE = "gps-latitude"; private static final String KEY_GPS_LONGITUDE = "gps-longitude"; private static final String KEY_GPS_ALTITUDE = "gps-altitude"; private static final String KEY_GPS_TIMESTAMP = "gps-timestamp"; private static final String KEY_GPS_PROCESSING_METHOD = "gps-processing-method"; private static final String KEY_WHITE_BALANCE = "whitebalance"; private static final String KEY_EFFECT = "effect"; private static final String KEY_ANTIBANDING = "antibanding"; private static final String KEY_SCENE_MODE = "scene-mode"; private static final String KEY_FLASH_MODE = "flash-mode"; private static final String KEY_FOCUS_MODE = "focus-mode"; private static final String KEY_FOCAL_LENGTH = "focal-length"; private static final String KEY_HORIZONTAL_VIEW_ANGLE = "horizontal-view-angle"; private static final String KEY_VERTICAL_VIEW_ANGLE = "vertical-view-angle"; private static final String KEY_EXPOSURE_COMPENSATION = "exposure-compensation"; private static final String KEY_MAX_EXPOSURE_COMPENSATION = "max-exposure-compensation"; private static final String KEY_MIN_EXPOSURE_COMPENSATION = "min-exposure-compensation"; private static final String KEY_EXPOSURE_COMPENSATION_STEP = "exposure-compensation-step"; private static final String KEY_ZOOM = "zoom"; private static final String KEY_MAX_ZOOM = "max-zoom"; private static final String KEY_ZOOM_RATIOS = "zoom-ratios"; private static final String KEY_ZOOM_SUPPORTED = "zoom-supported"; private static final String KEY_SMOOTH_ZOOM_SUPPORTED = "smooth-zoom-supported"; // Parameter key suffix for supported values. private static final String SUPPORTED_VALUES_SUFFIX = "-values"; private static final String TRUE = "true"; // Values for white balance settings. public static final String WHITE_BALANCE_AUTO = "auto"; public static final String WHITE_BALANCE_INCANDESCENT = "incandescent"; public static final String WHITE_BALANCE_FLUORESCENT = "fluorescent"; public static final String WHITE_BALANCE_WARM_FLUORESCENT = "warm-fluorescent"; public static final String WHITE_BALANCE_DAYLIGHT = "daylight"; public static final String WHITE_BALANCE_CLOUDY_DAYLIGHT = "cloudy-daylight"; public static final String WHITE_BALANCE_TWILIGHT = "twilight"; public static final String WHITE_BALANCE_SHADE = "shade"; // Values for color effect settings. public static final String EFFECT_NONE = "none"; public static final String EFFECT_MONO = "mono"; public static final String EFFECT_NEGATIVE = "negative"; public static final String EFFECT_SOLARIZE = "solarize"; public static final String EFFECT_SEPIA = "sepia"; public static final String EFFECT_POSTERIZE = "posterize"; public static final String EFFECT_WHITEBOARD = "whiteboard"; public static final String EFFECT_BLACKBOARD = "blackboard"; public static final String EFFECT_AQUA = "aqua"; // Values for antibanding settings. public static final String ANTIBANDING_AUTO = "auto"; public static final String ANTIBANDING_50HZ = "50hz"; public static final String ANTIBANDING_60HZ = "60hz"; public static final String ANTIBANDING_OFF = "off"; // Values for flash mode settings. /** * Flash will not be fired. */ public static final String FLASH_MODE_OFF = "off"; /** * Flash will be fired automatically when required. The flash may be fired * during preview, auto-focus, or snapshot depending on the driver. */ public static final String FLASH_MODE_AUTO = "auto"; /** * Flash will always be fired during snapshot. The flash may also be * fired during preview or auto-focus depending on the driver. */ public static final String FLASH_MODE_ON = "on"; /** * Flash will be fired in red-eye reduction mode. */ public static final String FLASH_MODE_RED_EYE = "red-eye"; /** * Constant emission of light during preview, auto-focus and snapshot. * This can also be used for video recording. */ public static final String FLASH_MODE_TORCH = "torch"; // Values for scene mode settings. public static final String SCENE_MODE_AUTO = "auto"; public static final String SCENE_MODE_ACTION = "action"; public static final String SCENE_MODE_PORTRAIT = "portrait"; public static final String SCENE_MODE_LANDSCAPE = "landscape"; public static final String SCENE_MODE_NIGHT = "night"; public static final String SCENE_MODE_NIGHT_PORTRAIT = "night-portrait"; public static final String SCENE_MODE_THEATRE = "theatre"; public static final String SCENE_MODE_BEACH = "beach"; public static final String SCENE_MODE_SNOW = "snow"; public static final String SCENE_MODE_SUNSET = "sunset"; public static final String SCENE_MODE_STEADYPHOTO = "steadyphoto"; public static final String SCENE_MODE_FIREWORKS = "fireworks"; public static final String SCENE_MODE_SPORTS = "sports"; public static final String SCENE_MODE_PARTY = "party"; public static final String SCENE_MODE_CANDLELIGHT = "candlelight"; /** * Applications are looking for a barcode. Camera driver will be * optimized for barcode reading. */ public static final String SCENE_MODE_BARCODE = "barcode"; // Values for focus mode settings. /** * Auto-focus mode. */ public static final String FOCUS_MODE_AUTO = "auto"; /** * Focus is set at infinity. Applications should not call * {@link #autoFocus(AutoFocusCallback)} in this mode. */ public static final String FOCUS_MODE_INFINITY = "infinity"; public static final String FOCUS_MODE_MACRO = "macro"; /** * Focus is fixed. The camera is always in this mode if the focus is not * adjustable. If the camera has auto-focus, this mode can fix the * focus, which is usually at hyperfocal distance. Applications should * not call {@link #autoFocus(AutoFocusCallback)} in this mode. */ public static final String FOCUS_MODE_FIXED = "fixed"; /** * Extended depth of field (EDOF). Focusing is done digitally and * continuously. Applications should not call {@link * #autoFocus(AutoFocusCallback)} in this mode. */ public static final String FOCUS_MODE_EDOF = "edof"; // Formats for setPreviewFormat and setPictureFormat. private static final String PIXEL_FORMAT_YUV422SP = "yuv422sp"; private static final String PIXEL_FORMAT_YUV420SP = "yuv420sp"; private static final String PIXEL_FORMAT_YUV422I = "yuv422i-yuyv"; private static final String PIXEL_FORMAT_RGB565 = "rgb565"; private static final String PIXEL_FORMAT_JPEG = "jpeg"; private HashMap<String, String> mMap; private Parameters() { mMap = new HashMap<String, String>(); } /** * Writes the current Parameters to the log. * @hide * @deprecated */ public void dump() { Log.e(TAG, "dump: size=" + mMap.size()); for (String k : mMap.keySet()) { Log.e(TAG, "dump: " + k + "=" + mMap.get(k)); } } /** * Creates a single string with all the parameters set in * this Parameters object. * <p>The {@link #unflatten(String)} method does the reverse. * * @return a String with all values from this Parameters object, in * semi-colon delimited key-value pairs */ public String flatten() { StringBuilder flattened = new StringBuilder(); for (String k : mMap.keySet()) { flattened.append(k); flattened.append("="); flattened.append(mMap.get(k)); flattened.append(";"); } // chop off the extra semicolon at the end flattened.deleteCharAt(flattened.length()-1); return flattened.toString(); } /** * Takes a flattened string of parameters and adds each one to * this Parameters object. * <p>The {@link #flatten()} method does the reverse. * * @param flattened a String of parameters (key-value paired) that * are semi-colon delimited */ public void unflatten(String flattened) { mMap.clear(); StringTokenizer tokenizer = new StringTokenizer(flattened, ";"); while (tokenizer.hasMoreElements()) { String kv = tokenizer.nextToken(); int pos = kv.indexOf('='); if (pos == -1) { continue; } String k = kv.substring(0, pos); String v = kv.substring(pos + 1); mMap.put(k, v); } } public void remove(String key) { mMap.remove(key); } /** * Sets a String parameter. * * @param key the key name for the parameter * @param value the String value of the parameter */ public void set(String key, String value) { if (key.indexOf('=') != -1 || key.indexOf(';') != -1) { Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ;)"); return; } if (value.indexOf('=') != -1 || value.indexOf(';') != -1) { Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ;)"); return; } mMap.put(key, value); } /** * Sets an integer parameter. * * @param key the key name for the parameter * @param value the int value of the parameter */ public void set(String key, int value) { mMap.put(key, Integer.toString(value)); } /** * Returns the value of a String parameter. * * @param key the key name for the parameter * @return the String value of the parameter */ public String get(String key) { return mMap.get(key); } /** * Returns the value of an integer parameter. * * @param key the key name for the parameter * @return the int value of the parameter */ public int getInt(String key) { return Integer.parseInt(mMap.get(key)); } /** * Sets the dimensions for preview pictures. * * @param width the width of the pictures, in pixels * @param height the height of the pictures, in pixels */ public void setPreviewSize(int width, int height) { String v = Integer.toString(width) + "x" + Integer.toString(height); set(KEY_PREVIEW_SIZE, v); } /** * Returns the dimensions setting for preview pictures. * * @return a Size object with the height and width setting * for the preview picture */ public Size getPreviewSize() { String pair = get(KEY_PREVIEW_SIZE); return strToSize(pair); } /** * Gets the supported preview sizes. * * @return a list of Size object. This method will always return a list * with at least one element. */ public List<Size> getSupportedPreviewSizes() { String str = get(KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX); return splitSize(str); } /** * Sets the dimensions for EXIF thumbnail in Jpeg picture. If * applications set both width and height to 0, EXIF will not contain * thumbnail. * * @param width the width of the thumbnail, in pixels * @param height the height of the thumbnail, in pixels */ public void setJpegThumbnailSize(int width, int height) { set(KEY_JPEG_THUMBNAIL_WIDTH, width); set(KEY_JPEG_THUMBNAIL_HEIGHT, height); } /** * Returns the dimensions for EXIF thumbnail in Jpeg picture. * * @return a Size object with the height and width setting for the EXIF * thumbnails */ public Size getJpegThumbnailSize() { return new Size(getInt(KEY_JPEG_THUMBNAIL_WIDTH), getInt(KEY_JPEG_THUMBNAIL_HEIGHT)); } /** * Gets the supported jpeg thumbnail sizes. * * @return a list of Size object. This method will always return a list * with at least two elements. Size 0,0 (no thumbnail) is always * supported. */ public List<Size> getSupportedJpegThumbnailSizes() { String str = get(KEY_JPEG_THUMBNAIL_SIZE + SUPPORTED_VALUES_SUFFIX); return splitSize(str); } /** * Sets the quality of the EXIF thumbnail in Jpeg picture. * * @param quality the JPEG quality of the EXIF thumbnail. The range is 1 * to 100, with 100 being the best. */ public void setJpegThumbnailQuality(int quality) { set(KEY_JPEG_THUMBNAIL_QUALITY, quality); } /** * Returns the quality setting for the EXIF thumbnail in Jpeg picture. * * @return the JPEG quality setting of the EXIF thumbnail. */ public int getJpegThumbnailQuality() { return getInt(KEY_JPEG_THUMBNAIL_QUALITY); } /** * Sets Jpeg quality of captured picture. * * @param quality the JPEG quality of captured picture. The range is 1 * to 100, with 100 being the best. */ public void setJpegQuality(int quality) { set(KEY_JPEG_QUALITY, quality); } /** * Returns the quality setting for the JPEG picture. * * @return the JPEG picture quality setting. */ public int getJpegQuality() { return getInt(KEY_JPEG_QUALITY); } /** * Sets the rate at which preview frames are received. This is the * target frame rate. The actual frame rate depends on the driver. * * @param fps the frame rate (frames per second) */ public void setPreviewFrameRate(int fps) { set(KEY_PREVIEW_FRAME_RATE, fps); } /** * Returns the setting for the rate at which preview frames are * received. This is the target frame rate. The actual frame rate * depends on the driver. * * @return the frame rate setting (frames per second) */ public int getPreviewFrameRate() { return getInt(KEY_PREVIEW_FRAME_RATE); } /** * Gets the supported preview frame rates. * * @return a list of supported preview frame rates. null if preview * frame rate setting is not supported. */ public List<Integer> getSupportedPreviewFrameRates() { String str = get(KEY_PREVIEW_FRAME_RATE + SUPPORTED_VALUES_SUFFIX); return splitInt(str); } /** * Sets the image format for preview pictures. * <p>If this is never called, the default format will be * {@link android.graphics.ImageFormat#NV21}, which * uses the NV21 encoding format.</p> * * @param pixel_format the desired preview picture format, defined * by one of the {@link android.graphics.ImageFormat} constants. * (E.g., <var>ImageFormat.NV21 (default), * <var>ImageFormat.RGB_565, or * <var>ImageFormat.JPEG) * @see android.graphics.ImageFormat */ public void setPreviewFormat(int pixel_format) { String s = cameraFormatForPixelFormat(pixel_format); if (s == null) { throw new IllegalArgumentException( "Invalid pixel_format=" + pixel_format); } set(KEY_PREVIEW_FORMAT, s); } /** * Returns the image format for preview frames got from * {@link PreviewCallback}. * * @return the preview format. * @see android.graphics.ImageFormat */ public int getPreviewFormat() { return pixelFormatForCameraFormat(get(KEY_PREVIEW_FORMAT)); } /** * Gets the supported preview formats. * * @return a list of supported preview formats. This method will always * return a list with at least one element. * @see android.graphics.ImageFormat */ public List<Integer> getSupportedPreviewFormats() { String str = get(KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX); ArrayList<Integer> formats = new ArrayList Other Android examples (source code examples)Here is a short list of links related to this Android Camera.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.