从网络摄像头捕捉静态图像(DirectSHowLib,VB.NET)
问题描述:
我很惭愧,但我仍会问:哪种方法可以从网络摄像头以默认尺寸和颜色深度拍摄图片?从网络摄像头捕捉静态图像(DirectSHowLib,VB.NET)
我开始玩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
答
DirectShowLib的samples DxSnap,DxWebCam(C#)展示了如何从一个摄像头捕捉。在那里还有VB.NET DxLogoVB,它做了一个不同的事情,但是如果你也寻找一些DriectShow.NET + VB.NET示例代码,它仍然很好。
DxWebCam:
一个穷人的网络摄像头程序。此应用程序作为Win32服务运行。
它需要捕获图的输出,将其转换成JPEG 文件流,并通过TCP/IP将其发送到客户端应用程序。
DxSnap:
使用DirectShow拍摄快照从捕捉 设备的静止针。请注意,MS鼓励您使用WIA,但如果您想使用DirectShow和C#执行 ,请执行以下操作。
请注意,此示例仅适用于将未压缩的 视频输出为RBG24的设备。这将包括大多数摄像头,但可能是零电视调谐器。
答
好吧,我能做到的最好取决于AForge.Controls和AForge.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
我试图运行DxSnap,每次运行计算机时都会发出哔哔声,就像我从usb上移除网络摄像头一样。无法弄清楚发生了什么问题,对不起。 – VBobCat
我的观点总的来说就是图书馆试图使用样本项目。即使他们似乎没有用完,也值得关注它们。该代码提供了如何实现捕获相关任务的提示。 –
是的,我认为你是对的,但是......是不是只有我或其他人发现离谱的人需要200行代码才能从.NET 4.5中的WebCam中拍照? – VBobCat