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)

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()

Returns the number of physical cameras available on this device.返回攝像頭個數。一般情況下只有一個攝像頭,但是又些手機有兩個攝像頭:比如說htc sensation

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

或者參數設置內部類對象

public static Camera open (int cameraId) 

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

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

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 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()

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)

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)

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)

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()

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()

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)

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


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

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.



發佈了213 篇原創文章 · 獲贊 21 · 訪問量 72萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章