使用表达式编码器从网络摄像头快照

时间:2012-10-11 18:16:05

标签: c# vb.net webcam expression-encoder-sdk expression-encoder-4

一直在使用我找到的例子 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'对象,但我找不到任何替代品。其他人有运气吗?

1 个答案:

答案 0 :(得分: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()

并检查这是否适合您。我猜想在拍摄快照时你的网络摄像头没有初始化。