教你如何利用vfw控制摄像头

操作摄像头我想主要有以下几点。启动摄像头;将摄像头的视频流转为图片或视频文件保存;一些设置。



启动摄像头

包括创建捕捉窗口,连接硬件设备,显示图像以及一些基本设置。view plaincopy to clipboardprint?
Dim lpszName As String * 100
Dim lpszVer As String * 100
Dim Caps As CAPDRIVERCAPS

'//Create Capture Window
capGetDriverDescriptionA 0, lpszName, 100, lpszVer, 100 '// Retrieves driver info
lwndC = capCreateCaptureWindowA(lpszName, WS_CAPTION Or WS_THICKFRAME Or WS_VISIBLE Or WS_CHILD, 0, 0, 160, 120, Me.hWnd, 0)

'// Set title of window to name of driver
SetWindowText lwndC, lpszName

'// Set the video stream callback function
'capSetCallbackOnStatus lwndC, AddressOf MyStatusCallback
'capSetCallbackOnError lwndC, AddressOf MyErrorCallback

'// Connect the capture window to the driver
If capDriverConnect(lwndC, 0) Then
'/////
'// Only do the following if the connect was successful.
'// if it fails, the error will be reported in the call
'// back function.
'/////
'// Get the capabilities of the capture driver
capDriverGetCaps lwndC, VarPtr(Caps), Len(Caps)

'// If the capture driver does not support a dialog, grey it out
'// in the menu bar.
If Caps.fHasDlgVideoSource = 0 Then mnuSource.Enabled = False
If Caps.fHasDlgVideoFormat = 0 Then mnuFormat.Enabled = False
If Caps.fHasDlgVideoDisplay = 0 Then mnuDisplay.Enabled = False

'// Turn Scale on
capPreviewScale lwndC, True

'// Set the preview rate in milliseconds
capPreviewRate lwndC, 66

'// Start previewing the image from the camera
capPreview lwndC, True

'// Resize the capture window to show the whole image
ResizeCaptureWindow lwndC

End If
Dim lpszName As String * 100
Dim lpszVer As String * 100
Dim Caps As CAPDRIVERCAPS

'//Create Capture Window
capGetDriverDescriptionA 0, lpszName, 100, lpszVer, 100 '// Retrieves driver info
lwndC = capCreateCaptureWindowA(lpszName, WS_CAPTION Or WS_THICKFRAME Or WS_VISIBLE Or WS_CHILD, 0, 0, 160, 120, Me.hWnd, 0)

'// Set title of window to name of driver
SetWindowText lwndC, lpszName

'// Set the video stream callback function
'capSetCallbackOnStatus lwndC, AddressOf MyStatusCallback
'capSetCallbackOnError lwndC, AddressOf MyErrorCallback

'// Connect the capture window to the driver
If capDriverConnect(lwndC, 0) Then
'/////
'// Only do the following if the connect was successful.
'// if it fails, the error will be reported in the call
'// back function.
'/////
'// Get the capabilities of the capture driver
capDriverGetCaps lwndC, VarPtr(Caps), Len(Caps)

'// If the capture driver does not support a dialog, grey it out
'// in the menu bar.
If Caps.fHasDlgVideoSource = 0 Then mnuSource.Enabled = False
If Caps.fHasDlgVideoFormat = 0 Then mnuFormat.Enabled = False
If Caps.fHasDlgVideoDisplay = 0 Then mnuDisplay.Enabled = False

'// Turn Scale on
capPreviewScale lwndC, True

'// Set the preview rate in milliseconds
capPreviewRate lwndC, 66

'// Start previewing the image from the camera
capPreview lwndC, True

'// Resize the capture window to show the whole image
ResizeCaptureWindow lwndC

End If




将视频流转为图片或文件保存

关于保存为图片好像没有什么直接方法,我用的是在窗体中加入了一个picture控件来搞定的。

view plaincopy to clipboardprint?
SendMessage(lwnd, WM_CAP_EDIT_COPY, 0, 0)
Picture1.Picture = Clipboard.GetData
SavePicture Picture1.Picture, "c:\test.jpg"
SendMessage(lwnd, WM_CAP_EDIT_COPY, 0, 0)
Picture1.Picture = Clipboard.GetData
SavePicture Picture1.Picture, "c:\test.jpg"


保存到视频文件

view plaincopy to clipboardprint?
Dim sFileName As String
Dim CAP_PARAMS As CAPTUREPARMS

capCaptureGetSetup lwndC, VarPtr(CAP_PARAMS), Len(CAP_PARAMS)

CAP_PARAMS.dwRequestMicroSecPerFrame = (1 * (10 ^ 6)) / 30 ' 30 Frames per second
CAP_PARAMS.fMakeUserHitOKToCapture = True
CAP_PARAMS.fCaptureAudio = False

capCaptureSetSetup lwndC, VarPtr(CAP_PARAMS), Len(CAP_PARAMS)

sFileName = "C:\myvideo.avi"

capCaptureSequence lwndC ' Start Capturing!
capFileSaveAs lwndC, sFileName ' Copy video from swap file into a real file.
Dim sFileName As String
Dim CAP_PARAMS As CAPTUREPARMS

capCaptureGetSetup lwndC, VarPtr(CAP_PARAMS), Len(CAP_PARAMS)

CAP_PARAMS.dwRequestMicroSecPerFrame = (1 * (10 ^ 6)) / 30 ' 30 Frames per second
CAP_PARAMS.fMakeUserHitOKToCapture = True
CAP_PARAMS.fCaptureAudio = False

capCaptureSetSetup lwndC, VarPtr(CAP_PARAMS), Len(CAP_PARAMS)

sFileName = "C:\myvideo.avi"

capCaptureSequence lwndC ' Start Capturing!
capFileSaveAs lwndC, sFileName ' Copy video from swap file into a real file.





一些设置

设置格式

view plaincopy to clipboardprint?
Dim CAPSTATUS As CAPSTATUS
Dim lCaptionHeight As Long
Dim lX_Border As Long
Dim lY_Border As Long

SendMessage(lwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0)

lCaptionHeight = GetSystemMetrics(SM_CYCAPTION)
lX_Border = GetSystemMetrics(SM_CXFRAME)
lY_Border = GetSystemMetrics(SM_CYFRAME)

'// Get the capture window attributes .. width and height
If capGetStatus(lwnd, VarPtr(CAPSTATUS), Len(CAPSTATUS)) Then

'// Resize the capture window to the capture sizes
SetWindowPos lwnd, HWND_BOTTOM, 0, 0, _
CAPSTATUS.uiImageWidth + (lX_Border * 2), _
CAPSTATUS.uiImageHeight + lCaptionHeight + (lY_Border * 2), _
SWP_NOMOVE Or SWP_NOZORDER
End If
Dim CAPSTATUS As CAPSTATUS
Dim lCaptionHeight As Long
Dim lX_Border As Long
Dim lY_Border As Long

SendMessage(lwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0)

lCaptionHeight = GetSystemMetrics(SM_CYCAPTION)
lX_Border = GetSystemMetrics(SM_CXFRAME)
lY_Border = GetSystemMetrics(SM_CYFRAME)

'// Get the capture window attributes .. width and height
If capGetStatus(lwnd, VarPtr(CAPSTATUS), Len(CAPSTATUS)) Then

'// Resize the capture window to the capture sizes
SetWindowPos lwnd, HWND_BOTTOM, 0, 0, _
CAPSTATUS.uiImageWidth + (lX_Border * 2), _
CAPSTATUS.uiImageHeight + lCaptionHeight + (lY_Border * 2), _
SWP_NOMOVE Or SWP_NOZORDER
End If


设置视频源

view plaincopy to clipboardprint?
SendMessage(lwnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0)
SendMessage(lwnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0)

设置压缩选项

view plaincopy to clipboardprint?
SendMessage(lwnd, WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0)
SendMessage(lwnd, WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0)



以上就是主要源代码,当然自己运用的时候还要进行api的申明,结构体的申明等等。

你可能感兴趣的:(教你如何利用vfw控制摄像头)