检查哪个相机是Open Front或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.我必须添加一个检查相机是否打开if FrontCamera is opened then matrix = 270. otherwise matrix =90.

onPreviewFrame(byte 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); 

  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设备可以有多个摄像头,例如用于摄影的后置摄像头和用于video呼叫的前置摄像头。

Android 2.3(API级别9)及更高版本允许您使用Camera.getNumberOfCameras()方法检查设备上可用的摄像机数量。

要访问主摄像头,请使用Camera.open()方法并确保捕获任何exception,如下面的代码所示:

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