2012-04-17 114 views
0

我正在使用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

回答

0

可以檢查與DirectShow庫的連接。獲取的第一

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

添加到您選擇的攝像機名稱類屬性的所有連接攝像機的數組,然後你可以檢查是否連接相機

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