By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,327 Members | 2,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,327 IT Pros & Developers. It's quick & easy.

Webcam automatic capture after failed login three times

P: 3
Im doing an application which is when user plugged in usb drive, login for will come out asking user to login first but if user failed login for three times, webcam will automatically capture. The problem here, how can I make the webcam to automatically capture after failed login for three times?

Coding for login
Expand|Select|Wrap|Line Numbers
  1. Public Class Login
  2.  
  3.     Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginButton.Click
  4.         'Check if username or password is empty
  5.         If PasswordTextBox.Text = "" Or UsernameTextBox.Text = "" Then
  6.             MessageBox.Show("Please fill-up all fields!", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  7.             'Clear all fields
  8.             PasswordTextBox.Text = ""
  9.             UsernameTextBox.Text = ""
  10.  
  11.             'Focus on Username field
  12.             UsernameTextBox.Focus()
  13.  
  14.         Else
  15.             ' Connect to DB
  16.             Dim conn As New System.Data.OleDb.OleDbConnection()
  17.             conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\PutLock.mdb;Persist Security Info=True;Jet OLEDB:Database Password=password"
  18.  
  19.             Try
  20.                 'Open Database Connection
  21.                 conn.Open()
  22.  
  23.                 Dim sql As String = "SELECT * FROM PutLockSignUp WHERE Username='" & UsernameTextBox.Text & "' AND Password = '" & PasswordTextBox.Text & "'"
  24.                 Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
  25.  
  26.                 Dim sqlRead As OleDbDataReader = cmd.ExecuteReader()
  27.  
  28.                 If sqlRead.Read() Then
  29.                     UsernameTextBox.Clear()
  30.                     PasswordTextBox.Clear()
  31.  
  32.                     MainPage.Show()
  33.                     Me.Hide()
  34.  
  35.                 Else
  36.                     ' If user enter wrong username or password
  37.                     MessageBox.Show("Sorry, username or password not found!", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Error)
  38.  
  39.                     'Clear all fields
  40.                     PasswordTextBox.Text = ""
  41.                     UsernameTextBox.Text = ""
  42.  
  43.                     'Focus on Username field
  44.                     UsernameTextBox.Focus()
  45.                 End If
  46.  
  47.             Catch ex As Exception
  48.                 MessageBox.Show("Failed to connect to Database", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  49.             End Try
  50.  
  51.         End If
  52.     End Sub
  53.  
  54.  
  55.     Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
  56.         End
  57.     End Sub
  58. End Class
  59.  
Here is the coding for webcam
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Const WM_CAP As Short = &H400S
  3.     Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
  4.     Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
  5.     Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
  6.     Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
  7.     Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
  8.     Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
  9.     Const WS_CHILD As Integer = &H40000000
  10.     Const WS_VISIBLE As Integer = &H10000000
  11.     Const SWP_NOMOVE As Short = &H2S
  12.     Const SWP_NOSIZE As Short = 1
  13.     Const SWP_NOZORDER As Short = &H4S
  14.     Const HWND_BOTTOM As Short = 1
  15.     Dim iDevice As Integer = 0
  16.     Dim hHwnd As Integer
  17.     Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef RECT As IntPtr) As IntPtr
  18.     Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
  19.     Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
  20.     Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Short, ByVal hWndParent As Integer, ByVal nID As Integer) As Integer
  21.     Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, ByVal cbVer As Integer) As Boolean
  22.  
  23.     Private Sub LoadDeviceList()
  24.         Dim strName As String = Space(100)
  25.         Dim strVer As String = Space(100)
  26.         Dim bReturn As Boolean
  27.         Dim x As Integer = 0
  28.         Do
  29.             bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
  30.             If bReturn Then lstDevices.Items.Add(strName.Trim)
  31.             x += 1
  32.         Loop Until bReturn = False
  33.     End Sub
  34.  
  35.     Private Sub OpenPreviewWindow()
  36.         Dim iHeight As Integer = picCapture.Height
  37.         Dim iWidth As Integer = picCapture.Width
  38.         hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.Handle.ToInt32, 0)
  39.         If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
  40.             SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
  41.             SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
  42.             SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
  43.             SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, SWP_NOMOVE Or SWP_NOZORDER)
  44.             btnSave.Enabled = True
  45.             btnStop.Enabled = True
  46.             btnStart.Enabled = False
  47.         Else
  48.             DestroyWindow(hHwnd)
  49.             btnSave.Enabled = False
  50.         End If
  51.     End Sub
  52.  
  53.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  54.         Dim data As IDataObject
  55.         Dim bmap As Image
  56.         SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
  57.         data = Clipboard.GetDataObject()
  58.         If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
  59.             bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
  60.             picCapture.Image = bmap
  61.             ClosePreviewWindow()
  62.             btnSave.Enabled = False
  63.             btnStop.Enabled = False
  64.             btnStart.Enabled = True
  65.             If sfdImage.ShowDialog = DialogResult.OK Then
  66.                 bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
  67.             End If
  68.         End If
  69.     End Sub
  70.  
  71.     Private Sub ClosePreviewWindow()
  72.         SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
  73.         DestroyWindow(hHwnd)
  74.     End Sub
  75.  
  76.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  77.         LoadDeviceList()
  78.     End Sub
  79.  
  80.     Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
  81.         OpenPreviewWindow()
  82.         btnStart.Enabled = False
  83.         btnStop.Enabled = True
  84.     End Sub
  85.  
  86.     Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
  87.         ClosePreviewWindow()
  88.         btnStart.Enabled = True
  89.         btnStop.Enabled = False
  90.  
  91.     End Sub
  92. End Class
Sep 25 '16 #1
Share this Question
Share on Google+
5 Replies


Oralloy
Expert 100+
P: 983
dyooshi,

Will you please reformat your code using "CODE" blocks?

Just click the CODE button on the icon bar, and the tags will be automatically inserted for you.

That said, have you run any tests?

What are the test results you've had?

--Oralloy
Sep 25 '16 #2

P: 3
I have tried using timer but I dont know how to use. Both of these coding work fine without error.
Sep 26 '16 #3

Oralloy
Expert 100+
P: 983
dyooshi,

Thanks for inserting the code blocks, that really helps with reading and understanding your work.

I'm where I don't have uSloth Office running on this computer, so I'll look around....

Ok, first we have Timer function and Timer event references:
  1. Timer Function
  2. Form.Timer Event

My Suggestion is that you add an attribute to your Login class which tracks the login failures, and initialise it in the New() method:
Expand|Select|Wrap|Line Numbers
  1. Private failCount As Integer
  2.  
  3. Public Sub New()
  4.     failCount = 0
  5. End Sub
  6.  
Then, when there is a login failure, increment it and test for too many tries in LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs):
Expand|Select|Wrap|Line Numbers
  1. failCount = 1 + failCount
  2. if (2 < failCount) then
  3.     '' take picture here
  4.     '' eject the USB?
  5.     '' force form closure here
  6. end if
  7.  
Are we thinking in similar ways?
Oct 2 '16 #4

P: 3
this is my updated coding that I did before you reply. it is kinda similar with what you gave but then I dont know how to make the webcam to take picture automatically...
Expand|Select|Wrap|Line Numbers
  1. Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginButton.Click
  2.         Dim LoginAttempts As Integer = 0
  3.         'Check if username or password is empty
  4.         If PasswordTextBox.Text = "" Or UsernameTextBox.Text = "" Then
  5.             MessageBox.Show("Please fill-up all fields!", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  6.             'Clear all fields
  7.             PasswordTextBox.Text = ""
  8.             UsernameTextBox.Text = ""
  9.  
  10.             'Focus on Username field
  11.             UsernameTextBox.Focus()
  12.  
  13.         Else
  14.             ' Connect to DB
  15.             Dim conn As New System.Data.OleDb.OleDbConnection()
  16.             conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\PutLock.mdb"
  17.  
  18.             Try
  19.                 'Open Database Connection
  20.                 conn.Open()
  21.  
  22.                 Dim sql As String = "SELECT * FROM PutLockSignUp WHERE Username='" & UsernameTextBox.Text & "' AND Password = '" & PasswordTextBox.Text & "'"
  23.                 Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
  24.  
  25.                 Dim sqlRead As OleDbDataReader = cmd.ExecuteReader()
  26.  
  27.                 If sqlRead.Read() Then
  28.                     MainPage.Show()
  29.                     Me.Hide()
  30.  
  31.                 Else
  32.                     LoginAttempts = LoginAttempts + 1
  33.                     If LoginAttempts >= 3 Then
  34.                         'take picture
  35.  
  36.                     Else
  37.                         ' If user enter wrong username or password
  38.                         MessageBox.Show("Sorry, username and password did not match!", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Error)
  39.  
  40.                         'Clear all fields
  41.                         PasswordTextBox.Text = ""
  42.                         UsernameTextBox.Text = ""
  43.  
  44.                         'Focus on Username field
  45.                         UsernameTextBox.Focus()
  46.                     End If
  47.                 End If
  48.  
  49.             Catch ex As Exception
  50.                 MessageBox.Show("Failed to connect to Database", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  51.             End Try
  52.         End If
  53.     End Sub
Oct 2 '16 #5

Oralloy
Expert 100+
P: 983
Well, to take the picture, I would try creating a function similar to that in the message handler in the Form1 class.

Note that you may have to adjust active libraries, so that the correct classes are used here.

Suggestion:
Expand|Select|Wrap|Line Numbers
  1. Private Sub TestCameraSave()
  2.     ''--local variables
  3.     Dim data As IDataObject
  4.     Dim bmap As Image
  5.  
  6.     ''--initiate the capture
  7.     SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
  8.  
  9.     ''--capture the image from the clip-board
  10.     data = Clipboard.GetDataObject()
  11.  
  12.     ''--process what we got
  13.     If (Not data.GetDataPresent(GetType(System.Drawing.Bitmap))) Then
  14.         '' failed, whinge
  15.         MessageBox "We have a problem here, please insert diagnostics..."
  16.  
  17.     Else
  18.         '' pull bit-map from clip-board
  19.         bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
  20.  
  21.         '' save the thing
  22.         bmap.Save("C:/my=image.bmp", Imaging.ImageFormat.Bmp)
  23.  
  24.     End If
  25. End Sub
  26.  
Good Luck!
Oralloy
Oct 2 '16 #6

Post your reply

Sign in to post your reply or Sign up for a free account.