namespace XHWK.WKTool.system { using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows.Media.Imaging; using System.Windows.Threading; using AForge.Controls; using AForge.Video.DirectShow; public static class CameraHelper { private static List _cameraDevices; private static VideoCaptureDevice _div; private static readonly VideoSourcePlayer SourcePlayer = new VideoSourcePlayer(); //指示_isDisplay设置为true后,是否设置了其他的sourcePlayer,若未设置则_isDisplay重设为false private static Dispatcher _dispatcher; public static Action imageCallback; /// /// 获取或设置摄像头设备,无设备为null /// public static List CameraDevices { get => _cameraDevices; set => _cameraDevices = value; } /// /// 更新摄像头设备信息 /// public static void UpdateCameraDevices() { // ReSharper disable once CollectionNeverUpdated.Local var devs = new FilterInfoCollection(FilterCategory.VideoInputDevice); //获取摄像头列表 List allCamera = new List(); foreach (FilterInfo dev in devs) { if (dev.Name != "screen-capture-recorder" && dev.Name != "OBS Virtual Camera") { allCamera.Add(dev); } } _cameraDevices = allCamera; } /// /// 设置使用的摄像头设备 /// /// 设备在CameraDevices中的索引 /// /// public static bool StartCameraDevice(int index, Dispatcher dispatcher) { _dispatcher = dispatcher; //无设备,返回false if (_cameraDevices.Count <= 0 || index < 0) { return false; } if (index > _cameraDevices.Count - 1) { return false; } // 设定初始视频设备 _div = new VideoCaptureDevice(_cameraDevices[index].MonikerString); _div.NewFrame += _div_NewFrame; SourcePlayer.VideoSource = _div; _div.Start(); SourcePlayer.Start(); return true; } private static void _div_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) { _dispatcher?.Invoke ( () => { using (MemoryStream ms = new MemoryStream()) { eventArgs.Frame.Save(ms, ImageFormat.Bmp); BitmapImage image = new BitmapImage { CacheOption = BitmapCacheOption.OnLoad }; image.BeginInit(); image.StreamSource = new MemoryStream(ms.GetBuffer()); image.EndInit(); imageCallback?.Invoke(image); } } ); } /// /// 截取一帧图像并保存 /// /// 图像保存路径 /// 保存的图像文件名 /// 如果保存成功,则返回完整路径,否则为 null public static string CaptureImage(string filePath, string fileName = null) { if (SourcePlayer.VideoSource == null) { return null; } if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } try { Image bitmap = SourcePlayer.GetCurrentVideoFrame(); if (bitmap != null) { if (fileName == null) { fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); } string fullPath = Path.Combine(filePath, fileName + "-cap.jpg"); Bitmap objNewPic = new Bitmap ( bitmap, 172, 124 ); //图片保存的大小尺寸 objNewPic.Save(fullPath, ImageFormat.Jpeg); //bitmap.Save(fullPath, ImageFormat.Jpeg); bitmap.Dispose(); objNewPic.Dispose(); return fullPath; } return null; } catch (Exception) { return null; } } /// /// 关闭摄像头设备 /// public static void CloseDevice() { if (_div != null) { _div.NewFrame -= _div_NewFrame; if (SourcePlayer.IsRunning) { SourcePlayer.Stop(); } if (_div.IsRunning) { _div.SignalToStop(); } _div = null; } } } }