Camera | API reference | Android Developers (original) (raw)
public class Camera
extends [Object](/reference/java/lang/Object)
``
This class was deprecated in API level 21.
We recommend using the new [android.hardware.camera2](/reference/android/hardware/camera2/package-summary)
API for new applications.
The Camera class is used to set image capture settings, start/stop preview, snap pictures, and retrieve frames for encoding for video. This class is a client for the Camera service, which manages the actual camera hardware.
To access the device camera, you must declare the[Manifest.permission.CAMERA](/reference/android/Manifest.permission#CAMERA)
permission in your Android Manifest. Also be sure to include the manifest element 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:
To take pictures with this class, use the following steps:
- Obtain an instance of Camera from
[open(int)](/reference/android/hardware/Camera#open%28int%29)
. - Get existing (default) settings with
[getParameters()](/reference/android/hardware/Camera#getParameters%28%29)
. - If necessary, modify the returned
[Camera.Parameters](/reference/android/hardware/Camera.Parameters)
object and call[setParameters(android.hardware.Camera.Parameters)](/reference/android/hardware/Camera#setParameters%28android.hardware.Camera.Parameters%29)
. - Call
[setDisplayOrientation(int)](/reference/android/hardware/Camera#setDisplayOrientation%28int%29)
to ensure correct orientation of preview. - Important: Pass a fully initialized
[SurfaceHolder](/reference/android/view/SurfaceHolder)
to[setPreviewDisplay(android.view.SurfaceHolder)](/reference/android/hardware/Camera#setPreviewDisplay%28android.view.SurfaceHolder%29)
. Without a surface, the camera will be unable to start the preview. - Important: Call
[startPreview()](/reference/android/hardware/Camera#startPreview%28%29)
to start updating the preview surface. Preview must be started before you can take a picture. - When you want, call
[takePicture(android.hardware.Camera.ShutterCallback, android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback)](/reference/android/hardware/Camera#takePicture%28android.hardware.Camera.ShutterCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback%29)
to capture a photo. Wait for the callbacks to provide the actual image data. - After taking a picture, preview display will have stopped. To take more photos, call
[startPreview()](/reference/android/hardware/Camera#startPreview%28%29)
again first. - Call
[stopPreview()](/reference/android/hardware/Camera#stopPreview%28%29)
to stop updating the preview surface. - Important: Call
[release()](/reference/android/hardware/Camera#release%28%29)
to release the camera for use by other applications. Applications should release the camera immediately in[Activity.onPause()](/reference/android/app/Activity#onPause%28%29)
(and re-[open()](/reference/android/hardware/Camera#open%28%29)
it in[Activity.onResume()](/reference/android/app/Activity#onResume%28%29)
).
To quickly switch to video recording mode, use these steps:
- Obtain and initialize a Camera and start preview as described above.
- Call
[unlock()](/reference/android/hardware/Camera#unlock%28%29)
to allow the media process to access the camera. - Pass the camera to
[MediaRecorder.setCamera(Camera)](/reference/android/media/MediaRecorder#setCamera%28android.hardware.Camera%29)
. See[MediaRecorder](/reference/android/media/MediaRecorder)
information about video recording. - When finished recording, call
[reconnect()](/reference/android/hardware/Camera#reconnect%28%29)
to re-acquire and re-lock the camera. - If desired, restart preview and take more photos or videos.
- Call
[stopPreview()](/reference/android/hardware/Camera#stopPreview%28%29)
and[release()](/reference/android/hardware/Camera#release%28%29)
as described above.
This class is not thread-safe, and is meant for use from one event thread. Most long-running operations (preview, focus, photo capture, etc) happen asynchronously and invoke callbacks as necessary. Callbacks will be invoked on the event thread [open(int)](/reference/android/hardware/Camera#open%28int%29)
was called from. This class's methods must never be called from multiple threads at once.
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.
Developer Guides
For more information about using cameras, read theCamera developer guide.
Summary
Nested classes | |
---|---|
class | Camera.Area This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
interface | Camera.AutoFocusCallback This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
interface | Camera.AutoFocusMoveCallback This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
class | Camera.CameraInfo This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
interface | Camera.ErrorCallback This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
class | Camera.Face This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
interface | Camera.FaceDetectionListener This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
interface | Camera.OnZoomChangeListener This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
class | Camera.Parameters This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
interface | Camera.PictureCallback This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
interface | Camera.PreviewCallback This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
interface | Camera.ShutterCallback This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
class | Camera.Size This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications. |
Constants | |
---|---|
String | ACTION_NEW_PICTURE Broadcast Action: A new picture is taken by the camera, and the entry of the picture has been added to the media store. |
String | ACTION_NEW_VIDEO Broadcast Action: A new video is recorded by the camera, and the entry of the video has been added to the media store. |
int | CAMERA_ERROR_EVICTED Camera was disconnected due to use by higher priority user. |
int | CAMERA_ERROR_SERVER_DIED Media server died. |
int | CAMERA_ERROR_UNKNOWN Unspecified camera error. |
Public methods | |
---|---|
final void | addCallbackBuffer(byte[] callbackBuffer) Adds a pre-allocated buffer to the preview callback buffer queue. |
final void | autoFocus(Camera.AutoFocusCallback cb) Starts camera auto-focus and registers a callback function to run when the camera is focused. |
final void | cancelAutoFocus() Cancels any auto-focus function in progress. |
final boolean | enableShutterSound(boolean enabled) Enable or disable the default shutter sound when taking a picture. |
static void | getCameraInfo(int cameraId, Camera.CameraInfo cameraInfo) Returns the information about a particular camera. |
static int | getNumberOfCameras() Returns the number of physical cameras available on this device. |
Camera.Parameters | getParameters() Returns the current settings for this Camera service. |
final void | lock() Re-locks the camera to prevent other processes from accessing it. |
staticCamera | open() Creates a new Camera object to access the first back-facing camera on the device. |
staticCamera | open(int cameraId) Creates a new Camera object to access a particular hardware camera. |
final void | reconnect() Reconnects to the camera service after another process used it. |
final void | release() Disconnects and releases the Camera object resources. |
void | setAutoFocusMoveCallback(Camera.AutoFocusMoveCallback cb) Sets camera auto-focus move callback. |
final void | setDisplayOrientation(int degrees) Set the clockwise rotation of preview display in degrees. |
final void | setErrorCallback(Camera.ErrorCallback cb) Registers a callback to be invoked when an error occurs. |
final void | setFaceDetectionListener(Camera.FaceDetectionListener listener) Registers a listener to be notified about the faces detected in the preview frame. |
final void | setOneShotPreviewCallback(Camera.PreviewCallback cb) Installs a callback to be invoked for the next preview frame in addition to displaying it on the screen. |
void | setParameters(Camera.Parameters params) Changes the settings for this Camera service. |
final void | setPreviewCallback(Camera.PreviewCallback cb) Installs a callback to be invoked for every preview frame in addition to displaying them on the screen. |
final void | setPreviewCallbackWithBuffer(Camera.PreviewCallback cb) Installs a callback to be invoked for every preview frame, using buffers supplied with addCallbackBuffer(byte[]), in addition to displaying them on the screen. |
final void | setPreviewDisplay(SurfaceHolder holder) Sets the Surface to be used for live preview. |
final void | setPreviewTexture(SurfaceTexture surfaceTexture) Sets the SurfaceTexture to be used for live preview. |
final void | setZoomChangeListener(Camera.OnZoomChangeListener listener) Registers a listener to be notified when the zoom value is updated by the camera driver during smooth zoom. |
final void | startFaceDetection() Starts the face detection. |
final void | startPreview() Starts capturing and drawing preview frames to the screen. |
final void | startSmoothZoom(int value) Zooms to the requested value smoothly. |
final void | stopFaceDetection() Stops the face detection. |
final void | stopPreview() Stops capturing and drawing preview frames to the surface, and resets the camera for a future call to startPreview(). |
final void | stopSmoothZoom() Stops the smooth zoom. |
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) Equivalent to takePicture(Shutter, raw, null, jpeg). |
final void | unlock() Unlocks the camera to allow another process to access it. |
Protected methods | |
---|---|
void | finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
Inherited methods |
---|
From class java.lang.Object Object clone() Creates and returns a copy of this object. boolean equals(Object obj) Indicates whether some other object is "equal to" this one. void finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. finalClass<?> getClass() Returns the runtime class of this Object. int hashCode() Returns a hash code value for the object. final void notify() Wakes up a single thread that is waiting on this object's monitor. final void notifyAll() Wakes up all threads that are waiting on this object's monitor. String toString() Returns a string representation of the object. final void wait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. final void wait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. final void wait() Causes the current thread to wait until it is awakened, typically by being notified or interrupted. |
Constants
ACTION_NEW_PICTURE
public static final String ACTION_NEW_PICTURE
Broadcast Action: A new picture is taken by the camera, and the entry of the picture has been added to the media store.[Intent.getData()](/reference/android/content/Intent#getData%28%29)
is URI of the picture.
In [Android N](/reference/android/os/Build.VERSION%5FCODES#N)
this broadcast was removed, and applications are recommended to use[JobInfo.Builder](/reference/android/app/job/JobInfo.Builder)
.[JobInfo.Builder.addTriggerContentUri(TriggerContentUri)](/reference/android/app/job/JobInfo.Builder#addTriggerContentUri%28android.app.job.JobInfo.TriggerContentUri%29)
instead.
In [Android O](/reference/android/os/Build.VERSION%5FCODES#O)
this broadcast has been brought back, but only for registered receivers. Apps that are actively running can again listen to the broadcast if they want an immediate clear signal about a picture being taken, however anything doing heavy work (or needing to be launched) as a result of this should still use JobScheduler.
Constant Value: "android.hardware.action.NEW_PICTURE"
ACTION_NEW_VIDEO
public static final String ACTION_NEW_VIDEO
Broadcast Action: A new video is recorded by the camera, and the entry of the video has been added to the media store.[Intent.getData()](/reference/android/content/Intent#getData%28%29)
is URI of the video.
In [Android N](/reference/android/os/Build.VERSION%5FCODES#N)
this broadcast was removed, and applications are recommended to use[JobInfo.Builder](/reference/android/app/job/JobInfo.Builder)
.[JobInfo.Builder.addTriggerContentUri(TriggerContentUri)](/reference/android/app/job/JobInfo.Builder#addTriggerContentUri%28android.app.job.JobInfo.TriggerContentUri%29)
instead.
In [Android O](/reference/android/os/Build.VERSION%5FCODES#O)
this broadcast has been brought back, but only for registered receivers. Apps that are actively running can again listen to the broadcast if they want an immediate clear signal about a video being taken, however anything doing heavy work (or needing to be launched) as a result of this should still use JobScheduler.
Constant Value: "android.hardware.action.NEW_VIDEO"
CAMERA_ERROR_EVICTED
public static final int CAMERA_ERROR_EVICTED
Camera was disconnected due to use by higher priority user.
Constant Value: 2 (0x00000002)
CAMERA_ERROR_SERVER_DIED
public static final int CAMERA_ERROR_SERVER_DIED
Media server died. In this case, the application must release the Camera object and instantiate a new one.
Constant Value: 100 (0x00000064)
CAMERA_ERROR_UNKNOWN
public static final int CAMERA_ERROR_UNKNOWN
Unspecified camera error.
Constant Value: 1 (0x00000001)
Public methods
autoFocus
public final void autoFocus (Camera.AutoFocusCallback cb)
Starts camera auto-focus and registers a callback function to run when the camera is focused. This method is only valid when preview is active (between [startPreview()](/reference/android/hardware/Camera#startPreview%28%29)
and before [stopPreview()](/reference/android/hardware/Camera#stopPreview%28%29)
).
Callers should check[Camera.Parameters.getFocusMode()](/reference/android/hardware/Camera.Parameters#getFocusMode%28%29)
to determine if this method should be called. If the camera does not support auto-focus, it is a no-op and [AutoFocusCallback.onAutoFocus(boolean, Camera)](/reference/android/hardware/Camera.AutoFocusCallback#onAutoFocus%28boolean,%20android.hardware.Camera%29)
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 manifest element.
If the current flash mode is not[Camera.Parameters.FLASH_MODE_OFF](/reference/android/hardware/Camera.Parameters#FLASH%5FMODE%5FOFF)
, flash may be fired during auto-focus, depending on the driver and camera hardware.
Auto-exposure lock [Camera.Parameters.getAutoExposureLock()](/reference/android/hardware/Camera.Parameters#getAutoExposureLock%28%29)
and auto-white balance locks [Camera.Parameters.getAutoWhiteBalanceLock()](/reference/android/hardware/Camera.Parameters#getAutoWhiteBalanceLock%28%29)
do not change during and after autofocus. But auto-focus routine may stop auto-exposure and auto-white balance transiently during focusing.
Stopping preview with [stopPreview()](/reference/android/hardware/Camera#stopPreview%28%29)
, or triggering still image capture with [takePicture(android.hardware.Camera.ShutterCallback, android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback)](/reference/android/hardware/Camera#takePicture%28android.hardware.Camera.ShutterCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback%29)
, will not change the the focus position. Applications must call cancelAutoFocus to reset the focus.
If autofocus is successful, consider using[MediaActionSound](/reference/android/media/MediaActionSound)
to properly play back an autofocus success sound to the user.
Parameters | |
---|---|
cb | Camera.AutoFocusCallback: the callback to run |
Throws | |
---|---|
RuntimeException | if starting autofocus fails; usually this would be because of a hardware or other low-level error, or because release() has been called on this Camera instance. |
See also:
[cancelAutoFocus()](/reference/android/hardware/Camera#cancelAutoFocus%28%29)
[Camera.Parameters.setAutoExposureLock(boolean)](/reference/android/hardware/Camera.Parameters#setAutoExposureLock%28boolean%29)
[Camera.Parameters.setAutoWhiteBalanceLock(boolean)](/reference/android/hardware/Camera.Parameters#setAutoWhiteBalanceLock%28boolean%29)
[MediaActionSound](/reference/android/media/MediaActionSound)
cancelAutoFocus
public final void cancelAutoFocus ()
Cancels any auto-focus function in progress. Whether or not auto-focus is currently in progress, this function will return the focus position to the default. If the camera does not support auto-focus, this is a no-op.
Throws | |
---|---|
RuntimeException | if canceling autofocus fails; usually this would be because of a hardware or other low-level error, or because release() has been called on this Camera instance. |
enableShutterSound
public final boolean enableShutterSound (boolean enabled)
Enable or disable the default shutter sound when taking a picture.
By default, the camera plays the system-defined camera shutter sound when [takePicture(ShutterCallback, PictureCallback, PictureCallback)](/reference/android/hardware/Camera#takePicture%28android.hardware.Camera.ShutterCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback%29)
is called. Using this method, the shutter sound can be disabled. It is strongly recommended that an alternative shutter sound is played in the [ShutterCallback](/reference/android/hardware/Camera.ShutterCallback)
when the system shutter sound is disabled.
Note that devices may not always allow disabling the camera shutter sound. If the shutter sound state cannot be set to the desired value, this method will return false. [CameraInfo.canDisableShutterSound](/reference/android/hardware/Camera.CameraInfo#canDisableShutterSound)
can be used to determine whether the device will allow the shutter sound to be disabled.
Parameters | |
---|---|
enabled | boolean: whether the camera should play the system shutter sound when takePicture is called. |
Returns | |
---|---|
boolean | true if the shutter sound state was successfully changed. false if the shutter sound state could not be changed. true is also returned if shutter sound playback is already set to the requested state. |
Throws | |
---|---|
RuntimeException | if the call fails; usually this would be because of a hardware or other low-level error, or because release() has been called on this Camera instance. |
See also:
[takePicture(ShutterCallback, PictureCallback, PictureCallback)](/reference/android/hardware/Camera#takePicture%28android.hardware.Camera.ShutterCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback%29)
[Camera.CameraInfo.canDisableShutterSound](/reference/android/hardware/Camera.CameraInfo#canDisableShutterSound)
[Camera.ShutterCallback](/reference/android/hardware/Camera.ShutterCallback)
getCameraInfo
public static void getCameraInfo (int cameraId, Camera.CameraInfo cameraInfo)
Returns the information about a particular camera. If [getNumberOfCameras()](/reference/android/hardware/Camera#getNumberOfCameras%28%29)
returns N, the valid id is 0 to N-1.
Parameters | |
---|---|
cameraId | int |
cameraInfo | Camera.CameraInfo |
Throws | |
---|---|
RuntimeException | if an invalid ID is provided, or if there is an error retrieving the information (generally due to a hardware or other low-level failure). |
getNumberOfCameras
public static int getNumberOfCameras ()
Returns the number of physical cameras available on this device. The return value of this method might change dynamically if the device supports external cameras and an external camera is connected or disconnected. If there is a[logical multi-camera](/reference/android/hardware/camera2/CameraMetadata#REQUEST%5FAVAILABLE%5FCAPABILITIES%5FLOGICAL%5FMULTI%5FCAMERA)
in the system, to maintain app backward compatibility, this method will only expose one camera per facing for all logical camera and physical camera groups. Use camera2 API to see all cameras.
Returns | |
---|---|
int | total number of accessible camera devices, or 0 if there are no cameras or an error was encountered enumerating them. |
lock
public final void lock ()
Re-locks the camera to prevent other processes from accessing it. Camera objects are locked by default unless [unlock()](/reference/android/hardware/Camera#unlock%28%29)
is called. Normally [reconnect()](/reference/android/hardware/Camera#reconnect%28%29)
is used instead.
Since API level 14, camera is automatically locked for applications in[MediaRecorder.start()](/reference/android/media/MediaRecorder#start%28%29)
. Applications can use the camera (ex: zoom) after recording starts. There is no need to call this after recording starts or stops.
If you are not recording video, you probably do not need this method.
Throws | |
---|---|
RuntimeException | if the camera cannot be re-locked (for example, if the camera is still in use by another process). |
open
public static Camera open ()
Creates a new Camera object to access the first back-facing camera on the device. If the device does not have a back-facing camera, this returns null. Otherwise acts like the [open(int)](/reference/android/hardware/Camera#open%28int%29)
call.
Returns | |
---|---|
Camera | a new Camera object for the first back-facing camera, or null if there is no backfacing camera |
open
public static Camera open (int cameraId)
Creates a new Camera object to access a particular hardware camera. If the same camera is opened by other applications, this will throw a RuntimeException.
You must call [release()](/reference/android/hardware/Camera#release%28%29)
when you are done using the camera, otherwise it will remain locked and be unavailable to other applications.
Your application should only have one Camera object active at a time for a particular hardware camera.
Callbacks from other methods are delivered to the event loop of the thread which called open(). If this thread has no event loop, then callbacks are delivered to the main application event loop. If there is no main application event loop, callbacks are not delivered.
Caution: On some devices, this method may take a long time to complete. It is best to call this method from a worker thread (possibly using [AsyncTask](/reference/android/os/AsyncTask)
) to avoid blocking the main application UI thread.
Parameters | |
---|---|
cameraId | int: the hardware camera to access, between 0 andgetNumberOfCameras()-1. |
Returns | |
---|---|
Camera | a new Camera object, connected, locked and ready for use. |
Throws | |
---|---|
RuntimeException | if opening the camera fails (for example, if the camera is in use by another process or device policy manager has disabled the camera). |
reconnect
public final void reconnect ()
Reconnects to the camera service after another process used it. After [unlock()](/reference/android/hardware/Camera#unlock%28%29)
is called, another process may use the camera; when the process is done, you must reconnect to the camera, which will re-acquire the lock and allow you to continue using the camera.
Since API level 14, camera is automatically locked for applications in[MediaRecorder.start()](/reference/android/media/MediaRecorder#start%28%29)
. Applications can use the camera (ex: zoom) after recording starts. There is no need to call this after recording starts or stops.
If you are not recording video, you probably do not need this method.
Throws | |
---|---|
IOException | if a connection cannot be re-established (for example, if the camera is still in use by another process). |
RuntimeException | if release() has been called on this Camera instance. |
release
public final void release ()
Disconnects and releases the Camera object resources.
You must call this as soon as you're done with the Camera object.
setAutoFocusMoveCallback
public void setAutoFocusMoveCallback (Camera.AutoFocusMoveCallback cb)
Sets camera auto-focus move callback.
Parameters | |
---|---|
cb | Camera.AutoFocusMoveCallback: the callback to run |
Throws | |
---|---|
RuntimeException | if enabling the focus move callback fails; usually this would be because of a hardware or other low-level error, or because release() has been called on this Camera instance. |
setDisplayOrientation
public final void setDisplayOrientation (int degrees)
Set the clockwise rotation of preview display in degrees. This affects the preview frames and the picture displayed after snapshot. This method is useful for portrait mode applications. Note that preview display of front-facing cameras is flipped horizontally before the rotation, that is, the image is reflected along the central vertical axis of the camera sensor. So the users can see themselves as looking into a mirror.
This does not affect the order of byte array passed in [Camera.PreviewCallback.onPreviewFrame(byte, Camera)](/reference/android/hardware/Camera.PreviewCallback#onPreviewFrame%28byte[],%20android.hardware.Camera%29)
, JPEG pictures, or recorded videos. This method is not allowed to be called during preview.
If you want to make the camera image show in the same orientation as the display, you can use the following code.
public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) { android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); android.hardware.Camera.getCameraInfo(cameraId, info); int rotation = activity.getWindowManager().getDefaultDisplay() .getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; }
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
Starting from API level 14, this method can be called when preview is active.
Note: Before API level 24, the default value for orientation is 0. Starting in API level 24, the default orientation will be such that applications in forced-landscape mode will have correct preview orientation, which may be either a default of 0 or 180. Applications that operate in portrait mode or allow for changing orientation must still call this method after each orientation change to ensure correct preview display in all cases.
Parameters | |
---|---|
degrees | int: the angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270. |
Throws | |
---|---|
RuntimeException | if setting orientation fails; usually this would be because of a hardware or other low-level error, or because release() has been called on this Camera instance. |
setErrorCallback
public final void setErrorCallback (Camera.ErrorCallback cb)
Registers a callback to be invoked when an error occurs.
Parameters | |
---|---|
cb | Camera.ErrorCallback: The callback to run |
setFaceDetectionListener
public final void setFaceDetectionListener (Camera.FaceDetectionListener listener)
Registers a listener to be notified about the faces detected in the preview frame.
Parameters | |
---|---|
listener | Camera.FaceDetectionListener: the listener to notify |
setOneShotPreviewCallback
public final void setOneShotPreviewCallback (Camera.PreviewCallback cb)
Installs a callback to be invoked for the next preview frame in addition to displaying it on the screen. After one invocation, the callback is cleared. This method can be called any time, even when preview is live. Any other preview callbacks are overridden.
If you are using the preview data to create video or still images, strongly consider using [MediaActionSound](/reference/android/media/MediaActionSound)
to properly indicate image capture or recording start/stop to the user.
Parameters | |
---|---|
cb | Camera.PreviewCallback: a callback object that receives a copy of the next preview frame, or null to stop receiving callbacks. |
Throws | |
---|---|
RuntimeException | if release() has been called on this Camera instance. |
setParameters
public void setParameters (Camera.Parameters params)
Changes the settings for this Camera service.
Parameters | |
---|---|
params | Camera.Parameters: the Parameters to use for this Camera service |
Throws | |
---|---|
RuntimeException | if any parameter is invalid or not supported. |
setPreviewCallback
public final void setPreviewCallback (Camera.PreviewCallback cb)
Installs a callback to be invoked for every preview frame in addition to displaying them on the screen. The callback will be repeatedly called for as long as preview is active. This method can be called at any time, even while preview is live. Any other preview callbacks are overridden.
If you are using the preview data to create video or still images, strongly consider using [MediaActionSound](/reference/android/media/MediaActionSound)
to properly indicate image capture or recording start/stop to the user.
Parameters | |
---|---|
cb | Camera.PreviewCallback: a callback object that receives a copy of each preview frame, or null to stop receiving callbacks. |
Throws | |
---|---|
RuntimeException | if release() has been called on this Camera instance. |
setPreviewCallbackWithBuffer
public final void setPreviewCallbackWithBuffer (Camera.PreviewCallback cb)
Installs a callback to be invoked for every preview frame, using buffers supplied with [addCallbackBuffer(byte[])](/reference/android/hardware/Camera#addCallbackBuffer%28byte[]%29)
, in addition to displaying them on the screen. The callback will be repeatedly called for as long as preview is active and buffers are available. Any other preview callbacks are overridden.
The purpose of this method is to improve preview efficiency and frame rate by allowing preview frame memory reuse. You must call[addCallbackBuffer(byte[])](/reference/android/hardware/Camera#addCallbackBuffer%28byte[]%29)
at some point -- before or after calling this method -- or no callbacks will received.
The buffer queue will be cleared if this method is called with a null callback, [setPreviewCallback(android.hardware.Camera.PreviewCallback)](/reference/android/hardware/Camera#setPreviewCallback%28android.hardware.Camera.PreviewCallback%29)
is called, or [setOneShotPreviewCallback(android.hardware.Camera.PreviewCallback)](/reference/android/hardware/Camera#setOneShotPreviewCallback%28android.hardware.Camera.PreviewCallback%29)
is called.
If you are using the preview data to create video or still images, strongly consider using [MediaActionSound](/reference/android/media/MediaActionSound)
to properly indicate image capture or recording start/stop to the user.
Parameters | |
---|---|
cb | Camera.PreviewCallback: a callback object that receives a copy of the preview frame, or null to stop receiving callbacks and clear the buffer queue. |
Throws | |
---|---|
RuntimeException | if release() has been called on this Camera instance. |
setPreviewDisplay
public final void setPreviewDisplay (SurfaceHolder holder)
Sets the [Surface](/reference/android/view/Surface)
to be used for live preview. Either a surface or surface texture is necessary for preview, and preview is necessary to take pictures. The same surface can be re-set without harm. Setting a preview surface will un-set any preview surface texture that was set via [setPreviewTexture(SurfaceTexture)](/reference/android/hardware/Camera#setPreviewTexture%28android.graphics.SurfaceTexture%29)
.
The [SurfaceHolder](/reference/android/view/SurfaceHolder)
must already contain a surface when this method is called. If you are using [SurfaceView](/reference/android/view/SurfaceView)
, you will need to register a [SurfaceHolder.Callback](/reference/android/view/SurfaceHolder.Callback)
with[SurfaceHolder.addCallback(SurfaceHolder.Callback)](/reference/android/view/SurfaceHolder#addCallback%28android.view.SurfaceHolder.Callback%29)
and wait for[SurfaceHolder.Callback.surfaceCreated(SurfaceHolder)](/reference/android/view/SurfaceHolder.Callback#surfaceCreated%28android.view.SurfaceHolder%29)
before calling setPreviewDisplay() or starting preview.
This method must be called before [startPreview()](/reference/android/hardware/Camera#startPreview%28%29)
. The one exception is that if the preview surface is not set (or set to null) before startPreview() is called, then this method may be called once with a non-null parameter to set the preview surface. (This allows camera setup and surface creation to happen in parallel, saving time.) The preview surface may not otherwise change while preview is running.
Parameters | |
---|---|
holder | SurfaceHolder: containing the Surface on which to place the preview, or null to remove the preview surface |
Throws | |
---|---|
IOException | if the method fails (for example, if the surface is unavailable or unsuitable). |
RuntimeException | if release() has been called on this Camera instance. |
setPreviewTexture
public final void setPreviewTexture (SurfaceTexture surfaceTexture)
Sets the [SurfaceTexture](/reference/android/graphics/SurfaceTexture)
to be used for live preview. Either a surface or surface texture is necessary for preview, and preview is necessary to take pictures. The same surface texture can be re-set without harm. Setting a preview surface texture will un-set any preview surface that was set via [setPreviewDisplay(SurfaceHolder)](/reference/android/hardware/Camera#setPreviewDisplay%28android.view.SurfaceHolder%29)
.
This method must be called before [startPreview()](/reference/android/hardware/Camera#startPreview%28%29)
. The one exception is that if the preview surface texture is not set (or set to null) before startPreview() is called, then this method may be called once with a non-null parameter to set the preview surface. (This allows camera setup and surface creation to happen in parallel, saving time.) The preview surface texture may not otherwise change while preview is running.
The timestamps provided by [SurfaceTexture.getTimestamp()](/reference/android/graphics/SurfaceTexture#getTimestamp%28%29)
for a SurfaceTexture set as the preview texture have an unspecified zero point, and cannot be directly compared between different cameras or different instances of the same camera, or across multiple runs of the same program.
If you are using the preview data to create video or still images, strongly consider using [MediaActionSound](/reference/android/media/MediaActionSound)
to properly indicate image capture or recording start/stop to the user.
Parameters | |
---|---|
surfaceTexture | SurfaceTexture: the SurfaceTexture to which the preview images are to be sent or null to remove the current preview surface texture |
Throws | |
---|---|
IOException | if the method fails (for example, if the surface texture is unavailable or unsuitable). |
RuntimeException | if release() has been called on this Camera instance. |
setZoomChangeListener
public final void setZoomChangeListener (Camera.OnZoomChangeListener listener)
Registers a listener to be notified when the zoom value is updated by the camera driver during smooth zoom.
Parameters | |
---|---|
listener | Camera.OnZoomChangeListener: the listener to notify |
startFaceDetection
public final void startFaceDetection ()
Starts the face detection. This should be called after preview is started. The camera will notify [FaceDetectionListener](/reference/android/hardware/Camera.FaceDetectionListener)
of the detected faces in the preview frame. The detected faces may be the same as the previous ones. Applications should call [stopFaceDetection()](/reference/android/hardware/Camera#stopFaceDetection%28%29)
to stop the face detection. This method is supported if [Camera.Parameters.getMaxNumDetectedFaces()](/reference/android/hardware/Camera.Parameters#getMaxNumDetectedFaces%28%29)
returns a number larger than 0. If the face detection has started, apps should not call this again.
When the face detection is running, [Parameters.setWhiteBalance(String)](/reference/android/hardware/Camera.Parameters#setWhiteBalance%28java.lang.String%29)
,[Parameters.setFocusAreas(List)](/reference/android/hardware/Camera.Parameters#setFocusAreas%28java.util.List<android.hardware.Camera.Area>%29)
, and [Parameters.setMeteringAreas(List)](/reference/android/hardware/Camera.Parameters#setMeteringAreas%28java.util.List<android.hardware.Camera.Area>%29)
have no effect. The camera uses the detected faces to do auto-white balance, auto exposure, and autofocus.
If the apps call [autoFocus(android.hardware.Camera.AutoFocusCallback)](/reference/android/hardware/Camera#autoFocus%28android.hardware.Camera.AutoFocusCallback%29)
, the camera will stop sending face callbacks. The last face callback indicates the areas used to do autofocus. After focus completes, face detection will resume sending face callbacks. If the apps call [cancelAutoFocus()](/reference/android/hardware/Camera#cancelAutoFocus%28%29)
, the face callbacks will also resume.
After calling [takePicture(android.hardware.Camera.ShutterCallback, android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback)](/reference/android/hardware/Camera#takePicture%28android.hardware.Camera.ShutterCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback%29)
or [stopPreview()](/reference/android/hardware/Camera#stopPreview%28%29)
, and then resuming preview with [startPreview()](/reference/android/hardware/Camera#startPreview%28%29)
, the apps should call this method again to resume face detection.
Throws | |
---|---|
IllegalArgumentException | if the face detection is unsupported. |
RuntimeException | if the method fails or the face detection is already running. |
stopFaceDetection
public final void stopFaceDetection ()
Stops the face detection.
stopPreview
public final void stopPreview ()
Stops capturing and drawing preview frames to the surface, and resets the camera for a future call to [startPreview()](/reference/android/hardware/Camera#startPreview%28%29)
.
Throws | |
---|---|
RuntimeException | if stopping preview fails; usually this would be because of a hardware or other low-level error, or because release() has been called on this Camera instance. |
takePicture
public final void takePicture (Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback postview, Camera.PictureCallback jpeg)
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 will be null if there is no raw image callback buffer available or the raw image callback buffer is not large enough to hold the raw image). 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 is only valid when preview is active (after[startPreview()](/reference/android/hardware/Camera#startPreview%28%29)
). Preview will be stopped after the image is taken; callers must call [startPreview()](/reference/android/hardware/Camera#startPreview%28%29)
again if they want to re-start preview or take more pictures. This should not be called between[MediaRecorder.start()](/reference/android/media/MediaRecorder#start%28%29)
and[MediaRecorder.stop()](/reference/android/media/MediaRecorder#stop%28%29)
.
After calling this method, you must not call [startPreview()](/reference/android/hardware/Camera#startPreview%28%29)
or take another picture until the JPEG callback has returned.
Parameters | |
---|---|
shutter | Camera.ShutterCallback: the callback for image capture moment, or null |
raw | Camera.PictureCallback: the callback for raw (uncompressed) image data, or null |
postview | Camera.PictureCallback: callback with postview image data, may be null |
jpeg | Camera.PictureCallback: the callback for JPEG image data, or null |
Throws | |
---|---|
RuntimeException | if starting picture capture fails; usually this would be because of a hardware or other low-level error, or because release() has been called on this Camera instance. |
unlock
public final void unlock ()
Unlocks the camera to allow another process to access it. Normally, the camera is locked to the process with an active Camera object until [release()](/reference/android/hardware/Camera#release%28%29)
is called. To allow rapid handoff between processes, you can call this method to release the camera temporarily for another process to use; once the other process is done you can call [reconnect()](/reference/android/hardware/Camera#reconnect%28%29)
to reclaim the camera.
This must be done before calling[MediaRecorder.setCamera(Camera)](/reference/android/media/MediaRecorder#setCamera%28android.hardware.Camera%29)
. This cannot be called after recording starts.
If you are not recording video, you probably do not need this method.
Throws | |
---|---|
RuntimeException | if the camera cannot be unlocked. |
Protected methods
finalize
protected void finalize ()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize
method to dispose of system resources or to perform other cleanup.
The general contract of finalize
is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize
method may take any action, including making this object available again to other threads; the usual purpose of finalize
, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.
The finalize
method of class Object
performs no special action; it simply returns normally. Subclasses ofObject
may override this definition.
The Java programming language does not guarantee which thread will invoke the finalize
method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.
After the finalize
method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.
The finalize
method is never invoked more than once by a Java virtual machine for any given object.
Any exception thrown by the finalize
method causes the finalization of this object to be halted, but is otherwise ignored.