东瓜豆腐 2020-09-11 09:40 采纳率: 25%
浏览 783

C# 调用摄像头拍照 video device was unplugged

最近在使用摄像头的时候,提示了这个 video device was unplugged错误

winform的形式,用了控件VideoSourcePlayer,下面是我的方法

videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count != 0)
            {
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);//连接摄像头。
                selectedDeviceIndex = 0;
                videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex];
                _vspTakePhoto.VideoSource = videoSource;
                // set NewFrame event handler
                _vspTakePhoto.Start();
            }
            else
            {
                throw new ApplicationException();
            }

但是我不断的刷新(写了方法不断地打开关闭摄像头),然后就可以。

所以述求是:

1.怎么循环获取到成功为止,而不是我手动刷新。

2.是代码问题还是硬件设备的问题呢?

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-09-11 10:26
    关注
    while (true)
    {
    
    videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    
                if (videoDevices.Count != 0)
                {
                    videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                    videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);//连接摄像头。
                    selectedDeviceIndex = 0;
                    videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex];
                    _vspTakePhoto.VideoSource = videoSource;
                    // set NewFrame event handler
                    _vspTakePhoto.Start();
                                    break;
                }
                else
                {
                    //throw new ApplicationException();
                }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能