Camera接口

最近做得项目和摄像头有关,所以恶补摄像头相关知识,查看sdk文档,网上找找例子程序。

英文一般都看的懂。有些地方我就不解释了。

Summary

                内部类:

                CameraInfo:包含两个常量一个是前置摄像头CAMERA_FACING_FRONT,一个是后置摄像头CAMERA_FACING_BACK

                两个字段:facing 值:(摄像头前置、后置)。orientation 0, 90, 180, or 270,preview显示角度。


Camera.OnZoomChangeListener接口:

public abstract void onZoomChange(int zoomValue, boolean stopped, Camera camera)

Since: API Level 8

Called when the zoom value has changed during a smooth zoom.

Parameters
zoomValue the current zoom value. In smooth zoom mode, camera calls this for every new zoom value.
stopped whether smooth zoom is stopped. If the value is true, this is the last zoom update for the application.
camera the Camera service object

Camera.Parameters 这个类就不说了,设置相关参数,太多了。setParameters(Camera.Parameters).


一些常用的方法:

public static int getNumberOfCameras()

Since: API Level 9

Returns the number of physical cameras available on this device.返回摄像头个数。一般情况下只有一个摄像头,但是又些手机有两个摄像头:比如说htc sensation

public Camera.Parameters getParameters ()

Since: API Level 1

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.

或者参数设置内部类对象

public static Camera open (int cameraId) 

如果getNumberOfCameras ()返回只有1个,其实使用public static Camera open ()就ok了

public final void release()

Since: API Level 1

Disconnects and releases the Camera object resources.

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

public final void setDisplayOrientation(int degrees)这个就是摄像头预览的角度,一般情况下不要设置,但是如果有特殊要求,就要调角度了。下面的给出的例子,可以去用一下。

Since: API Level 8

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);
 }
 

public final void lock()

Since: API Level 5

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

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


public final void setPreviewCallbackWithBuffer(Camera.PreviewCallback cb)

Since: API Level 8

Installs a callback to be invoked for every preview frame, using buffers supplied withaddCallbackBuffer(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.

这个我们项目当中用到了这个函数,来注册监听器,用来向远端发送视频数据。

public final void setZoomChangeListener(Camera.OnZoomChangeListener listener)

Since: API Level 8

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

public final void startSmoothZoom(int value)

Since: API Level 8

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 ifisSmoothZoomSupported() returns true.

注意这个函数调用,必须在 isSmoothZoomSupported() returns true.的前提下。

public final void stopSmoothZoom()

Since: API Level 8

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

同样这个函数调用必须在 isSmoothZoomSupported() is true.的前提下。

public final void stopPreview()

Since: API Level 1

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

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

Since: API Level 1

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


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 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.

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

照相功能没有使用过。


public final void unlock()

Since: API Level 5

Unlocks the camera to allow another process to access it. Normally, the camera is locked to the process with an active Camera object untilrelease() 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).

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



你可能感兴趣的:(Camera接口)