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 找名字
'''