Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 檢查Android是否具有攝像頭 代碼

檢查Android是否具有攝像頭 代碼

編輯:關於Android編程

通常我們進行攝像頭操作,如掃描二維碼需要判斷是否有後置攝像頭(Rear camera),比如Nexus 7 一代就沒有後置攝像頭,這樣在嘗試使用的時候,我們需要進行判斷進行一些提示或者處理。

以下代碼為一系列的方法,用來判斷是否有前置攝像頭(Front Camera),後置攝像頭。
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;
}

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. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved