在 C# 中从网络摄像头捕获图像
Capture images from webcam in c#
我一直在开发一个生成和读取(解码)二维码的应用程序。在解码部分,用户捕获二维码图片,程序将开始解码过程。
我的问题是我不知道如何拍照。
P.S:
如果您提供库,请提供 link,其中包含使用该库的教程。
谢谢。
我一直在寻找网络摄像头录像,你可以使用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;
您可以根据自己的方便设置更改帧率。
我一直在开发一个生成和读取(解码)二维码的应用程序。在解码部分,用户捕获二维码图片,程序将开始解码过程。 我的问题是我不知道如何拍照。 P.S: 如果您提供库,请提供 link,其中包含使用该库的教程。 谢谢。
我一直在寻找网络摄像头录像,你可以使用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;
您可以根据自己的方便设置更改帧率。