/// <summary> /// Allow selection of camera settings. /// </summary> /// <param name="mediaStreamType" type="Windows.Media.Capture.MediaStreamType"> /// Type of a the media stream. /// </param> /// <param name="filterSettings" type="Func<Windows.Media.MediaProperties.IMediaEncodingProperties, bool>"> /// A predicate function, which will be called to filter the correct settings. /// </param> public async Task <IMediaEncodingProperties> SelectPreferredCameraStreamSettingAsync(MediaStreamType mediaStreamType, Func <IMediaEncodingProperties, bool> filterSettings) { IMediaEncodingProperties previewEncodingProperties = null; if (mediaStreamType == MediaStreamType.Audio || mediaStreamType == MediaStreamType.Photo) { throw new ArgumentException("mediaStreamType value of MediaStreamType.Audio or MediaStreamType.Photo is not supported", "mediaStreamType"); } if (filterSettings == null) { throw new ArgumentNullException("filterSettings"); } var properties = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(mediaStreamType); var filterredProperties = properties.Where(filterSettings); var preferredSettings = filterredProperties.ToArray(); Array.Sort <IMediaEncodingProperties>(preferredSettings, (x, y) => { return((int)(((x as VideoEncodingProperties).Width) - (y as VideoEncodingProperties).Width)); }); if (preferredSettings.Length > 0) { previewEncodingProperties = preferredSettings[0]; await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(mediaStreamType, preferredSettings[0]); } return(previewEncodingProperties); }
private IMFMediaType MediaTypeFromProperties(IMediaEncodingProperties properties) { IMFMediaType mediaType = null; MFPlat.MFCreateMediaTypeFromProperties(properties, out mediaType); return(mediaType); }
private async void OpenCamera() { try { capture = new MediaCapture(); await capture.InitializeAsync(); capture.Failed += delegate { CloseCamera(); }; camPreview.Source = capture; camPreview.FlowDirection = FlowDirection.RightToLeft; await capture.StartPreviewAsync(); isPreviewing = true; previewProperties = capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); cameraProperties = new CameraStreamProperties(previewProperties); camPreview.Height = facesCanvas.Height = 0.8 * page.ActualHeight; camPreview.Width = facesCanvas.Width = cameraProperties.AspectRatio * camPreview.Height; FaceDetectionInitialization(); } catch (Exception ex) { await CloseCamera(); errorTxt.Text = "There was a problem while opening the camera. Be sure it's connected and not used by onother app"; } }
/// <summary> /// Starts the preview and adjusts it for for rotation and mirroring after making a request to keep the screen on /// </summary> /// <returns></returns> private async Task StartPreviewAsync() { // Prevent the device from sleeping while the preview is running _displayRequest.RequestActive(); // Set the preview source in the UI and mirror it if necessary PreviewControl.Source = _mediaCapture; PreviewControl.FlowDirection = _mirroringPreview ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; // Start the preview try { await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, FindBestPreviewResolution()); await _mediaCapture.StartPreviewAsync(); _previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); } catch (Exception ex) { Debug.WriteLine("Exception when starting the preview: {0}", ex.ToString()); } // Initialize the preview to the current orientation if (_previewProperties != null) { _displayOrientation = _displayInformation.CurrentOrientation; await SetPreviewRotationAsync(); } }
private JsonObject MediaPropertiesToJson(IMediaEncodingProperties mediaProperties) { JsonObject properties = new JsonObject(); if (mediaProperties is VideoEncodingProperties) { VideoEncodingProperties videoProperties = mediaProperties as VideoEncodingProperties; properties.AddValue(nameof(videoProperties.Bitrate), videoProperties.Bitrate); properties.AddValue(nameof(videoProperties.FrameRate), $"{videoProperties.FrameRate.Denominator}/{videoProperties.FrameRate.Numerator}"); properties.AddValue(nameof(videoProperties.Height), videoProperties.Height); properties.AddValue(nameof(videoProperties.ProfileId), videoProperties.ProfileId); properties.AddValue(nameof(videoProperties.PixelAspectRatio), $"{videoProperties.PixelAspectRatio.Denominator}:{videoProperties.PixelAspectRatio.Numerator}"); properties.AddValue(nameof(videoProperties.Subtype), videoProperties.Subtype); properties.AddValue(nameof(videoProperties.Type), videoProperties.Type); properties.AddValue(nameof(videoProperties.Width), videoProperties.Width); } else if (mediaProperties is ImageEncodingProperties) { ImageEncodingProperties imageProperties = mediaProperties as ImageEncodingProperties; properties.AddValue(nameof(imageProperties.Height), imageProperties.Height); properties.AddValue(nameof(imageProperties.Subtype), imageProperties.Subtype); properties.AddValue(nameof(imageProperties.Type), imageProperties.Type); properties.AddValue(nameof(imageProperties.Width), imageProperties.Width); } return(properties); }
private async Task StartPreviewAsync() { // Prevent the device from sleeping while the preview is running displayRequest.RequestActive(); // Set the preview source in the UI and mirror it if necessary PreviewControl.Source = mediaCapture; PreviewControl.FlowDirection = reverseCamera ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; //PreviewControl.FlowDirection = FlowDirection.RightToLeft; //PreviewControl.FlowDirection = mirroringPreview ? FlowDirection.RightToLeft : FlowDirection.RightToLeft; // Start the preview await mediaCapture.StartPreviewAsync(); previewProperties = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); // Initialize the preview to the current orientation if (previewProperties != null) { displayOrientation = displayInformation.CurrentOrientation; await SetPreviewRotationAsync(); } }
/// <summary> /// Gets the current orientation of the UI in relation to the device and applies a corrective rotation to the preview /// </summary> private async Task SetPreviewRotationAsync(IMediaEncodingProperties props) { // Only need to update the orientation if the camera is mounted on the device. if (mediaCapture == null) { return; } // Calculate which way and how far to rotate the preview. int rotationDegrees; VideoRotation sourceRotation; CalculatePreviewRotation(out sourceRotation, out rotationDegrees); // Set preview rotation in the preview source. mediaCapture.SetPreviewRotation(sourceRotation); // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames //var props = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); props.Properties.Add(RotationKey, rotationDegrees); await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, props); var currentPreviewResolution = GetPreviewResolution(props); // Setup a frame to use as the input settings videoFrame = new VideoFrame(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, (int)currentPreviewResolution.Width, (int)currentPreviewResolution.Height); }
public CameraSizeInfo(IMediaEncodingProperties item, uint width, uint height) { Data = item; Width = width; Height = height; SizeTag = Math.Round(Convert.ToDouble(width) * height / 1000000.0, 1); ShowText = width + " x " + height + " (" + SizeTag + "MP)"; }
private async void btnCamera_Click(object sender, RoutedEventArgs e) { _mediaCapture = new MediaCapture(); await _mediaCapture.InitializeAsync(); cePreview.Source = _mediaCapture; await _mediaCapture.StartPreviewAsync(); _previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); }
/// <summary> /// Sets encoding properties on a camera stream. Ensures CaptureElement and preview stream are stopped before setting properties. /// </summary> public async Task SetMediaStreamPropertiesAsync(MediaStreamType streamType, IMediaEncodingProperties encodingProperties) { // Stop preview and unlink the CaptureElement from the MediaCapture object await MediaCapture.StopPreviewAsync(); _previewControl.Source = null; // Apply desired stream properties await MediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, encodingProperties); // Recreate the CaptureElement pipeline and restart the preview _previewControl.Source = MediaCapture; await MediaCapture.StartPreviewAsync(); }
/// <summary> /// Initializes a new instance of the <see cref="StreamResolution"/> class. /// </summary> /// <param name="properties">The properties</param> internal StreamResolution(IMediaEncodingProperties properties) { if (properties == null) { throw new ArgumentNullException(nameof(properties)); } if (!(properties is ImageEncodingProperties) && !(properties is VideoEncodingProperties)) { throw new ArgumentException("Argument is of the wrong type. Required: " + typeof(ImageEncodingProperties).Name + " or " + typeof(VideoEncodingProperties).Name); } this.properties = properties; }
/// <summary> /// Initializes a new instance of the <see cref="StreamResolution"/> class. /// </summary> /// <param name="properties"></param> internal StreamResolution(IMediaEncodingProperties properties) { if (properties == null) { throw new ArgumentNullException(nameof(properties)); } if (!(properties is ImageEncodingProperties) && !(properties is VideoEncodingProperties)) { throw new ArgumentException("Argument is of the wrong type. Required: " + typeof(ImageEncodingProperties).Name + " or " + typeof(VideoEncodingProperties).Name); } this.properties = properties; }
private Rectangle MapRectangleToDetectFace(BitmapBounds detectedfaceBoxCoordinates) { var faceRectangle = new Rectangle(); var deviceController = this._mediaCapture.VideoDeviceController; this._previewProperties = deviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); var previewStreamProperties = this._previewProperties as VideoEncodingProperties; double mediaStreamWidth = previewStreamProperties.Width; double mediaStreamHeight = previewStreamProperties.Height; var faceHighlightRect = LocatePreviewStreamCoordinates(previewStreamProperties, this.cePreview); faceRectangle.Width = (detectedfaceBoxCoordinates.Width / mediaStreamWidth) * faceHighlightRect.Width; faceRectangle.Height = (detectedfaceBoxCoordinates.Height / mediaStreamHeight) * faceHighlightRect.Height; faceRectangle.Margin = new Thickness((uint)(detectedfaceBoxCoordinates.X / mediaStreamWidth), (uint)(detectedfaceBoxCoordinates.Y / mediaStreamHeight), 0, 0); return(faceRectangle); }
public StreamResolution(IMediaEncodingProperties properties) { if (properties == null) { throw new ArgumentNullException(nameof(properties)); } // Only handle ImageEncodingProperties and VideoEncodingProperties, which are the two types that GetAvailableMediaStreamProperties can return if (!(properties is ImageEncodingProperties) && !(properties is VideoEncodingProperties)) { throw new ArgumentException($"Argument is of the wrong type. Required: {typeof(ImageEncodingProperties).Name} or {typeof(VideoEncodingProperties).Name}.", nameof(properties)); } // Store the actual instance of the IMediaEncodingProperties for setting them later this.properties = properties; }
/// <summary> /// Stops the preview and deactivates a display request, to allow the screen to go into power saving modes /// </summary> /// <returns></returns> private async Task StopPreviewAsync() { // Stop the preview _previewProperties = null; await _mediaCapture.StopPreviewAsync(); // Use the dispatcher because this method is sometimes called from non-UI threads await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Cleanup the UI PreviewControl.Source = null; // Allow the device screen to sleep now that the preview is stopped _displayRequest.RequestRelease(); }); }
/// <summary> /// Initializes the camera capture. /// </summary> /// <param name="captureUse"> /// The capture use. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> public async Task <MediaCapture> Initialize(CaptureUse captureUse) { if (this._mediaCapture != null) { this.Dispose(); } var camera = await GetDeviceInfo(Panel.Back); this._mediaCapture = new MediaCapture(); this._mediaCapture.Failed += this.OnCaptureFailed; await this._mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = camera.Id }); this._mediaCapture.VideoDeviceController.PrimaryUse = captureUse; this._imgEncodingProperties = ImageEncodingProperties.CreateJpeg(); this._videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); if (captureUse == CaptureUse.Photo) { // get all possible resolutions for the current controller var resolutions = this._mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo) .ToList(); var max = 0; foreach ( var res in resolutions.OfType <VideoEncodingProperties>().Where(res => res.Width * res.Height > max)) { // find the maximum resolution possible max = (int)(res.Width * res.Height); this._mediaEncodingProperties = res; this._imgEncodingProperties.Width = res.Width; this._imgEncodingProperties.Height = res.Height; } await this._mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync( MediaStreamType.Photo, this._mediaEncodingProperties); } return(this._mediaCapture); }
public StreamPropertiesHelper(IMediaEncodingProperties properties) { if (properties == null) { throw new ArgumentNullException(nameof(properties)); } // This helper class only uses VideoEncodingProperties or VideoEncodingProperties if (!(properties is ImageEncodingProperties) && !(properties is VideoEncodingProperties)) { throw new ArgumentException("Argument is of the wrong type. Required: " + typeof(ImageEncodingProperties).Name + " or " + typeof(VideoEncodingProperties).Name + ".", nameof(properties)); } // Store the actual instance of the IMediaEncodingProperties for setting them later _properties = properties; }
public StreamResolution(IMediaEncodingProperties properties) { if (properties == null) { throw new ArgumentNullException(nameof(properties)); } // Only handle ImageEncodingProperties and VideoEncodingProperties, which are the two types that GetAvailableMediaStreamProperties can return if (!(properties is ImageEncodingProperties) && !(properties is VideoEncodingProperties)) { throw new ArgumentException("Argument is of the wrong type. Required: " + typeof(ImageEncodingProperties).Name + " or " + typeof(VideoEncodingProperties).Name + ".", nameof(properties)); } // Store the actual instance of the IMediaEncodingProperties for setting them later _properties = properties; }
internal async void addEffectToImageStream() { if ((m_mediaCaptureMgr.MediaCaptureSettings.VideoDeviceCharacteristic != Windows.Media.Capture.VideoDeviceCharacteristic.AllStreamsIdentical) && (m_mediaCaptureMgr.MediaCaptureSettings.VideoDeviceCharacteristic != Windows.Media.Capture.VideoDeviceCharacteristic.PreviewPhotoStreamsIdentical) && (m_mediaCaptureMgr.MediaCaptureSettings.VideoDeviceCharacteristic != Windows.Media.Capture.VideoDeviceCharacteristic.RecordPhotoStreamsIdentical)) { IMediaEncodingProperties props2 = m_mediaCaptureMgr.VideoDeviceController.GetMediaStreamProperties(Windows.Media.Capture.MediaStreamType.Photo); if (props2.GetType().Equals("Image")) //Cant add an effect to an image type { //Change the media type on the stream System.Collections.Generic.IReadOnlyList <IMediaEncodingProperties> supportedPropsList = m_mediaCaptureMgr.VideoDeviceController.GetAvailableMediaStreamProperties(Windows.Media.Capture.MediaStreamType.Photo); int i = 0; while (i++ < supportedPropsList.Count) { if (supportedPropsList[i].Type.Equals("Video")) { await m_mediaCaptureMgr.VideoDeviceController.SetMediaStreamPropertiesAsync(Windows.Media.Capture.MediaStreamType.Photo, supportedPropsList[i]); ShowStatusMessage("Change type on image pin successful"); await m_mediaCaptureMgr.AddEffectAsync(Windows.Media.Capture.MediaStreamType.Photo, "GrayscaleTransform.GrayscaleEffect", null); ShowStatusMessage("Add effect to photo successful"); m_bEffectAddedToPhoto = true; chkAddRemoveEffect.IsEnabled = true; break; } } chkAddRemoveEffect.IsEnabled = true; } else { await m_mediaCaptureMgr.AddEffectAsync(Windows.Media.Capture.MediaStreamType.Photo, "GrayscaleTransform.GrayscaleEffect", null); ShowStatusMessage("Add effect to photo successful"); chkAddRemoveEffect.IsEnabled = true; m_bEffectAddedToPhoto = true; } } else { chkAddRemoveEffect.IsEnabled = true; } }
private IMediaEncodingProperties GetMediaEncodingProperties(IEnumerable <StreamResolution> resolutions) { var deviceFamily = AnalyticsInfo.VersionInfo.DeviceFamily; IMediaEncodingProperties defaultEncodingProperties = null; if (deviceFamily == Constants.WindowsIoTFamily) { defaultEncodingProperties = resolutions.FirstOrDefault(x => x.Width == 640)?.EncodingProperties; } else { defaultEncodingProperties = resolutions.FirstOrDefault(x => x.Width == 1440)?.EncodingProperties ?? resolutions.FirstOrDefault(x => x.Width == 1280)?.EncodingProperties ?? resolutions.FirstOrDefault(x => x.Width == 800)?.EncodingProperties ?? resolutions.FirstOrDefault(x => x.Width == 640)?.EncodingProperties; } return(defaultEncodingProperties); }
Size GetPreviewResolution(IMediaEncodingProperties props) { // Get our preview properties if (props is VideoEncodingProperties previewProperties) { var streamWidth = previewProperties.Width; var streamHeight = previewProperties.Height; // For portrait orientations, the width and height need to be swapped if (displayOrientation == DisplayOrientations.Portrait || displayOrientation == DisplayOrientations.PortraitFlipped) { streamWidth = previewProperties.Height; streamHeight = previewProperties.Width; } return(new Size(streamWidth, streamHeight)); } return(default);
private async Task <bool> SetCameraResolutionAsync(int pr_width, int pr_height) { System.Collections.Generic.IReadOnlyList <IMediaEncodingProperties> PreviewPropsList = _mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview); for (int i = 0; i < PreviewPropsList.Count; i++) { IMediaEncodingProperties EncodingProps = PreviewPropsList[i]; VideoEncodingProperties spVideoEncodingProperties = (VideoEncodingProperties)EncodingProps; UInt32 propwidth = spVideoEncodingProperties.Width; UInt32 propheight = spVideoEncodingProperties.Height; if (propwidth == pr_width && propheight == pr_height) { await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, EncodingProps); return(true); } } // requested resolution not supported by device return(false); }
private async Task InitMediaDevice() { try { if (!FaceDetector.IsSupported) { return; } var cameraDevice = await CameraActions.FindCameraDeviceByPanelAsync(Panel.Front); var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id }; _mediaCapture = new MediaCapture(); await _mediaCapture.InitializeAsync(settings); var definition = new FaceDetectionEffectDefinition { SynchronousDetectionEnabled = false, DetectionMode = FaceDetectionMode.HighPerformance }; var faceDetectionEffect = (FaceDetectionEffect)await _mediaCapture.AddVideoEffectAsync(definition, MediaStreamType.VideoPreview); faceDetectionEffect.DesiredDetectionInterval = TimeSpan.FromMilliseconds(33); faceDetectionEffect.Enabled = true; faceDetectionEffect.FaceDetected += FaceDetectionEffect_FaceDetected; CameraViewer.Source = _mediaCapture; await _mediaCapture.StartPreviewAsync(); _previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); } catch (Exception exception) { Messages = exception.ToString(); } }
/// <summary> /// Sets encoding properties on a camera stream. Ensures CaptureElement and preview stream are stopped before setting properties. /// </summary> public async Task SetMediaStreamPropertiesAsync(MediaStreamType streamType, IMediaEncodingProperties encodingProperties) { // Stop preview and unlink the CaptureElement from the MediaCapture object await MediaCapture.StopPreviewAsync(); _previewControl.Source = null; // Apply desired stream properties await MediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, encodingProperties); // Find the sources var allGroups = await MediaFrameSourceGroup.FindAllAsync(); var sourceGroups = allGroups.Select(g => new { Group = g, SourceInfo = g.SourceInfos.FirstOrDefault(i => i.SourceKind == MediaFrameSourceKind.Color) }).Where(g => g.SourceInfo != null).ToList(); if (sourceGroups.Count == 0) { // No camera sources found return; } var selectedSource = sourceGroups.FirstOrDefault(); // Recreate the CaptureElement pipeline and restart the preview _previewControl.Source = MediaCapture; var settings = new MediaCaptureInitializationSettings() { SourceGroup = selectedSource.Group, SharingMode = MediaCaptureSharingMode.ExclusiveControl, StreamingCaptureMode = StreamingCaptureMode.Video, MemoryPreference = MediaCaptureMemoryPreference.Cpu }; await MediaCapture.InitializeAsync(settings); // await MediaCapture.StartPreviewAsync(); }
/// <summary> /// Gets the current orientation of the UI in relation to the device and applies a corrective rotation to the preview /// </summary> /// <returns>Task representing the async event status</returns> private async Task SetPreviewRotationAsync() { // Only need to update the orientation if the camera is mounted on the device if (this._externalCamera) { return; } // Calculate which way and how far to rotate the preview int rotationDegrees = ConvertDisplayOrientationToDegrees(this._displayOrientation); // The rotation direction needs to be inverted if the preview is being mirrored if (this._mirroringPreview) { rotationDegrees = (360 - rotationDegrees) % 360; } // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames IMediaEncodingProperties props = this._mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); props.Properties.Add(RotationKey, rotationDegrees); await this._mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null); }
/// <summary> /// Stops the preview and deactivates a display request, to allow the screen to go into power saving modes /// </summary> /// <returns></returns> private async Task StopPreviewAsync() { // Stop the preview try { _previewProperties = null; await _mediaCapture.StopPreviewAsync(); } catch (Exception ex) { Debug.WriteLine("Exception when stopping the preview: {0}", ex.ToString()); } // Use the dispatcher because this method is sometimes called from non-UI threads await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Cleanup the UI PreviewControl.Source = null; // Allow the device screen to sleep now that the preview is stopped _displayRequest.RequestRelease(); }); }
internal async void chkAddRemoveEffect_Checked(Object sender, Windows.UI.Xaml.RoutedEventArgs e) { try { chkAddRemoveEffect.IsEnabled = false; await m_mediaCaptureMgr.AddEffectAsync(Windows.Media.Capture.MediaStreamType.VideoPreview, "GrayscaleTransform.GrayscaleEffect", null); ShowStatusMessage("Add effect to video preview successful"); if ((m_mediaCaptureMgr.MediaCaptureSettings.VideoDeviceCharacteristic != Windows.Media.Capture.VideoDeviceCharacteristic.AllStreamsIdentical) && (m_mediaCaptureMgr.MediaCaptureSettings.VideoDeviceCharacteristic != Windows.Media.Capture.VideoDeviceCharacteristic.PreviewRecordStreamsIdentical)) { IMediaEncodingProperties props = m_mediaCaptureMgr.VideoDeviceController.GetMediaStreamProperties(Windows.Media.Capture.MediaStreamType.VideoRecord); if (!props.GetType().Equals("H264")) //Cant add an effect to an H264 stream { await m_mediaCaptureMgr.AddEffectAsync(Windows.Media.Capture.MediaStreamType.VideoRecord, "GrayscaleTransform.GrayscaleEffect", null); ShowStatusMessage("Add effect to video record successful"); m_bEffectAddedToRecord = true; addEffectToImageStream(); } else { addEffectToImageStream(); chkAddRemoveEffect.IsEnabled = true; } } else { addEffectToImageStream(); chkAddRemoveEffect.IsEnabled = true; } } catch (Exception exception) { ShowExceptionMessage(exception); } }
public static Image ConvertPreviewToUiHatImage(BitmapBounds faceBoxInPreviewCoordinates, CaptureElement cameraViewer, IMediaEncodingProperties previewProperties, Image imageHat) { var previewStream = previewProperties as VideoEncodingProperties; if (previewStream == null) { return(imageHat); } if (previewStream.Width == 0 || previewStream.Height == 0) { return(imageHat); } double streamWidth = previewStream.Width; double streamHeight = previewStream.Height; var previewInUi = GetPreviewStreamRectInControl(previewStream, cameraViewer); imageHat.Width = (faceBoxInPreviewCoordinates.Width / streamWidth) * previewInUi.Width; imageHat.Height = (faceBoxInPreviewCoordinates.Height / streamHeight) * previewInUi.Height; var x = (faceBoxInPreviewCoordinates.X / streamWidth) * previewInUi.Width; var y = (faceBoxInPreviewCoordinates.Y / streamHeight) * previewInUi.Height; Canvas.SetLeft(imageHat, x); Canvas.SetTop(imageHat, y - 20); imageHat.Visibility = Visibility.Visible; return(imageHat); }
/// <summary> /// Gets the current orientation of the UI in relation to the device and applies a corrective rotation to the preview /// </summary> private async Task SetPreviewRotationAsync(IMediaEncodingProperties props) { // Only need to update the orientation if the camera is mounted on the device. if (mediaCapture == null) return; // Calculate which way and how far to rotate the preview. int rotationDegrees; VideoRotation sourceRotation; CalculatePreviewRotation(out sourceRotation, out rotationDegrees); // Set preview rotation in the preview source. mediaCapture.SetPreviewRotation(sourceRotation); // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames //var props = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); props.Properties.Add(RotationKey, rotationDegrees); await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, props); var currentPreviewResolution = GetPreviewResolution(props); // Setup a frame to use as the input settings videoFrame = new VideoFrame(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, (int)currentPreviewResolution.Width, (int)currentPreviewResolution.Height); }
private async void MainPage_Unloaded(object sender, RoutedEventArgs e) { _previewProperties = null; await _mediaCapture.StopPreviewAsync(); }
private async Task InitializeCameraAsync() { isInitialized = false; //Get back camera or default if back not found var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); DeviceInformation desiredDevice = allVideoDevices.FirstOrDefault( x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back ); var cameraDevice = desiredDevice ?? allVideoDevices.FirstOrDefault(); if (cameraDevice == null) { CameraNotFoundMessage.Visibility = Visibility.Visible; return; } //Create and initialize Media Capture mediaCapture = new MediaCapture(); mediaCapture.Failed += MediaCapture_Failed; var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id }; try { await mediaCapture.InitializeAsync(settings); isInitialized = true; CameraDisabledMessage.Visibility = Visibility.Collapsed; } catch (UnauthorizedAccessException) { CameraDisabledMessage.Visibility = Visibility.Visible; } //if initialized - start preview if (isInitialized) { displayRequest.RequestActive(); if (cameraDevice.EnclosureLocation == null) { mirroringPreview = false; } else { mirroringPreview = (cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front); } CameraPreview.FlowDirection = mirroringPreview ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; CameraPreview.Source = mediaCapture; await mediaCapture.StartPreviewAsync(); BtnPhoto.IsEnabled = true; previewProperties = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); if (previewProperties != null) { displayOrientation = displayInformation.CurrentOrientation; await SetPreviewRotationAsync(); } } }
private async Task StartPreviewAsync() { // Prevent the device from sleeping while the preview is running _displayRequest.RequestActive(); // Set the preview source in the UI and mirror it if necessary HoldCamera.Source = mc; HoldCamera.FlowDirection = _mirroringPreview ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; // Start the preview try { await mc.StartPreviewAsync(); _isPreviewing = true; _previewProperties = mc.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); } catch (Exception ex) { Debug.WriteLine("Exception when starting the preview: {0}", ex.ToString()); } // Initialize the preview to the current orientation if (_previewProperties != null) { _displayOrientation = _displayInformation.CurrentOrientation; await SetPreviewRotationAsync(); } }
/// <summary> /// Starts the preview and adjusts it for for rotation and mirroring after making a request to keep the screen on /// </summary> /// <returns></returns> private async Task StartPreviewAsync() { // Prevent the device from sleeping while the preview is running _displayRequest.RequestActive(); // Set the preview source in the UI and mirror it if necessary PreviewControl.Source = _mediaCapture; PreviewControl.FlowDirection = _mirroringPreview ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; // Start the preview await _mediaCapture.StartPreviewAsync(); _previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); // Initialize the preview to the current orientation if (_previewProperties != null) { _displayOrientation = _displayInformation.CurrentOrientation; await SetPreviewRotationAsync(); } }
private void SetEncodingProperties(CaptureUse captureUse) { if (this._videoEncodingProfile.Video != null) { if (captureUse == CaptureUse.Photo) { var allProps = this._mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties( MediaStreamType.Photo); var photoProps = GetMediaProperties(allProps, CaptureUse.Photo) as ImageEncodingProperties; if (photoProps == null) { var videoProps = GetMediaProperties(allProps, CaptureUse.Video) as VideoEncodingProperties; if (videoProps != null) { this._imageEncodingProperties.Height = videoProps.Height; this._imageEncodingProperties.Width = videoProps.Width; this._mediaEncodingProperties = videoProps; } else { VideoEncodingProperties maxProps = null; var max = 0; foreach (var res in from res in allProps.OfType<VideoEncodingProperties>() where res.Width * res.Height > max select res) { max = (int)(res.Width * res.Height); maxProps = res; } if (maxProps != null) { this._imageEncodingProperties.Height = maxProps.Height; this._imageEncodingProperties.Width = maxProps.Width; this._mediaEncodingProperties = maxProps; } } } else { this._imageEncodingProperties = photoProps; this._mediaEncodingProperties = this._imageEncodingProperties; } } else { var mediaStreamProps = this._mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties( MediaStreamType.VideoPreview).ToList(); var videoProps = mediaStreamProps.OfType<VideoEncodingProperties>() .FirstOrDefault( x => x != null && this._supportedVideoFormats.Contains(x.Subtype.ToLower()) && x.Width == this._videoEncodingProfile.Video.Width && x.Height == this._videoEncodingProfile.Video.Height); if (videoProps != null) { this._mediaEncodingProperties = videoProps; } else { var allVideoProps = mediaStreamProps.OfType<VideoEncodingProperties>() .Where( x => x != null && this._supportedVideoFormats.Contains(x.Subtype.ToLower()) && x.Width <= this._videoEncodingProfile.Video.Width && x.Height <= this._videoEncodingProfile.Video.Height); VideoEncodingProperties maxVideoPropsRatio = null; var max = 0; foreach (var res in from res in allVideoProps where res.Width * res.Height > max let ratio = (double)res.Width / res.Height where ratio > 1.34 select res) { max = (int)(res.Width * res.Height); maxVideoPropsRatio = res; } if (maxVideoPropsRatio != null) { this._mediaEncodingProperties = maxVideoPropsRatio; } else { VideoEncodingProperties maxVideoProps = null; max = 0; foreach (var res in from res in allVideoProps where res.Width * res.Height > max select res) { max = (int)(res.Width * res.Height); maxVideoProps = res; } if (maxVideoProps != null) { this._mediaEncodingProperties = maxVideoProps; } } } } } }
/// <summary> /// プレビュー開始 /// </summary> /// <param name="mediaStreamTypeIndex"></param> public async void StartPreview(int mediaEncodingPropertyIndex) { selectedProperty = _MediaEncodingProperties[mediaEncodingPropertyIndex]; await _MediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, selectedProperty); await _MediaCapture.StartPreviewAsync(); }
/// <summary> /// Initialises the camera and resolves the resolution for both the /// full resolution and preview images. /// </summary> private async void InitializeCameraAsync() { if (CameraState != CameraStates.NotInitialized) { Debug.WriteLine(DebugTag + "InitializeCameraAsync(): Invalid state"); return; } Debug.WriteLine(DebugTag + "InitializeCameraAsync() ->"); CameraState = CameraStates.Initializing; ProgressIndicator.IsActive = true; MessageDialog messageDialog = null; #if WINDOWS_PHONE_APP DeviceInformationCollection devices; devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); if (devices.Count == 0) { ProgressIndicator.IsActive = false; CameraState = CameraStates.NotInitialized; messageDialog = new MessageDialog( _resourceLoader.GetString("FailedToFindCameraDevice/Text"), _resourceLoader.GetString("CameraInitializationFailed/Text")); await messageDialog.ShowAsync(); return; } // Use the back camera DeviceInformation info = devices[0]; _cam = true; foreach (var devInfo in devices) { if (devInfo.Name.ToLowerInvariant().Contains("back")) { info = devInfo; _cam = false; // Set this to true if you use front camera break; } } MyCaptureElement.FlowDirection = _cam ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; #endif _mediaCapture = new MediaCapture(); try { #if WINDOWS_PHONE_APP await _mediaCapture.InitializeAsync( new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Video, PhotoCaptureSource = PhotoCaptureSource.VideoPreview, AudioDeviceId = string.Empty, VideoDeviceId = info.Id }); #else await _mediaCapture.InitializeAsync(); #endif } catch (Exception ex) { messageDialog = new MessageDialog(ex.ToString(), _resourceLoader.GetString("CameraInitializationFailed/Text")); } MyCaptureElement.Source = _mediaCapture; if (messageDialog != null) { /* Add commands and set their callbacks; both buttons use the * same callback function instead of inline event handlers */ if (messageDialog.Commands != null) { messageDialog.Commands.Add(new UICommand(_resourceLoader.GetString("Retry/Text"), CommandInvokedHandler)); messageDialog.Commands.Add(new UICommand(_resourceLoader.GetString("Cancel/Text"), CommandInvokedHandler)); } // Set the command that will be invoked by default messageDialog.DefaultCommandIndex = 0; // Set the command to be invoked when escape is pressed messageDialog.CancelCommandIndex = 1; await messageDialog.ShowAsync(); } else { // Get the resolution uint width = 0; uint height = 0; IMediaEncodingProperties propertiesToSet = null; AppUtils.GetBestResolution(_mediaCapture, ref width, ref height, ref propertiesToSet); if (width > 0 && height > 0) { _dataContext.SetFullResolution((int)width, (int)height); int previewWidth = (int)FilterEffects.DataContext.DefaultPreviewResolutionWidth; int previewHeight = 0; AppUtils.CalculatePreviewResolution((int)width, (int)height, ref previewWidth, ref previewHeight); _dataContext.SetPreviewResolution(previewWidth, previewHeight); } if (propertiesToSet != null) { await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync( MediaStreamType.Photo, propertiesToSet); Debug.WriteLine(DebugTag + "Capture resolution set to " + width + "x" + height + "!"); } else { Debug.WriteLine(DebugTag + "Failed to set capture resolution! Using fallback resolution."); var fallbackResolution = new Size( FilterEffects.DataContext.DefaultPreviewResolutionWidth, FilterEffects.DataContext.DefaultPreviewResolutionHeight); _dataContext.PreviewResolution = fallbackResolution; _dataContext.FullResolution = fallbackResolution; } _mediaCapture.SetPreviewMirroring(false); await _mediaCapture.StartPreviewAsync(); } #if WINDOWS_PHONE_APP // In case front camera is used, we need to handle the rotations DisplayInformation displayInfo = DisplayInformation.GetForCurrentView(); displayInfo.OrientationChanged += DisplayInfo_OrientationChanged; DisplayInfo_OrientationChanged(displayInfo, null); #endif CameraState = CameraStates.Initialized; ProgressIndicator.IsActive = false; CaptureButton.IsEnabled = true; Debug.WriteLine(DebugTag + "InitializeCameraAsync() <-"); }
/// <summary> /// Retrieves the available resolutions from the given MediaCapture /// instance and places the best values into the given references. /// </summary> /// <param name="mediaCapture">A MediaCapture instance, e.g. a camera device.</param> /// <param name="width">The width of the best resolution.</param> /// <param name="height">The height of the best resolution.</param> /// <param name="mediaEncodingProperties">The IMediaEncodingProperties for the best resolution.</param> public static void GetBestResolution(MediaCapture mediaCapture, ref uint width, ref uint height, ref IMediaEncodingProperties mediaEncodingProperties) { if (mediaCapture.VideoDeviceController != null) { IReadOnlyList <IMediaEncodingProperties> mediaEncodingPropertiesList = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo); IEnumerator <IMediaEncodingProperties> enumerator = mediaEncodingPropertiesList.GetEnumerator(); while (enumerator.MoveNext()) { IMediaEncodingProperties encodingProperties = enumerator.Current; if (encodingProperties == null) { continue; } uint foundWidth = 0; uint foundHeight = 0; try { var imageEncodingProperties = encodingProperties as ImageEncodingProperties; if (imageEncodingProperties != null) { foundWidth = imageEncodingProperties.Width; foundHeight = imageEncodingProperties.Height; } else { var videoEncodingProperties = encodingProperties as VideoEncodingProperties; if (videoEncodingProperties != null) { foundWidth = videoEncodingProperties.Width; foundHeight = videoEncodingProperties.Height; } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(DebugTag + "Failed to resolve available resolutions: " + ex.Message); } System.Diagnostics.Debug.WriteLine(DebugTag + "Available resolution: " + foundWidth + "x" + foundHeight); //if (encodingProperties.Width * encodingProperties.Height > width * height) // Use this to get the resolution with the most pixels if (foundWidth > width) // Use this to get the widest resolution { width = foundWidth; height = foundHeight; mediaEncodingProperties = encodingProperties; } } } }
private Size GetPreviewResolution(IMediaEncodingProperties props) { // Get our preview properties var previewProperties = props as VideoEncodingProperties; if (previewProperties != null) { var streamWidth = previewProperties.Width; var streamHeight = previewProperties.Height; // For portrait orientations, the width and height need to be swapped if (displayOrientation == DisplayOrientations.Portrait || displayOrientation == DisplayOrientations.PortraitFlipped) { streamWidth = previewProperties.Height; streamHeight = previewProperties.Width; } return new Size(streamWidth, streamHeight); } return default(Size); }
public async Task InitializeCameraAsync() { try { if (_setup == null) { _setup = new SetupService(); } isAutoShot = await _setup.GetAutomode(); if (_mediaCapture == null) { _mediaCapture = new MediaCapture(); _mediaCapture.Failed += MediaCapture_Failed; _cameraDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); if (_cameraDevices == null || !_cameraDevices.Any()) { throw new NotSupportedException(); } DeviceInformation device; if (_cameraDevices.Count > 1) { device = _cameraDevices.FirstOrDefault(camera => camera.EnclosureLocation?.Panel == Windows.Devices.Enumeration.Panel.Back); } else { device = _cameraDevices.FirstOrDefault(camera => camera.EnclosureLocation?.Panel == Panel); } var cameraId = device?.Id ?? _cameraDevices.First().Id; await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = cameraId }); if (_mediaCapture.VideoDeviceController.FocusControl.Supported) { isAutoFocusCapable = true; errorMessage.Text = "VIZZoneInFront".GetLocalized(); } else { isAutoFocusCapable = false; errorMessage.Text = "NoFocusCamera".GetLocalized(); } IMediaEncodingProperties IProps = this._mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); vep = (VideoEncodingProperties)IProps; DrawLineOnCanvas(vep.Width, vep.Height); if (Panel == Windows.Devices.Enumeration.Panel.Back) { //_mediaCapture.SetRecordRotation(VideoRotation.Clockwise90Degrees); //_mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees); _mirroringPreview = false; } else { _mirroringPreview = false; } IsInitialized = true; CanSwitch = _cameraDevices?.Count > 1; RegisterOrientationEventHandlers(); await StartPreviewAsync(); } } catch (UnauthorizedAccessException) { errorMessage.Text = "Camera_Exception_UnauthorizedAccess".GetLocalized(); } catch (NotSupportedException) { errorMessage.Text = "Camera_Exception_NotSupported".GetLocalized(); } catch (TaskCanceledException) { errorMessage.Text = "Camera_Exception_InitializationCanceled".GetLocalized(); } catch (Exception) { errorMessage.Text = "Camera_Exception_InitializationError".GetLocalized(); } }