// Sets up member variables related to camera. private void SetUpCameraOutputs(int width, int height) { CameraManager manager = (CameraManager)Activity.GetSystemService(Context.CameraService); try { foreach (string cameraId in manager.GetCameraIdList()) { CameraCharacteristics characteristics = manager.GetCameraCharacteristics(cameraId); Integer facing = (Integer)characteristics.Get(CameraCharacteristics.LensFacing); if (facing != null && facing == (Integer.ValueOf((int)LensFacing.Front))) { continue; } StreamConfigurationMap 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()); _imageReader = ImageReader.NewInstance(largest.Width, largest.Height, ImageFormatType.Jpeg, 2); _imageReader.SetOnImageAvailableListener(OnImageAvailableListener, BackgroundHandler); _sensorOrientation = (int)characteristics.Get(CameraCharacteristics.SensorOrientation); bool swapped = IsCameraRoationSameAsDevice(Activity.WindowManager.DefaultDisplay.Rotation, _sensorOrientation); Size rotatedSize = GetRotatedPreviewSize(width, height, swapped); Size maxSize = GetMaxPrieviewSize(width, height, swapped); // 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(Class.FromType(typeof(SurfaceTexture))), rotatedSize.Width, rotatedSize.Height, maxSize.Width, maxSize.Height, largest); SetTextureViewAspectRatio(); // Check if the flash is supported. Boolean available = (Boolean)characteristics.Get(CameraCharacteristics.FlashInfoAvailable); _flashSupported = (available == null ? false : (bool)available); _cameraId = cameraId; return; } } catch (CameraAccessException e) { e.PrintStackTrace(); } catch//camera_error { ErrorDialog.NewInstance(GetString(Resource.String.camera_error)).Show(ChildFragmentManager, FRAGMENT_DIALOG); } }
public void OnRequestPermissionsResult(int requestCode, string[] permissions, int[] grantResults) { if (requestCode != REQUEST_CAMERA_PERMISSION || requestCode != REQUEST_CAMERA_PERMISSION) { return; } if (grantResults.Length != 1 || grantResults[0] != (int)Permission.Granted) { ErrorDialog.NewInstance(GetString(Resource.String.request_permission)) .Show(ChildFragmentManager, FRAGMENT_DIALOG); } }
// 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; } // For still image captures, we use the largest available size. //Size largest = (Size)Collections.Max(Arrays.AsList(map.GetOutputSizes((int)ImageFormatType.Jpeg)), // new CompareSizesByArea()); // 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 _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: 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 > 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. //_previewSize = ChooseOptimalSize(map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture))), // rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth, // maxPreviewHeight, largest); _previewSize = ChooseOptimalSize(map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture))), rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, new Size(maxPreviewWidth, maxPreviewHeight)); // We fit the aspect ratio of TextureView to the size of preview we picked. var orientation = Resources.Configuration.Orientation; if (orientation == Orientation.Landscape) { _textureView.SetAspectRatio(_previewSize.Width, _previewSize.Height); } else { _textureView.SetAspectRatio(_previewSize.Height, _previewSize.Width); } _cameraId = 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); } }
// 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; } // 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 > 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 = new Size(960, 540); // 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); } }