jhylands

Intruder alert

Jun 3rd, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.67 KB | None | 0 0
  1. Public Class Form1
  2.  
  3.     Const WM_CAP As Short = &H400S
  4.     Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
  5.     Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
  6.     Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
  7.     Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
  8.     Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
  9.     Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
  10.     Const WS_CHILD As Integer = &H40000000
  11.     Const WS_VISIBLE As Integer = &H10000000
  12.     Const SWP_NOMOVE As Short = &H2S
  13.     Const SWP_NOSIZE As Short = 1
  14.     Const SWP_NOZORDER As Short = &H4S
  15.     Const HWND_BOTTOM As Short = 1
  16.     Dim iDevice As Integer = 0
  17.     Dim hHwnd As Integer
  18.     Dim last As Bitmap
  19.     Dim starting As Boolean = True
  20.     Dim thread As System.Threading.Thread
  21.     Dim percent As Double
  22.  
  23.     Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Integer
  24.     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
  25.     Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
  26.     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
  27.     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
  28.  
  29.  
  30.  
  31.     Private Sub LoadDeviceList()
  32.         Dim strName As String = Space(100)
  33.         Dim strVer As String = Space(100)
  34.         Dim bReturn As Boolean
  35.         Dim x As Integer = 0
  36.         Do
  37.             bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
  38.             If bReturn Then lstDevices.Items.Add(strName.Trim)
  39.             x += 1
  40.         Loop Until bReturn = False
  41.     End Sub
  42.     Private Sub OpenPreviewWindow()
  43.         Dim iHeight As Integer = picCapture.Height
  44.         Dim iWidth As Integer = picCapture.Width
  45.         hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.Handle.ToInt32, 0)
  46.         If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
  47.             SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
  48.             SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
  49.             SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
  50.             SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, SWP_NOMOVE Or SWP_NOZORDER)
  51.             btnSave.Enabled = True
  52.             btnstop.Enabled = True
  53.             btnstart.Enabled = False
  54.         Else
  55.             DestroyWindow(hHwnd)
  56.             btnSave.Enabled = False
  57.         End If
  58.     End Sub
  59.  
  60.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  61.         Dim data As IDataObject
  62.         Dim bmap As Image
  63.         SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
  64.         data = Clipboard.GetDataObject()
  65.         If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
  66.             bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
  67.             picCapture.Image = bmap
  68.             ClosePreviewWindow()
  69.             btnSave.Enabled = False
  70.             btnstop.Enabled = False
  71.             btnstart.Enabled = True
  72.         End If
  73.     End Sub
  74.  
  75.     Private Sub ClosePreviewWindow()
  76.         SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
  77.         DestroyWindow(hHwnd)
  78.     End Sub
  79.  
  80.  
  81.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  82.         LoadDeviceList()
  83.         Me.CheckForIllegalCrossThreadCalls = False
  84.     End Sub
  85.  
  86.  
  87.  
  88.     Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
  89.  
  90.         OpenPreviewWindow()
  91.  
  92.         btnStart.Enabled = False
  93.  
  94.         btnStop.Enabled = True
  95.  
  96.     End Sub
  97.  
  98.  
  99.  
  100.     Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
  101.  
  102.         ClosePreviewWindow()
  103.  
  104.         btnStart.Enabled = True
  105.  
  106.         btnStop.Enabled = False
  107.  
  108.     End Sub
  109.  
  110.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  111.         'checking the image
  112.         'define data to collect the image from the webcam
  113.         Dim data As IDataObject
  114.         'define bmap to contain the bitmap that comes from
  115.         'the webcam
  116.         Dim bmap As Bitmap
  117.         'The first time the timer runs we need to set everything up
  118.         'or we will get errors
  119.         If starting Then ' if this is the first time we have run the code
  120.             OpenPreviewWindow() ' start the preveiw window
  121.  
  122.             'get the first image
  123.             SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
  124.             data = Clipboard.GetDataObject()
  125.             'if the image is there then save it to last
  126.             'which is a global variable that stores the last image for comparison
  127.             If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
  128.                 last = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
  129.             End If
  130.             'set the start variable to false so that we start the comparing next run of the timer
  131.             starting = False
  132.         Else
  133.             'This is at least our second run so we can
  134.             'compare
  135.             'We get another image from the webcam
  136.             SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
  137.             data = Clipboard.GetDataObject()
  138.             'if we recive an image from the webcam then:
  139.             If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
  140.  
  141.                 'get the image data
  142.                 bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
  143.                 'calculate the percentage of pixels the same
  144.                 Dim total As Integer
  145.                 Dim number As Integer = 0
  146.                 Dim absolutle As Integer = 0
  147.                 Dim a, b As Integer 'to store the color value for the calulation
  148.                 'set total to the size of the image
  149.                 total = (bmap.Width) * (bmap.Height)
  150.                 For i = 0 To bmap.Height - 1
  151.                     For n = 0 To bmap.Width - 1
  152.                         absolutle += 1
  153.                         'each pixle
  154.                         'each color
  155.                         a = bmap.GetPixel(n, i).R
  156.                         b = last.GetPixel(n, i).R
  157.                         If (a - b) ^ 2 < 100 Then
  158.                             a = bmap.GetPixel(n, i).G
  159.                             b = last.GetPixel(n, i).G
  160.                             If (a - b) ^ 2 < 100 Then
  161.                                 a = bmap.GetPixel(n, i).B
  162.                                 b = last.GetPixel(n, i).B
  163.                                 If (a - b) ^ 2 < 100 Then
  164.                                     number += 1
  165.                                 End If
  166.                             End If
  167.                         End If
  168.                     Next
  169.                 Next
  170.                 Label1.Text = number & "/" & total & " : " & FormatPercent(number / total) & " Absolute : " & absolutle
  171.                 percent = number / total
  172.                 If percent < 0.7 Then
  173.                     thread = New System.Threading.Thread(AddressOf Speak)
  174.                     thread.Start()
  175.                 End If
  176.                 last = bmap
  177.  
  178.             End If
  179.         End If
  180.     End Sub
  181.     Public Sub Speak()
  182.         Dim SAPI
  183.         SAPI = CreateObject("SAPI.spvoice")
  184.         SAPI.Speak("Intruder alert.")
  185.     End Sub
  186. End Class
Advertisement
Add Comment
Please, Sign In to add comment