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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值