簡體   English   中英

檢查哪個攝像頭是 Open Front 或 Back Android

[英]Check which camera is Open Front or Back Android

我知道我可以boolean flag while opening front Camera設置一個boolean flag while opening front Camera 如果 flag 為 true,則表示前置攝像頭已打開。

但是有沒有辦法使用 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
}

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

onPreviewFrame(byte abyte0[] , Camera camera)

 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);
    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) 訪問特定相機。
上面的示例代碼將訪問具有多個攝像頭的設備上的第一個后置攝像頭。

在新的android.hardware.camera2包中,您可以從CameraCharacteristics.LENS_FACING屬性中查詢,並且每個CameraDevice使用CameraDevice.getId()發布其id很容易獲得特征。

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

private int cameraId;

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

然后稍后使用cameraId ,這個小片段可能是實現您想要的更好的方法:

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

如果您有自定義相機活動,您可以嘗試此方法。

boolean inPreview;

在您的 SurfaceChanged 方法中從 SurfaceView 集

inPreview = True;

在您的 Camera.CallbackListener 變量集中

inPreview = false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM