Web cam capture onto a form (1 Viewer)

DennisJones

Registered User.
Local time
Today, 05:05
Joined
Feb 27, 2007
Messages
36
I have written an MS Access system for entering competitors at a competition and then displaying storing scores . The requirement now is to photograph every entrant with a web cam and store their image with their scores. So basically I want to be able to capture an image from a webcam on a form so that I can see what they look like when they bring their score cards in. I am sure that this must have been done loads of times before so how do I do it?

Thanks
Dennis
 

dinorb

New member
Local time
Yesterday, 21:05
Joined
Aug 24, 2011
Messages
1
I think you have missunderstand the main Problem here. The thing is to capture photos from a webcamera, all done inside Access i.e. Form.

So how can I connect my webcamera to a Access Form and make Snapshots from that.

I have this code in VB.net that actually works perfectly fine. Can someone help me convert this code and make it work in MS Access. Please someone make a sample.

Here the code:

Code:
Public Class ScreenCapture

    ' The ScreenCapture class allows you to take screenshots (printscreens)
    ' of the desktop or of individual windows.
    '
    ' Usage:
    '
    ' PictureBox1.Image = ScreenCapture.GrabScreen()
    ' PictureBox1.Image = ScreenCapture.GrabActiveWindow()
    ' PictureBox1.Image = ScreenCapture.GrabWindow(SomeHwnd)
    '
    ' PictureBox1.Image = ScreenCapture.GrabScreen(X, Y, Width, Height)
    ' PictureBox1.Image = ScreenCapture.GrabScreen(Rect)
    ' PictureBox1.Image = ScreenCapture.GrabScreen(Location, Size)


#Region "Constants"

    Private Const HORZRES As Integer = 8
    Private Const VERTRES As Integer = 10
    Private Const SRCCOPY = &HCC0020
    Private Const SRCINVERT = &H660046

    Private Const USE_SCREEN_WIDTH = -1
    Private Const USE_SCREEN_HEIGHT = -1

#End Region

#Region "API's"

    Private Structure RECT
        Public Left As Int32
        Public Top As Int32
        Public Right As Int32
        Public Bottom As Int32
    End Structure

    Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
    Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
    Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
    Private Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Long) As Integer
    Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
    Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
    Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
    Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
    Private Declare Function BitBlt Lib "GDI32" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal SrcX As Integer, ByVal SrcY As Integer, ByVal Rop As Integer) As Integer
    Private Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Integer
    Private Declare Function IsWindow Lib "user32" Alias "IsWindow" (ByVal hwnd As Integer) As Long
    Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As Integer, ByRef lpRect As RECT) As Int32

#End Region

#Region "Public Methods."

    Public Shared Function GrabScreen() As Bitmap

        Return GrabScreen(0, 0, USE_SCREEN_WIDTH, USE_SCREEN_HEIGHT)

    End Function

    Public Shared Function GrabScreen(ByVal Rect As Rectangle) As Bitmap

        Return GrabScreen(Rect.X, Rect.Y, Rect.Width, Rect.Height)

    End Function

    Public Shared Function GrabScreen(ByVal Location As System.Drawing.Point, ByVal Size As System.Drawing.Size) As Bitmap

        Return GrabScreen(Location.X, Location.Y, Size.Width, Size.Height)

    End Function

    Public Shared Function GrabScreen(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer) As Bitmap

        Dim hDesktopDC As Integer
        Dim hOffscreenDC As Integer
        Dim hBitmap As Integer
        Dim hOldBmp As Integer
        Dim MyBitmap As Bitmap

        ' Get the desktop device context.
        hDesktopDC = CreateDC("DISPLAY", "", "", "")
        If hDesktopDC Then
            ' Adjust width and height.
            If Width = USE_SCREEN_WIDTH Then
                Width = GetDeviceCaps(hDesktopDC, HORZRES)
            End If
            If Height = USE_SCREEN_HEIGHT Then
                Height = GetDeviceCaps(hDesktopDC, VERTRES)
            End If
            ' Create an offscreen device context.
            hOffscreenDC = CreateCompatibleDC(hDesktopDC)
            If hOffscreenDC Then
                ' Create a bitmap for our offscreen device context.
                hBitmap = CreateCompatibleBitmap(hDesktopDC, Width, Height)
                If hBitmap Then
                    ' Copy the image and create an instance of the Bitmap class.
                    hOldBmp = SelectObject(hOffscreenDC, hBitmap)
                    BitBlt(hOffscreenDC, 0, 0, Width, Height, hDesktopDC, X, Y, SRCCOPY)
                    MyBitmap = Bitmap.FromHbitmap(New IntPtr(hBitmap))
                    ' Clean up.
                    DeleteObject(SelectObject(hOffscreenDC, hOldBmp))
                End If
                DeleteDC(hOffscreenDC)
            End If
            DeleteDC(hDesktopDC)
        End If
        ' Return our Bitmap instance.
        Return MyBitmap

    End Function

    Public Shared Function GrabActiveWindow() As Bitmap

        Return GrabWindow(GetForegroundWindow())

    End Function

    Public Shared Function GrabWindow(ByVal hWnd As Int32) As Bitmap

        Dim hWindowDC As Long
        Dim hOffscreenDC As Long
        Dim rec As RECT
        Dim nWidth As Long
        Dim nHeight As Long
        Dim hBitmap As Long
        Dim hOldBmp As Long
        Dim MyBitmap As Bitmap

        ' Verify if a valid window handle was provided.
        If hWnd <> 0 And IsWindow(hWnd) Then
            ' Get the window's device context.
            hWindowDC = GetWindowDC(hWnd)
            If hWindowDC Then
                ' Get width and height.
                If GetWindowRect(hWnd, rec) Then
                    nWidth = rec.Right - rec.Left
                    nHeight = rec.Bottom - rec.Top
                    ' Create an offscreen device context.
                    hOffscreenDC = CreateCompatibleDC(hWindowDC)
                    If hOffscreenDC Then
                        ' Create a bitmap for our offscreen device context.
                        hBitmap = CreateCompatibleBitmap(hWindowDC, nWidth, nHeight)
                        If hBitmap Then
                            ' Copy the image and create an instance of the Bitmap class.
                            hOldBmp = SelectObject(hOffscreenDC, hBitmap)
                            BitBlt(hOffscreenDC, 0, 0, nWidth, nHeight, hWindowDC, 0, 0, SRCCOPY)
                            MyBitmap = Bitmap.FromHbitmap(New IntPtr(hBitmap))
                            ' Clean up.
                            DeleteObject(SelectObject(hOffscreenDC, hOldBmp))
                        End If
                        DeleteDC(hOffscreenDC)
                    End If
                End If
                ReleaseDC(hWnd, hWindowDC)
            End If
        End If
        ' Return our Bitmap instance.
        Return MyBitmap

    End Function

#End Region

End Class
 

DennisJones

Registered User.
Local time
Today, 05:05
Joined
Feb 27, 2007
Messages
36
I eventually solved the problem using a program called "dorgem" written by a guy from Holland. It's been a couple of years sine I set it up and it's not used any more but I was able to capture images of people and associate them with their data record in Access. Basically look up dorgem on google, that's your solution.

Dennis
 

DennisJones

Registered User.
Local time
Today, 05:05
Joined
Feb 27, 2007
Messages
36
OK basically what you do is put a button on your form which runs a bit of code like this
Private Sub Command323_Click()
On Error GoTo Err_Command323_Click

Shell ("C:\Program Files\dorgem-nightly[1]\Dorgem.exe /m /capnow")
[FilePath] = "c:\capture\abc" & Format(Now(), "hhnnss") & ".jpg"
Refresh

Exit_Command323_Click:
Exit Sub

Err_Command323_Click:
MsgBox Err.Description
Resume Exit_Command323_Click

End Sub

This stores the image in a folder called "capture" with the name "abc" followed by the date and time so that you now have a picture with a unique name. This filename is stored as a field in the user's record. Incidentally you should have a default image of white space or something when the record is created so that you don't get an error message before you take the picture.
You then want to display the image on the form (to make sure it's a good one)

Just looking at the above I now recall that I had to get a special version of Dorgem from the writer but that has probably been superseeded by now.

The problem for me is that I haven't used the system for a few years (we decided there would be data protection issues and some users objected) - but that's another story.

I managed to get a networked version of it working too with two pcs with camers recording onto the same database, the only clash would be if two images were taken at exactly the same time so they would have the same filename (you can make this more difficult by setting the clock on each PC to be different)

See how you get on with this, I will see if I can dig out a sample database if you like but it won't have many comments in it.
 

DennisJones

Registered User.
Local time
Today, 05:05
Joined
Feb 27, 2007
Messages
36
Do you have an email address for me to email a database to you? It's a stripped down version of a programme for competition entries. It doesn't work and only has a few records but it should show you the bits that you are interested in. I seem to recall that it was necessary to take a photo first to get the camera warmed up(!) it takes a couple of seconds but first time round you don't get a picture (at least I didn't). After that it's OK but the system I was using only shows stills ie the Access database is showing you what is in it the database rather than a moving picture that you do a freeze frame on. However, doing what you want shouldn't be that much more trouble, you could even have an extra window with the dorgem real time image going as well.

Incidentally where are you? I am in the UK on the south coast. Cheers. Dennis
 

Stemdriller

Registered User.
Local time
Today, 05:05
Joined
May 29, 2008
Messages
187
Hi Guys

This is exactly what I am looking for.

Did you ever get it sorted, Whenever a contractor is entered I need to take a photo and save the photo in a folder with the Contractors Unique ID.

I can take the photo but cannot automate the save location.

Can you help?

Regards

Gareth
 

DennisJones

Registered User.
Local time
Today, 05:05
Joined
Feb 27, 2007
Messages
36
It's easy, you save the captured image from Dorgem with a unique name into a local folder, my code below shows how to create a filename prefixed with "abc" plus the date and time (to make it unique). Then you store this filename in your Access record and show it on the form.
 

Stemdriller

Registered User.
Local time
Today, 05:05
Joined
May 29, 2008
Messages
187
Hi Dennis

Thanks for replying

I have taken your code and tried to modify as follows

Private Sub Command4_Click()
On Error GoTo Err_Command4_Click
Shell ("C:\Program Files\Dorgem\Dorgem.exe /m/")

[FilePath] = "E:\Database Folder\Work Permit\2011 Edition\Back 1End\WorkPermitPhotos\" & Forms!Form2.ID & ".jpg"
'[FilePath] = "E:\Database Folder\Work Permit\2011 Edition\Back End\WorkPermitPhotos\" & Format(Now(), "hhnnss") & ".jpg"
Refresh

I am trying to get it to save with the ID number of the Form. However, your original code does not save with the Date and Time either.

Gareth
 

DennisJones

Registered User.
Local time
Today, 05:05
Joined
Feb 27, 2007
Messages
36
give me an email address and I will send you a cut down copy of what I was working on.
 

Semipro

New member
Local time
Yesterday, 21:05
Joined
May 3, 2014
Messages
1
Stemdriller, I am struggling with exactly the same problem. I was wondering if you have sorted out the problem. I suppose that there's many users struggling, could you maybe post some results?
 

Users who are viewing this thread

Top Bottom