从相机捕获图像并保存
Posted
技术标签:
【中文标题】从相机捕获图像并保存【英文标题】:Capture image from camera and save it 【发布时间】:2017-05-12 20:47:10 【问题描述】:我想在我的表单(MS Access 数据库)中添加一个按钮,以便它可以从我的相机(笔记本电脑)捕获图像并将其保存在特定位置(c:\image)。
我正在使用带有 Office 2010 或 Office 365 的 Windows 10。
带有 WIA 的代码:
Private Sub Command1_Click()
Dim oWIA_DeviceManager As WIA.DeviceManager
Dim oWIA_Device As WIA.Device
Dim oWIA_ComDlg As WIA.CommonDialog
Dim oImageFile As WIA.ImageFile
Dim i As Long
Set oWIA_DeviceManager = New WIA.DeviceManager
If oWIA_DeviceManager.DeviceInfos.Count > 0 Then
Set oWIA_ComDlg = New WIA.CommonDialog
' Index the Devices property starting here at 1, not 0 .
For i = 1 To oWIA_DeviceManager.DeviceInfos.Count
Set oWIA_Device = oWIA_DeviceManager.DeviceInfos.Item(i).Connect
' Use this to show Acquisition CommonDialog
Set oImageFile = oWIA_ComDlg.ShowAcquireImage
' Use this to show Acquisition Wizard
'Set oImageFile = oWIA_ComDlg.ShowAcquisitionWizard(oWIA_Device)
Next i
Else
MsgBox "No WIA compatible device attached!"
End If
End Sub
用这个我打开我的 iPhone 相机(USB 连接)。我需要使用笔记本电脑的内置摄像头。
【问题讨论】:
【参考方案1】:这个页面可能是你需要的。 http://www.developerfusion.com/thread/46191/how-to-capture-picture-using-webcam-in-vb60/
'******************* module code **************
Public Const WS_CHILD As Long = &H40000000
Public Const WS_VISIBLE As Long = &H10000000
Public Const WM_USER As Long = &H400
Public Const WM_CAP_START As Long = WM_USER
Public Const WM_CAP_DRIVER_CONNECT As Long = WM_CAP_START + 10
Public Const WM_CAP_DRIVER_DISCONNECT As Long = WM_CAP_START + 11
Public Const WM_CAP_SET_PREVIEW As Long = WM_CAP_START + 50
Public Const WM_CAP_SET_PREVIEWRATE As Long = WM_CAP_START + 52
Public Const WM_CAP_DLG_VIDEOFORMAT As Long = WM_CAP_START + 41
Public Const WM_CAP_FILE_SAVEDIB As Long = WM_CAP_START + 25
Public 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
Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long _
, ByVal wParam As Long, ByRef lParam As Any) As Long
'************* end of module code ******************
Add the following controls in a form
1. A picture box with name "PicWebCam"
2. A commondialog control with name "CDialog"
3. Add 4 command buttons with name "cmd1","cmd2,"cmd3","cmd4"
then paste the following code
'************************** Code **************
Dim hCap As Long
Private Sub cmd4_Click()
Dim sFileName As String
Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(False), 0&)
With CDialog
.CancelError = True
.Flags = cdlOFNPathMustExist Or cdlOFNOverwritePrompt
.Filter = "Bitmap Picture(*.bmp)|*.bmp|JPEG Picture(*.jpg)|*.jpg|All Files|*.*"
.ShowSave
sFileName = .FileName
End With
Call SendMessage(hCap, WM_CAP_FILE_SAVEDIB, 0&, ByVal CStr(sFileName))
DoFinally:
Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(True), 0&)
End Sub
Private Sub Cmd3_Click()
Dim temp As Long
temp = SendMessage(hCap, WM_CAP_DRIVER_DISCONNECT, 0&, 0&)
End Sub
Private Sub Cmd1_Click()
hCap = capCreateCaptureWindow("Take a Camera Shot", WS_CHILD Or WS_VISIBLE, 0, 0, PicWebCam.Width, PicWebCam.Height, PicWebCam.hWnd, 0)
If hCap <> 0 Then
Call SendMessage(hCap, WM_CAP_DRIVER_CONNECT, 0, 0)
Call SendMessage(hCap, WM_CAP_SET_PREVIEWRATE, 66, 0&)
Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(True), 0&)
End If
End Sub
Private Sub Cmd2_Click()
Dim temp As Long
temp = SendMessage(hCap, WM_CAP_DLG_VIDEOFORMAT, 0&, 0&)
End Sub
Private Sub Form_Load()
cmd1.Caption = "Start &Cam"
cmd2.Caption = "&Format Cam"
cmd3.Caption = "&Close Cam"
cmd4.Caption = "&Save Image"
End Sub
'**************** Code end ************************
基本上,这是使用 Windows 消息泵向网络摄像头驱动程序发送消息,要求它拍照。 另外,给未来的自助小费。搜索VB6通常可以得到更好的结果,这和VBA几乎一模一样。 VBA 只是少了一些函数。
如果您缺少通用对话框控件。你可以把代码改成这样
Private Sub cmd4_Click()
Dim sFileName As String
Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(False), 0&)
sFileName="C:\PathToNewImageFile.bmp"
Call SendMessage(hCap, WM_CAP_FILE_SAVEDIB, 0&, ByVal CStr(sFileName))
DoFinally:
Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(True), 0&)
End Sub
【讨论】:
感谢您的回复。我遇到了“2.一个名为“CDialog”的通用对话框控件”的问题。在 access 2007 哪里可以找到它? 点击您的工具菜单。然后是附加控件。找到“Microsoft Common Dialog”控件。并检查一下。然后单击确定,一个新项目将出现在您的工具箱中。将其绘制到您的用户窗体上以添加它,然后在底部单击它以显示属性并为其命名。但是,我尝试在我的 excel 2013 上执行此操作,但无法做到,所以是的……祈祷,否则我们必须使用 Windows API 来执行此操作。看起来它只是用来给你一个保存位置。如果需要,您可以将其硬编码为测试。答案已更新 我必须对***.com/questions/56757965/…中描述的Access db进行调整【参考方案2】:过去我曾使用 WIA(Microsoft Windows Image Acquisition)扫描仪,但它适用于网络摄像头。我一定会尝试的。
【讨论】:
您认为它适用于我的笔记本电脑摄像头吗?你有任何代码要测试吗?提前谢谢。以上是关于从相机捕获图像并保存的主要内容,如果未能解决你的问题,请参考以下文章