Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compile Exe
#Dim All
#Debug Error On
#Debug Display On
%Unicode = 1
#Include "Win32API.inc"
#Include Once "dshow.inc"
'$Camera = "TX-1/LX-1"
$Camera = "Logitech HD Pro Webcam C920"
Global hDlg, CameraFound As Dword
Global pGraph As IGraphBuilder 'Filter Graph Manager
Global pBuild As ICaptureGraphBuilder2 'Capture Graph Builder
Global pSysDevEnum As ICreateDevEnum 'enumeration object
Global pEnumCat As IEnumMoniker
Global pMoniker As IMoniker 'contains information about other objects
Global pbc As IBindCTX
Global pCap As IBaseFilter 'Video capture filter
Global pPropBag As IPropertyBag
Global pControl As IMediaControl
Global pWindow As IVideoWindow 'Display Window
Function PBMain() As Long
Dialog New Pixels, 0, "Video Preview",,,600,400, %WS_SysMenu Or %WS_ClipChildren To hDlg
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local w,h As Long
Select Case Cb.Msg
Case %WM_InitDialog
GetMatchingMoniker
If CameraFound Then DisplayNamedCamera
Case %WM_Size
If IsObject(pWindow) Then
Dialog Get Client hDlg To w,h
pWindow.SetWindowPosition(0,0,w,h)
Else
Dialog Set Text hDlg, "Camera Not Found!"
End If
End Select
End Function
Sub DisplayNamedCamera
pGraph = NewCom ClsId $CLSID_FilterGraph 'filter graph
pBuild = NewCom ClsId $CLSID_CaptureGraphBuilder2 'capture graph builder
pMoniker.BindToObject(Nothing, Nothing, $IID_IBaseFilter, pCap) 'create device filter for the chosen device
pGraph.AddFilter(pCap,$Camera) 'add chosen device filter to the filter graph
pBuild.SetFilterGraph(pGraph) 'initialize pBuild
pBuild.RenderStream $Pin_Category_Preview, $MediaType_Video, pCap, Nothing, Nothing 'render the live source
pWindow = pGraph : pWindow.Owner = hDlg : pWindow.WindowStyle = %WS_Child Or %WS_ClipSiblings Or %WS_ClipChildren 'video window settings
pControl = pGraph
pControl.Run
End Sub
Sub GetMatchingMoniker 'stops enumeration on moniker which has the friendly name
Local pceltFetched As Dword, pwszDisplayName As WStringZ Ptr, varName As Variant
pSysDevEnum = NewCom ClsId $CLSID_SystemDeviceEnum 'enumeration object
If pSysDevEnum.CreateClassEnumerator($CLSID_VideoInputDeviceCategory, pEnumCat, 0) <> %S_Ok Then Exit Sub
While pEnumCat.next(1, pMoniker, pceltFetched) <> %S_False 'cycle through monikers
pMoniker.GetDisplayName(pbc, Nothing, pwszDisplayName) 'get complex camera name
pMoniker.BindToStorage(Nothing, Nothing, $IID_IPropertyBag, pPropBag) 'get info about Moniker
pPropBag.Read("FriendlyName", varName, Nothing) 'get friendly name
If Variant$$(varName) = $Camera Then CameraFound = 1 : Exit Do
Loop
End Sub
http://www.garybeene.com/sw/gbsnippets.htm