c# Get camera code

In C#, you can use the `System.Windows.Media.Capture` namespace to implement the code to obtain the camera. Here is a simple sample code that demonstrates how to get a camera and display a live video stream:

```csharp
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Effects;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Threading;

namespace CameraApp
{
    public partial class MainWindow : Window
    {
        private CaptureSource captureSource;
        private VideoCaptureDevice videoCaptureDevice;
        private VideoBrush videoBrush;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void StartCamera()
        {
            if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
            {
                videoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
                captureSource = new CaptureSource();
                captureSource.VideoCaptureDevice = videoCaptureDevice;
                videoBrush = new VideoBrush();
                videoBrush.SetSource(captureSource);
                captureScreen.Fill = videoBrush;
                captureSource.Start();
            }
        }

        private void StopCamera()
        {
            if (captureSource != null && captureSource.State == CaptureState.Started)
            {
                captureSource.Stop();
                captureSource = null;
                videoCaptureDevice = null;
                videoBrush = null;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            StartCamera();
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            StopCamera();
        }
    }
}
```

In this example, `MainWindow` is a WPF window that implements camera calling and display through `CaptureSource`, `VideoCaptureDevice`, `VideoBrush` and other classes. The `StartCamera` function is used to start acquiring the camera video stream, and the `StopCamera` function is used to stop acquiring the camera video stream. You can call the `StartCamera` function in the `Window_Loaded` event to start acquiring the camera video stream, and call the `StopCamera` function in the `Window_Closing` event to stop acquiring the camera video stream.

When the camera video stream is set to `VideoBrush`, you can apply `VideoBrush` to the background of any WPF control to display the camera live video. In this example, we apply a `VideoBrush` to the background of a `Grid` named `captureScreen`.

Please note that you need to add references to the `PresentationCore` and `System.Windows.Media` assemblies in your project to use the above namespaces and classes.

Supongo que te gusta

Origin blog.csdn.net/qq_32134891/article/details/131413104
Recomendado
Clasificación