用button1开启摄像头,button2关闭摄像头
时间: 2024-01-05 07:16:35 浏览: 31
下面是一个使用C#和Emgu CV实现打开和关闭摄像头的示例代码:
```csharp
using Emgu.CV;
using Emgu.CV.Structure;
private Capture _capture;
private bool _captureInProgress;
private void ProcessFrame(object sender, EventArgs arg)
{
using (var frame = _capture.QueryFrame().ToImage<Bgr, byte>())
{
if (frame != null)
{
pictureBox1.Image = frame.Bitmap;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (_capture == null)
{
_capture = new Capture();
}
if (!_captureInProgress)
{
_captureInProgress = true;
Application.Idle += ProcessFrame;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (_captureInProgress)
{
_captureInProgress = false;
Application.Idle -= ProcessFrame;
}
if (_capture != null)
{
_capture.Dispose();
_capture = null;
}
}
```
在上述代码中,我们首先定义了一个Capture对象来打开摄像头,然后我们定义了一个ProcessFrame方法来处理每一帧图像,将其转换为Bgr格式的图像,然后在PictureBox中显示。然后我们定义了一个button1_Click方法来启动摄像头,并且定义了一个button2_Click方法来停止摄像头。当我们点击button1按钮时,我们创建了_capture对象,并注册了ProcessFrame方法来处理每一帧图像。当我们点击button2按钮时,我们停止了摄像头的捕获,并释放了_capture对象的资源。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)