2015-04-21 26 views
1

我知道我可以設置一個boolean flag while opening front Camera。如果標誌爲真,則表示前置攝像頭處於打開狀態。檢查哪個攝像頭是開放的正面或背面Android

但是有沒有一種方法可以使用Android API來知道哪個相機現在打開?前面或後面。

public int getFrontCameraId() { 
    CameraInfo ci = new CameraInfo(); 
    for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) { 
     Camera.getCameraInfo(i, ci); 
     if (ci.facing == CameraInfo.CAMERA_FACING_FRONT) return i; 
    } 
    return -1; // No front-facing camera found 
} 

相機預覽是反相(倒置donw)當我打開前置攝像頭。所以我必須添加檢查哪個攝像機是開放if FrontCamera is opened then matrix = 270. otherwise matrix =90.

onPreviewFrame(字節abyte0 [],相機攝像機)

int[] rgbData = YuvUtils.decodeGreyscale(abyte0, mWidth,mHeight); 

    editedBitmap.setPixels(rgbData, 0, widthPreview, 0, 0, widthPreview, heightPreview); 

    finalBitmap = Bitmap.createBitmap(editedBitmap, 0, 0, widthPreview, heightPreview, matrix, true); 
+0

http://stackoverflow.com/questions/15862621/how-to-check - 如果相機被任何應用程序打開 –

回答

1
private boolean safeCameraOpen(int id) { 
    boolean qOpened = false; 

    try { 
     releaseCameraAndPreview(); 
     mCamera = Camera.open(id); 
     qOpened = (mCamera != null); 
    } catch (Exception e) { 
     Log.e(getString(R.string.app_name), "failed to open Camera"); 
     e.printStackTrace(); 
    } 

    return qOpened;  
} 

private void releaseCameraAndPreview() { 
    mPreview.setCamera(null); 
    if (mCamera != null) { 
     mCamera.release(); 
     mCamera = null; 
    } 
} 

由於API級如圖9所示,照相機框架支持多個攝像機。
如果您使用傳統API並在沒有參數的情況下調用open(),則會獲得第一個後置攝像頭。

Android設備可以有多個攝像頭,例如用於攝影的後置攝像頭和用於視頻通話的前置攝像頭。

Android 2.3(API級別9)及更高版本允許您使用Camera.getNumberOfCameras()方法檢查設備上可用攝像頭的數量。

要訪問主照相機,使用Camera.open()方法並確保捕獲任何異常,如示於以下的代碼:

/** A safe way to get an instance of the Camera object. */ 
public static Camera getCameraInstance(){ 
    Camera c = null; 
    try { 
     c = Camera.open(); // attempt to get a Camera instance 
    } 
    catch (Exception e){ 
     // Camera is not available (in use or does not exist) 
    } 
    return c; // returns null if camera is unavailable 
} 

在運行Android 2.3(API等級9)或更高,可以裝置使用Camera.open(int)訪問特定的攝像機。
上面的示例代碼將訪問具有多個攝像頭的設備上的第一個後置攝像頭。

+0

不要忘記在Camera.release()使用後釋放相機' – bhansa

+0

'android.hardware.Camera'已棄用 –

+0

@Gaan_setaglA這只是因爲除了最新的Nexus設備之外,任何手機幾乎都不支持新的Camera2 API。 – EpicPandaForce

1

在新android.hardware.camera2包,你可以從CameraCharacteristics.LENS_FACING財產查詢和每個CameraDevice公佈其idCameraDevice.getId()很容易到達的特點。

在舊的相機API中,我認爲唯一的方法是跟蹤您打開它的索引。

private int cameraId; 

public void openFrontCamera(){ 
    cameraId = getFrontCameraId(); 
    if (cameraId != -1) 
    camera = Camera.open(cameraId); //try catch omitted for brevity 
} 

然後用cameraId後,這個little snippet可能是你正在嘗試實現一個更好的辦法:

public void onOrientationChanged(int orientation) { 
    if (orientation == ORIENTATION_UNKNOWN) return; 
    android.hardware.Camera.CameraInfo info = 
      new android.hardware.Camera.CameraInfo(); 
    android.hardware.Camera.getCameraInfo(cameraId, info); 
    orientation = (orientation + 45)/90 * 90; 
    int rotation = 0; 
    if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
     rotation = (info.orientation - orientation + 360) % 360; 
    } else { // back-facing camera 
     rotation = (info.orientation + orientation) % 360; 
    } 
    mParameters.setRotation(rotation); 
} 
+0

我應該在哪裏放置代碼或將其稱爲onOrientationChanged – Nepster

+0

請按照鏈接「小片段」 – weston

相關問題