// Sets up member variables related to camera. private void SetUpCameraOutputs(int width, int height) { var manager = (CameraManager)Context.GetSystemService(Context.CameraService); try { for (var i = 0; i < manager.GetCameraIdList().Length; i++) { var cameraId = manager.GetCameraIdList()[i]; CameraCharacteristics characteristics = manager.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 CompareSizesByArea()); mImageReader = ImageReader.NewInstance(largest.Width, largest.Height, ImageFormatType.Jpeg, /*maxImages*/ 2); mImageReader.SetOnImageAvailableListener(mOnImageAvailableListener, BackgroundHandler); // Find out if we need to swap dimension to get the preview size relative to sensor // coordinate. var windowManager = Context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>(); 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(TAG, "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 = (Java.Lang.Boolean)characteristics.Get(CameraCharacteristics.FlashInfoAvailable); 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. throw new System.Exception("This device doesn\'t support Camera2 API."); } }
// Sets up member variables related to camera. private void SetUpCameraOutputs(int width, int height) { var activity = Activity; var manager = (CameraManager)activity.GetSystemService(Context.CameraService); try { for (var i = 0; i < manager.GetCameraIdList().Length; i++) { var cameraId = manager.GetCameraIdList()[i]; CameraCharacteristics characteristics = manager.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; } var jpegSizes = ((StreamConfigurationMap)characteristics .Get(CameraCharacteristics.ScalerStreamConfigurationMap)) .GetOutputSizes((int)ImageFormatType.Jpeg); // We're targeting 2mp - good trade-off between file size and quality jpegSize = new Size(640, 480); const int target = 1600 * 1200; //2mp int currentDiff = System.Math.Abs(jpegSize.Height * jpegSize.Width - target); if (jpegSizes != null && jpegSizes.Length > 0) { foreach (Size thisSize in jpegSizes) { int thisDiff = System.Math.Abs(thisSize.Height * thisSize.Width - target); if (thisDiff >= currentDiff) { continue; } jpegSize = thisSize; currentDiff = thisDiff; } } // Find out if we need to swap dimension to get the preview size relative to sensor // coordinate. var displayRotation = activity.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(TAG, "Display rotation is invalid: " + displayRotation); break; } Point displaySize = new Point(); activity.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 > MaxPreviewWidth) { maxPreviewWidth = MaxPreviewWidth; } if (maxPreviewHeight > MaxPreviewHeight) { maxPreviewHeight = MaxPreviewHeight; } // 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, jpegSize); // We fit the aspect ratio of TextureView to the size of preview we picked. var orientation = Resources.Configuration.Orientation; if (orientation == Orientation.Landscape) { mTextureView.SetAspectRatio(mPreviewSize.Width, mPreviewSize.Height); } else { mTextureView.SetAspectRatio(mPreviewSize.Height, mPreviewSize.Width); } // Check if the flash is supported. var available = (Boolean)characteristics.Get(CameraCharacteristics.FlashInfoAvailable); 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. System.Console.WriteLine(e.Message); //ErrorDialog.NewInstance(GetString(Resource.String.camera_error)).Show(ChildFragmentManager, FRAGMENT_DIALOG); } }
private void SetUpCameraOutputs(string selectedCameraId) { var map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap); if (map == null) { return; } // For still image captures, we use the largest available size. Size largest = (Size)Collections.Max(Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)), new CompareSizesByArea()); if (imageReader == null) { imageReader = ImageReader.NewInstance(largest.Width, largest.Height, ImageFormatType.Jpeg, maxImages: 1); imageReader.SetOnImageAvailableListener(onImageAvailableListener, backgroundHandler); } // Find out if we need to swap dimension to get the preview size relative to sensor // coordinate. var displayRotation = windowManager.DefaultDisplay.Rotation; sensorOrientation = (int)characteristics.Get(CameraCharacteristics.SensorOrientation); bool swappedDimensions = false; switch (displayRotation) { case SurfaceOrientation.Rotation0: case SurfaceOrientation.Rotation180: if (sensorOrientation == 90 || sensorOrientation == 270) { swappedDimensions = true; } break; case SurfaceOrientation.Rotation90: case SurfaceOrientation.Rotation270: if (sensorOrientation == 0 || sensorOrientation == 180) { swappedDimensions = true; } break; default: System.Diagnostics.Debug.WriteLine($"Display rotation is invalid: {displayRotation}"); break; } Point displaySize = new Point(); windowManager.DefaultDisplay.GetSize(displaySize); var rotatedPreviewWidth = surfaceTextureView.Width; var rotatedPreviewHeight = surfaceTextureView.Height; var maxPreviewWidth = displaySize.X; var maxPreviewHeight = displaySize.Y; if (swappedDimensions) { rotatedPreviewWidth = surfaceTextureView.Height; rotatedPreviewHeight = surfaceTextureView.Width; maxPreviewWidth = displaySize.Y; maxPreviewHeight = displaySize.X; } // 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. previewSize = ChooseOptimalSize(map.GetOutputSizes(Java.Lang.Class.FromType(typeof(SurfaceTexture))), rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest); // We fit the aspect ratio of TextureView to the size of preview we picked. // The commented code handles landscape layouts. This app is portrait only, so this is not needed /* * var orientation = Application.Context.Resources.Configuration.Orientation; * if (orientation == global::Android.Content.Res.Orientation.Landscape) * { * surfaceTextureView.SetAspectRatio(previewSize.Width, previewSize.Height); * } * else * {*/ surfaceTextureView.SetAspectRatio(previewSize.Height, previewSize.Width); /*}*/ // Check if the flash is supported. var available = (bool?)characteristics.Get(CameraCharacteristics.FlashInfoAvailable); if (available == null) { flashSupported = false; } else { flashSupported = (bool)available; } return; }
// Sets up member variables related to camera. private void SetUpCameraOutputs(int width, int height) { var activity = Activity; var manager = (CameraManager)activity.GetSystemService(Context.CameraService); try { for (var i = 0; i < manager.GetCameraIdList().Length; i++) { var cameraId = manager.GetCameraIdList()[i]; CameraCharacteristics characteristics = manager.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.Back))) { continue; } var map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap); if (map == null) { continue; } Size largest = (Size)Collections.Max(Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)), new CompareSizesByArea()); mImageReader = ImageReader.NewInstance(largest.Width, largest.Height, ImageFormatType.Jpeg, /*maxImages*/ 2); mImageReader.SetOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler); Point displaySize = new Point(); activity.WindowManager.DefaultDisplay.GetSize(displaySize); var rotatedPreviewWidth = height; var rotatedPreviewHeight = width; var maxPreviewWidth = displaySize.Y; var maxPreviewHeight = displaySize.X; // 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. mTextureView.SetAspectRatio(mPreviewSize.Height, mPreviewSize.Width); // Check if the flash is supported. var available = (Boolean)characteristics.Get(CameraCharacteristics.FlashInfoAvailable); 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); } }