Emgu CV检查网络摄像头连接

问题描述:

我正在使用Emgu CV编写一个C#网络摄像头应用程序。我试图在用户在pictureBox中捕获帧的时候拔掉网络摄像头。
如果网络摄像头被拔掉,那么应用程序应该每2秒开始扫描一次新的网络摄像头连接,直到pictureBox可以再次更新。
以下计时器代码无法捕捉任何内容,程序最初会捕捉帧,我拔下相机,然后插回,但相机无法重新启动。Emgu CV检查网络摄像头连接

private void timer1_Tick(object sender, EventArgs e) 
    { 

     if (cap == null) 
     { 
      try 
      { 
       cap = new Capture(0); 
       cap.SetCaptureProperty(CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 320); 
       cap.SetCaptureProperty(CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 240); 

       Console.WriteLine("Restarting Cam"); 
      } 
      catch (Exception ee){ 
       Console.WriteLine("null"); cap = null; return; 
      } 
     } 
     else 
     { 
      Console.WriteLine("NO null"); 
     } 

     try 
     { 
      Image<Bgr, byte> nextFrame = cap.QueryFrame(); 
     } 
     catch(Exception ee) 
     { 
      Console.WriteLine("Frame Capture fail"); 
      cap.Dispose(); 
      cap = null; 
      return; 
     } 
     using (Image<Bgr, byte> nextFrame = cap.QueryFrame()) 
     { 
      if (nextFrame != null) 
      { 
       Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>(); 
       videoBox.Image = nextFrame.ToBitmap(); 
      } 
     } 
    } 

程序保持打印“不空”,拔出照相机经过20秒,输出控制台打印出线程“”(0xb96c)已经与代码0(为0x0)退出

+1

好呀!而.......... – Shoban 2012-04-17 13:56:56

+0

你的问题是什么? – 2012-04-17 14:15:10

可以检查与DirectShow库的连接。获取的第一

DirectShowLib.DsDevice[] allCameras = DirectShowLib.DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); 

添加到您选择的摄像机名称类属性的所有连接摄像机的数组,然后你可以检查是否连接相机

bool isConnected = DirectShowLib.DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice).Any(c => c.Name == selectedCameraName);