/// <summary> /// Initializes video preview and updates the <see cref="PreviewBrush"/> accordingly. /// </summary> /// <param name="previewFormatSelector"> /// Function that chooses the most suitable preview format from the provided collection. /// </param> /// <returns>Awaitable task.</returns> /// <exception cref="ArgumentNullException"><paramref name="previewFormatSelector"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidOperationException">No suitable video preview formats found.</exception> public async Task StartPreviewAsync(Func<IEnumerable<VideoEncodingProperties>, VideoEncodingProperties> previewFormatSelector) { if (previewFormatSelector == null) { throw new ArgumentNullException("previewFormatSelector"); } Tracing.Trace("CameraController: Starting video preview."); if (!string.IsNullOrEmpty(this.PreviewVideoPort)) { Tracing.Trace("CameraController: Video preview is already started."); return; } try { // Set the video preview format. VideoEncodingProperties previewFormat = await this.DoSetMediaFormatAsync(MediaStreamType.VideoPreview, previewFormatSelector); MediaCapturePreviewSink previewSink = new MediaCapturePreviewSink(); VideoBrush videoBrush = new VideoBrush(); videoBrush.SetSource(previewSink); MediaEncodingProfile profile = new MediaEncodingProfile { Audio = null, Video = previewFormat }; await this.MediaCapture.StartPreviewToCustomSinkAsync(profile, previewSink); this.PreviewFormat = previewFormat; this.PreviewResolution = new Size(previewFormat.Width, previewFormat.Height); this.PreviewBrush = videoBrush; this.PreviewVideoPort = previewSink.ConnectionPort; //// //// Update camera properties. //// this.Rotation = this.MediaCapture.GetPreviewRotation(); this.FocusSupported = this.MediaCapture.VideoDeviceController.FocusControl.Supported; this.FocusAtPointSupported = this.MediaCapture.VideoDeviceController.RegionsOfInterestControl.AutoFocusSupported && this.MediaCapture.VideoDeviceController.RegionsOfInterestControl.MaxRegions > 0; this.ContinuousAutoFocusSupported = this.MediaCapture.VideoDeviceController.FocusControl.SupportedFocusModes.Contains(FocusMode.Continuous); this.FlashSupported = this.MediaCapture.VideoDeviceController.FlashControl.Supported; if (this.FocusSupported) { this.ConfigureAutoFocus(continuous: false); if (this.MediaCapture.VideoDeviceController.FocusControl.FocusChangedSupported) { this.MediaCapture.FocusChanged += this.MediaCaptureFocusChanged; } } this.NotifyPropertiesChanged(); } catch (Exception e) { Tracing.Trace("CameraController: StartPreviewAsync: 0x{0:X8}\r\n{1}", e.HResult, e); throw; } Tracing.Trace("CameraController: Video preview started."); }
/// <summary> /// Initializes video preview and updates the <see cref="PreviewBrush"/> accordingly. /// </summary> /// <param name="previewFormatSelector"> /// Function that chooses the most suitable preview format from the provided collection. /// </param> /// <returns>Awaitable task.</returns> /// <exception cref="ArgumentNullException"><paramref name="previewFormatSelector"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidOperationException">No suitable video preview formats found.</exception> public async Task StartPreviewAsync(Func <IEnumerable <VideoEncodingProperties>, VideoEncodingProperties> previewFormatSelector) { if (previewFormatSelector == null) { throw new ArgumentNullException("previewFormatSelector"); } Tracing.Trace("CameraController: Starting video preview."); if (!string.IsNullOrEmpty(this.PreviewVideoPort)) { Tracing.Trace("CameraController: Video preview is already started."); return; } try { // Set the video preview format. VideoEncodingProperties previewFormat = await this.DoSetMediaFormatAsync(MediaStreamType.VideoPreview, previewFormatSelector); MediaCapturePreviewSink previewSink = new MediaCapturePreviewSink(); VideoBrush videoBrush = new VideoBrush(); videoBrush.SetSource(previewSink); MediaEncodingProfile profile = new MediaEncodingProfile { Audio = null, Video = previewFormat }; await this.MediaCapture.StartPreviewToCustomSinkAsync(profile, previewSink); this.PreviewFormat = previewFormat; this.PreviewResolution = new Size(previewFormat.Width, previewFormat.Height); this.PreviewBrush = videoBrush; this.PreviewVideoPort = previewSink.ConnectionPort; //// //// Update camera properties. //// this.Rotation = this.MediaCapture.GetPreviewRotation(); this.FocusSupported = this.MediaCapture.VideoDeviceController.FocusControl.Supported; this.FocusAtPointSupported = this.MediaCapture.VideoDeviceController.RegionsOfInterestControl.AutoFocusSupported && this.MediaCapture.VideoDeviceController.RegionsOfInterestControl.MaxRegions > 0; this.ContinuousAutoFocusSupported = this.MediaCapture.VideoDeviceController.FocusControl.SupportedFocusModes.Contains(FocusMode.Continuous); this.FlashSupported = this.MediaCapture.VideoDeviceController.FlashControl.Supported; if (this.FocusSupported) { this.ConfigureAutoFocus(continuous: false); if (this.MediaCapture.VideoDeviceController.FocusControl.FocusChangedSupported) { this.MediaCapture.FocusChanged += this.MediaCaptureFocusChanged; } } this.NotifyPropertiesChanged(); } catch (Exception e) { Tracing.Trace("CameraController: StartPreviewAsync: 0x{0:X8}\r\n{1}", e.HResult, e); throw; } Tracing.Trace("CameraController: Video preview started."); }