阿杰 发表于 2007-6-9 19:48:43

在VB实现摄像头编程

<p>1 你要有视频捕捉设备<br/>2 正确安装在你的电脑上。<br/>3 新建一个VB项目,添加一个Module</p><p>Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _<br/> Alias "capCreateCaptureWindowA" ( _<br/> ByVal lpszWindowName As String, _<br/> ByVal dwStyle As Long, _<br/> ByVal x As Long, _<br/> ByVal y As Long, _<br/> ByVal nWidth As Long, _<br/> ByVal nHeight As Long, _<br/> ByVal hWndParent As Long, _<br/> ByVal nID As Long) As Long</p><p>Private Const WS_CHILD = &amp;H40000000<br/>Private Const WS_VISIBLE = &amp;H10000000<br/>Private Const WM_USER = &amp;H400<br/>Private Const WM_CAP_START = &amp;H400<br/>Private Const WM_CAP_EDIT_COPY = (WM_CAP_START + 30)<br/>Private Const WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10)<br/>Private Const WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52)<br/>Private Const WM_CAP_SET_OVERLAY = (WM_CAP_START + 51)<br/>Private Const WM_CAP_SET_PREVIEW = (WM_CAP_START + 50)<br/>Private Const WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11)</p><p>Private Declare Function SendMessage Lib "user32" _<br/> Alias "SendMessageA" ( _<br/> ByVal hwnd As Long, _<br/> ByVal wMsg As Long, _<br/> ByVal wParam As Long, _<br/> lParam As Any) As Long</p><p>Private Preview_Handle As Long</p><p><br/>Public Function CreateCaptureWindow( _<br/> hWndParent As Long, _<br/> Optional x As Long = 0, _<br/> Optional y As Long = 0, _<br/> Optional nWidth As Long = 320, _<br/> Optional nHeight As Long = 240, _<br/> Optional nCameraID As Long = 0) As Long</p><p> Preview_Handle = capCreateCaptureWindow("Video", _<br/>  WS_CHILD + WS_VISIBLE, x, y, _<br/>  nWidth, nHeight, hWndParent, 1)</p><p> SendMessage Preview_Handle, WM_CAP_DRIVER_CONNECT, nCameraID, 0<br/> SendMessage Preview_Handle, WM_CAP_SET_PREVIEWRATE, 30, 0<br/> SendMessage Preview_Handle, WM_CAP_SET_OVERLAY, 1, 0<br/> SendMessage Preview_Handle, WM_CAP_SET_PREVIEW, 1, 0</p><p> CreateCaptureWindow = Preview_Handle<br/>End Function</p><p><br/>Public Function CapturePicture(nCaptureHandle As Long) As StdPicture<br/> Clipboard.Clear<br/> SendMessage nCaptureHandle, WM_CAP_EDIT_COPY, 0, 0<br/> Set CapturePicture = Clipboard.GetData<br/>End Function</p><p><br/>Public Sub Disconnect(nCaptureHandle As Long, _<br/> Optional nCameraID = 0)</p><p> SendMessage nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, _<br/>  nCameraID, 0<br/>End Sub</p><p>4 在form上添加一个PictureBox,一个按钮,Caption设为 Save Pic<br/>Dim Video_Handle As Long</p><p>Private Sub Form_Load()<br/>  Video_Handle = CreateCaptureWindow(PicCapture.hwnd)<br/>End Sub</p><p>Private Sub Command1_Click()<br/>  Dim x As StdPicture<br/>  Set x = CapturePicture(Video_Handle)<br/>  SavePicture x, "c:\a.bmp"<br/>End Sub</p><p>Private Sub Form_Unload(Cancel As Integer)<br/>  Disconnect Video_Handle<br/>End Sub</p><p><br/>--------------------------------<br/>QQ群[1422505]</p>

阿杰 发表于 2007-6-9 20:04:12

<p>源码在这:</p><p>1 你要有视频捕捉设备<br/>2 正确安装在你的电脑上。<br/>3 新建一个VB项目,添加一个Module</p><p>Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _<br/> Alias "capCreateCaptureWindowA" ( _<br/> ByVal lpszWindowName As String, _<br/> ByVal dwStyle As Long, _<br/> ByVal x As Long, _<br/> ByVal y As Long, _<br/> ByVal nWidth As Long, _<br/> ByVal nHeight As Long, _<br/> ByVal hWndParent As Long, _<br/> ByVal nID As Long) As Long</p><p>Private Const WS_CHILD = &amp;H40000000<br/>Private Const WS_VISIBLE = &amp;H10000000<br/>Private Const WM_USER = &amp;H400<br/>Private Const WM_CAP_START = &amp;H400<br/>Private Const WM_CAP_EDIT_COPY = (WM_CAP_START + 30)<br/>Private Const WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10)<br/>Private Const WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52)<br/>Private Const WM_CAP_SET_OVERLAY = (WM_CAP_START + 51)<br/>Private Const WM_CAP_SET_PREVIEW = (WM_CAP_START + 50)<br/>Private Const WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11)</p><p>Private Declare Function SendMessage Lib "user32" _<br/> Alias "SendMessageA" ( _<br/> ByVal hwnd As Long, _<br/> ByVal wMsg As Long, _<br/> ByVal wParam As Long, _<br/> lParam As Any) As Long</p><p>Private Preview_Handle As Long</p><p><br/>Public Function CreateCaptureWindow( _<br/> hWndParent As Long, _<br/> Optional x As Long = 0, _<br/> Optional y As Long = 0, _<br/> Optional nWidth As Long = 320, _<br/> Optional nHeight As Long = 240, _<br/> Optional nCameraID As Long = 0) As Long</p><p> Preview_Handle = capCreateCaptureWindow("Video", _<br/>  WS_CHILD + WS_VISIBLE, x, y, _<br/>  nWidth, nHeight, hWndParent, 1)</p><p> SendMessage Preview_Handle, WM_CAP_DRIVER_CONNECT, nCameraID, 0<br/> SendMessage Preview_Handle, WM_CAP_SET_PREVIEWRATE, 30, 0<br/> SendMessage Preview_Handle, WM_CAP_SET_OVERLAY, 1, 0<br/> SendMessage Preview_Handle, WM_CAP_SET_PREVIEW, 1, 0</p><p> CreateCaptureWindow = Preview_Handle<br/>End Function</p><p><br/>Public Function CapturePicture(nCaptureHandle As Long) As StdPicture<br/> Clipboard.Clear<br/> SendMessage nCaptureHandle, WM_CAP_EDIT_COPY, 0, 0<br/> Set CapturePicture = Clipboard.GetData<br/>End Function</p><p><br/>Public Sub Disconnect(nCaptureHandle As Long, _<br/> Optional nCameraID = 0)</p><p> SendMessage nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, _<br/>  nCameraID, 0<br/>End Sub</p><p>4 在form上添加一个PictureBox,一个按钮,Caption设为 Save Pic<br/>Dim Video_Handle As Long</p><p>Private Sub Form_Load()<br/>  Video_Handle = CreateCaptureWindow(PicCapture.hwnd)<br/>End Sub</p><p>Private Sub Command1_Click()<br/>  Dim x As StdPicture<br/>  Set x = CapturePicture(Video_Handle)<br/>  SavePicture x, "c:\a.bmp"<br/>End Sub</p><p>Private Sub Form_Unload(Cancel As Integer)<br/>  Disconnect Video_Handle<br/>End Sub<br/></p>

马大哈 发表于 2007-6-9 22:57:06

<p>呵呵,顶!!!</p><p>版主辛苦了!!</p><p>这是一个好代码!</p>

hd37 发表于 2007-6-10 01:30:01

页: [1]
查看完整版本: 在VB实现摄像头编程