1

我正在使用 Emgu CV 编写 C# 网络摄像头应用程序。当用户在pictureBox中的帧捕获期间拔下网络摄像头时,我试图处理。
如果网络摄像头已拔下,则应用程序应每 2 秒开始扫描新的网络摄像头连接,直到可以再次更新图片框。
以下计时器代码无法捕捉任何内容,程序最初捕捉帧,我拔下相机,然后重新插上,但相机无法重新启动。

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();
            }
        }
    }

程序继续打印“No null”,拔掉相机后 20 秒,输出控制台打印出线程 '' (0xb96c) has exited with code 0 (0x0)

4

1 回答 1

0

您可以使用 DirectShow lib 检查连接性。首先获取所有连接的摄像头阵列

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

将所选相机名称的类属性添加到您的类属性中,然后您可以检查相机是否已连接

bool isConnected = DirectShowLib.DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice).Any(c => c.Name == selectedCameraName);
于 2017-03-04T19:01:44.453 回答