Advertisement
LYSoft

Untitled

May 7th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.94 KB | None | 0 0
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.ComponentModel
  4. Imports System.Data
  5. Imports System.Drawing
  6. Imports System.Text
  7. Imports System.Windows.Forms
  8. Imports AForge.Video
  9. Imports AForge.Video.DirectShow
  10.  
  11. Public Class Form1
  12.     Dim VideoCaptureSource As VideoCaptureDevice
  13.     'Dim VideoCaptureSource2 As VideoCaptureDevice
  14.     Dim VideoDevices As New FilterInfoCollection(FilterCategory.VideoInputDevice)
  15.     'Dim VideoDevices2 As New FilterInfoCollection(FilterCategory.VideoInputDevice)
  16.  
  17.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  18.         Try
  19.             Timer1.Interval = 80
  20.             Timer1.Enabled = False
  21.  
  22.             If VideoSourcePlayer1.IsRunning = True Then
  23.                 VideoSourcePlayer1.GetCurrentVideoFrame()
  24.             End If
  25.  
  26.             If VideoSourcePlayer2.IsRunning = True Then
  27.                 VideoSourcePlayer2.GetCurrentVideoFrame()
  28.             End If
  29.         Catch ex As Exception
  30.  
  31.         End Try
  32.     End Sub
  33.  
  34.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  35.         Try
  36.             ' enumerate video devices
  37.             If VideoDevices.Count = 0 Then
  38.                 ComboBox1.Items.Add("No Video Devices")
  39.             End If
  40.  
  41.             Dim i As Integer = 1, n As Integer = VideoDevices.Count
  42.             While i <= n
  43.                 Dim cameraName As String = i + " : " + VideoDevices(i - 1).Name
  44.                 ComboBox1.Items.Add(cameraName)
  45.                 ComboBox2.Items.Add(cameraName)
  46.                 System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
  47.             End While
  48.  
  49.             ' check cameras count
  50.             If VideoDevices.Count = 1 Then
  51.                 ComboBox2.Items.Clear()
  52.                 ComboBox2.Items.Add("Only one camera found")
  53.                 ComboBox2.SelectedIndex = 0
  54.                 ComboBox2.Enabled = False
  55.             Else
  56.                 ComboBox2.SelectedIndex = 1
  57.             End If
  58.  
  59.             ComboBox1.SelectedIndex = 0
  60.         Catch
  61.             Button2.Enabled = False
  62.             ComboBox1.Items.Add("No cameras found")
  63.             ComboBox2.Items.Add("No cameras found")
  64.             ComboBox1.SelectedIndex = 0
  65.             ComboBox2.SelectedIndex = 0
  66.             ComboBox1.Enabled = True
  67.             ComboBox2.Enabled = True
  68.         End Try
  69.     End Sub
  70.  
  71.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  72.         StopCameras()
  73.     End Sub
  74.  
  75.     Sub StartCameras()
  76.         ' create first video source
  77.         Dim videoSource1 As New VideoCaptureDevice(VideoDevices(ComboBox1.SelectedIndex).MonikerString)
  78.         VideoSourcePlayer1.VideoSource = videoSource1
  79.         VideoSourcePlayer1.Start()
  80.  
  81.         ' create second video source
  82.         If ComboBox2.Enabled = True Then
  83.             System.Threading.Thread.Sleep(500)
  84.  
  85.             Dim videoSource2 As New VideoCaptureDevice(VideoDevices(ComboBox2.SelectedIndex).MonikerString)
  86.             VideoSourcePlayer2.VideoSource = videoSource2
  87.             VideoSourcePlayer2.Start()
  88.         End If
  89.         ' start timer
  90.         Timer1.Start()
  91.     End Sub
  92.  
  93.     Sub StopCameras()
  94.         VideoSourcePlayer1.SignalToStop()
  95.         VideoSourcePlayer2.SignalToStop()
  96.         VideoSourcePlayer1.WaitForStop()
  97.         VideoSourcePlayer2.WaitForStop()
  98.         VideoDevices = Nothing
  99.         VideoCaptureSource = Nothing
  100.     End Sub
  101.  
  102.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  103.         StartCameras()
  104.         Button1.Enabled = False
  105.         Button2.Enabled = True
  106.     End Sub
  107.  
  108.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  109.         StopCameras()
  110.         Button1.Enabled = True
  111.         Button2.Enabled = False
  112.     End Sub
  113. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement