我正在尝试从最多6个不同的相机中收集图像。我的代码有效,但它似乎不是面向对象的,因为我必须切换摄像机的索引,将NewFrame事件处理程序设置为单独命名的处理程序,以将图像放入每个摄像机的图片框中。有没有更好的方法(见下面的代码)。
private void ImageCollector_Load(object sender, EventArgs e)
{
webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
Cameras = new VideoCaptureDevice[webcams.Count];
int index = 0;
foreach (FilterInfo CaptureDevice in webcams)
{
// Enable the cameras display
ViewLabelflowLayoutPanel.Controls[index].Visible = true;
ImageLabelflowLayoutPanel.Controls[index].Visible = true;
ViewflowLayoutPanel.Controls[index].Visible = true;
ImageflowLayoutPanel.Controls[index].Visible = true;
CameracomboBox.Items.Add(CaptureDevice.Name);
try
{
Cameras[index] = new VideoCaptureDevice(webcams[index].MonikerString);
switch (index)
{
case 0:
Cameras[index].NewFrame += new NewFrameEventHandler(cam1_NewFrame);
break;
case 1:
Cameras[index].NewFrame += new NewFrameEventHandler(cam2_NewFrame);
break;
case 2:
Cameras[index].NewFrame += new NewFrameEventHandler(cam3_NewFrame);
break;
case 3:
Cameras[index].NewFrame += new NewFrameEventHandler(cam4_NewFrame);
break;
case 4:
Cameras[index].NewFrame += new NewFrameEventHandler(cam5_NewFrame);
break;
case 5:
Cameras[index].NewFrame += new NewFrameEventHandler(cam6_NewFrame);
break;
}//endswitch
Cameras[index].Start();
}//endtry
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}//endcatch
}//endforeach
CameracomboBox.SelectedIndex = 0;
index++;
}//endImageCollector_Load
由于
答案 0 :(得分:0)
对你有好处,你可以识别代码味道!
我不知道NewFrame是如何工作的,但它可能有一个发送者,它是触发事件的Camera,所以你可以这样做。
foreach (FilterInfo CaptureDevice in webcams)
{
// Enable the cameras display
ViewLabelflowLayoutPanel.Controls[index].Visible = true;
ImageLabelflowLayoutPanel.Controls[index].Visible = true;
ViewflowLayoutPanel.Controls[index].Visible = true;
ImageflowLayoutPanel.Controls[index].Visible = true;
CameracomboBox.Items.Add(CaptureDevice.Name);
try
{
Cameras[index] = new VideoCaptureDevice(webcams[index].MonikerString);
Cameras[index].NewFrame += new NewFrameEventHandler(cam_NewFrame);
Cameras[index].Start();
}//endtry
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}//endcatch
}//endforeach
CameracomboBox.SelectedIndex = 0;
index++;
public void cam_NewFrame(object sender, EventArgs e){
VideoCaptureDevice cam = sender as VideoCaptureDevice;
//do what you need to do with cam here.
}
这假设发件人设置为相机对象。