public IGameImageSource Get(string key) { if (loadedSources.TryGetValue(key, out var device)) { return(device); } var devices = new FilterInfoCollection(FilterCategory.VideoInputDevice); //var devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); return(loadedSources[key] = new CaptureDeviceGameImageSource(devices.First(x => x.Name == key))); }
/// <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++; }; }