Accordを使用してUSB接続カメラから画像を取得して画面表示する方法を記載します。
USB接続カメラから画像を取得して表示方法として、Accordを使用すると簡単に出来ます。
接続にはDirectShowを使用しますので、
・Accord.Video
・Accord.Video.DirectShow
をインポートします。
AccordのUSBカメラは、AForgeを使用されているいますので、AForgeと同じコードで動作します。
USB接続カメラの画像を表示
Dim _videoDevices As FilterInfoCollection
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'ビデオデバイス一覧を表示する
_videoDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)
ComboBox1.Items.Clear()
If _videoDevices.Count <> 0 Then
For Each device In _videoDevices
ComboBox1.Items.Add(device.Name)
Next
ComboBox1.SelectedIndex = 0
End If
End Sub
Dim _videoSource As VideoCaptureDevice = Nothing
'[開始]
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
If ComboBox1.Items.Count = 0 Then
Return
End If
Dim MonikerString = _videoDevices(ComboBox1.SelectedIndex).MonikerString '最初のビデオデバイスを使用
_videoSource = New VideoCaptureDevice(MonikerString)
AddHandler _videoSource.NewFrame, AddressOf Me.Video_NewFrame
_videoSource.Start()
End Sub
'[停止]
Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click
If _videoSource Is Nothing Then
Return
End If
If _videoSource.IsRunning Then
_videoSource.SignalToStop() 'ビデオデバイスの停止
_videoSource.WaitForStop() '完全に停止するまで待つ
_videoSource = Nothing
End If
End Sub
'ビデオデバイス取得画像表示
Private Sub Video_NewFrame(sender As Object, eventArgs As NewFrameEventArgs)
Dim img = DirectCast(eventArgs.Frame.Clone(), Bitmap)
PictureBox1.Image = img
End Sub