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

小歆12年前软件源码03110

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操作INI文件方案

VB读写ini文件(1) 自从注册表诞生以来ini文件正在逐渐失去其市场占有率,然而基于ini文件的独立性,致使其还没有到达退出历史舞台的地步,很多应用程序的初始化和一些界面参数的设置仍然很愿意...

破解内存01.jpg

破解 Win7 32位系统的4G内存限制

ReadyFor4GB.rar 先解压附件压缩包,如图所示。   然后管理员身份运行ReadyFor4GB   依次点击“检查”,“应用...

最全电子元器件介绍(图文教程)

本文档对常用元器件概念、分类、主要参数、功能等进行了详细阐述。 很详细,实物图展示。楼主出血大放送! 为了大家让能看懂BOM中电子料的规格描述,了解常用电子元器件的类别...

MX3205简介.png

移动电源三合一5V(1A-2A)方案

移动电源三合一5V(1A-2A)方案 & N5 F7 R0 R0 | 下载地址 & B9 n/ V$ }- w   &...

用VB快速读取TextBox第N行的数据

TextBox 是以 vbCr+vbLf 为分行符号, 如果我们要逐一读取 TextBox 每一行, 无非是寻找 vbCr+vbLf 的所在位置, 然后取出每一行的字串, 不过这个方法不快,而且...

PCB布线要点准则

一、电路板设计步骤 一般而言,设计电路板最基本的过程可以分为三大步骤。 (1). 电路原理图的设计: 电路原理图的设计主要是PROTEL099的原理图设计系统(Advanced Schemat...

发表评论    

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