从网络摄像头捕捉静止图像(DirectSHowLib、VB.NET)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45354472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:18:54  来源:igfitidea点击:

Capture still image from webcam (DirectSHowLib, VB.NET)

vb.netwebcamdirectshowvideo-capturedirectshow.net

提问by VBobCat

I'm ashamed, but I'll ask anyway: which is the most straightforward way to take a picture from a webcam with its default size and color-depth?

我很惭愧,但无论如何我会问:从具有默认大小和颜色深度的网络摄像头拍摄照片的最直接方法是什么?

I started playing with DirectShowLibbut I'm clueless... Can anyone guive me a guidance?

我开始玩DirectShowLib但我一无所知......谁能给我一个指导?

Imports DirectShowLib

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        PictureBox1.Image = Nothing

        Dim Cam As DsDevice = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice).FirstOrDefault

        If Cam IsNot Nothing Then

            Stop
            ' ... what now?

        End If

    End Sub

End Class

回答by Roman R.

DirectShowLib's samplesDxSnap, DxWebCam (C#) show how to capture from a webcam. There is also VB.NET DxLogoVB there, it does a different thing but is still good if you also look for some DriectShow.NET + VB.NET sample code.

DirectShowLib 的示例DxSnap、DxWebCam (C#) 展示了如何从网络摄像头捕获。那里还有 VB.NET DxLogoVB,它做不同的事情,但如果您还寻找一些 DriectShow.NET + VB.NET 示例代码,它仍然很好。

DxWebCam:

Dx网络摄像头:

A poor man's web cam program. This application runs as a Win32 Service.
It takes the output of a capture graph, turns it into a stream of JPEG files, and sends it thru TCP/IP to a client application.

一个穷人的网络摄像头程序。此应用程序作为 Win32 服务运行。
它获取捕获图的输出,将其转换为 JPEG 文件流,然后通过 TCP/IP 将其发送到客户端应用程序。

DxSnap:

DxSnap:

Use DirectShow to take snapshots from the Still pin of a capture device.Note the MS encourages you to use WIA for this, but if you want to do in with DirectShow and C#, here's how.

Note that this sample will only work with devices that output uncompressed video as RBG24. This will include most webcams, but probably zero tv tuners.

使用 DirectShow 从捕获设备的 Still 引脚拍摄快照。请注意,MS 鼓励您为此使用 WIA,但如果您想使用 DirectShow 和 C#,方法如下。

请注意,此示例仅适用于将未压缩视频输出为 RBG24 的设备。这将包括大多数网络摄像头,但可能包括零电视调谐器。

回答by VBobCat

Ok, the best I was able to do depends on AForge.Controlsand AForge.Video.DirectShowand is working with this code, which I intend to improve (it is a rough scratch - but takes the picture):

好的,我能做的最好的事情取决于AForge.ControlsAForge.Video.DirectShow并且正在使用此代码,我打算改进它(这是一个粗略的划痕 - 但需要拍照):

Public Class Form1
    Private Sub Test() Handles Me.Load
        Dim rf As New RolleiFlex
        PictureBox1.Image = rf.Click
    End Sub
End Class

Public Class RolleiFlex

    Public Sub New()
        Dim vDevices = New AForge.Video.DirectShow.FilterInfoCollection(FilterCategory.VideoInputDevice)
        Devices = vDevices.Cast(Of FilterInfo).Select(
            Function(fi) New Device With {
            .Name = fi.Name,
            .MonikerString = fi.MonikerString}).ToArray
        SelectedDevice = Devices.FirstOrDefault
        vDevices = Nothing
    End Sub

    Public Devices As Device()

    Public Property SelectedDevice As Device

    Public Class Device
        Public Property Name As String
        Public Property MonikerString As String
    End Class

    Public Function Click() As Bitmap
        Dim retBmp As Bitmap
        Dim camera As New AForge.Controls.VideoSourcePlayer
        camera.VideoSource = New VideoCaptureDevice(SelectedDevice.MonikerString)
        camera.Start()
        Do
            retBmp = camera.GetCurrentVideoFrame
            If retBmp Is Nothing Then Threading.Thread.Sleep(100)
        Loop While retBmp Is Nothing
        camera.Stop()
        camera.Dispose()
        camera = Nothing
        Return retBmp
    End Function

End Class