本文介绍了C#和USB红外摄像机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好,我在c#中设计了一个窗口,其中包含一个组合框(包含检测到的USB设备),一个图片框(用于视频),一个标签,一个启动按钮(启动摄像头)和一个刷新按钮。 我有一个USB安全红外摄像头,我想连接到电脑,希望在图片盒中获得一些视频。 我调试并构建了项目,设计的窗口弹出应有的位置。当我连接USB相机时按下刷新按钮,它将被写入组合框USB 2.0 grabber。当我按下启动按钮时没有任何反应。有人可以帮助我/向我解释如何将视频输入到图片框中吗?我真的很感激一些帮助。 源代码如下: 使用System; 使用System.Collections.Generic;使用System.ComponentModel ; 使用System.Data;使用System.Drawing ; 使用System.Text; 使用System.Windows.Forms; 使用AForge.Video; 使用AForge.Video.DirectShow; 命名空间cam_aforge1 {公共部分类Form1:表格 { private bool DeviceExist = false; private FilterInfoCollection videoDevices; private VideoCaptureDevice videoSource = null; public Form1() { InitializeComponent(); } //获取设备名称 private void getCamList() { try { videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); comboBox1.Items.Clear(); if(videoDevices.Count == 0)抛出新的ApplicationException(); DeviceExist = true; foreach(videoDevices中的FilterInfo设备) { comboBox1.Items.Add(device.Name); } comboBox1.SelectedIndex = 0; //使dafault转到第一个凸轮} catch(ApplicationException) { DeviceExist = false; comboBox1.Items.Add(系统上没有捕获设备); } } //刷新按钮 private void rfsh_Click(object sender,EventArgs e) { getCamList(); } //切换开始和停止按钮 private void start_Click(object sender,EventArgs e) { if(start.Text == & Start) { if(DeviceExist) { videoSource = new VideoCaptureDevice(videoDevices [comboBox1.SelectedIndex] .MonikerString); videoSource.NewFrame + = new NewFrameEventHandler(video_NewFrame); 064 CloseVideoSource(); videoSource.DesiredFrameSize = new Size(160,120); //videoSource.DesiredFrameRate = 10; videoSource.Start(); label2.Text =设备运行......; start.Text =& Stop; timer1.Enabled = true; } 其他 { label2.Text =错误:未选择任何设备。; } } else { if(videoSource.IsRunning) { timer1.Enabled = false; CloseVideoSource(); label2.Text =设备已停止。; start.Text =& Start; } } } //如果新帧准备好,则为eventhandler private void video_NewFrame(object sender,NewFrameEventArgs eventArgs) { Bitmap img =(Bitmap)eventArgs.Frame.Clone(); //在这里处理 pictureBox1.Image = img; } //安全关闭设备 private void CloseVideoSource() { if(!(videoSource == null)) if(videoSource.IsRunning) { videoSource.SignalToStop(); videoSource = null; } } //获得1秒钟收到的总帧数 private void timer1_Tick(object sender,EventArgs e) { label2.Text =设备运行...+ videoSource.FramesReceived.ToString()+FPS; } //防止设备运行时突然关闭 private void Form1_FormClosed(object sender,FormClosedEventArgs e) { CloseVideoSource(); } } } 解决方案 Hello, i have designed a window in c# which contains one combobox(contains detected USB devices), one picturebox(for the video), one label, one start button(starts the camera)and one refresh button. I have a USB security IR camera which i want to connect to the computer and hopefully get some video feed in the picturebox. I have debugged and builded the project and the designed window pops up as it should. When i push the refresh button while the USB camera is connected,it is written in the combobox "USB 2.0 grabber". When i then push the start button nothing happens. Can somebody help me/explain to me how to get the video feed into the picture box? I really appreciate some help. Source code follows:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using AForge.Video; using AForge.Video.DirectShow; namespace cam_aforge1 { public partial class Form1 : Form { private bool DeviceExist = false; private FilterInfoCollection videoDevices; private VideoCaptureDevice videoSource = null; public Form1() { InitializeComponent(); } // get the devices name private void getCamList() { try { videoDevices = new FilterInfoCollection (FilterCategory.VideoInputDevice); comboBox1.Items.Clear(); if (videoDevices.Count == 0) throw new ApplicationException(); DeviceExist = true; foreach (FilterInfo device in videoDevices) { comboBox1.Items.Add(device.Name); } comboBox1.SelectedIndex = 0; //make dafault to first cam } catch (ApplicationException) { DeviceExist = false; comboBox1.Items.Add("No capture device on your system"); } } //refresh button private void rfsh_Click(object sender, EventArgs e) { getCamList(); } //toggle start and stop button private void start_Click(object sender, EventArgs e) { if (start.Text == "&Start") { if (DeviceExist) { videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString); videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); 064 CloseVideoSource(); videoSource.DesiredFrameSize = new Size(160,120); //videoSource.DesiredFrameRate = 10; videoSource.Start(); label2.Text = "Device running..."; start.Text = "&Stop"; timer1.Enabled = true; } else { label2.Text = "Error: No Device selected."; } } else { if (videoSource.IsRunning) { timer1.Enabled = false; CloseVideoSource(); label2.Text = "Device stopped."; start.Text = "&Start"; } } } //eventhandler if new frame is ready private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap img = (Bitmap)eventArgs.Frame.Clone(); //do processing here pictureBox1.Image = img; } //close the device safely private void CloseVideoSource() { if (!(videoSource == null)) if (videoSource.IsRunning) { videoSource.SignalToStop(); videoSource = null; } } //get total received frame at 1 second tick private void timer1_Tick(object sender, EventArgs e) { label2.Text = "Device running... " + videoSource.FramesReceived.ToString() + " FPS"; } //prevent sudden close while device is running private void Form1_FormClosed(object sender, FormClosedEventArgs e) { CloseVideoSource(); } } } 解决方案 这篇关于C#和USB红外摄像机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-19 03:53