C# Emgu CV检查网络摄像头连接
我正在使用Emgu CV编写一个C#web摄像头应用程序。在pictureBox中捕获帧时,我试图处理用户拔下网络摄像头的问题。C# Emgu CV检查网络摄像头连接,c#,emgucv,C#,Emgucv,我正在使用Emgu CV编写一个C#web摄像头应用程序。在pictureBox中捕获帧时,我试图处理用户拔下网络摄像头的问题。 如果网络摄像头已拔出,则应用程序应每隔2秒开始扫描新的网络摄像头连接,直到pictureBox可以再次更新。 下面的计时器代码无法捕获任何内容,程序最初捕获帧,我拔下相机,然后重新插入,但相机无法重新启动 private void timer1_Tick(object sender, EventArgs e) { if (cap == nul
如果网络摄像头已拔出,则应用程序应每隔2秒开始扫描新的网络摄像头连接,直到pictureBox可以再次更新。
下面的计时器代码无法捕获任何内容,程序最初捕获帧,我拔下相机,然后重新插入,但相机无法重新启动
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();
}
}
}
private void timer1\u勾选(对象发送方,事件参数e)
{
如果(cap==null)
{
尝试
{
cap=新捕获(0);
cap.SetCaptureProperty(cap_PROP.CV_cap_PROP_FRAME_WIDTH,320);
cap.SetCaptureProperty(cap_PROP.CV_cap_PROP_FRAME_HEIGHT,240);
控制台写入线(“重新启动Cam”);
}
捕获(异常ee){
Console.WriteLine(“null”);cap=null;return;
}
}
其他的
{
Console.WriteLine(“无空”);
}
尝试
{
Image nextFrame=cap.QueryFrame();
}
捕获(异常ee)
{
控制台写入线(“帧捕获失败”);
第()章;
cap=null;
回来
}
使用(Image nextFrame=cap.QueryFrame())
{
if(nextFrame!=null)
{
Image grayframe=nextFrame.Convert();
videoBox.Image=nextFrame.ToBitmap();
}
}
}
程序继续打印“No null”,拔下摄像头20秒后,输出控制台打印出线程“”(0xb96c)已退出,代码为0(0x0)您可以检查与DirectShow lib的连接。首先获取所有连接摄像头的阵列
DirectShowLib.DsDevice[] allCameras = DirectShowLib.DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
将选定摄影机名称添加到类属性中,然后可以检查摄影机是否已连接
bool isConnected = DirectShowLib.DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice).Any(c => c.Name == selectedCameraName);