private int FindBackFacingCamera() { var cameraId = 0; // Search for the front facing camera int numberOfCameras = Camera.NumberOfCameras; for (int i = 0; i < numberOfCameras; i++) { var info = new Camera.CameraInfo(); Camera.GetCameraInfo(i, info); if (info.Facing == Camera.CameraInfo.CameraFacingBack) { cameraId = i; break; } } return(cameraId); }
private void prepareCamera(int encWidth, int encHeight) { if (_camera != null) { throw new RuntimeException("camera already initialized"); } Camera.CameraInfo info = new Camera.CameraInfo(); // Try to find a front-facing camera (e.g. for videoconferencing). int numCameras = Camera.NumberOfCameras; for (int i = 0; i < numCameras; i++) { Camera.GetCameraInfo(i, info); if (info.Facing == Camera.CameraInfo.CameraFacingFront) { _camera = Camera.Open(i); break; } } if (_camera == null) { Log.Debug(TAG, "No front-facing camera found; opening default"); _camera = Camera.Open(); // opens first back-facing camera } if (_camera == null) { throw new RuntimeException("Unable to open camera"); } Camera.Parameters parms = _camera.GetParameters(); choosePreviewSize(parms, encWidth, encHeight); // leave the frame rate set to default _camera.SetParameters(parms); Camera.Size size = parms.PreviewSize; Log.Debug(TAG, "Camera preview size is " + size.Width + "x" + size.Height); }
public override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Create a RelativeLayout container that will hold a SurfaceView, // and set it as the content of our activity. mPreview = new Preview(this.Activity); // Find the total number of cameras available mNumberOfCameras = Camera.NumberOfCameras; // Find the ID of the default camera Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int i = 0; i < mNumberOfCameras; i++) { Camera.GetCameraInfo(i, cameraInfo); if (cameraInfo.Facing == Camera.CameraInfo.CameraFacingBack) { mDefaultCameraId = i; } } SetHasOptionsMenu(mNumberOfCameras > 1); }
/** * Gets the current screen rotation in order to understand how much * the surface needs to be rotated */ private void UpdateCameraDisplayOrientation() { int cameraID = _cameraId; if (_camera == null) { Console.WriteLine("updateCameraDisplayOrientation(): warning, camera is null"); return; } int result = 0; MainActivity parentActivity = (MainActivity)Context; int rotation = (int)parentActivity.Window.WindowManager.DefaultDisplay.Rotation; int degrees = 0; switch (rotation) { case (int)SurfaceOrientation.Rotation0: degrees = 0; break; case (int)SurfaceOrientation.Rotation90: degrees = 90; break; case (int)SurfaceOrientation.Rotation180: degrees = 180; break; case (int)SurfaceOrientation.Rotation270: degrees = 270; break; } if ((int)Build.VERSION.SdkInt >= 9) { // on >= API 9 we can proceed with the CameraInfo method // and also we have to keep in mind that the camera could be the front one Camera.CameraInfo info = new Camera.CameraInfo(); Camera.GetCameraInfo(cameraID, info); if (info.Facing == Camera.CameraInfo.CameraFacingFront) { result = (info.Orientation + degrees) % 360; result = (360 - result) % 360; // compensate the mirror } else { // back-facing result = (info.Orientation - degrees + 360) % 360; } } else { // TODO: on the majority of API 8 devices, this trick works good // and doesn't produce an upside-down preview. // ... but there is a small amount of devices that don't like it! result = Math.Abs(degrees - 90); } _camera.SetDisplayOrientation(result); // save settings }