/// <summary> /// Dispose method /// </summary> public void Dispose() { // check for null since it's possible that Start was never called if (this.camera != null) { this.camera.Shutdown(); this.camera.Dispose(); this.camera = null; } }
/// <summary> /// Called by the pipeline when media capture should be stopped /// </summary> void IStartable.Stop() { if (this.camera != null) { this.camera.Shutdown(); this.camera.Dispose(); this.camera = null; } MediaCaptureDevice.Uninitialize(); }
/// <summary> /// Initializes a new instance of the <see cref="MediaCaptureInfo"/> class. /// </summary> /// <param name="device">Media capture device to query for parameter info</param> public MediaCaptureInfo(MediaCaptureDevice device) { this.BacklightCompensationInfo = this.GetInfo(VideoProperty.BacklightCompensation, device); this.BrightnessInfo = this.GetInfo(VideoProperty.Brightness, device); this.ColorEnableInfo = this.GetInfo(VideoProperty.ColorEnable, device); this.ContrastInfo = this.GetInfo(VideoProperty.Contrast, device); this.GainInfo = this.GetInfo(VideoProperty.Gain, device); this.GammaInfo = this.GetInfo(VideoProperty.Gamma, device); this.HueInfo = this.GetInfo(VideoProperty.Hue, device); this.SaturationInfo = this.GetInfo(VideoProperty.Saturation, device); this.SharpnessInfo = this.GetInfo(VideoProperty.Sharpness, device); this.WhiteBalanceInfo = this.GetInfo(VideoProperty.WhiteBalance, device); this.FocusInfo = this.GetInfo(ManagedCameraControlProperty.Focus, device); }
/// <summary> /// 初始化<see cref="CameraSence" />的新实例. /// </summary> /// <param name="id">The key.</param> /// <param name="control">The image.</param> /// <param name="cameraDevice">The camera.</param> public CameraSence(string id, Control control, MediaCaptureDevice cameraDevice) : base(id, control) { cameraDevice.NotNull(); CameraDevice = cameraDevice; RedBrush = new SolidColorBrush(SurfaceRenderTarget, Colors.Red); BlueBrush = new SolidColorBrush(SurfaceRenderTarget, Colors.Blue); LawnGreenBrush = new SolidColorBrush(SurfaceRenderTarget, Colors.LawnGreen); TextFactory = new SharpDX.DirectWrite.Factory(); YaHei36Font = new TextFormat(TextFactory, "Microsoft YaHei", FontWeight.Light, FontStyle.Normal, 36); YaHei18Font = new TextFormat(TextFactory, "Microsoft YaHei", FontWeight.Light, FontStyle.Normal, 18); _cameraOnlineLocation = new RawRectangleF(CameraDevice.Region.Width - 96, CameraDevice.Region.Height - 60, CameraDevice.Region.Width, CameraDevice.Region.Height); }
private PropertyInfo GetInfo(VideoProperty prop, MediaCaptureDevice device) { PropertyInfo info = new PropertyInfo(); int min = 0, max = 0, stepSize = 0, defValue = 0, flag = 0; if (device.GetRange(prop, ref min, ref max, ref stepSize, ref defValue, ref flag)) { info.MinValue = min; info.MaxValue = max; info.StepSize = stepSize; info.DefaultValue = defValue; info.AutoControlled = ((flag & (int)VideoPropertyFlags.Auto) != 0) ? true : false; info.Supported = true; } else { info.Supported = false; } return(info); }
private void ComboBoxCamera_Loaded(object sender, RoutedEventArgs e) { MediaCaptureDevice.Initialize(); ComboBoxCamera.Items.Clear(); foreach (var device in MediaCaptureDevice.AllDevices.OrderBy(d => d.FriendlyName)) { device.Attach(Config.Raw.UseInSharedMode);//required, otherwise no detail will show var item = new ComboBoxItem() { Content = device.FriendlyName, Tag = device }; ComboBoxCamera.Items.Add(item); } foreach (var item in ComboBoxCamera.Items.Cast <ComboBoxItem>()) { var dev = (MediaCaptureDevice)item.Tag; if (Config.Raw.DeviceId != dev.SymbolicLink) { continue; } ComboBoxCamera.SelectedItem = item; return; } foreach (var item in ComboBoxCamera.Items.Cast <ComboBoxItem>()) { var dev = (MediaCaptureDevice)item.Tag; if (Config.Raw.DeviceId != dev.SymbolicLink) { continue; } ComboBoxCamera.SelectedItem = item; return; } Debug.Assert(ComboBoxCamera.SelectedItem is null); ComboBoxCamera.SelectedItem = ComboBoxCamera.Items.Cast <ComboBoxItem>().FirstOrDefault(); ComboBoxCamera_SelectionChanged(sender, e); }
/// <summary> /// Called by the pipeline when media capture should be stopped /// </summary> private void OnPipelineStop() { this.Dispose(); MediaCaptureDevice.Uninitialize(); }
/// <summary> /// Called once all the subscriptions are established. /// </summary> private void OnPipelineStart() { MediaCaptureDevice.Initialize(); CaptureFormat found = null; foreach (var device in MediaCaptureDevice.AllDevices) { if (!device.Attach(this.configuration.UseInSharedMode)) { continue; } Trace.WriteLine($"MediaCapture - Searching for width={this.configuration.Width} height={this.configuration.Height} deviceId={this.configuration.DeviceId}"); Trace.WriteLine($"MediaCapture - Found: Name: '{device.FriendlyName}' SymLink: {device.SymbolicLink}"); Trace.WriteLine($"MediaCapture - Current - Width: {device.CurrentFormat.nWidth} Height: {device.CurrentFormat.nHeight} Type: {device.CurrentFormat.subType.Name}/{device.CurrentFormat.subType.Guid} Framerate: {device.CurrentFormat.nFrameRateNumerator}/{device.CurrentFormat.nFrameRateDenominator}"); if (string.IsNullOrEmpty(this.configuration.DeviceId) || device.FriendlyName == this.configuration.DeviceId || device.SymbolicLink == this.configuration.DeviceId) { foreach (var format in device.Formats) { Trace.WriteLine($"MediaCapture - Supported - Width: {format.nWidth} Height: {format.nHeight} Type: {format.subType.Name}/{format.subType.Guid} Framerate: {format.nFrameRateNumerator}/{format.nFrameRateDenominator}"); if (this.configuration.Width == format.nWidth && this.configuration.Height == format.nHeight) { // found suitable width/height if (this.configuration.Framerate == format.nFrameRateNumerator / format.nFrameRateDenominator) { // found suitable framerate if (found == null || this.configuration.Framerate == found.nFrameRateNumerator / found.nFrameRateDenominator) { // found first suitable or closer framerate match this.camera = device; found = format; } } } } } if (found != null) { Trace.WriteLine($"MediaCapture - Using - Width: {found.nWidth} Height: {found.nHeight} Type: {found.subType.Name}/{found.subType.Guid} Framerate: {found.nFrameRateNumerator}/{found.nFrameRateDenominator}"); break; } } if (found != null) { this.camera.CurrentFormat = found; this.deviceInfo = new MediaCaptureInfo(this.camera); var width = this.camera.CurrentFormat.nWidth; var height = this.camera.CurrentFormat.nHeight; // Get default settings for other properties. var currentConfig = this.GetDeviceConfiguration(); this.configuration.BacklightCompensation = currentConfig.BacklightCompensation; this.configuration.Brightness = currentConfig.Brightness; this.configuration.ColorEnable = currentConfig.ColorEnable; this.configuration.Contrast = currentConfig.Contrast; this.configuration.Gain = currentConfig.Gain; this.configuration.Gamma = currentConfig.Gamma; this.configuration.Hue = currentConfig.Hue; this.configuration.Saturation = currentConfig.Saturation; this.configuration.Sharpness = currentConfig.Sharpness; this.configuration.WhiteBalance = currentConfig.WhiteBalance; this.configuration.Focus = currentConfig.Focus; this.SetDeviceConfiguration(this.configuration); this.camera.CaptureSample((data, length, timestamp) => { var time = DateTime.FromFileTimeUtc(timestamp); using (var sharedImage = ImagePool.GetOrCreate(this.configuration.Width, this.configuration.Height, Microsoft.Psi.Imaging.PixelFormat.BGR_24bpp)) { sharedImage.Resource.CopyFrom(data); var originatingTime = this.pipeline.GetCurrentTimeFromElapsedTicks(timestamp); this.Out.Post(sharedImage, originatingTime); } }); } else { throw new ArgumentException("Camera specification not found"); } }
/// <inheritdoc/> public void Stop() { this.Dispose(); MediaCaptureDevice.Uninitialize(); }
/// <inheritdoc/> public void Stop(DateTime finalOriginatingTime, Action notifyCompleted) { this.Dispose(); MediaCaptureDevice.Uninitialize(); notifyCompleted(); }
/// <inheritdoc/> public void Start(Action <DateTime> notifyCompletionTime) { // notify that this is an infinite source component notifyCompletionTime(DateTime.MaxValue); MediaCaptureDevice.Initialize(); CaptureFormat found = null; foreach (var device in MediaCaptureDevice.AllDevices) { if (!device.Attach(this.configuration.UseInSharedMode)) { continue; } // Trace.WriteLine($"MediaCapture - Searching for width={this.configuration.Width} height={this.configuration.Height} deviceId={this.configuration.DeviceId}"); // Trace.WriteLine($"MediaCapture - Found: Name: '{device.FriendlyName}' SymLink: {device.SymbolicLink}"); // Trace.WriteLine($"MediaCapture - Current - Width: {device.CurrentFormat.nWidth} Height: {device.CurrentFormat.nHeight} Type: {device.CurrentFormat.subType.Name}/{device.CurrentFormat.subType.Guid} Framerate: {device.CurrentFormat.nFrameRateNumerator}/{device.CurrentFormat.nFrameRateDenominator}"); if (string.IsNullOrEmpty(this.configuration.DeviceId) || device.FriendlyName == this.configuration.DeviceId || device.SymbolicLink == this.configuration.DeviceId) { foreach (var format in device.Formats) { // Trace.WriteLine($"MediaCapture - Supported - Width: {format.nWidth} Height: {format.nHeight} Type: {format.subType.Name}/{format.subType.Guid} Framerate: {format.nFrameRateNumerator}/{format.nFrameRateDenominator}"); if (this.configuration.Width == format.nWidth && this.configuration.Height == format.nHeight) { // found suitable width/height if (this.configuration.Framerate == format.nFrameRateNumerator / format.nFrameRateDenominator) { // found suitable framerate if (found == null || this.configuration.Framerate == found.nFrameRateNumerator / found.nFrameRateDenominator) { // found first suitable or closer framerate match this.camera = device; found = format; } } } } } if (found != null) { // Trace.WriteLine($"MediaCapture - Using - Width: {found.nWidth} Height: {found.nHeight} Type: {found.subType.Name}/{found.subType.Guid} Framerate: {found.nFrameRateNumerator}/{found.nFrameRateDenominator}"); break; } } if (found != null) { this.camera.CurrentFormat = found; this.deviceInfo = new MediaCaptureInfo(this.camera); var width = this.camera.CurrentFormat.nWidth; var height = this.camera.CurrentFormat.nHeight; // Get default settings for other properties. var currentConfig = this.GetDeviceConfiguration(); this.configuration.BacklightCompensation = currentConfig.BacklightCompensation; this.configuration.Brightness = currentConfig.Brightness; this.configuration.ColorEnable = currentConfig.ColorEnable; this.configuration.Contrast = currentConfig.Contrast; this.configuration.Gain = currentConfig.Gain; this.configuration.Gamma = currentConfig.Gamma; this.configuration.Hue = currentConfig.Hue; this.configuration.Saturation = currentConfig.Saturation; this.configuration.Sharpness = currentConfig.Sharpness; this.configuration.WhiteBalance = currentConfig.WhiteBalance; this.configuration.Focus = currentConfig.Focus; this.SetDeviceConfiguration(this.configuration); this.camera.CaptureSample((data, length, timestamp) => { var time = DateTime.FromFileTimeUtc(timestamp); using var sharedImage = ImagePool.GetOrCreate(this.configuration.Width, this.configuration.Height, PixelFormat.BGR_24bpp); sharedImage.Resource.CopyFrom(data); var originatingTime = this.pipeline.GetCurrentTimeFromElapsedTicks(timestamp); this.Out.Post(sharedImage, originatingTime); }); } else { // Requested camera capture format was not found. Construct an exception message with a list of supported formats. var exceptionMessageBuilder = new StringBuilder(); if (string.IsNullOrEmpty(this.configuration.DeviceId)) { exceptionMessageBuilder.Append($"No cameras were found that support the requested capture format of {this.configuration.Width}x{this.configuration.Height} @ {this.configuration.Framerate} fps. "); } else { exceptionMessageBuilder.Append($"The specified camera {this.configuration.DeviceId} does not support the requested capture format of {this.configuration.Width}x{this.configuration.Height} @ {this.configuration.Framerate} fps. "); } exceptionMessageBuilder.AppendLine("Use one of the following supported camera capture formats instead:"); this.AppendSupportedCaptureFormats(exceptionMessageBuilder); throw new ArgumentException(exceptionMessageBuilder.ToString()); } }