public class

Camera

extends Object
java.lang.Object
   ↳ android.hardware.Camera

Class Overview

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.

There is no default constructor for this class. Use open() to get a Camera object.

In order to use the device camera, you must declare the CAMERA permission in your Android Manifest. Also be sure to include the <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:

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

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.

Summary

Nested Classes
interface Camera.AutoFocusCallback Handles the callback for the camera auto focus. 
interface Camera.ErrorCallback Handles the camera error callback. 
class Camera.Parameters Handles the parameters for pictures created by a Camera service. 
interface Camera.PictureCallback Handles the callback for when a picture is taken. 
interface Camera.PreviewCallback Used to get a copy of each preview frame. 
interface Camera.ShutterCallback An interface which contains a callback for the shutter closing after taking a picture. 
class Camera.Size Handles the picture size (dimensions). 
Constants
int CAMERA_ERROR_SERVER_DIED Media server died.
int CAMERA_ERROR_UNKNOWN Unspecified camerar error.
Public Methods
final void autoFocus(Camera.AutoFocusCallback cb)
Starts auto-focus function and registers a callback function to run when camera is focused.
final void cancelAutoFocus()
Cancels auto-focus function.
Camera.Parameters getParameters()
Returns the picture Parameters for this Camera service.
final void lock()
Lock the camera to prevent other processes from accessing it.
static Camera open()
Returns a new Camera object.
final void release()
Disconnects and releases the Camera object resources.
final void setErrorCallback(Camera.ErrorCallback cb)
Registers a callback to be invoked when an error occurs.
final void setOneShotPreviewCallback(Camera.PreviewCallback cb)
Installs a callback to retrieve a single preview frame, after which the callback is cleared.
void setParameters(Camera.Parameters params)
Sets the Parameters for pictures from this Camera service.
final void setPreviewCallback(Camera.PreviewCallback cb)
Can be called at any time to instruct the camera to use a callback for each preview frame in addition to displaying it.
final void setPreviewDisplay(SurfaceHolder holder)
Sets the SurfaceHolder to be used for a picture preview.
final void startPreview()
Start drawing preview frames to the surface.
final void stopPreview()
Stop drawing preview frames to the surface.
final void takePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback postview, Camera.PictureCallback jpeg)
Triggers an asynchronous image capture.
final void takePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg)
Triggers an asynchronous image capture.
final void unlock()
Unlock the camera to allow another process to access it.
Protected Methods
void finalize()
Is called before the object's memory is being reclaimed by the VM.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int CAMERA_ERROR_SERVER_DIED

Since: API Level 1

Media server died. In this case, the application must release the Camera object and instantiate a new one. @see #ErrorCallback

Constant Value: 100 (0x00000064)

public static final int CAMERA_ERROR_UNKNOWN

Since: API Level 1

Unspecified camerar error. @see #ErrorCallback

Constant Value: 1 (0x00000001)

Public Methods

public final void autoFocus (Camera.AutoFocusCallback cb)

Since: API Level 1

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 getFocusMode() to determine if this method should be called. If the camera does not support auto-focus, it is a no-op and onAutoFocus(boolean, Camera) callback will be called immediately.

If your application should not be installed on devices without auto-focus, you must declare that your application uses auto-focus with the <uses-feature> manifest element.

If the current flash mode is not FLASH_MODE_OFF, flash may be fired during auto-focus depending on the driver.

Parameters
cb the callback to run

public final void cancelAutoFocus ()

Since: API Level 5

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 Camera.Parameters getParameters ()

Since: API Level 1

Returns the picture Parameters for this Camera service.

public final void lock ()

Since: API Level 5

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 static Camera open ()

Since: API Level 1

Returns a new Camera object.

public final void release ()

Since: API Level 1

Disconnects and releases the Camera object resources.

It is recommended that you call this as soon as you're done with the Camera object.

public final void setErrorCallback (Camera.ErrorCallback cb)

Since: API Level 1

Registers a callback to be invoked when an error occurs.

Parameters
cb the callback to run

public final void setOneShotPreviewCallback (Camera.PreviewCallback cb)

Since: API Level 3

Installs a callback to retrieve a single preview frame, after which the callback is cleared.

Parameters
cb A callback object that receives a copy of the preview frame.

public void setParameters (Camera.Parameters params)

Since: API Level 1

Sets the Parameters for pictures from this Camera service.

Parameters
params the Parameters to use for this Camera service

public final void setPreviewCallback (Camera.PreviewCallback cb)

Since: API Level 1

Can be called at any time to instruct the camera to use a callback for each preview frame in addition to displaying it.

Parameters
cb A callback object that receives a copy of each preview frame. Pass null to stop receiving callbacks at any time.

public final void setPreviewDisplay (SurfaceHolder holder)

Since: API Level 1

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.

Parameters
holder the SurfaceHolder upon which to place the picture preview
Throws
IOException if the method fails.

public final void startPreview ()

Since: API Level 1

Start drawing preview frames to the surface.

public final void stopPreview ()

Since: API Level 1

Stop drawing preview frames to the surface.

public final void takePicture (Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback postview, Camera.PictureCallback jpeg)

Since: API Level 5

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 stopPreview() before this. After jpeg callback is received, applications can call startPreview() to restart the preview.

Parameters
shutter callback after the image is captured, may be null
raw callback with raw image data, may be null
postview callback with postview image data, may be null
jpeg callback with jpeg image data, may be null

public final void takePicture (Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg)

Since: API Level 1

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 stopPreview() before this. After jpeg callback is received, applications can call startPreview() to restart the preview.

Parameters
shutter callback after the image is captured, may be null
raw callback with raw image data, may be null
jpeg callback with jpeg image data, may be null

public final void unlock ()

Since: API Level 5

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.

Protected Methods

protected void finalize ()

Since: API Level 1

Is called before the object's memory is being reclaimed by the VM. This can only happen once the VM has detected, during a run of the garbage collector, that the object is no longer reachable by any thread of the running application.

The method can be used to free system resources or perform other cleanup before the object is garbage collected. The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required. Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored.

Note that the VM does guarantee that finalize() is called at most once for any object, but it doesn't guarantee when (if at all) finalize() will be called. For example, object B's finalize() can delay the execution of object A's finalize() method and therefore it can delay the reclamation of A's memory. To be safe, use a ReferenceQueue, because it provides more control over the way the VM deals with references during garbage collection.