2013-01-18 148 views
8

我的應用程序具有需要後置攝像頭的功能。是否有前置攝像頭與我的需求無關。在任何情況下,整合一個強大的程序來檢測後置攝像頭是否存在,這證明是棘手的。例如,一位擁有HTC Evo 3D的用戶抱怨說該應用說沒有後置攝像頭(顯然是),而且我也收到了一些其他用戶的類似投訴。檢測後置攝像頭的不足

這是一個棘手的事情來測試,因爲儘管有很多設備,我沒有一個只有前置攝像頭的設備,比如Nexus 7,或者任何用戶提到的型號。

這是我擁有的,這是從代碼採取本網站其他答案:

boolean rearCameraFound = false; 
    if(BUILD_VERSION > 8){ 
     int cameraCount = 0; 
     Camera cam = null; 
     Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); 
     cameraCount = Camera.getNumberOfCameras(); 
     for (int camIdx = 0; camIdx < cameraCount; camIdx++) { 
      Camera.getCameraInfo(camIdx, cameraInfo); 
      if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK ) { 
       try { 
        cam = Camera.open(camIdx); 
        Log.d("TAG", "Rear camera detected"); 
        rearCameraFound = true; 
       } catch (RuntimeException e) { 
        Log.e("TAG", "Camera failed to open: " + e.getLocalizedMessage()); 
       } 
      } 
      if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
       Log.d("TAG", "Front camera detected"); 
      } 
     } 
     return rearCameraFound; 
    }else{ 
     if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ 
      // this device has a camera 
      return true; 
     } else { 
      // no camera on this device 
      return false; 
     } 
    } 

現在我把它換成這個代碼與此更簡單的版本:

PackageManager pm = context.getPackageManager(); 
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA); 

然而,我不知道Nexus 7會發生什麼情況,例如只有前置攝像頭。這將返回真實?

我正在尋找能夠確定是否存在後置攝像頭的代碼。

+2

FYI ,此代碼在Nexus 7運行股票JB 4.2 –

+0

上返回false。Sahil - 在過去,我試圖這樣做,但被告知我沒有足夠的聲譽來這樣做。我想我現在有了,所以一定會經歷我的問題並確保完成,謝謝。 – androidneil

回答

9

Nexus 7(僅具有一個正面照相機)返回false到FEATURE_CAMERA,而不是它,您可以使用FEATURE_CAMERA_FRONT。看看這個discussion

現在,通過使用上述內容,您可以確保至少有一個攝像頭。所以現在,你可以檢查手機中存在的相機數量,如果它大於1,那麼肯定會有後置相機。

import android.hardware.Camera; 

int numCameras = Camera.getNumberOfCameras(); 
if (numCameras > 1) { 
    rearCamera = true; 
} 

這是非常棘手的。但就是這些,我現在可以想到。試試吧。

+0

因此,如果我只想知道後置攝像頭,那麼看起來我的更新,更簡單的代碼可能就是所需的全部代碼。我所能做的就是嘗試並等待用戶反饋,我猜。 – androidneil

+4

這種情況不適用於只有後置攝像頭可用的情況。 –

+0

@TranHoangDuyAnh你到底想幹什麼? –

2

使用此片段。請注意,這僅適用於API 9或更高版本。

private static boolean checkCameraFacing(final int facing) { 
    if (getSdkVersion() < Build.VERSION_CODES.GINGERBREAD) { 
     return false; 
    } 
    final int cameraCount = Camera.getNumberOfCameras(); 
    CameraInfo info = new CameraInfo(); 
    for (int i = 0; i < cameraCount; i++) { 
     Camera.getCameraInfo(i, info); 
     if (facing == info.facing) { 
      return true; 
     } 
    } 
    return false; 
} 

public static boolean hasBackFacingCamera() { 
    final int CAMERA_FACING_BACK = 0; 
    return checkCameraFacing(CAMERA_FACING_BACK); 
} 

public static boolean hasFrontFacingCamera() { 
    final int CAMERA_FACING_BACK = 1; 
    return checkCameraFacing(CAMERA_FACING_BACK); 
} 

public static int getSdkVersion() { 
    return android.os.Build.VERSION.SDK_INT; 
} 

而這不需要打開相機。

1

對於前攝像機

Context context = this; //If its Activity 

// Context context=getActivity(); If its a Fragment 

PackageManager packageManager = context.getPackageManager(); 

if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) { 

    Log.i("CameraLog", "Front Camera Available"); 

} else { 

    Log.i("CameraLog", "No Front Camera Available"); 
     } 

用於後視照相機

Context context = this; //If its an Activity 

// Context context=getActivity(); If its a fragments 

PackageManager packageManager = context.getPackageManager(); 

if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 

    Log.i("CameraLog", "Back Camera is Available"); 

} else { 

    Log.i("CameraLog", "No Back Camera Available"); 

     } 
1

如果裝置只有一個攝像頭,它可以有背面僅相機,或僅前攝像機,首先檢查是否回照相機存在:

  • 檢查前置攝像頭是否存在,但帶有兩個攝像頭的設備顯然有前置攝像頭
  • 檢查設備只有一個攝像頭,但它可以有背部或前置攝像頭
  • 最終結果:如果設備有前置攝像頭,它只有一個攝像頭,後置攝像頭不存在
private boolean backCameraExists() { 
    boolean isFrontExists = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT); 
    if (isFrontExists && (Camera.getNumberOfCameras()<2)) { 
     return false; 
    } 
    return true; 
}