android-android.hardware.Camera

来源:互联网 发布:mybatis输出sql语句 编辑:程序博客网 时间:2024/05/08 17:57

Camera

public class Camera 
extends Object 

java.lang.Object   ↳android.hardware.Camera

This class was deprecated in API level 21.
We recommend using the new android.hardware.camera2 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 CAMERA permission in your Android Manifest. Also be sure to include the <uses-feature> 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:访问设备的摄像头,你必须声明相机允许Android清单。也一定要包括< uses-feature >清单元素声明相机功能所使用的应用程序。例如,如果您使用相机和自动对焦功能,你的清单应该包括以下:

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

To take pictures with this class, use the following steps:采用这个类拍照,使用以下步骤:

  1. Obtain an instance of Camera from open(int).
  2. Get existing (default) settings with getParameters().
  3. If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
  4. Call setDisplayOrientation(int) to ensure correct orientation of preview.
  5. Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.
  6. Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.
  7. When you want, call takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback) to capture a photo. Wait for the callbacks to provide the actual image data.
  8. After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.
  9. Call stopPreview() to stop updating the preview surface.
  10. Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).
  11. 从开放获取摄像机的一个实例(int)。
  12. 得到现有(默认)设置与getparameter()。
  13. 如果有必要,修改返回相机。参数对象并调用setParameters(Camera.Parameters)。

  14. 调用setDisplayOrientation(int),以确保正确方向的预览。

  15. 重要的是:通过一个完全初始化SurfaceHolder setPreviewDisplay(SurfaceHolder)。没有一个表面,相机将无法启动预览。

  16. 重要:调用startPreview()开始更新预览表面。预览必须启动才能拍照。

  17. 当你想要的,调用takePicture(相机。ShutterCallback,相机。PictureCallback,相机。PictureCallback Camera.PictureCallback)捕捉照片。等待回调提供实际的图像数据。

  18. 拍照后,预览显示将会停止。采取更多的照片,叫startPreview()。

  19. 调用stopPreview()停止更新预览表面。

  20. 重要:调用释放()来释放相机供其他应用程序使用。应用程序应该立即释放相机onPause()(在onResume重开()())。

To quickly switch to video recording mode, use these steps:快速切换到录像模式,使用这些步骤:

  1. Obtain and initialize a Camera and start preview as described above.
  2. Call unlock() to allow the media process to access the camera.
  3. Pass the camera to setCamera(Camera). See MediaRecorder information about video recording.
  4. When finished recording, call reconnect() to re-acquire and re-lock the camera.
  5. If desired, restart preview and take more photos or videos.
  6. Call stopPreview() and release() 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) 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 the Camera developer guide.

Summary


Nested classes

classCamera.Area

This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

这个类在API级别21弃用。我们建议使用新的android.hardware。camera2 API的新应用程序。

interfaceCamera.AutoFocusCallback

This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

interfaceCamera.AutoFocusMoveCallback

This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

classCamera.CameraInfo

This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

interfaceCamera.ErrorCallback

This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

classCamera.Face

This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

interfaceCamera.FaceDetectionListener

This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

interfaceCamera.OnZoomChangeListener

This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

classCamera.Parameters

This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

interfaceCamera.PictureCallback

This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

interfaceCamera.PreviewCallback

This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

interfaceCamera.ShutterCallback

This interface was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

classCamera.Size

This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.  

Constants

StringACTION_NEW_PICTURE

This constant was deprecated in API level 24. This broadcast is no longer delivered by the system; useJobInfo.Builder.addTriggerContentUri(JobInfo.TriggerContentUri) instead. Broadcast Action: A new picture is taken by the camera, and the entry of the picture has been added to the media store. getData() is URI of the picture.

StringACTION_NEW_VIDEO

This constant was deprecated in API level 24. This broadcast is no longer delivered by the system; useJobInfo.Builder.addTriggerContentUri(JobInfo.TriggerContentUri) instead. Broadcast Action: A new video is recorded by the camera, and the entry of the video has been added to the media store. getData() is URI of the video.

intCAMERA_ERROR_EVICTED

Camera was disconnected due to use by higher priority user.

intCAMERA_ERROR_SERVER_DIED

Media server died.

intCAMERA_ERROR_UNKNOWN

Unspecified camera error.

Public methods

final voidaddCallbackBuffer(byte[] callbackBuffer)

Adds a pre-allocated buffer to the preview callback buffer queue.

final voidautoFocus(Camera.AutoFocusCallback cb)

Starts camera auto-focus and registers a callback function to run when the camera is focused.

final voidcancelAutoFocus()

Cancels any auto-focus function in progress.

final booleanenableShutterSound(boolean enabled)

Enable or disable the default shutter sound when taking a picture.

static voidgetCameraInfo(int cameraId, Camera.CameraInfo cameraInfo)

Returns the information about a particular camera.

static intgetNumberOfCameras()

Returns the number of physical cameras available on this device.

Camera.ParametersgetParameters()

Returns the current settings for this Camera service.

final voidlock()

Re-locks the camera to prevent other processes from accessing it.

static Cameraopen()

Creates a new Camera object to access the first back-facing camera on the device.

static Cameraopen(int cameraId)

Creates a new Camera object to access a particular hardware camera.

final voidreconnect()

Reconnects to the camera service after another process used it.

final voidrelease()

Disconnects and releases the Camera object resources.

voidsetAutoFocusMoveCallback(Camera.AutoFocusMoveCallback cb)

Sets camera auto-focus move callback.

final voidsetDisplayOrientation(int degrees)

Set the clockwise rotation of preview display in degrees.

final voidsetErrorCallback(Camera.ErrorCallback cb)

Registers a callback to be invoked when an error occurs.

final voidsetFaceDetectionListener(Camera.FaceDetectionListener listener)

Registers a listener to be notified about the faces detected in the preview frame.

final voidsetOneShotPreviewCallback(Camera.PreviewCallback cb)

Installs a callback to be invoked for the next preview frame in addition to displaying it on the screen.

voidsetParameters(Camera.Parameters params)

Changes the settings for this Camera service.

final voidsetPreviewCallback(Camera.PreviewCallback cb)

Installs a callback to be invoked for every preview frame in addition to displaying them on the screen.

final voidsetPreviewCallbackWithBuffer(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 voidsetPreviewDisplay(SurfaceHolder holder)

Sets the Surface to be used for live preview.

final voidsetPreviewTexture(SurfaceTexture surfaceTexture)

Sets the SurfaceTexture to be used for live preview.

final voidsetZoomChangeListener(Camera.OnZoomChangeListener listener)

Registers a listener to be notified when the zoom value is updated by the camera driver during smooth zoom.

final voidstartFaceDetection()

Starts the face detection.

final voidstartPreview()

Starts capturing and drawing preview frames to the screen.

final voidstartSmoothZoom(int value)

Zooms to the requested value smoothly.

final voidstopFaceDetection()

Stops the face detection.

final voidstopPreview()

Stops capturing and drawing preview frames to the surface, and resets the camera for a future call to startPreview().

final voidstopSmoothZoom()

Stops the smooth zoom.

final voidtakePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback postview,Camera.PictureCallback jpeg)

Triggers an asynchronous image capture.

final voidtakePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg)

Equivalent to takePicture(shutter, raw, null, jpeg).

final voidunlock()

Unlocks the camera to allow another process to access it.解锁相机让另一个进程访问它。

Protected methods

voidfinalize()

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

Constants


ACTION_NEW_PICTURE

Added in API level 14
String ACTION_NEW_PICTURE

This constant was deprecated in API level 24.
This broadcast is no longer delivered by the system; use JobInfo.Builder.addTriggerContentUri(JobInfo.TriggerContentUri) instead. Broadcast Action: A new picture is taken by the camera, and the entry of the picture has been added to the media store. getData() is URI of the picture.

Constant Value: "android.hardware.action.NEW_PICTURE"

ACTION_NEW_VIDEO

Added in API level 14
String ACTION_NEW_VIDEO

This constant was deprecated in API level 24.
This broadcast is no longer delivered by the system; use JobInfo.Builder.addTriggerContentUri(JobInfo.TriggerContentUri) instead. Broadcast Action: A new video is recorded by the camera, and the entry of the video has been added to the media store. getData() is URI of the video.

Constant Value: "android.hardware.action.NEW_VIDEO"

CAMERA_ERROR_EVICTED

Added in API level 23
int CAMERA_ERROR_EVICTED

Camera was disconnected due to use by higher priority user.

See also:

  • Camera.ErrorCallback

Constant Value: 2 (0x00000002)

CAMERA_ERROR_SERVER_DIED

Added in API level 1
int CAMERA_ERROR_SERVER_DIED

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

See also:

  • Camera.ErrorCallback

Constant Value: 100 (0x00000064)

CAMERA_ERROR_UNKNOWN

Added in API level 1
int CAMERA_ERROR_UNKNOWN

Unspecified camera error.

See also:

  • Camera.ErrorCallback

Constant Value: 1 (0x00000001)

Public methods


addCallbackBuffer

Added in API level 8
void addCallbackBuffer (byte[] callbackBuffer)

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 at least one available buffer, the buffer will be used and 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 buffers back when they finish processing the data in them.

For formats besides YV12, the size of the buffer is determined by multiplying the preview image width, height, and bytes per pixel. The width and height can be read from getPreviewSize(). Bytes per pixel can be computed from getBitsPerPixel(int) / 8, using the image format from getPreviewFormat().

If using the YV12 format, the size can be calculated using the equations listed in setPreviewFormat(int).

This method is only necessary when setPreviewCallbackWithBuffer(PreviewCallback) is used. When setPreviewCallback(PreviewCallback) orsetOneShotPreviewCallback(PreviewCallback) are used, buffers are automatically allocated. When a supplied buffer is too small to hold the preview frame data, preview callback will return null and the buffer will be removed from the buffer queue.

ParameterscallbackBufferbyte: the buffer to add to the queue. The size of the buffer must match the values described above.

See also:

  • setPreviewCallbackWithBuffer(PreviewCallback)

autoFocus

Added in API level 1
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 (betweenstartPreview() and before stopPreview()).

Callers should check getFocusMode() to determine if this method should be called. If the camera does not support auto-focus, it is a no-op andonAutoFocus(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 and camera hardware.

Auto-exposure lock getAutoExposureLock() and auto-white balance locks getAutoWhiteBalanceLock() 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(), or triggering still image capture with takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback), will not change the the focus position. Applications must call cancelAutoFocus to reset the focus.

If autofocus is successful, consider using MediaActionSound to properly play back an autofocus success sound to the user.

ParameterscbCamera.AutoFocusCallback: the callback to run

See also:

  • cancelAutoFocus()
  • setAutoExposureLock(boolean)
  • setAutoWhiteBalanceLock(boolean)
  • MediaActionSound

cancelAutoFocus

Added in API level 5
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.

See also:

  • autoFocus(Camera.AutoFocusCallback)

enableShutterSound

Added in API level 17
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(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback) is called. Using this method, the shutter sound can be disabled. It is strongly recommended that an alternative shutter sound is played in the 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. canDisableShutterSound can be used to determine whether the device will allow the shutter sound to be disabled.

Parametersenabledboolean: whether the camera should play the system shutter sound when takePicture is called.Returnsbooleantrue 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.

See also:

  • takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback)
  • canDisableShutterSound
  • Camera.ShutterCallback

getCameraInfo

Added in API level 9
void getCameraInfo (int cameraId,                 Camera.CameraInfo cameraInfo)

Returns the information about a particular camera. If getNumberOfCameras() returns N, the valid id is 0 to N-1.

ParameterscameraIdintcameraInfoCamera.CameraInfo

getNumberOfCameras

Added in API level 9
int getNumberOfCameras ()

Returns the number of physical cameras available on this device.

Returnsint 

getParameters

Added in API level 1
Camera.Parameters getParameters ()

Returns the current settings for this Camera service. If modifications are made to the returned Parameters, they must be passed tosetParameters(Camera.Parameters) to take effect.

ReturnsCamera.Parameters 

See also:

  • setParameters(Camera.Parameters)

lock

Added in API level 5
void lock ()

Re-locks the camera to prevent other processes from accessing it. Camera objects are locked by default unless unlock() is called. Normally reconnect()is used instead.

Since API level 14, camera is automatically locked for applications in start(). 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.

ThrowsRuntimeExceptionif the camera cannot be re-locked (for example, if the camera is still in use by another process).

open

Added in API level 1
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.

ReturnsCamera 

See also:

  • open(int)

open

Added in API level 9
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() 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 usingAsyncTask) to avoid blocking the main application UI thread.

ParameterscameraIdint: the hardware camera to access, between 0 and getNumberOfCameras()-1.ReturnsCameraa new Camera object, connected, locked and ready for use.ThrowsRuntimeExceptionif opening the camera fails (for example, if the camera is in use by another process or device policy manager has disabled the camera).

See also:

  • getCameraDisabled(android.content.ComponentName)

reconnect

Added in API level 8
void reconnect ()

Reconnects to the camera service after another process used it. After unlock() 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 start(). 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.

ThrowsIOExceptionif a connection cannot be re-established (for example, if the camera is still in use by another process).

release

Added in API level 1
void release ()

Disconnects and releases the Camera object resources.

You must call this as soon as you're done with the Camera object.

setAutoFocusMoveCallback

Added in API level 16
void setAutoFocusMoveCallback (Camera.AutoFocusMoveCallback cb)

Sets camera auto-focus move callback.

ParameterscbCamera.AutoFocusMoveCallback: the callback to run

setDisplayOrientation

Added in API level 8
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 onPreviewFrame(byte[], Camera), 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.

Parametersdegreesint: the angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270.

See also:

  • setPreviewDisplay(SurfaceHolder)

setErrorCallback

Added in API level 1
void setErrorCallback (Camera.ErrorCallback cb)

Registers a callback to be invoked when an error occurs.

ParameterscbCamera.ErrorCallback: The callback to run

setFaceDetectionListener

Added in API level 14
void setFaceDetectionListener (Camera.FaceDetectionListener listener)

Registers a listener to be notified about the faces detected in the preview frame.

ParameterslistenerCamera.FaceDetectionListener: the listener to notify

See also:

  • startFaceDetection()

setOneShotPreviewCallback

Added in API level 3
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 to properly indicate image capture or recording start/stop to the user.

ParameterscbCamera.PreviewCallback: a callback object that receives a copy of the next preview frame, or null to stop receiving callbacks.

See also:

  • MediaActionSound

setParameters

Added in API level 1
void setParameters (Camera.Parameters params)

Changes the settings for this Camera service.

ParametersparamsCamera.Parameters: the Parameters to use for this Camera serviceThrowsRuntimeExceptionif any parameter is invalid or not supported.

See also:

  • getParameters()

setPreviewCallback

Added in API level 1
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 to properly indicate image capture or recording start/stop to the user.

ParameterscbCamera.PreviewCallback: a callback object that receives a copy of each preview frame, or null to stop receiving callbacks.

See also:

  • MediaActionSound

setPreviewCallbackWithBuffer

Added in API level 8
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. 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 calladdCallbackBuffer(byte[]) 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(Camera.PreviewCallback) is called, orsetOneShotPreviewCallback(Camera.PreviewCallback) is called.

If you are using the preview data to create video or still images, strongly consider using MediaActionSound to properly indicate image capture or recording start/stop to the user.

ParameterscbCamera.PreviewCallback: a callback object that receives a copy of the preview frame, or null to stop receiving callbacks and clear the buffer queue.

See also:

  • addCallbackBuffer(byte[])
  • MediaActionSound

setPreviewDisplay

Added in API level 1
void setPreviewDisplay (SurfaceHolder holder)

Sets the 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 viasetPreviewTexture(SurfaceTexture).

The SurfaceHolder must already contain a surface when this method is called. If you are using SurfaceView, you will need to register aSurfaceHolder.Callback with addCallback(SurfaceHolder.Callback) and wait for surfaceCreated(SurfaceHolder) before calling setPreviewDisplay() or starting preview.

This method must be called before startPreview(). 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.

ParametersholderSurfaceHolder: containing the Surface on which to place the preview, or null to remove the preview surfaceThrowsIOExceptionif the method fails (for example, if the surface is unavailable or unsuitable).

setPreviewTexture

Added in API level 11
void setPreviewTexture (SurfaceTexture surfaceTexture)

Sets the 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 viasetPreviewDisplay(SurfaceHolder).

This method must be called before startPreview(). 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 getTimestamp() 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 to properly indicate image capture or recording start/stop to the user.

ParameterssurfaceTextureSurfaceTexture: the SurfaceTexture to which the preview images are to be sent or null to remove the current preview surface textureThrowsIOExceptionif the method fails (for example, if the surface texture is unavailable or unsuitable).

See also:

  • MediaActionSound
  • SurfaceTexture
  • TextureView

setZoomChangeListener

Added in API level 8
void setZoomChangeListener (Camera.OnZoomChangeListener listener)

Registers a listener to be notified when the zoom value is updated by the camera driver during smooth zoom.

ParameterslistenerCamera.OnZoomChangeListener: the listener to notify

See also:

  • startSmoothZoom(int)

startFaceDetection

Added in API level 14
void startFaceDetection ()

Starts the face detection. This should be called after preview is started. The camera will notify 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() to stop the face detection. This method is supported if getMaxNumDetectedFaces() returns a number larger than 0. If the face detection has started, apps should not call this again.

When the face detection is running, setWhiteBalance(String)setFocusAreas(List), and setMeteringAreas(List) have no effect. The camera uses the detected faces to do auto-white balance, auto exposure, and autofocus.

If the apps call autoFocus(AutoFocusCallback), 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(), the face callbacks will also resume.

After calling takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback) or stopPreview(), and then resuming preview with startPreview(), the apps should call this method again to resume face detection.

ThrowsIllegalArgumentExceptionif the face detection is unsupported.RuntimeExceptionif the method fails or the face detection is already running.

See also:

  • Camera.FaceDetectionListener
  • stopFaceDetection()
  • getMaxNumDetectedFaces()

startPreview

Added in API level 1
void startPreview ()

Starts capturing and drawing preview frames to the screen. Preview will not actually start until a surface is supplied withsetPreviewDisplay(SurfaceHolder) or setPreviewTexture(SurfaceTexture).

If setPreviewCallback(Camera.PreviewCallback)setOneShotPreviewCallback(Camera.PreviewCallback), orsetPreviewCallbackWithBuffer(Camera.PreviewCallback) were called, onPreviewFrame(byte[], Camera) will be called when preview data becomes available.

startSmoothZoom

Added in API level 8
void startSmoothZoom (int value)

Zooms to the requested value smoothly. The driver will notify Camera.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. The onZoomChange(int, boolean, Camera) method will be called three times with zoom values 1, 2, and 3. Applications can call stopSmoothZoom() to stop the zoom earlier. Applications should not call startSmoothZoom again or change the zoom value before zoom stops. If the supplied zoom value equals to the current zoom value, no zoom callback will be generated. This method is supported if isSmoothZoomSupported() returns true.

Parametersvalueint: zoom value. The valid range is 0 to getMaxZoom().ThrowsIllegalArgumentExceptionif the zoom value is invalid.RuntimeExceptionif the method fails.

See also:

  • setZoomChangeListener(OnZoomChangeListener)

stopFaceDetection

Added in API level 14
void stopFaceDetection ()

Stops the face detection.

See also:

  • startFaceDetection()

stopPreview

Added in API level 1
void stopPreview ()

Stops capturing and drawing preview frames to the surface, and resets the camera for a future call to startPreview().

stopSmoothZoom

Added in API level 8
void stopSmoothZoom ()

Stops the smooth zoom. Applications should wait for the Camera.OnZoomChangeListener to know when the zoom is actually stopped. This method is supported if isSmoothZoomSupported() is true.

ThrowsRuntimeExceptionif the method fails.

takePicture

Added in API level 5
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()). Preview will be stopped after the image is taken; callers must callstartPreview() again if they want to re-start preview or take more pictures. This should not be called between start() and stop().

After calling this method, you must not call startPreview() or take another picture until the JPEG callback has returned.

ParametersshutterCamera.ShutterCallback: the callback for image capture moment, or nullrawCamera.PictureCallback: the callback for raw (uncompressed) image data, or nullpostviewCamera.PictureCallback: callback with postview image data, may be nulljpegCamera.PictureCallback: the callback for JPEG image data, or null

takePicture

Added in API level 1
void takePicture (Camera.ShutterCallback shutter,                 Camera.PictureCallback raw,                 Camera.PictureCallback jpeg)

Equivalent to takePicture(shutter, raw, null, jpeg).

ParametersshutterCamera.ShutterCallbackrawCamera.PictureCallbackjpegCamera.PictureCallback

See also:

  • takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)

unlock

Added in API level 5
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() 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() to reclaim the camera.

This must be done before calling setCamera(Camera). This cannot be called after recording starts.

If you are not recording video, you probably do not need this method.

ThrowsRuntimeExceptionif the camera cannot be unlocked.

Protected methods


finalize

Added in API level 1
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 JavaTM 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 of Object 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.

0 0
原创粉丝点击