public Camera2Fragment(Video.CAMERA_POSITION camera) { CURRENTCAMERA = camera; ORIENTATIONS.Append((int)SurfaceOrientation.Rotation0, 90); ORIENTATIONS.Append((int)SurfaceOrientation.Rotation90, 0); ORIENTATIONS.Append((int)SurfaceOrientation.Rotation180, 270); ORIENTATIONS.Append((int)SurfaceOrientation.Rotation270, 180); surfaceTextureListener = new MySurfaceTextureListener(this); stateListener = new MyCameraStateCallback(this); }
//Tries to open a CameraDevice public void openCamera(int width, int height, Bundle savedInstanceState) { if (null == Activity || Activity.IsFinishing) { return; } manager = (CameraManager)Activity.GetSystemService(Context.CameraService); try { if (!cameraOpenCloseLock.TryAcquire(2500, TimeUnit.Milliseconds)) { throw new RuntimeException("Time out waiting to lock camera opening."); } string cameraId = GetCam(manager, CURRENTCAMERA); CameraCharacteristics characteristics = manager.GetCameraCharacteristics(cameraId); StreamConfigurationMap map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap); videoSize = ChooseVideoSize(map.GetOutputSizes(Class.FromType(typeof(MediaRecorder)))); previewSize = ChooseOptimalSize(map.GetOutputSizes(Class.FromType(typeof(MediaRecorder))), width, height, videoSize); int orientation = (int)Resources.Configuration.Orientation; if (orientation == (int)Android.Content.Res.Orientation.Landscape) { textureView.SetAspectRatio(previewSize.Width, previewSize.Height); } else { textureView.SetAspectRatio(previewSize.Height, previewSize.Width); } configureTransform(width, height); mediaRecorder = new MediaRecorder(); if (stateListener == null) { stateListener = new MyCameraStateCallback(this); } manager.OpenCamera(cameraId, stateListener, null); SetupComplete?.Invoke(); max = (Rect)characteristics.Get(CameraCharacteristics.SensorInfoActiveArraySize); size = (Size)characteristics.Get(CameraCharacteristics.SensorInfoPixelArraySize); controls = new Camera2Controls(cameraDevice, size, max); controls.OnFocus += DoFocus; try { ChildFragmentManager.BeginTransaction().Replace(Resource.Id.camera_controls, controls).Commit(); } catch (System.Exception) { } } catch (CameraAccessException) { Toast.MakeText(Activity, "Cannot access the camera.", ToastLength.Short).Show(); Activity.Finish(); } catch (NullPointerException) { //var dialog = new ErrorDialog(); //dialog.Show(ChildFragmentManager, "dialog"); } catch (InterruptedException) { throw new RuntimeException("Interrupted while trying to lock camera opening."); } }