VB 调用摄像头拍照,并保存图片...

小歆11年前软件源码02044

VB 调用摄像头拍照


1、首先创建一个标准EXE工程

2、在窗体代码中加入如下必需的API及一个拍照的自定义函数



Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _
Alias "capCreateCaptureWindowA" ( _
ByVal lpszWindowName As String, _
ByVal dwStyle As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal hWndParent As Long, _
ByVal nID As Long) As Long

Private Const WS_CHILD = &H40000000
Private Const WS_VISIBLE = &H10000000
Private Const WM_USER = &H400
Private Const WM_CAP_START = &H400
Private Const WM_CAP_EDIT_COPY = (WM_CAP_START + 30)
Private Const WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10)
Private Const WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52)
Private Const WM_CAP_SET_OVERLAY = (WM_CAP_START + 51)
Private Const WM_CAP_SET_PREVIEW = (WM_CAP_START + 50)
Private Const WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11)

Private Preview_Handle As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long


Private Function CapturePicture(nCaptureHandle As Long) As StdPicture
Clipboard.Clear
SendMessage nCaptureHandle, WM_CAP_EDIT_COPY, 0, 0
Set CapturePicture = Clipboard.GetData
End Function


3、在窗体上加入一个Picturebox和两个button,一个caption为拍照,一个为保存

4、给窗体的Load加如下代码


Preview_Handle = capCreateCaptureWindow("Video", WS_CHILD + WS_VISIBLE, 2, 2, 220, 156, Me.hwnd, 1)
SendMessage Preview_Handle, WM_CAP_DRIVER_CONNECT, 0, 0
SendMessage Preview_Handle, WM_CAP_SET_PREVIEWRATE, 1, 0
SendMessage Preview_Handle, WM_CAP_SET_PREVIEW, 1, 0


5、双击拍照按钮

Picture1.Picture = CapturePicture(Preview_Handle)



6、双击保存按钮


SavePicture Picture1.Picture, "c:\a.bmp"



7、在窗体的unload事件中加入如下代码


SendMessage Preview_Handle, WM_CAP_DRIVER_DISCONNECT, 0, 0



8、运行程序,调试

全部程序清单:



Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _
Alias "capCreateCaptureWindowA" ( _
ByVal lpszWindowName As String, _
ByVal dwStyle As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal hWndParent As Long, _
ByVal nID As Long) As Long

Private Const WS_CHILD = &H40000000
Private Const WS_VISIBLE = &H10000000
Private Const WM_USER = &H400
Private Const WM_CAP_START = &H400
Private Const WM_CAP_EDIT_COPY = (WM_CAP_START + 30)
Private Const WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10)
Private Const WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52)
Private Const WM_CAP_SET_OVERLAY = (WM_CAP_START + 51)
Private Const WM_CAP_SET_PREVIEW = (WM_CAP_START + 50)
Private Const WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11)

Private Preview_Handle As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Private Function CapturePicture(nCaptureHandle As Long) As StdPicture
Clipboard.Clear
SendMessage nCaptureHandle, WM_CAP_EDIT_COPY, 0, 0
Set CapturePicture = Clipboard.GetData
End Function

Private Sub Command1_Click()
SavePicture Picture1.Picture, "c:\a.bmp"
End Sub

Private Sub Command2_Click()
Picture1.Picture = CapturePicture(Preview_Handle)
End Sub

Private Sub Form_Load()
Preview_Handle = capCreateCaptureWindow("Video", WS_CHILD + WS_VISIBLE, 2, 2, 220, 156, Me.hwnd, 1)
SendMessage Preview_Handle, WM_CAP_DRIVER_CONNECT, 0, 0
SendMessage Preview_Handle, WM_CAP_SET_PREVIEWRATE, 1, 0
SendMessage Preview_Handle, WM_CAP_SET_PREVIEW, 1, 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
SendMessage Preview_Handle, WM_CAP_DRIVER_DISCONNECT, 0, 0
End Sub





相关文章

vb中Msgbox函数的用法

VB中的Msgbox函数,可实现弹出窗口 作用:在对话框中显示消息,等待用户单击按钮,并返回一个 Integer 告诉用户单击哪一个按钮。 语法: MsgBox...

MD5加密算法VB源码

今天在在网页的源代码中找到了MD5加密算法的代码,呵呵,搬过来VB用了~~ 直接调用就可以了  Private Const BITS_TO_A_BYTE = 8 P...

[Matze][FinalBIG 免安装汉化版][0.4][2010.06.16]

原创作者:Matze  原版下载:www.wagnerma.de 汉化作者:星空 免责声明: 本软件只可供学习研究使用,使用本软件造成一切后果后使用者自行承担。汉化作者不提供任何...

PCB布线的地线干扰与抑制方法

1.地线的定义 什么是地线?大家在教科书上学的地线定义是:地线是作为电路电位基准点的等电位体。这个定义是不符合实际情况的。实际地线上的电位并不是恒定的。如果用仪表测量一下地线上各点之间的电位,会发现...

VB URL编码函数

VB UTF-8 URL编码函数: Public Function UTF8_URLEncoding(szInput)  ...

VB代码优化的六条军规

       在优化程序代码大小的诸多技术中,大多包括从代码中删除不必要的元素。在编译应用程序时,Visual Basic自动删除某些元素。而标识符名称、...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。