0

一直在使用我在 http://www.codeproject.com/Articles/285964/WPF-Webcam-Control找到的示例 来制作我需要的一些网络摄像头功能。我对快照最感兴趣,我可以让它们在上面的示例中正常工作,没有任何问题。我的主要问题是我希望能够从网络摄像头拍摄快照,而无需事先为用户显示“预览窗口”。图像只是自动保存,没有任何显示或任何东西给用户。以下是我所拥有的(在 vb.net 中,但我不介意在 c# 中回答):

Public Shared Function TakeSnapshotReturnBytes(panelHeight As Integer, panelWidth As Integer) As Byte()
    Dim b() As Byte = Nothing
    Dim vidDevCol As IEnumerable(Of EncoderDevice) = EncoderDevices.FindDevices(EncoderDeviceType.Video)
    If vidDevCol IsNot Nothing AndAlso vidDevCol.Count > 0 AndAlso vidDevCol(0) IsNot Nothing Then
        Dim tmpJob As LiveJob = Nothing
        Dim lvDevSrc As LiveDeviceSource = Nothing
        Try
            tmpJob = New LiveJob
            Using tmpPanel As New System.Windows.Forms.Panel
                tmpPanel.Height = panelHeight
                tmpPanel.Width = panelWidth

                lvDevSrc = tmpJob.AddDeviceSource(vidDevCol(0), Nothing)
                lvDevSrc.PreviewWindow = New PreviewWindow(New HandleRef(tmpPanel, tmpPanel.Handle))
                tmpJob.ActivateSource(lvDevSrc)

                Using MS As New IO.MemoryStream()
                    Using bmp As New Bitmap(panelWidth, panelHeight)
                        tmpPanel.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))

                        bmp.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg)
                        MS.Position = 0
                        Using br As New IO.BinaryReader(MS)
                            b = br.ReadBytes(CInt(MS.Length))
                        End Using
                    End Using
            End Using
            End Using
        Finally
            If lvDevSrc IsNot Nothing Then
                tmpJob.RemoveDeviceSource(lvDevSrc)
                lvDevSrc.Dispose()
            End If
            If tmpJob IsNot Nothing Then
                tmpJob.Dispose()
            End If
        End Try
    End If
    Return b
End Function

我得到的只是一个灰色的窗口。我猜我不应该使用“PreviewWindow”对象,但我找不到任何替代品。其他人有任何运气这样做吗?

4

1 回答 1

0

像这样更改您的代码:

tmpJob.ActivateSource(lvDevSrc) 

// This delay let your camera to initialize and ready to capture image.
// Actualy we should find another safer :) way to do this but just to check if it works!
System.Threading.Thread.Sleep(5000)

Using MS As New IO.MemoryStream()

并检查这是否适合您。我猜想您的网络摄像头在您捕获快照时尚未初始化。

于 2012-10-13T11:47:05.187 回答