WPF利用MediaFoundation打开摄像头捕捉图片

主要代码 以同步的方式获得帧

REM MediaFoundation的.net 类库 http://mfnet.sourceforge.net
Imports MediaFoundation
Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging


Class MainWindow
    Private mediaSource As IMFMediaSource
    Private attribute As IMFAttributes
    Private activateDevices() As IMFActivate
    Private deviceName As String REM 设备名字


    ''' <summary>
    ''' 打开摄像头设备
    ''' </summary>
    Private Sub OpenCaptureDevice()
        Dim result As Integer
        result = MFExtern.MFCreateAttributes(attribute, 1) REM 创建一个属性
        If (result <> 0) Then Return
        attribute.SetGUID(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, CLSID.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID) REM 设置属性
        Dim devicescount As Integer
        MFExtern.MFEnumDeviceSources(attribute, activateDevices, devicescount) REM 枚举满足属性的摄像头设备
        If (result <> 0) Then Return
        If (devicescount = 0) Then Return
        activateDevices(0).GetAllocatedString(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, deviceName, 0)
        Console.WriteLine(deviceName)
        activateDevices(0).ActivateObject(GetType(IMFMediaSource).GUID, mediaSource) REM 激活设备
    End Sub


    Private presentationDescriptor As IMFPresentationDescriptor = Nothing
    Private streamDescriptor As IMFStreamDescriptor = Nothing
    Private mediatypeHandler As IMFMediaTypeHandler = Nothing
    Private mediatypeCount As Integer
    Private mediaType As IMFMediaType = Nothing
    ''' <summary>
    ''' 枚举摄像头支持的参数,选择匹配的参数捕捉图像
    ''' </summary>
    ''' <param name="width"></param>
    ''' <param name="height"></param>
    ''' <param name="fps"></param>
    ''' <param name="typename"></param>
    Private Sub SetupCatureDevice(width As Integer, height As Integer, fps As Double, typename As String)
        Dim result As Integer
        REM 创建SourceReader
        result = MFExtern.MFCreateSourceReaderFromMediaSource(mediaSource, attribute, sourcereader)
        If (result <> 0) Then Return
        REM 获得一个表现描述符
        result = mediaSource.CreatePresentationDescriptor(presentationDescriptor)
        If (result <> 0) Then Return
        Dim bselected As Boolean
        REM 从表现描述符中获得流描述符
        result = presentationDescriptor.GetStreamDescriptorByIndex(0, bselected, streamDescriptor)
        If (result <> 0) Then Return
        REM 从流描述器中或得媒体类型操作器
        result = streamDescriptor.GetMediaTypeHandler(mediatypeHandler)
        If (result <> 0) Then Return
        REM 获得支持的媒体类型
        result = mediatypeHandler.GetMediaTypeCount(mediatypeCount)
        If (result <> 0) Then Return
        For i = 0 To mediatypeCount - 1 REM 遍历媒体类型,选择合适的读取
            result = mediatypeHandler.GetMediaTypeByIndex(i, mediaType)
            If (result <> 0) Then Continue For
            Dim framesize As UInt64
            mediaType.GetUINT64(MFAttributesClsid.MF_MT_FRAME_SIZE, framesize)
            Dim w, h As UInt32
            w = (framesize >> 32).ToString()
            h = (framesize And &H0FFFFFFF)
            Dim framerate As UInt64
            Dim frame As Int32 = (framerate >> 32).ToString()
            Dim ratio As Int32 = (framerate And &H00000000FFFFFFFFL).ToString()
            mediaType.GetUINT64(MFAttributesClsid.MF_MT_FRAME_RATE, framerate)
            Dim samplesize As Int32
            mediaType.GetUINT32(MFAttributesClsid.MF_MT_SAMPLE_SIZE, samplesize)
            Dim subtype As Guid
            mediaType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, subtype)
            Console.WriteLine(w.ToString + " x " + h.ToString() + " @ " + (frame / ratio).ToString("f1") + "hz" _
                + vbTab + "samplesize:" + samplesize.ToString() _
                + vbTab + "type:" + NameofGUID(subtype))
            If (w = width And h = height And frame / ratio = fps And NameofGUID(subtype) = typename) Then
                result = mediatypeHandler.SetCurrentMediaType(mediaType)
                If (result <> 0) Then Return
            End If
        Next
    End Sub


    ''' <summary>
    ''' 工具函数,根据GUID 找名字
    ''' 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在WPF应用程序中打开多个摄像头,可以使用OpenCV库来实现。首先,需要在项目中添加对OpenCV的引用。 接下来,可以使用OpenCV的VideoCapture类来访问和控制摄像头。为了打开多个摄像头,可以创建多个VideoCapture对象,并分别指定不同的设备ID。设备ID通常以0开始递增,表示不同的摄像头。 在WPF应用程序的代码中,可以创建并初始化多个VideoCapture对象,如下所示: ```csharp using OpenCvSharp; ... // 打开第一个摄像头 VideoCapture capture1 = new VideoCapture(0); // 打开第二个摄像头 VideoCapture capture2 = new VideoCapture(1); // 打开第三个摄像头 VideoCapture capture3 = new VideoCapture(2); ``` 然后,可以使用OpenCV的函数来读取和显示每个摄像头的视频帧。例如,可以使用`Mat`类来存储图像帧,然后将其显示在WPF应用程序的图像控件中。 ```csharp while (true) { // 读取第一个摄像头的帧 Mat frame1 = new Mat(); capture1.Read(frame1); // 将帧显示在WPF的图像控件中 // 读取第二个摄像头的帧 Mat frame2 = new Mat(); capture2.Read(frame2); // 将帧显示在WPF的图像控件中 // 读取第三个摄像头的帧 Mat frame3 = new Mat(); capture3.Read(frame3); // 将帧显示在WPF的图像控件中 } ``` 最后,可以将每个摄像头的帧显示在WPF应用程序的图像控件中,以实时显示摄像头的视频。这可以通过将`Mat`对象转换为WPF的`BitmapSource`对象来实现。 以上就是在WPF应用程序中打开多个摄像头的基本步骤。通过创建多个VideoCapture对象,并读取和显示每个摄像头的帧,可以实现多摄像头的实时视频显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值