// We fit the aspect ratio of TextureView to the size of preview we picked. private void SetTextureViewAspectRatio() { var orientation = Resources.Configuration.Orientation; if (orientation == Orientation.Landscape) { _textureView.SetAspectRatio(_previewSize.Width, _previewSize.Height); } else { _textureView.SetAspectRatio(_previewSize.Height, _previewSize.Width); } }
// Opens a CameraDevice. The result is listened to by 'mStateListener'. private void OpenCamera() { Activity activity = Activity; if (activity == null || activity.IsFinishing || mOpeningCamera) { return; } mOpeningCamera = true; CameraManager manager = (CameraManager)activity.GetSystemService(Context.CameraService); try { String cameraId = manager.GetCameraIdList()[0]; // To get a list of available sizes of camera preview, we retrieve an instance of // StreamConfigurationMap from CameraCharacteristics CameraCharacteristics characteristics = manager.GetCameraCharacteristics(cameraId); StreamConfigurationMap map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap); mPreviewSize = map.GetOutputSizes(Java.Lang.Class.FromType(typeof(SurfaceTexture)))[0]; Android.Content.Res.Orientation orientation = Resources.Configuration.Orientation; if (orientation == Android.Content.Res.Orientation.Landscape) { mTextureView.SetAspectRatio(mPreviewSize.Width, mPreviewSize.Height); } else { mTextureView.SetAspectRatio(mPreviewSize.Height, mPreviewSize.Width); } // We are opening the camera with a listener. When it is ready, OnOpened of mStateListener is called. manager.OpenCamera(cameraId, mStateListener, null); } catch (Exception ex) { Toast.MakeText(activity, "Cannot access the camera.", ToastLength.Short).Show(); Activity.Finish(); } }
// Sets up member variables related to camera. private void SetUpCameraOutputs(int width, int height) { var cameraManager = (CameraManager)Context.GetSystemService(Context.CameraService); try { for (var i = 0; i < cameraManager.GetCameraIdList().Length; i++) { var cameraId = cameraManager.GetCameraIdList()[i]; CameraCharacteristics characteristics = cameraManager.GetCameraCharacteristics(cameraId); // We don't use a front facing camera in this sample. var facing = (Integer)characteristics.Get(CameraCharacteristics.LensFacing); if (facing != null && facing == (Integer.ValueOf((int)LensFacing.Front))) { continue; } var map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap); if (map == null) { continue; } // For still image captures, we use the largest available size. Size largest = (Size)Collections.Max(Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)), new Camera2Basic.CompareSizesByArea()); mImageReader = ImageReader.NewInstance(largest.Width, largest.Height, ImageFormatType.Jpeg, 2); mImageReader.SetOnImageAvailableListener(mOnImageAvailableListener, BackgroundHandler); var windowManager = Context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>(); // Find out if we need to swap dimension to get the preview size relative to sensor // coordinate. var displayRotation = windowManager.DefaultDisplay.Rotation; //noinspection ConstantConditions mSensorOrientation = (int)characteristics.Get(CameraCharacteristics.SensorOrientation); bool swappedDimensions = false; switch (displayRotation) { case SurfaceOrientation.Rotation0: case SurfaceOrientation.Rotation180: if (mSensorOrientation == 90 || mSensorOrientation == 270) { swappedDimensions = true; } break; case SurfaceOrientation.Rotation90: case SurfaceOrientation.Rotation270: if (mSensorOrientation == 0 || mSensorOrientation == 180) { swappedDimensions = true; } break; default: Log.Error("CameraPreview", "Display rotation is invalid: " + displayRotation); break; } Point displaySize = new Point(); windowManager.DefaultDisplay.GetSize(displaySize); var rotatedPreviewWidth = width; var rotatedPreviewHeight = height; var maxPreviewWidth = displaySize.X; var maxPreviewHeight = displaySize.Y; if (swappedDimensions) { rotatedPreviewWidth = height; rotatedPreviewHeight = width; maxPreviewWidth = displaySize.Y; maxPreviewHeight = displaySize.X; } if (maxPreviewWidth > MAX_PREVIEW_WIDTH) { maxPreviewWidth = MAX_PREVIEW_WIDTH; } if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) { maxPreviewHeight = MAX_PREVIEW_HEIGHT; } // Danger, W.R.! Attempting to use too large a preview size could exceed the camera // bus' bandwidth limitation, resulting in gorgeous previews but the storage of // garbage capture data. mPreviewSize = ChooseOptimalSize(map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture))), rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest); // We fit the aspect ratio of TextureView to the size of preview we picked. var orientation = Resources.Configuration.Orientation; if (orientation == Android.Content.Res.Orientation.Landscape) { mTextureView.SetAspectRatio(mPreviewSize.Width, mPreviewSize.Height); } else { mTextureView.SetAspectRatio(mPreviewSize.Height, mPreviewSize.Width); } // Check if the flash is supported. var available = characteristics.Get(CameraCharacteristics.FlashInfoAvailable).JavaCast <Java.Lang.Boolean>(); if (available == null) { mFlashSupported = false; } else { mFlashSupported = (bool)available; } mCameraId = cameraId; return; } } catch (CameraAccessException e) { e.PrintStackTrace(); } catch (NullPointerException e) { // Currently an NPE is thrown when the Camera2API is used but not supported on the // device this code runs. //ErrorDialog.NewInstance(GetString(Resource.String.camera_error)).Show(ChildFragmentManager, FRAGMENT_DIALOG); Log.Error("CameraPreviewAlt", "SetUpCameraOutputs: " + e.ToString()); } }