using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Threading;using System.Windows.Forms;using ThridLibray;namespace _1_5相机SDK图像采集{ public class Camera { Listm_frameList = new List (); /* 图像缓存列表 */ Thread renderThread = null; /* 显示线程 */ bool m_bShowLoop = true; /* 线程控制变量 */ Mutex m_mutex = new Mutex(); /* 锁,保证多线程安全 */ public event Action NewImage; public Camera() { if (null == renderThread) { renderThread = new Thread(new ThreadStart(ShowThread)); renderThread.IsBackground = true; renderThread.Start(); } } /* 转码显示线程 */ private void ShowThread() { while (m_bShowLoop) { if (m_frameList.Count == 0) { Thread.Sleep(1); continue; } /* 图像队列取最新帧 */ m_mutex.WaitOne(); IGrabbedRawData frame = m_frameList.ElementAt(0); m_frameList.RemoveAt(0); m_frameList.Clear(); m_mutex.ReleaseMutex(); /* 主动调用回收垃圾 */ GC.Collect(); try { /* 图像转码成bitmap图像 */ var bitmap = frame.ToBitmap(false); if (NewImage!=null) { NewImage(bitmap); } } catch (Exception exception) { Catcher.Show(exception); } } } /* 设备对象 */ private IDevice m_dev; /* 相机打开回调 */ private void OnCameraOpen(object sender, EventArgs e) { MessageBox.Show("相机已打开!"); } /* 相机关闭回调 */ private void OnCameraClose(object sender, EventArgs e) { MessageBox.Show("相机已关闭!"); } /* 相机丢失回调 */ private void OnConnectLoss(object sender, EventArgs e) { m_dev.ShutdownGrab(); m_dev.Dispose(); m_dev = null; } public void btnOpen() { try { /* 设备搜索 */ List li = Enumerator.EnumerateDevices(); if (li.Count > 0) { /* 获取搜索到的第一个设备 */ m_dev = Enumerator.GetDeviceByIndex(0); /* 注册链接事件 */ m_dev.CameraOpened += OnCameraOpen; m_dev.CameraClosed += OnCameraClose; /* 打开设备 */ if (!m_dev.Open()) { MessageBox.Show(@"连接相机失败"); return; } /* 打开Software Trigger */ m_dev.TriggerSet.Open(TriggerSourceEnum.Software); /* 设置图像格式 */ using (IEnumParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImagePixelFormat]) { p.SetValue("Mono8"); } /* 设置曝光 */ using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ExposureTime]) { p.SetValue(1000); } /* 设置增益 */ using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.GainRaw]) { p.SetValue(1.0); } /* 设置缓存个数为8(默认值为16) */ m_dev.StreamGrabber.SetBufferCount(8); /* 注册码流回调事件 */ m_dev.StreamGrabber.ImageGrabbed += OnImageGrabbed; /* 开启码流 */ if (!m_dev.GrabUsingGrabLoopThread()) { MessageBox.Show(@"开启码流失败"); return; } } } catch (Exception exception) { Catcher.Show(exception); } } /* 码流数据回调 */ private void OnImageGrabbed(Object sender, GrabbedEventArgs e) { m_mutex.WaitOne(); m_frameList.Add(e.GrabResult.Clone()); m_mutex.ReleaseMutex(); } /* 停止码流 */ public void btnClose() { try { if (m_dev == null) { throw new InvalidOperationException("Device is invalid"); } m_dev.StreamGrabber.ImageGrabbed -= OnImageGrabbed; /* 反注册回调 */ m_dev.ShutdownGrab(); /* 停止码流 */ m_dev.Close(); /* 关闭相机 */ } catch (Exception exception) { Catcher.Show(exception); } } /* 窗口关闭 */ public void OnClosed(EventArgs e) { if (m_dev != null) { m_dev.Dispose(); m_dev = null; } m_bShowLoop = false; renderThread.Join(); } /* 执行软触发 */ public void SoftwareTrigger() { if (m_dev == null) { throw new InvalidOperationException("Device is invalid"); } try { m_dev.ExecuteSoftwareTrigger(); } catch (Exception exception) { Catcher.Show(exception); } } }}