0

http://www.codeproject.com/Articles/285964/WPF-Webcam-Controlで見つけた例を使用して、 必要なWebカメラ機能を作成しています。私は主にスナップショットに興味があり、上記の例で問題なくスナップショットを機能させることができます。私の主な問題は、事前にユーザーに「プレビューウィンドウ」を表示せずに、Webカメラからスナップショットを取得できるようにしたいことです。画像は、ユーザーに表示したり何も表示せずに自動的に保存されます。以下は私が持っているものです(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()

そして、これがうまくいくかどうかを確認してください。スナップショットをキャプチャしたときに Web カメラが初期化されていなかったと推測します。

于 2012-10-13T11:47:05.187 に答える