Metricam SDK:连接两个WebCams

时间:2015-08-11 07:43:36

标签: webcam

这是我在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

1 个答案:

答案 0 :(得分:-1)

尝试使用此代码(在此演示中,我假设我想在可用的凸轮列表中使用" last"相机...):

camera = new WebCam();
string[] mycams = WebCam.ScanForCameras();            
int indice = mycams.Length - 1;
((WebCam)camera).SetSerialNumberToConnect(mycams[indice]);
camera.Connect();