Video source for local video capture device (for example USB webcam).

This video source class captures video data from local video capture device, like USB web camera (or internal), frame grabber, capture board - anything which supports DirectShow interface. For devices which has a shutter button or support external software triggering, the class also allows to do snapshots. Both video size and snapshot size can be configured.

Sample usage:

// enumerate video devices videoDevices = new FilterInfoCollection( FilterCategory.VideoInputDevice ); // create video source VideoCaptureDevice videoSource = new VideoCaptureDevice( videoDevices[0].MonikerString ); // set NewFrame event handler videoSource.NewFrame += new NewFrameEventHandler( video_NewFrame ); // start the video source videoSource.Start( ); // ... // signal to stop when you no longer need capturing videoSource.SignalToStop( ); // ... private void video_NewFrame( object sender, NewFrameEventArgs eventArgs ) { // get new frame Bitmap bitmap = eventArgs.Frame; // process the frame }
상속: IVideoSource
예제 #1
0
 // New video device is selected
 private void devicesCombo_SelectedIndexChanged( object sender, EventArgs e )
 {
     if ( videoDevices.Count != 0 )
     {
         videoDevice = new VideoCaptureDevice( videoDevices[devicesCombo.SelectedIndex].MonikerString );
         EnumeratedSupportedFrameSizes( videoDevice );
     }
 }
예제 #2
0
        // Open local video capture device
        private void localVideoCaptureDeviceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();

            if (form.ShowDialog(this) == DialogResult.OK)
            {
                // create video source
                VideoCaptureDevice videoSource = new VideoCaptureDevice(form.VideoDevice);

                // open it
                OpenVideoSource(videoSource);
            }
        }
예제 #3
0
        private void btnSelectCamera_Click(object sender, EventArgs e)
        {
            VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();

            if (form.ShowDialog() == DialogResult.OK)
            {
                VideoCaptureDevice device = new VideoCaptureDevice(form.VideoDevice);

                device.VideoResolution = selectResolution(device);

                controller.Device = device;
                controller.Start();

                toolStripStatusLabel1.Text = "Initializing...";
            }
        }
예제 #4
0
        private static VideoCapabilities selectResolution(VideoCaptureDevice device)
        {
            foreach (var cap in device.VideoCapabilities)
            {
                if (cap.FrameSize.Height == 240)
                    return cap;
                if (cap.FrameSize.Width == 320)
                    return cap;
            }

            return device.VideoCapabilities.Last();
        }
예제 #5
0
        // Start cameras
        private void StartCameras()
        {
            // create first video source
            var videoSource1 = new VideoCaptureDevice(videoDevices[camera1Combo.SelectedIndex].MonikerString);
            // videoSource1.DesiredFrameRate = 10;

            videoSourcePlayer1.VideoSource = videoSource1;
            videoSourcePlayer1.Start();

            // create second video source
            if (camera2Combo.Enabled == true)
            {
                System.Threading.Thread.Sleep(500);

                var videoSource2 = new VideoCaptureDevice(videoDevices[camera2Combo.SelectedIndex].MonikerString);
                // videoSource2.DesiredFrameRate = 10;

                videoSourcePlayer2.VideoSource = videoSource2;
                videoSourcePlayer2.Start();
            }

            // reset stop watch
            stopWatch = null;
            // start timer
            timer.Start();
        }
        // Collect supported video and snapshot sizes
        private void EnumeratedSupportedFrameSizes(VideoCaptureDevice videoDevice)
        {
            this.Cursor = Cursors.WaitCursor;

            videoResolutionsCombo.Items.Clear();
            snapshotResolutionsCombo.Items.Clear();
            videoInputsCombo.Items.Clear();

            videoCapabilitiesDictionary.Clear();
            snapshotCapabilitiesDictionary.Clear();

            try
            {
                // collect video capabilities
                VideoCapabilities[] videoCapabilities = videoDevice.VideoCapabilities;
                int videoResolutionIndex = 0;

                foreach (VideoCapabilities capabilty in videoCapabilities)
                {
                    string item = string.Format(
                        "{0} x {1}", capabilty.FrameSize.Width, capabilty.FrameSize.Height);

                    if (!videoResolutionsCombo.Items.Contains(item))
                    {
                        if (captureSize == capabilty.FrameSize)
                        {
                            videoResolutionIndex = videoResolutionsCombo.Items.Count;
                        }

                        videoResolutionsCombo.Items.Add(item);
                    }

                    if (!videoCapabilitiesDictionary.ContainsKey(item))
                    {
                        videoCapabilitiesDictionary.Add(item, capabilty);
                    }
                }

                if (videoCapabilities.Length == 0)
                {
                    videoResolutionsCombo.Items.Add("Not supported");
                }

                videoResolutionsCombo.SelectedIndex = videoResolutionIndex;


                if (configureSnapshots)
                {
                    // collect snapshot capabilities
                    VideoCapabilities[] snapshotCapabilities = videoDevice.SnapshotCapabilities;
                    int snapshotResolutionIndex = 0;

                    foreach (VideoCapabilities capabilty in snapshotCapabilities)
                    {
                        string item = string.Format(
                            "{0} x {1}", capabilty.FrameSize.Width, capabilty.FrameSize.Height);

                        if (!snapshotResolutionsCombo.Items.Contains(item))
                        {
                            if (snapshotSize == capabilty.FrameSize)
                            {
                                snapshotResolutionIndex = snapshotResolutionsCombo.Items.Count;
                            }

                            snapshotResolutionsCombo.Items.Add(item);
                            snapshotCapabilitiesDictionary.Add(item, capabilty);
                        }
                    }

                    if (snapshotCapabilities.Length == 0)
                    {
                        snapshotResolutionsCombo.Items.Add("Not supported");
                    }

                    snapshotResolutionsCombo.SelectedIndex = snapshotResolutionIndex;
                }

                // get video inputs
                availableVideoInputs = videoDevice.AvailableCrossbarVideoInputs;
                int videoInputIndex = 0;

                foreach (VideoInput input in availableVideoInputs)
                {
                    string item = string.Format("{0}: {1}", input.Index, input.Type);

                    if ((input.Index == videoInput.Index) && (input.Type == videoInput.Type))
                    {
                        videoInputIndex = videoInputsCombo.Items.Count;
                    }

                    videoInputsCombo.Items.Add(item);
                }

                if (availableVideoInputs.Length == 0)
                {
                    videoInputsCombo.Items.Add("Not supported");
                }

                videoInputsCombo.SelectedIndex = videoInputIndex;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
예제 #7
0
 // Constructor
 public Grabber(VideoCaptureDevice parent, bool snapshotMode, PixelFormat pixelFormat = PixelFormat.Format24bppRgb)
 {
     this.parent = parent;
     this.snapshotMode = snapshotMode;
     this.pixelFormat = pixelFormat;
 }
예제 #8
0
        // Collect supported video and snapshot sizes
        private void EnumeratedSupportedFrameSizes( VideoCaptureDevice videoDevice )
        {
            this.Cursor = Cursors.WaitCursor;

            videoResolutionsCombo.Items.Clear( );
            snapshotResolutionsCombo.Items.Clear( );

            try
            {
                videoCapabilities = videoDevice.VideoCapabilities;
                snapshotCapabilities = videoDevice.SnapshotCapabilities;

                foreach ( VideoCapabilities capabilty in videoCapabilities )
                {
                    videoResolutionsCombo.Items.Add( string.Format( "{0} x {1}",
                        capabilty.FrameSize.Width, capabilty.FrameSize.Height ) );
                }

                foreach ( VideoCapabilities capabilty in snapshotCapabilities )
                {
                    snapshotResolutionsCombo.Items.Add( string.Format( "{0} x {1}",
                        capabilty.FrameSize.Width, capabilty.FrameSize.Height ) );
                }

                if ( videoCapabilities.Length == 0 )
                {
                    videoResolutionsCombo.Items.Add( "Not supported" );
                }
                if ( snapshotCapabilities.Length == 0 )
                {
                    snapshotResolutionsCombo.Items.Add( "Not supported" );
                }

                videoResolutionsCombo.SelectedIndex = 0;
                snapshotResolutionsCombo.SelectedIndex = 0;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }