/// <summary> /// Enumerate video preview formats and select the one /// whose dimensions are nearest to the input width/height. /// </summary> /// <returns>Selected format</returns> public static async Task <VideoEncodingProperties> SelectNearestPreviewResolutionAsync(this VideoDeviceController controller, double width, double height) { var formats = controller.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview); var format = (VideoEncodingProperties)formats.OrderBy((item) => { var props = (VideoEncodingProperties)item; return(Math.Abs(props.Width - width) + Math.Abs(props.Height - height)); }).First(); await controller.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, format); return(format); }
public async Task <bool> InitializeAsync() { if (Initialized) { // Already intialized return(true); } _deviceInformationCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); // Find the back camera _deviceInformation = (from camera in _deviceInformationCollection where camera.EnclosureLocation != null && camera.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back select camera).FirstOrDefault(); if (_deviceInformation == null && _deviceInformationCollection.Count != 0) { // Fallback to whatever is available (e.g. webcam on laptop) _deviceInformation = _deviceInformationCollection[0]; } if (_deviceInformation == null) { throw new Exception("No camera device found"); } MediaCapture = new MediaCapture(); MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = StreamingCaptureMode.Video; settings.VideoDeviceId = _deviceInformation.Id; await MediaCapture.InitializeAsync(settings); #if WINDOWS_PHONE_APP _lightSensor.ReportInterval = _lightSensor.MinimumReportInterval; _lightSensor.ReadingChanged += OnLightSensorReadingChangedAsync; #endif _videoDeviceController = MediaCapture.VideoDeviceController; IList <VideoEncodingProperties> listOfPropertiesWithHighestFrameRate = CameraUtils.ResolveAllVideoEncodingPropertiesWithHighestFrameRate(_videoDeviceController, RecordMediaStreamType); if (listOfPropertiesWithHighestFrameRate != null && listOfPropertiesWithHighestFrameRate.Count > 0) { VideoEncodingProperties selectedRecordingVideoEncodingProperties = listOfPropertiesWithHighestFrameRate.ElementAt(0); uint selectedRecordingVideoWidth = selectedRecordingVideoEncodingProperties.Width; for (int i = 1; i < listOfPropertiesWithHighestFrameRate.Count; ++i) { VideoEncodingProperties currentProperties = listOfPropertiesWithHighestFrameRate.ElementAt(i); if (selectedRecordingVideoWidth > MaximumVideoWidth || (currentProperties.Width <= MaximumVideoWidth && currentProperties.Width > selectedRecordingVideoWidth)) { selectedRecordingVideoEncodingProperties = currentProperties; selectedRecordingVideoWidth = selectedRecordingVideoEncodingProperties.Width; } } _hfrVideoEncodingProperties = selectedRecordingVideoEncodingProperties; await _videoDeviceController.SetMediaStreamPropertiesAsync(RecordMediaStreamType, selectedRecordingVideoEncodingProperties); VideoEncodingProperties previewVideoEncodingProperties = CameraUtils.FindVideoEncodingProperties( _videoDeviceController, PreviewMediaStreamType, PreviewFrameRate, selectedRecordingVideoWidth, selectedRecordingVideoEncodingProperties.Height); System.Diagnostics.Debug.WriteLine("Highest framerate for recording is " + CameraUtils.ResolveFrameRate(selectedRecordingVideoEncodingProperties) + " frames per second with selected resolution of " + selectedRecordingVideoWidth + "x" + selectedRecordingVideoEncodingProperties.Height + "\nThe preview properties for viewfinder are " + CameraUtils.ResolveFrameRate(previewVideoEncodingProperties) + " FPS and " + previewVideoEncodingProperties.Width + "x" + previewVideoEncodingProperties.Height); await _videoDeviceController.SetMediaStreamPropertiesAsync(PreviewMediaStreamType, previewVideoEncodingProperties); } if (_videoDeviceController.WhiteBalanceControl.Supported) { await _videoDeviceController.WhiteBalanceControl.SetPresetAsync(ColorTemperaturePreset.Fluorescent); } Initialized = true; return(Initialized); }