c#获取视频采集卡信息并且能跟电脑互动
获取视频采集卡信息可以使用DirectShow技术,通过DirectShow可以获取到视频采集卡的名称、厂商、设备ID等信息。具体实现可以参考以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DirectShowLib;
namespace VideoCaptureCardInfo
{
class Program
{
static void Main(string[] args)
{
DsDevice[] devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
foreach (DsDevice device in devices)
{
Console.WriteLine("Device Name: {0}", device.Name);
Console.WriteLine("Device Moniker: {0}", device.MonikerString);
Console.WriteLine("Device Class Name: {0}", device.ClassName);
Console.WriteLine("Device Device Path: {0}", device.DevicePath);
}
Console.ReadLine();
}
}
}
与电脑互动可以使用Windows Media Foundation技术,使用Windows Media Foundation可以实现视频采集、编码、解码、播放等功能。具体实现可以参考以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Media.Capture;
using Windows.Media.Core;
using Windows.Media.Devices;
using Windows.Media.MediaProperties;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
namespace VideoCapture
{
class Program
{
static async Task Main(string[] args)
{
var mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync();
// Set the video capture device
var videoDevice = await FindVideoDevice();
var videoSource = await SelectVideoSource(mediaCapture, videoDevice);
// Set the video encoding properties
var videoEncoding = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
// Set the output file
var file = await KnownFolders.VideosLibrary.CreateFileAsync("captured_video.mp4", CreationCollisionOption.GenerateUniqueName);
// Start recording
await mediaCapture.StartRecordToStorageFileAsync(videoEncoding, file);
// Wait for the recording to complete
await Task.Delay(10000);
// Stop recording
await mediaCapture.StopRecordAsync();
// Play the recorded video
var mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
mediaPlayer.Play();
Console.ReadLine();
}
private static async Task<DeviceInformation> FindVideoDevice()
{
// Get all video capture devices
var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
// Select the first device
return devices.FirstOrDefault();
}
private static async Task<VideoCaptureDevice> SelectVideoSource(MediaCapture mediaCapture, DeviceInformation videoDevice)
{
// Get all video capture sources for the device
var videoSources = MediaCapture.FindAllVideoProfiles(videoDevice.Id);
// Select the highest resolution video capture source
var selectedVideoSource = videoSources.OrderByDescending(source => source.Width * source.Height).FirstOrDefault();
// Set the video capture source
await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, selectedVideoSource);
return new VideoCaptureDevice(mediaCapture, selectedVideoSource);
}
}
class VideoCaptureDevice
{
private MediaCapture mediaCapture;
private VideoEncodingProperties videoEncodingProperties;
public VideoCaptureDevice(MediaCapture mediaCapture, VideoEncodingProperties videoEncodingProperties)
{
this.mediaCapture = mediaCapture;
this.videoEncodingProperties = videoEncodingProperties;
}
public async Task<byte[]> CaptureFrame()
{
// Create a new video frame
var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)videoEncodingProperties.Width, (int)videoEncodingProperties.Height);
// Capture the current frame
var frame = await mediaCapture.GetPreviewFrameAsync(videoFrame);
// Convert the frame to a byte array
var buffer = await frame.SoftwareBitmap.ToByteArrayAsync(BitmapEncoder.PngEncoderId);
return buffer;
}
}
}
``
原文地址: https://www.cveoy.top/t/topic/fhXP 著作权归作者所有。请勿转载和采集!