在c#中从网络摄像头捕获图像

时间:2016-02-27 12:23:39

标签: c# webcam qr-code webcam-capture

我一直致力于生成和读取(解码)QR码的应用程序。在解码部分,用户捕获QR码的图像,程序将开始解码过程。 我的问题是我不知道如何拍照。 P.S: 如果您提供库,请提供一个链接,其中包含使用该库的教程。 谢谢。

2 个答案:

答案 0 :(得分:1)

我一直在寻找网络摄像机记录很长一段时间,你可以使用Aforge.NET。

以下是使用WPF的相同代码:

public partial class MainWindow : Window
    {
        private FilterInfoCollection VideoCaptureDevices;
        private VideoCaptureDevice FinalVideo;
        public VideoFileWriter writer= new VideoFileWriter();

        public MainWindow()
        {
            InitializeComponent();

            VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
            {
                comboBox1.Items.Add(VideoCaptureDevice.Name);
            }
            comboBox1.SelectedIndex = 0;


        }

        private void button1_Click(object sender, EventArgs e)
        {
            writer.Open(@"d:\\newVid.avi", 640, 480, 25, VideoCodec.MPEG4);

            FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
            FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
            FinalVideo.Start();

        }

        void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {

            System.Drawing.Image imgforms = (Bitmap)eventArgs.Frame.Clone();

            Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();

            BitmapImage bi = new BitmapImage();
            bi.BeginInit();

            MemoryStream ms = new MemoryStream();
            imgforms.Save(ms, ImageFormat.Bmp);
            ms.Seek(0, SeekOrigin.Begin);

            bi.StreamSource = ms;
            bi.EndInit();            

            //Using the freeze function to avoid cross thread operations 
            bi.Freeze();

            //Calling the UI thread using the Dispatcher to update the 'Image' WPF control         
            Dispatcher.BeginInvoke(new ThreadStart(delegate
            {
                pictureBox1.Source = bi; /*frameholder is the name of the 'Image' WPF control*/
            }));

            for (int i = 0; i < 2; i++)
            {
                writer.WriteVideoFrame(bmp);
            }

        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            writer.Close();
            FinalVideo.Stop();
            this.Close();
        }

    }

包含以下命名空间:

using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using System.Drawing.Drawing2D;
using AForge.Video.VFW;

您可以根据自己的方便设置更改帧率。

答案 1 :(得分:-1)

我建议你往下看

http://www.codeproject.com/Articles/125478/Versatile-WebCam-C-library

您需要的所有信息都在那里。