Advertisement
Robomatics

HandsFreeBrowsing

Jan 1st, 2013
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.11 KB | None | 0 0
  1. Imports TouchlessLib
  2.  
  3. Public Class Form1
  4.     Public Touchless As New TouchlessLib.TouchlessMgr
  5.     Public Camera1 As TouchlessLib.Camera = Touchless.Cameras.ElementAt(0)
  6.     Dim camw As Integer = 1280 'mine is a 720P webcam, yours might be 640x480
  7.     Dim camh As Integer = 720
  8.     Dim boximage As New Bitmap(camw, camh)
  9.     Dim boxgfx As Graphics
  10.     Dim mouselocation As Point = New Point(0, 0)
  11.     Dim box1 As Rectangle
  12.     Dim box2 As Rectangle
  13.     Dim box1set As Boolean
  14.     Dim box2set As Boolean
  15.     Dim box1active As Boolean
  16.     Dim box2active As Boolean
  17.     Dim box1on As Boolean
  18.     Dim box2on As Boolean
  19.     Dim box1on2 As Boolean
  20.     Dim box2on2 As Boolean
  21.     Dim box1checksum As Integer
  22.     Dim box2checksum As Integer
  23.     Dim box1newchecksum As Integer
  24.     Dim box2newchecksum As Integer
  25.     Dim differencepercent As Integer = 20
  26.  
  27.     'Form Load
  28.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  29.  
  30.         Touchless.CurrentCamera = Camera1
  31.        
  32.         Touchless.CurrentCamera.CaptureWidth = camw 'Tell camera what size to get
  33.         Touchless.CurrentCamera.CaptureHeight = camh
  34.         PictureBox1.Size = New Size(camw, camh) 'Set size of picturebox and program
  35.         Me.Size = New Size(camw, camh)
  36.         PictureBox1.Location = New Point(0, 0)
  37.  
  38.     End Sub
  39.  
  40.     'Timer Tick
  41.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  42.  
  43.         'Setup the image
  44.         boximage = Touchless.CurrentCamera.GetCurrentImage
  45.         If boximage IsNot Nothing Then
  46.             boxgfx = Graphics.FromImage(boximage)
  47.  
  48.             'Draw boxes, and clear appropriate checksums
  49.             If box1active = True Then
  50.                 boxgfx.DrawRectangle(New Pen(Brushes.Red, 2), New Rectangle(box1.X, box1.Y, mouselocation.X - box1.X, mouselocation.Y - box1.Y))
  51.                 box1checksum = 0
  52.             End If
  53.             If box2active = True Then
  54.                 boxgfx.DrawRectangle(New Pen(Brushes.Blue, 2), New Rectangle(box2.X, box2.Y, mouselocation.X - box2.X, mouselocation.Y - box2.Y))
  55.                 box2checksum = 0
  56.             End If
  57.             If box1set = True Then
  58.                 If box1on = True Then
  59.                     boxgfx.DrawRectangle(New Pen(Brushes.Pink, 2), box1)
  60.                 Else
  61.                     boxgfx.DrawRectangle(New Pen(Brushes.Red, 2), box1)
  62.                 End If
  63.                 box1newchecksum = 0
  64.             End If
  65.             If box2set = True Then
  66.                 If box2on = True Then
  67.                     boxgfx.DrawRectangle(New Pen(Brushes.LightBlue, 2), box2)
  68.                 Else
  69.                     boxgfx.DrawRectangle(New Pen(Brushes.Blue, 2), box2)
  70.                 End If
  71.                 box2newchecksum = 0
  72.             End If
  73.  
  74.             'Call pixel checksum checker
  75.             imagediff()
  76.  
  77.             'Draw the differences to the image
  78.             If box1set = True And box1newchecksum <> 0 Then
  79.                 Dim diff1 As Integer = (box1checksum / box1newchecksum) * 100
  80.                 boxgfx.DrawString(diff1.ToString & "%", SystemFonts.DefaultFont, Brushes.Red, box1.X, box1.Y)
  81.                 If diff1 > 100 + differencepercent Or diff1 < 100 - differencepercent Then
  82.                     box1on = True
  83.                 Else
  84.                     box1on = False
  85.                 End If
  86.             End If
  87.             If box2set = True And box2newchecksum <> 0 Then
  88.                 Dim diff2 As Integer = (box2checksum / box2newchecksum) * 100
  89.                 boxgfx.DrawString(diff2.ToString & "%", SystemFonts.DefaultFont, Brushes.Blue, box2.X, box2.Y)
  90.                 If diff2 > 100 + differencepercent Or diff2 < 100 - differencepercent Then
  91.                     box2on = True
  92.                 Else
  93.                     box2on = False
  94.                 End If
  95.             End If
  96.  
  97.             'Send arrow commands
  98.             If box1on = True And box1on2 = False Then 'On2 is for doing a one-shot, as we don't want to hold the key down
  99.                 box1on2 = True
  100.                 SendKeys.Send("{RIGHT}")
  101.             End If
  102.             If box2on = True And box2on2 = False Then
  103.                 box2on2 = True
  104.                 SendKeys.Send("{PGDN}")
  105.             End If
  106.             If box1on = False Then
  107.                 box1on2 = False
  108.             End If
  109.             If box2on = False Then
  110.                 box2on2 = False
  111.             End If
  112.  
  113.             'Send image to the picturebox
  114.             PictureBox1.Image = boximage
  115.         End If
  116.  
  117.     End Sub
  118.  
  119.     'Mouse controls for box draws
  120.     Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  121.         If e.Button = Windows.Forms.MouseButtons.Left And box2active = False Then
  122.             box1set = False
  123.             box1.X = e.X
  124.             box1.Y = e.Y
  125.             box1active = True
  126.         End If
  127.         If e.Button = Windows.Forms.MouseButtons.Right And box1active = False Then
  128.             box2set = False
  129.             box2.X = e.X
  130.             box2.Y = e.Y
  131.             box2active = True
  132.         End If
  133.  
  134.     End Sub
  135.     Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
  136.         If box1active = True Then
  137.             box1.Width = e.X - box1.X
  138.             box1.Height = e.Y - box1.Y
  139.             box1active = False
  140.             box1set = True
  141.         End If
  142.         If box2active = True Then
  143.             box2.Width = e.X - box2.X
  144.             box2.Height = e.Y - box2.Y
  145.             box2active = False
  146.             box2set = True
  147.         End If
  148.  
  149.     End Sub
  150.     Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
  151.         mouselocation.X = e.X
  152.         mouselocation.Y = e.Y
  153.     End Sub
  154.  
  155.     Private Sub imagediff()
  156.         Dim rect As New Rectangle(0, 0, camw, camh)
  157.         Dim bmpData As System.Drawing.Imaging.BitmapData = boximage.LockBits(rect, _
  158.             Drawing.Imaging.ImageLockMode.ReadWrite, boximage.PixelFormat)
  159.         Dim ptr As IntPtr = bmpData.Scan0
  160.         Dim bytes As Integer = bmpData.Stride * camh
  161.         Dim rgbValues(bytes - 1) As Byte
  162.         System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
  163.  
  164.         Dim secondcounter As Integer
  165.         Dim tempred As Integer
  166.         Dim tempblue As Integer
  167.         Dim tempgreen As Integer
  168.         Dim tempalpha As Integer
  169.         Dim tempx As Integer
  170.         Dim tempy As Integer
  171.         secondcounter = 0
  172.  
  173.         While secondcounter < rgbValues.Length
  174.             tempblue = rgbValues(secondcounter)
  175.             tempgreen = rgbValues(secondcounter + 1)
  176.             tempred = rgbValues(secondcounter + 2)
  177.             tempalpha = rgbValues(secondcounter + 3)
  178.             tempalpha = 255
  179.  
  180.             tempy = ((secondcounter * 0.25) / camw)
  181.             tempx = (secondcounter * 0.25) - (tempy * camw)
  182.             If tempx < 0 Then
  183.                 tempx = tempx + camw
  184.             End If
  185.  
  186.             If box1active = True Then
  187.                 If tempx >= box1.X And tempx <= mouselocation.X And tempy >= box1.Y And tempy <= mouselocation.Y Then
  188.                     box1checksum = box1checksum + tempred
  189.                     box1checksum = box1checksum + tempgreen
  190.                     box1checksum = box1checksum + tempblue
  191.                 End If
  192.             End If
  193.             If box2active = True Then
  194.                 If tempx >= box2.X And tempx <= mouselocation.X And tempy >= box2.Y And tempy <= mouselocation.Y Then
  195.                     box2checksum = box2checksum + tempred
  196.                     box2checksum = box2checksum + tempgreen
  197.                     box2checksum = box2checksum + tempblue
  198.                 End If
  199.             End If
  200.             If box1set = True Then
  201.                 If tempx >= box1.X And tempx <= (box1.X + box1.Width) And tempy >= box1.Y And tempy <= (box1.Y + box1.Height) Then
  202.                     box1newchecksum = box1newchecksum + tempred
  203.                     box1newchecksum = box1newchecksum + tempgreen
  204.                     box1newchecksum = box1newchecksum + tempblue
  205.                 End If
  206.             End If
  207.             If box2set = True Then
  208.                 If tempx >= box2.X And tempx <= (box2.X + box2.Width) And tempy >= box2.Y And tempy <= (box2.Y + box2.Height) Then
  209.                     box2newchecksum = box2newchecksum + tempred
  210.                     box2newchecksum = box2newchecksum + tempgreen
  211.                     box2newchecksum = box2newchecksum + tempblue
  212.                 End If
  213.             End If
  214.  
  215.             rgbValues(secondcounter) = tempblue
  216.             rgbValues(secondcounter + 1) = tempgreen
  217.             rgbValues(secondcounter + 2) = tempred
  218.             rgbValues(secondcounter + 3) = tempalpha
  219.  
  220.             secondcounter = secondcounter + 4
  221.         End While
  222.         ' Copy the RGB values back to the bitmap
  223.         System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
  224.  
  225.         ' Unlock the bits.
  226.         boximage.UnlockBits(bmpData)
  227.     End Sub
  228.  
  229. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement