如何在Microsoft Access表单上使用网络摄像头捕获? [英] How to use webcam capture on a Microsoft Access form?

查看:160
本文介绍了如何在Microsoft Access表单上使用网络摄像头捕获?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Microsoft Access 2013中设计一个数据库,用于存储工厂中发现的有故障零件的记录.

I'm designing a database in Microsoft Access 2013 to store records of faulty parts found in the plant.

我正在尝试在表单上实现一个按钮,用户可以单击该按钮以访问其设备的相机,以在表单中附加故障图片.用户正在Dell Latitude 5290上二合一使用Windows 10.

I'm trying to implement a button on my form the user can click to access their device's camera, to attach a picture of the fault in the form. The user is using Windows 10 on a Dell latitude 5290 two in one.

我尝试了在网上找到的代码,但是它非常老. https://www.developerfusion .com/thread/46191/how-to-capture-picture-using-webcam-in-vb60/

I tried code I found online but it is extremely old. https://www.developerfusion.com/thread/46191/how-to-capture-picture-using-webcam-in-vb60/

推荐答案

我看到您自己调整代码遇到了麻烦,所以让我逐步介绍一下针对VBA进行调整的过程.

I see you've had trouble adjusting the code yourself, so let me walk you through the process of adjusting it for VBA.

首先,我们将创建一个包含网络摄像头代码的表单,并向其中添加所需的控件.控件为:

First, we're going to create a form that holds the webcam code, and add the required controls to it. The controls are:

4个按钮(分别称为cmd1,cmd2,cmd3和cmd4)和1个子窗体控件(称为PicWebCam).我们正在使用一个子窗体来替换PictureBox对象,因为该对象在Access中不可用.

4 buttons, called cmd1, cmd2, cmd3, and cmd4, and 1 subform control, called PicWebCam. We're using a subform to replace the PictureBox object, since that's not available in Access.

由于子窗体需要显示某些内容,因此我们在设计视图中创建了第二个窗体,并将记录选择器和导航按钮设置为否".我们不向该窗体添加任何控件,并且使其足够小以使其没有滚动酒吧.然后,将子窗体控件的源对象设置为刚创建的窗体.

Since the subform needs to display something, we create a second form in design view, and set record selectors and navigation buttons to No. We add no controls to the form, and make it small enough so it doesn't have scroll bars. Then, we set our subform control's source object to the form we just created.

然后,代码还使用CommonDialog控件让我们选择一个文件路径来保存图片.尽管Windows + Access的某些组合提供了该功能,但我们不能依靠它,因此我们将改用FileDialog.

Then, the code also uses a CommonDialog control to let us choose a file path to save the picture. While that's available with some combinations of Windows + Access, we can't rely on that, so we'll use a FileDialog instead.

要获取文件路径,我们将以下代码添加到表单模块中:

To get a file path, we add the following code to our form module:

Function GetSavePath() As String
    Dim f As Object 'FileDialog
    Set f = Application.FileDialog(2) 'msoFileDialogSaveAs
    If f.Show <> 0 Then GetSavePath = f.SelectedItems(1)
End Function

然后,我们复制粘贴初始声明(类型和声明函数语句),并进行2次调整:

Then, we copy-paste the initial declarations (types and declare function statements), and make 2 adjustments:

  1. 由于我们要将它们放置在表单模块中,因此对于默认情况下的所有私有内容,都需要将Public删除,对于非私有内容,则将其更改为Private.

  1. Since we're going to place them in the form module, Public needs to be removed for everything that's private by default, and changed to Private for the stuff that isn't.

由于我们希望与64位访问兼容(您说过并不需要,但无论如何都要添加它),因此我们想在所有外部函数中添加PtrSafe关键字,并进行更改从LongLongPtr的所有指针的类型.该代码位于我们刚刚创建的函数之前.

Since we want to be compatible with 64-bit Access (you said you didn't need to be, but adding it anyway), we want to add the PtrSafe keyword to all external functions, and change the type for all pointers from Long to LongPtr. This code comes before the function we just created.

Const WS_CHILD As Long = &H40000000
Const WS_VISIBLE As Long = &H10000000

Const WM_USER As Long = &H400
Const WM_CAP_START As Long = WM_USER

Const WM_CAP_DRIVER_CONNECT As Long = WM_CAP_START + 10
Const WM_CAP_DRIVER_DISCONNECT As Long = WM_CAP_START + 11
Const WM_CAP_SET_PREVIEW As Long = WM_CAP_START + 50
Const WM_CAP_SET_PREVIEWRATE As Long = WM_CAP_START + 52
Const WM_CAP_DLG_VIDEOFORMAT As Long = WM_CAP_START + 41
Const WM_CAP_FILE_SAVEDIB As Long = WM_CAP_START + 25

Private Declare PtrSafe 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 LongPtr _
        , ByVal nID As Long) As Long

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

Dim hCap As LongPtr

现在,我们可以复制粘贴实际功能,并进行2个更改:

Now, we can copy paste the actual functions, and make 2 changes:

  1. 我们使用GetSavePath函数来代替用户常用的对话框控制代码.
  2. 我们使用PicWebCam.Form.hWnd而不是PicWebCam.hWnd来获取要用网络摄像头feed填充的帧的hWnd.
  1. Instead of the common dialog control code, we use the GetSavePath function to get the path the user wants to save the file at.
  2. Instead of PicWebCam.hWnd, we use PicWebCam.Form.hWnd to get the hWnd for the frame we want to fill with the webcam feed.

Private Sub cmd4_Click()
Dim sFileName As String
    Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(False), 0&)
    sFileName = GetSavePath
    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.Form.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

最后,由于我们为Form_Load事件添加了事件处理程序,因此我们需要确保表单的On Load属性设置为[Event Procedure].添加的所有命令按钮的On Click属性也是如此.

Finally, since we added event handlers for the Form_Load event, we need to make sure the On Load property of the form is set to [Event Procedure]. The same goes for the On Click property of all command buttons we've added.

就这样,我们已经成功地将网络摄像头代码从VB6迁移到了VBA,并重新创建了您提供的链接中稀疏描述的表单.大部分代码都归功于该链接上的作者.

And, that's it, we've succesfully migrated the webcam code from VB6 to VBA, and recreated the form that was sparsely described in the link you provided. Credits to most of the code go to the author on that link.

您可以此处一个>.请注意,出于教育目的,我建议您不要这样做,因为您不应该信任Internet上的随机陌生人,因为他们会给您未签名的可执行文件.但是,如果遇到错误,此功能很有用,因此可以检查它是否是网络摄像头兼容性问题或错误.

You can temporarily download the result here. Note that I recommend you don't, both for educational purposes, and because you shouldn't trust random strangers on the internet giving you unsigned executables. But it's useful if you encounter an error, so you can check if it might be a webcam compatibility issue, or a mistake.

请注意,我尚未对原始代码进行任何实际的功能更改.

Note that I haven't made any real functional changes to the original code.

这篇关于如何在Microsoft Access表单上使用网络摄像头捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆