C# AForge.Video 使用调用usb uvc协议调用摄像头 service 与 winform组合使用 视频呈现及抓拍 视频呈现秒开。

 1 public partial class Form3 : Form
 2 {
 3     private CameraService cameraService;
 4 
 5     public Form3()
 6     {
 7         InitializeComponent();
 8     }
 9 
10     private void Form3_Load(object sender, EventArgs e)
11     {
12         try
13         {
14             cameraService = new CameraService();
15             cameraService.NewFrame += CameraService_NewFrame;
16             cameraService.StartCapture();
17         }
18         catch (Exception ex)
19         {
20             MessageBox.Show(ex.Message);
21         }
22     }
23 
24     private void CameraService_NewFrame(object sender, NewFrameEventArgs eventArgs)
25     {
26         // 获取新帧的图像
27         Bitmap videoFrame = (Bitmap)eventArgs.Frame.Clone();
28 
29         // 在 PictureBox 中显示图像
30         videoSourcePlayer.Image = videoFrame;
31     }
32 
33     private void btnStart_Click_1(object sender, EventArgs e)
34     {
35         if (cameraService.CaptureAndSaveImage())
36         {
37             MessageBox.Show($"图像已保存");
38         }
39         else
40         {
41             MessageBox.Show($"图像抓拍失败");
42         }
43     }
44 
45     private void Form3_FormClosing(object sender, FormClosingEventArgs e)
46     {
47         cameraService.Dispose();
48     }
49 }
public class CameraService : IDisposable
{
    private VideoCaptureDevice videoSource;
    private FilterInfoCollection videoDevices;
    private Bitmap latestFrame;

    public event NewFrameEventHandler NewFrame;

    public CameraService()
    {
        // 枚举所有视频输入设备
        videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        if (videoDevices.Count == 0)
        {
            throw new Exception("未找到视频输入设备!");
        }

        // 选择第一个视频设备
        videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
        videoSource.NewFrame += VideoSource_NewFrame;
    }

    private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        // 保存最新的帧图像
        latestFrame = (Bitmap)eventArgs.Frame.Clone();
        NewFrame?.Invoke(sender, eventArgs);
    }

    public void StartCapture()
    {
        if (videoSource != null && !videoSource.IsRunning)
        {
            videoSource.Start();
        }
    }

    public void StopCapture()
    {
        if (videoSource != null && videoSource.IsRunning)
        {
            videoSource.SignalToStop();
            videoSource.WaitForStop();
        }
    }

    public bool CaptureAndSaveImage()
    {
        if (latestFrame != null)
        {
            // 生成一个唯一的文件名
            string fileName = $"capture_{DateTime.Now:yyyyMMddHHmmss}.jpg";
            latestFrame.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            return true;
        }
        return false;
    }

    public void Dispose()
    {
        StopCapture();
        videoSource = null;
    }
}

 

posted @ 2025-03-08 13:36  摩诘  阅读(170)  评论(0)    收藏  举报