我正在使用Emgu CV编写C#网络摄像头应用程序。当用户在pictureBox中捕获帧时拔出网络摄像头时,我试图处理 如果网络摄像头已拔下,则应用程序应每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();
}
}
}
程序保持打印“No null”,拔出相机后20秒,输出控制台打印出来''(0xb96c)已经退出,代码为0(0x0)
答案 0 :(得分:0)
您可以检查与DirectShow lib的连接。首先获取所有已连接摄像机的阵列
DirectShowLib.DsDevice[] allCameras = DirectShowLib.DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
为您选择的摄像机名称添加您的类属性,然后您可以检查摄像机是否已连接
bool isConnected = DirectShowLib.DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice).Any(c => c.Name == selectedCameraName);