当前位置:首页 > Windows程序 > 正文

C#调用摄像头的几种方式

2021-05-25 Windows程序

首先给上我所使用的DLL

技术分享

调用USB摄像头

调用USB摄像头其实比较简单,就是通过读取电脑自身所拥有的设备数,再执行预览。
videoSourcePlayer是AForge中的控件。

private FilterInfoCollection videoDevices; this.videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (this.videoDevices.Count > 0) { VideoCaptureDevice source = new VideoCaptureDevice(this.videoDevices[0].MonikerString); this.videoSourcePlayer.SignalToStop(); this.videoSourcePlayer.WaitForStop(); this.videoSourcePlayer.VideoSource = source; this.videoSourcePlayer.Start(); } 调用普通IP摄像头

首先要确认HTTP方式传送的图片的地址,我用的SAMSUN地址是{0}/cgi-bin/video.cgi?msubmenu=jpg,还有其他的{0}/axis-cgi/jpg/image.cgi?camera=1
使用JPEGStream或者MJPEGStream,有用户名和密码的就加上。有了source其他的和上面USB一样。

JPEGStream source = new JPEGStream(URL); //MJPEGStream source = new MJPEGStream(URL); source.Login = username; source.Password = password; this.OpenVideoSource(source); 通过摄像头SDK进行调用

这个调用还是很简单的,一般都是使用控件的Handle来进行预览的,我们可能会想取每一帧的图片,那么如何取得每一帧呢?因为是通过Handle来预览的所以控件中无法取得,,我们需要调用摄像头SDK的获取每一帧的接口,但是这个接口是直接将图片保存到本地的,感觉巨坑有木有。不过我们还是有方法处理的。下面是我对三星摄像头的处理:

public override Bitmap CapturePicture() { Bitmap bitmap = null; string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Templates) + @"\SAMSUNG_CapturePicture.bmp"; if (File.Exists(path)) { File.Delete(path); } if (SSNetSDK.XNS_DEV_SaveSnapshot(playHandle, path,0)) { using (MemoryStream stream = new MemoryStream(File.ReadAllBytes(path))) { bitmap = (Bitmap)Image.FromStream(stream); } File.Delete(path); } return bitmap; }

DEMO

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70623.html