private static void WriteProperty(VideoCaptureDevice device, CameraControlProperty property, CameraProperty value) { if (!value.Supported) { return; } try { CameraControlFlags flags = value.Automatic ? CameraControlFlags.Auto : CameraControlFlags.Manual; int v; bool parsed = int.TryParse(value.CurrentValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v); if (parsed) { device.SetCameraProperty(property, v, flags); } else { log.ErrorFormat("Could not parse property {0}, value: {1}.", value.Identifier, value.CurrentValue); } } catch (Exception e) { log.ErrorFormat("Could not write property {0}. {1}.", value.Identifier, e.Message); } }
public bool GetCameraProperty(CameraControlProperty property, out int value, out CameraControlFlags controlFlags) { bool flag = true; if (deviceMoniker == null || string.IsNullOrEmpty(deviceMoniker)) { throw new ArgumentException("Video source is not specified."); } lock (sync) { object obj = null; try { obj = FilterInfo.CreateFilter(deviceMoniker); } catch { throw new ApplicationException("Failed creating device object for moniker."); } if (!(obj is IAMCameraControl)) { throw new NotSupportedException("The video source does not support camera control."); } IAMCameraControl iAMCameraControl = (IAMCameraControl)obj; int num = iAMCameraControl.Get(property, out value, out controlFlags); flag = (num >= 0); Marshal.ReleaseComObject(obj); return(flag); } }
private static void WriteProperty(VideoCaptureDevice device, CameraControlProperty property, CameraProperty value) { CameraControlFlags flags = value.Automatic ? CameraControlFlags.Auto : CameraControlFlags.Manual; int v = int.Parse(value.CurrentValue, CultureInfo.InvariantCulture); device.SetCameraProperty(property, v, flags); }
private static void OnAutoExposureEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { bool newVal = (bool)e.NewValue; CameraControlFlags flagSetting = newVal ? CameraControlFlags.Auto : CameraControlFlags.Manual; SetCameraFlag(CameraControlProperty.Exposure, flagSetting); }
/// <summary> /// /// </summary> /// <param name="dsDevice"></param> /// <param name="prop"></param> /// <returns></returns> public int GetCameraControl(DsDevice dsDevice, CameraControlProperty prop) { IFilterGraph2 filterGraph = new FilterGraph() as IFilterGraph2; IBaseFilter capFilter = null; int retVal = 0; try { // add the video input device int hr = filterGraph.AddSourceFilterForMoniker(dsDevice.Mon, null, "Source Filter", out capFilter); DsError.ThrowExceptionForHR(hr); IAMCameraControl cameraControl = capFilter as IAMCameraControl; int min, max, step, default_val; CameraControlFlags flag = 0; cameraControl.GetRange(prop, out min, out max, out step, out default_val, out flag); cameraControl.Get(prop, out retVal, out flag); } catch (Exception ex) { Console.WriteLine(ex.Message); } return(retVal); }
private int GetCurrentZoom() { int oldZoom = 0; CameraControlFlags oldFlags = CameraControlFlags.Manual; var e = _camControl.Get(CameraControlProperty.Zoom, out oldZoom, out oldFlags); return(oldZoom); }
public void SetExposure(int val, CameraControlFlags flags = CameraControlFlags.Manual) { if (_values["Exposure"] != val) { _videoSource.SetCameraProperty(CameraControlProperty.Exposure, val, flags); _values["Exposure"] = val; } }
public void PrintAllProperties() { int minValue = 0; int maxValue = 0; int stepSize = 1; int defaultValue = 0; CameraControlFlags controlFlags = 0; bool success; //for (int i = 0; i <= 6; i++) //{ // success = cam.GetCameraPropertyRange((CameraControlProperty)i, out minValue, out maxValue, out stepSize, out defaultValue, out controlFlags); //} success = cam.GetCameraProperty(0, out minValue, out controlFlags); }
public void ReadCamParaConfig() { try { if ((ctl == null) || (m_devicePath.IndexOf(GlobalVar.gl_LifeCam_DeviceID) < 0)) { return; } CameraControlFlags s = new CameraControlFlags(); ctl.Get(CameraControlProperty.Focus, out m_value_Focus, out s); ctl.Get(CameraControlProperty.Zoom, out m_value_Zoom, out s); ctl.Get(CameraControlProperty.Pan, out m_value_Pan, out s); ctl.Get(CameraControlProperty.Tilt, out m_value_Tilt, out s); ctl.Get(CameraControlProperty.Exposure, out m_value_Exposure, out s); } catch { } }
public void UpdateRange() { int min = 0, max = 0, stepping = 0, defaultValue = 0; CameraControlFlags flags = CameraControlFlags.None; if (_cameraControl == null) { Supported = false; } else { Supported = _cameraControl.GetRange(_cameraProperty, out min, out max, out stepping, out defaultValue, out flags) == 0; } Min = min; Max = max; Default = defaultValue; MinimumStepSize = stepping; Capabilities = (CameraPropertyFlags)flags; }
public bool GetCameraProperty(CameraControlProperty property, out int value, out CameraControlFlags controlFlags) { bool ret = true; if ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker))) { throw new ArgumentException("Video source is not specified."); } lock ( sync ) { object tempSourceObject = null; try { tempSourceObject = FilterInfo.CreateFilter(deviceMoniker); } catch { throw new ApplicationException("Failed creating device object for moniker."); } if (!(tempSourceObject is IAMCameraControl)) { throw new NotSupportedException("The video source does not support camera control."); } IAMCameraControl pCamControl = (IAMCameraControl)tempSourceObject; int hr = pCamControl.Get(property, out value, out controlFlags); ret = (hr >= 0); Marshal.ReleaseComObject(tempSourceObject); } return(ret); }
private int GetControlProperties(DirectShowLib.CameraControlProperty CtrlProp, ref int iMin, ref int iMax) { try { int iStep = 0; int iDft = 0; CameraControlFlags flags = new CameraControlFlags(); flags = CameraControlFlags.Auto; int iResult = 0; iResult = m_CamControl.GetRange(CtrlProp, out iMin, out iMax, out iStep, out iDft, out flags); string s1; s1 = String.Format("{0:X2}", iResult); return(iResult); } catch (Exception ex) { Console.WriteLine(ex.Message); return(-1); // Interaction.MsgBox(ex.Message); } }
private static void SetCameraFlag(CameraControlProperty camProperty, CameraControlFlags flagVal) { if (camera != null) { // Get the current settings from the webcam camera.Get(camProperty, out int v, out CameraControlFlags f); // If the camera property differs from the desired value, adjust it leaving value the same. if (f != flagVal) { camera.Set(camProperty, v, flagVal); //Console.WriteLine($"{cameraName} {camProperty} set to {flagVal}"); } else { //Console.WriteLine($"{cameraName} {camProperty} already {flagVal}"); } } else { //Console.WriteLine($"No physical camera matching \"{cameraName}\" found"); } }
/// <summary> /// Initialize access to the camera hardware /// </summary> public Cam() { // Event used to wait for completion of camera tasks _finished = new EventWaitHandle(false, EventResetMode.ManualReset); // Find the integrated camera device var coll = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (!coll.Any(fi => fi.MonikerString.StartsWith(@"@device:pnp:\\?\usb#vid_5986&pid_2115"))) { throw new Exception("Integrated Camera device not found"); } _vdev = new VideoCaptureDevice(coll.First(fi => fi.MonikerString.StartsWith(@"@device:pnp:\\?\usb#vid_5986&pid_2115")).MonikerString); // Initial settings and hook handlers _vdev.VideoResolution = _vdev.VideoCapabilities.First(sn => sn.FrameSize == new System.Drawing.Size(320, 240)); _vdev.PlayingFinished += (sender, e) => _finished.Set(); _vdev.NewFrame += (sender, e) => { int o_exp = -3; CameraControlFlags o_flag = CameraControlFlags.Auto; if (_cnt == 0) { // 1st frame, camera is open and working. Change settings to fixed constants. _vdev.GetCameraProperty(CameraControlProperty.Exposure, out o_exp, out o_flag); _vdev.SetCameraProperty(CameraControlProperty.Exposure, -3, CameraControlFlags.Manual); } if (_cnt == 4) { // 5th frame, keep this frame. Revert camera settings. Signal to stop querying for frames. _img = e.Frame.Clone(new System.Drawing.Rectangle(0, 0, e.Frame.Width, e.Frame.Height), System.Drawing.Imaging.PixelFormat.DontCare); _vdev.SetCameraProperty(CameraControlProperty.Exposure, o_exp, o_flag); _vdev.SignalToStop(); } _cnt++; }; }
/// <summary> /// Gets the range and default value of a specified camera property. /// </summary> /// /// <param name="property">Specifies the property to query.</param> /// <param name="minValue">Receives the minimum value of the property.</param> /// <param name="maxValue">Receives the maximum value of the property.</param> /// <param name="stepSize">Receives the step size for the property.</param> /// <param name="defaultValue">Receives the default value of the property.</param> /// <param name="controlFlags">Receives a member of the <see cref="CameraControlFlags"/> enumeration, indicating whether the property is controlled automatically or manually.</param> /// /// <returns>Returns true on sucee or false otherwise.</returns> /// /// <exception cref="ArgumentException">Video source is not specified - device moniker is not set.</exception> /// <exception cref="ApplicationException">Failed creating device object for moniker.</exception> /// <exception cref="NotSupportedException">The video source does not support camera control.</exception> /// public bool GetCameraPropertyRange( CameraControlProperty property, out int minValue, out int maxValue, out int stepSize, out int defaultValue, out CameraControlFlags controlFlags ) { bool ret = true; // check if source was set if ( ( deviceMoniker == null ) || ( string.IsNullOrEmpty( deviceMoniker ) ) ) { throw new ArgumentException( "Video source is not specified." ); } lock ( sync ) { object tempSourceObject = null; // create source device's object try { tempSourceObject = FilterInfo.CreateFilter( deviceMoniker ); } catch { throw new ApplicationException( "Failed creating device object for moniker." ); } if ( !( tempSourceObject is IAMCameraControl ) ) { throw new NotSupportedException( "The video source does not support camera control." ); } IAMCameraControl pCamControl = (IAMCameraControl) tempSourceObject; int hr = pCamControl.GetRange( property, out minValue, out maxValue, out stepSize, out defaultValue, out controlFlags ); ret = ( hr >= 0 ); Marshal.ReleaseComObject( tempSourceObject ); } return ret; }
/// <summary> /// Gets the current setting of a camera property. /// </summary> /// /// <param name="property">Specifies the property to retrieve.</param> /// <param name="value">Receives the value of the property.</param> /// <param name="controlFlags">Receives the value indicating whether the setting is controlled manually or automatically</param> /// /// <returns>Returns true on sucee or false otherwise.</returns> /// /// <exception cref="ArgumentException">Video source is not specified - device moniker is not set.</exception> /// <exception cref="ApplicationException">Failed creating device object for moniker.</exception> /// <exception cref="NotSupportedException">The video source does not support camera control.</exception> /// public bool GetCameraProperty( CameraControlProperty property, out int value, out CameraControlFlags controlFlags ) { bool ret; // check if source was set if ( string.IsNullOrEmpty( _deviceMoniker ) ) { throw new ArgumentException( "Video source is not specified." ); } lock ( _sync ) { object tempSourceObject; // create source device's object try { tempSourceObject = FilterInfo.CreateFilter( _deviceMoniker ); } catch { throw new ApplicationException( "Failed creating device object for moniker." ); } if ( !( tempSourceObject is IAMCameraControl ) ) { throw new NotSupportedException( "The video source does not support camera control." ); } var pCamControl = (IAMCameraControl) tempSourceObject; int hr = pCamControl.Get( property, out value, out controlFlags ); ret = ( hr >= 0 ); Marshal.FinalReleaseComObject( tempSourceObject ); } return ret; }
static void SetCameraFlag(DsDevice[] devs, string cameraName, CameraControlProperty camProperty, CameraControlFlags flagVal) { IAMCameraControl camera = GetCamera(devs.Where(d => d.Name.ToLower().Contains(cameraName.ToLower())).FirstOrDefault()); if (camera != null) { // Get the current settings from the webcam camera.Get(camProperty, out int v, out CameraControlFlags f); // If the camera property differs from the desired value, adjust it leaving value the same. if (f != flagVal) { camera.Set(camProperty, v, flagVal); Console.WriteLine($"{cameraName} {camProperty} set to {flagVal}"); } else { Console.WriteLine($"{cameraName} {camProperty} already {flagVal}"); } } else { Console.WriteLine($"No physical camera matching \"{cameraName}\" found"); } }
// 获取相机参数 public bool SetCameraProperty(CameraControlProperty property, int value, CameraControlFlags controlFlags) { return(this.videoSource.SetCameraProperty(property, value, controlFlags)); }
/// <summary> /// Gets the range and default value of a specified camera property. /// </summary> /// /// <param name="property">Specifies the property to query.</param> /// <param name="minValue">Receives the minimum value of the property.</param> /// <param name="maxValue">Receives the maximum value of the property.</param> /// <param name="stepSize">Receives the step size for the property.</param> /// <param name="defaultValue">Receives the default value of the property.</param> /// <param name="controlFlags">Receives a member of the <see cref="CameraControlFlags"/> enumeration, indicating whether the property is controlled automatically or manually.</param> /// /// <returns>Returns true on sucee or false otherwise.</returns> /// /// <exception cref="ArgumentException">Video source is not specified - device moniker is not set.</exception> /// <exception cref="ApplicationException">Failed creating device object for moniker.</exception> /// <exception cref="NotSupportedException">The video source does not support camera control.</exception> /// public bool GetCameraPropertyRange( CameraControlProperty property, out int minValue, out int maxValue, out int stepSize, out int defaultValue, out CameraControlFlags controlFlags ) { bool ret = true; // check if source was set if ( ( deviceMoniker == null ) || ( string.IsNullOrEmpty( deviceMoniker ) ) ) { throw new ArgumentException( "Видеоисточник не указан." ); } lock ( sync ) { object tempSourceObject = null; // create source device's object try { tempSourceObject = FilterInfo.CreateFilter( deviceMoniker ); } catch { throw new ApplicationException( "Не удалось создать объект устройства." ); } if ( !( tempSourceObject is IAMCameraControl ) ) { throw new NotSupportedException( "Видеисточник не поддерживает управление камерой." ); } IAMCameraControl pCamControl = (IAMCameraControl) tempSourceObject; int hr = pCamControl.GetRange( property, out minValue, out maxValue, out stepSize, out defaultValue, out controlFlags ); ret = ( hr >= 0 ); Marshal.ReleaseComObject( tempSourceObject ); } return ret; }
/// <summary> /// /// </summary> /// <param name="dsDevice"></param> /// <param name="prop"></param> /// <param name="value"></param> /// <param name="flag"></param> public void SetCameraControl(DsDevice dsDevice, CameraControlProperty prop, int value, CameraControlFlags flag = CameraControlFlags.Auto) { IFilterGraph2 filterGraph = new FilterGraph() as IFilterGraph2; IBaseFilter capFilter = null; try { // add the video input device int hr = filterGraph.AddSourceFilterForMoniker(dsDevice.Mon, null, "Source Filter", out capFilter); DsError.ThrowExceptionForHR(hr); IAMCameraControl cameraControl = capFilter as IAMCameraControl; cameraControl.Set(prop, value, flag); } catch (Exception ex) { Console.WriteLine(ex.Message); } }