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

小歆12年前软件源码02843

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





相关文章

[安卓]dSploit汉化修正版-秒杀WIFI杀手系列软件!

  话说原来当初就在用他 , 后来换了手机等原因就没鼓捣过了,功能那是没得说,WIFI网络之主宰者,渗透网络之无双神器,无所不渗,防线穿透,可惜是英文吧的那个时候版本还很低本人还没...

STM32软件 RealView MDK-ARM 3.80A下载(RVMDK3.80A)

RealView MDK-ARM 3.80A RVMDK3.80A简介:       RVMDK源自德国的 KEIL 公司,是 Real...

小歆工作室管理工作说明以及一些公告

1。论坛版主的申请,前提是你找对你喜欢的版块,喜欢的内容。并且有信心将这个版块发展起来。如果仅仅是为了个版主头衔。建议不要进行申请,最多一个星期就会被...

VB UTC+8校正 源码

Function HttpGet(url) With CreateObject("Msxml2.ServerXMLHTTP") .open "GET", url,...

u808_01.png

中兴U808 官方深度精简版 全局透明美化稳定流畅 可长期使用

中兴U808是我个人非常喜欢的一款机型。本ROM是基于官方4.0.3制作,在保证官方原汁原味的基础上,添加了三星风格,删除了大部分没有实际用途的软件,释放更多系统内存从而达到让系统分配更多内存供主固件...

STM32软件 RealView MDK-ARM 4.12下载(RVMDK4.12)

STM32软件 RealView MDK-ARM 4.12下载(RVMDK4.12)

RealView MDK-ARM 4.12 RVMDK4.12简介:        RealView MDK出众的价格优势和功能优势,势将成为...

发表评论    

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