using AForge.Video; using AForge.Video.DirectShow; using HalconDotNet; using MvCamCtrl.NET; using MVSDK; using LB_VisionProcesses; using OpenCvSharp; using SharpCompress; using System.Diagnostics; using System.Reflection.Metadata; using System.Text.RegularExpressions; using System.Timers; using System.Windows.Forms; using LB_VisionProcesses.Cameras; namespace LB_VisionProcesses.Cameras.LocalCameras { public class LocalCamera : BaseCamera { #region variable VideoCaptureDevice _capture = null; IntPtr Handle = IntPtr.Zero; TriggerMode _triggerMode = TriggerMode.On; // 触发模式 #endregion public LocalCamera() { Brand = CameraBrand.LocalCamera; } public override bool AutoBalanceWhite() { return true; } public override bool CloseDevice() { try { if (_capture != null) { _capture.NewFrame -= VideoSource_NewFrame; // 停止采集 _capture.SignalToStop(); _capture.WaitForStop(); _capture.Stop(); _capture = null; } isGrabbing = false; return true; } catch { return false; } } public override bool GetExpouseTime(out double value) { value = 0; return true; } public override bool GetGain(out double gain) { gain = 0; return true; } public override bool GetLineStatus(IOLines line, out LineStatus lineStatus) { lineStatus = LineStatus.Hight; return true; } public override List GetListEnum() { List cameras = new List(); FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); foreach (FilterInfo device in videoDevices) { cameras.Add($"{device.Name}({device.MonikerString})"); } return cameras; } public override bool GetTriggerDelay(out double delay) { delay = 0; return true; } public override bool GetTriggerFliter(out double flitertime) { flitertime = 0; return true; } public override bool GetTriggerMode(out TriggerMode mode, out TriggerSource source) { mode = _triggerMode; source = TriggerSource.Software; return true; } public override bool GetTriggerPolarity(out TriggerPolarity polarity) { polarity = TriggerPolarity.RisingEdge; return true; } public override bool InitDevice(string SN, object Handle) { try { if (!(Handle is IntPtr) || Handle == null) { MessageBox.Show("初始化失败,窗体句柄为null !", "异常"); return false; } FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); foreach (FilterInfo device in videoDevices) { if (SN.Equals($"{device.Name}({device.MonikerString})")) { _capture = new VideoCaptureDevice(device.MonikerString); var formats = _capture.VideoCapabilities; //设置为最高分辨率 if (formats.Length > 1) _capture.VideoResolution = formats[0]; if (_capture.IsRunning) _capture.Stop(); this.SN = SN; this.Handle = (IntPtr)Handle; try { // 订阅 _capture.NewFrame -= VideoSource_NewFrame; _capture.NewFrame += VideoSource_NewFrame; // 设置为Off会一直触发回调 SetTriggerMode(TriggerMode.On, TriggerSource.Software); // 开始拉流 // Start grabbing if (!StartGrabbing()) { Debug.WriteLine("开始采集失败"); return false; } } catch { /* Ignore if not supported */ } return true; } } return false; } catch { return false; } } private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs) { try { Bitmap frame = (Bitmap)eventArgs.Frame.Clone(); // 防止资源占用 if (frame != null) { try { if (this.ImageGrabbed != null) this.ImageGrabbed(this, new CameraEventArgs(SN, frame)); //if (this.TriggerRunMessageReceived != null) // this.TriggerRunMessageReceived(SN, "Line0"); CallBackImg = frame; } catch { } } } catch { } } void Timer_CaptureFrame(object sender, ElapsedEventArgs e) { try { if (!isGrabbing) return; //触发模式为软触发时,定时模拟触发 if (_triggerMode != TriggerMode.On) _capture.SimulateTrigger(); // 模拟触发 } catch { } } public override bool SetExpouseTime(double value) { return true; } public override bool SetGain(double gain) { return true; } public override bool SetLineMode(IOLines line, LineMode mode) { return true; } public override bool SetLineStatus(IOLines line, LineStatus linestatus) { return true; } public override bool SetTriggerDelay(double delay) { return true; } public override bool SetTriggerFliter(double flitertime) { return true; } public override bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0) { try { _triggerMode = mode; if (_triggerMode == TriggerMode.On) { // 停止采集 _capture.SignalToStop(); _capture.WaitForStop(); } else _capture.Start(); return true; } catch { return false; } } public override bool SetTriggerPolarity(TriggerPolarity polarity) { return true; } public override bool SoftTrigger() { CallBackImg = null; if (_capture == null) return false; try { _capture.Start(); DateTime StartTime = DateTime.Now; while (CallBackImg == null && (DateTime.Now - StartTime).TotalSeconds < 5) Thread.Sleep(200); if (_capture.IsRunning) { _capture.SignalToStop(); _capture.WaitForStop(); } return true; } catch { return false; } } public override bool StartGrabbing() { try { if (_capture == null) return false; _capture.Start(); if (_triggerMode == TriggerMode.On) { // 停止采集 _capture.SignalToStop(); _capture.WaitForStop(); } CallBackImg = null; isGrabbing = true; return true; } catch { return false; } } public override bool StopGrabbing() { try { if (_capture == null) return false; _capture.SignalToStop(); _capture.WaitForStop(); isGrabbing = false; CallBackImg = null; return true; } catch { return false; } } public void SetSetting(int show = 1) { if (_capture != null && Handle != IntPtr.Zero) _capture.DisplayPropertyPage(this.Handle); } public override bool GetImage(out Bitmap bitmap, int outtime = 3000) { bitmap = null; try { // 设置超时时间 DateTime lastTime = DateTime.Now.AddMilliseconds(outtime); // 判断是否超时 while (lastTime > DateTime.Now)// 设置超时时间为 3 秒 { try { if (CallBackImg != null) { // 保存旧 Bitmap 并释放 bitmap = CallBackImg.Clone() as Bitmap; // 创建副本 // 释放旧资源 CallBackImg.Dispose(); CallBackImg = null; return true; } } catch { } Thread.Sleep(10); } return false; } catch { return bitmap == null ? false : true; } } public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000) { if (!isGrabbing) { StartGrabbing(); } GetTriggerMode(out TriggerMode triggerMode, out TriggerSource triggerSource); if (triggerMode != TriggerMode.On && triggerSource != TriggerSource.Software) { SetTriggerMode(TriggerMode.On, TriggerSource.Software); } bitmap = null; CallBackImg = null; if (!SoftTrigger()) { return false; } // 开始时间 DateTime startTime = DateTime.Now; // 当前时间 // 判断是否超时 while (DateTime.Now < startTime.AddMilliseconds(outtime))// 设置超时时间为 3 秒 { GetImage(out bitmap, 50); if (bitmap != null) { break; } Thread.Sleep(10); } if (triggerMode != TriggerMode.On) { SetTriggerMode(TriggerMode.On, triggerSource); } return (bitmap != null); } public override void SetCamConfig(CameraConfig config) { if (Enum.TryParse(config.Params.Inputs["触发模式"].ToString(), out TriggerMode TriggerMode) && Enum.TryParse(config.Params.Inputs["触发方式"].ToString(), out TriggerSource TriggerSource) && Enum.TryParse(config.Params.Inputs["触发极性"].ToString(), out TriggerPolarity TriggerPolarity) ) { SetTriggerMode(TriggerMode, TriggerSource); SetTriggerPolarity(TriggerPolarity); //SetTriggerFliter(Convert.ToDouble(config.Params.Inputs["触发消抖"].ToString())); //SetTriggerDelay(Convert.ToDouble(config.Params.Inputs["触发延时"].ToString())); SetExpouseTime(Convert.ToDouble(config.Params.Inputs["曝光时间"].ToString())); SetGain(Convert.ToDouble(config.Params.Inputs["增益"].ToString())); } } public override void GetCamConfig(out CameraConfig config) { GetTriggerMode(out TriggerMode triggerMode, out TriggerSource triggerSource); GetTriggerPolarity(out TriggerPolarity triggerPolarity); //GetTriggerFliter(out double triggerfilter); //GetTriggerDelay(out double triggerdelay); GetExpouseTime(out double expouseTime); GetGain(out double gain); config = new CameraConfig(null); config.Params.Inputs.Add("触发模式", triggerMode); config.Params.Inputs.Add("触发方式", triggerSource); config.Params.Inputs.Add("触发极性", triggerPolarity); //config.Params.Inputs.Add("触发消抖", triggerfilter); //config.Params.Inputs.Add("触发延时", triggerdelay); config.Params.Inputs.Add("曝光时间", expouseTime); config.Params.Inputs.Add("增益", gain); } public override bool StartWith_SoftTriggerModel() { SetTriggerMode(TriggerMode.Off, TriggerSource.Software); return StartGrabbing(); } public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0) { if (hardtriggeritem == TriggerSource.Software) hardtriggeritem = TriggerSource.Line0; SetTriggerMode(TriggerMode.On, hardtriggeritem); return StartGrabbing(); } public override bool StartContinuousGrab() { throw new NotImplementedException(); } } }