【发布时间】:2015-08-11 07:43:35
【问题描述】:
这是我在 stackoverflow 上的第一篇文章。 有人试过使用 www.metrilus.de 的 Metricam DLL 吗?我想将 两个 网络摄像头作为 Metricam.Webcam() 连接到一个“表单”中,并且我想一次只使用一个。当我按照他们的“MultiCam”示例尝试时,对于相机(0)和相机(1),只有我的第一台相机工作。这是我附加的代码。如果我犯了错误,请告诉我...
'#########################################################
'References added: Metricam.dll & Webcam.dll
'#########################################################
Imports MetriCam
Public Class Form1
Dim x As Integer = Nothing
Dim cam As String() = WebCam.ScanForCameras
Private camera As WebCam() = New WebCam(2) {}
Public Sub New()
InitializeComponent()
End Sub
Private Sub BtnPhotoCapture_Click(sender As Object, e As EventArgs) Handles BtnPhotoCapture.Click
Try
If BtnPhotoCapture.Text = "Start Capture" Then
' Select camera.
x = ComboBox1.SelectedIndex()
If cam.Count > 0 Then
Try
camera(x) = New WebCam() ' Instead of 'x', I have tried simply putting 0 and 1.
camera(x).Connect()
BtnPhotoCapture.Text = "Grab"
BackgroundWorker.RunWorkerAsync()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Else
BackgroundWorker.CancelAsync()
MessageBox.Show("No camera detected.")
End If
Exit Sub
Else
Try
'Stop camera updating.
BackgroundWorker.CancelAsync()
' Get the source bitmap.
Dim bm_source As New Bitmap(camera(x).GetBitmap())
' Calculate scale factor to make source bitmap height 200 pixel.
Dim BitmapHeight As Integer = bm_source.Height
Dim scale_factor As Integer = 1
' Make a bitmap for the result.
Dim bm_dest As New Bitmap(CInt(bm_source.Width * scale_factor), CInt(bm_source.Height * scale_factor))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)
' Display the result.
PictureBox1.Image = bm_dest
BtnPhotoCapture.Text = "Start Capture"
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork
Try
While Not BackgroundWorker.CancellationPending
' Camera updates for streaming.
camera(x).Update()
' Get the source bitmap.
Dim bm_source As New Bitmap(camera(x).GetBitmap())
' Calculate scale factor to make source bitmap height 200 pixel.
Dim BitmapHeight As Integer = bm_source.Height
Dim scale_factor As Integer = 1
' Make a bitmap for the result.
Dim bm_dest As New Bitmap(CInt(bm_source.Width * scale_factor), CInt(bm_source.Height * scale_factor))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)
' Display the result.
PictureBox1.Image = bm_dest
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker.RunWorkerCompleted
Try
'Disconnecting camera.
camera(x).Disconnect()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BtnScanCameras_Click(sender As Object, e As EventArgs) Handles BtnScanCameras.Click
'Add detected cameras to combo box.
For i = 0 To cam.Count - 1
ComboBox1.Items.Add("Camera " & i & " : [" & cam(i) & "]")
Next
ComboBox1.SelectedIndex = 0
BtnScanCameras.Enabled = False
End Sub
End Class
【问题讨论】:
标签: webcam