1 class Camera // 大华相机类 2 { 3 private IDevice m_dev; /* 设备对象 */ 4 List<IGrabbedRawData> m_frameList = new List<IGrabbedRawData>(); /* 图像缓存列表 */ 5 Thread renderThread = null; /* 显示线程 */ 6 bool m_bShowLoop = true; /* 线程控制变量 */ 7 Mutex m_mutex = new Mutex(); /* 锁,保证多线程安全 */ 8 // public event Action<HObject> NewImage; // 图像显示事件 9 10 public Camera() 11 { 12 if (null == renderThread) 13 { 14 renderThread = new Thread(new ThreadStart(ShowThread)); 15 renderThread.IsBackground = true; 16 renderThread.Start(); 17 } 18 m_stopWatch.Start();//时间计时开始 19 } 20 21 /* 转码显示线程 */ 22 private voID ShowThread() 23 { 24 while (m_bShowLoop)//线程控制变量为True 25 { 26 if (m_frameList.Count == 0) //图像缓存列表 27 { 28 Thread.Sleep(10); 29 continue; 30 } 31 32 /* 图像队列取最新帧 */ 33 m_mutex.WaitOne();//阻止当前线程,直到当前收到信号 34 IGrabbedRawData frame = m_frameList.ElementAt(0);//返回队列中指定索引处的元素 35 m_frameList.RemoveAt(0); 36 m_frameList.Clear(); 37 m_mutex.ReleaseMutex(); 38 39 /* 主动调用回收垃圾 */ 40 GC.Collect(); 41 42 /* 控制显示最高帧率为25FPS */ 43 if (false == isTimetodisplay()) 44 { 45 continue; 46 } 47 48 try 49 { 50 /* 图像转码成bitmap图像 */ 51 var bitmap = frame.ToBitmap(false); 52 53 // 在这里使用采集的图像 54 //if (NewImage != null) 55 //{ 56 // NewImage(image); 57 //} 58 } 59 catch 60 { 61 62 } 63 } 64 } 65 66 const int DEFAulT_INTERVAL = 40;//默认的时间间隔 67 Stopwatch m_stopWatch = new Stopwatch(); /* 时间统计器 */ 68 69 /* 判断是否应该做显示 *** 作 */ 70 private bool isTimetodisplay() 71 { 72 m_stopWatch.Stop(); 73 long m_ldisplayInterval = m_stopWatch.ElapsedMilliseconds;//获取实例测量出的总时间 74 if (m_ldisplayInterval <= DEFAulT_INTERVAL) 75 { 76 m_stopWatch.Start(); 77 return false; 78 } 79 else 80 { 81 m_stopWatch.reset(); 82 m_stopWatch.Start(); 83 return true; 84 } 85 } 86 87 /* 相机打开回调 */ 88 private voID OnCameraOpen(object sender,EventArgs e) 89 { 90 // 在这里记录相机已打开 91 } 92 93 /* 相机关闭回调 */ 94 private voID OnCameraClose(object sender,EventArgs e) 95 { 96 // 在这里记录相机已关闭 97 } 98 99 /* 相机丢失回调 */100 private voID OnConnectLoss(object sender,EventArgs e)101 {102 m_dev.ShutdownGrab();103 m_dev.dispose();104 m_dev = null;105 }106 107 /* 码流数据回调 */108 private voID OnImageGrabbed(Object sender,GrabbedEventArgs e)109 {110 m_mutex.WaitOne();// m_mutex锁,保证多线程安全111 m_frameList.Add(e.GrabResult.Clone());/* 图像缓存列表 */112 m_mutex.ReleaseMutex();113 }114 115 /// <summary>116 /// 打开相机117 /// </summary>118 /// <returns>返回字符串为空表示打开成功,不为空表示打开失败,字符串内容为异常信息</returns> 119 /// <param name="triggerSource">触发源选择 软触发或外触发</param>120 /// <returns></returns>121 public string Open(string triggerSource)122 {123 string sRet = string.Empty;124 try125 {126 /* 设备搜索 */127 List<IDeviceInfo> li = Enumerator.EnumerateDevices();128 if (li.Count > 0)129 {130 /* 获取搜索到的第一个设备 */131 m_dev = Enumerator.GetDeviceByIndex(0);132 133 /* 注册链接事件 */134 m_dev.CameraOpened += OnCameraOpen;135 m_dev.ConnectionLost += OnConnectLoss;136 m_dev.CameraClosed += OnCameraClose;137 138 /* 打开设备 */139 if (!m_dev.open())140 sRet = "打开相机失败";141 142 /* 设置TriggerSource 软触发还是外触发*/143 m_dev.TriggerSet.Open(triggerSource);144 145 /* 设置图像格式 */146 using (IEnumParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImagePixelFormat])147 {148 p.SetValue("Mono8");149 }150 151 /* 设置缓存个数为8(默认值为16) */152 m_dev.StreamGrabber.SetBufferCount(8);153 154 /* 注册码流回调事件 */155 m_dev.StreamGrabber.ImageGrabbed += OnImageGrabbed;156 157 /* 开启码流 */158 if (!m_dev.GrabUsingGrabLoopThread())159 sRet = "开启码流失败";160 }161 else162 {163 sRet = "未找到相机设备";164 }165 }166 catch (Exception e)167 {168 sRet = e.Message;169 }170 return sRet;171 }172 173 /// <summary>174 /// 关闭相机175 /// </summary>176 /// <returns>返回字符串为空表示关闭成功,不为空表示关闭失败,字符串内容为异常信息</returns>177 public string Close()178 {179 string sRet = String.Empty;180 try181 {182 if (m_dev == null)183 {184 sRet = "Device is invalID";185 }186 187 m_dev.StreamGrabber.ImageGrabbed -= OnImageGrabbed; /* 反注册回调 */188 m_dev.ShutdownGrab(); /* 停止码流 */189 m_dev.Close(); /* 关闭相机 */190 }191 catch (Exception e)192 {193 sRet = e.Message;194 }195 return sRet;196 }197 198 /* 窗口关闭 */199 public voID OnClosed(EventArgs e)200 {201 if (m_dev != null)202 {203 m_dev.dispose();204 m_dev = null;205 }206 207 m_bShowLoop = false;208 renderThread.Join();209 }210 /// <summary>211 /// 设置触发模式 软触发下设置On后自由拉流(连续触发)Off(单次触发)212 /// </summary>213 /// <param name="value"></param>214 public string SetTriggerMode(string value="On")215 {216 if (m_dev == null)217 {218 return "Device is invalID";219 }220 if (!m_dev.IsOpen)221 {222 return "相机未打开";223 }224 using (IEnumParameter p = m_dev.ParameterCollection[ParametrizenameSet.TriggerMode])225 {226 p.SetValue(value);227 }228 return "";229 }230 /// <summary>231 /// 设置曝光232 /// </summary>233 /// <param name="value"></param>234 public string SetExposureTime(float value)235 {236 if (m_dev == null)237 {238 return "Device is invalID";239 }240 if (!m_dev.IsOpen)241 {242 return "相机未打开";243 }244 double min = GetminexposureTime();245 double max = GetMaxExposureTime();246 if (value < min || value > max)247 {248 return string.Format("参数值不在范围内,min:{0},max:{1}",min,max);249 }250 using (IfloatParameter p = m_dev.ParameterCollection[ParametrizenameSet.ExposureTime])251 {252 p.SetValue(value);253 }254 return "";255 }256 /// <summary>257 /// 设置增益258 /// </summary>259 /// <param name="value"></param>260 public string SetGainRaw(float value)261 {262 if (m_dev == null)263 {264 return "Device is invalID";265 }266 if (!m_dev.IsOpen)267 {268 return "相机未打开";269 }270 double min = GetMinGainRaw();271 double max = GetMaxGainRaw();272 if (value < min || value > max)273 {274 return string.Format("参数值不在范围内,min:{0},max:{1}",max);275 }276 using (IfloatParameter p = m_dev.ParameterCollection[ParametrizenameSet.GainRaw])277 {278 p.SetValue(value);279 }280 return "";281 }282 /// <summary>283 /// 设置图像的宽度284 /// </summary>285 /// <param name="value"></param>286 /// <returns></returns>287 public string SetimageWIDth(int value)288 {289 if (m_dev == null)290 {291 return "Device is invalID";292 }293 if (!m_dev.IsOpen)294 {295 return "相机未打开";296 }297 double min = GetMinImageWIDth();298 double max = GetMaxImageWIDth();299 if (value < min || value > max)300 {301 return string.Format("参数值不在范围内,min:{0},max:{1}",max);302 }303 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageWIDth])304 {305 p.SetValue(value);306 }307 return "";308 }309 /// <summary>310 /// 设置图像的宽度311 /// </summary>312 /// <param name="value"></param>313 /// <returns></returns>314 public string SetimageHeight(int value)315 {316 if (m_dev == null)317 {318 return "Device is invalID";319 }320 if (!m_dev.IsOpen)321 {322 return "相机未打开";323 }324 double min = GetMinImageHeight();325 double max = GetMaxImageHeight();326 if (value < min || value > max)327 {328 return string.Format("参数值不在范围内,min:{0},max:{1}",max);329 }330 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageHeight])331 {332 p.SetValue(value);333 }334 return "";335 }336 /// <summary>337 /// 设置图像偏移X338 /// </summary>339 /// <param name="value"></param>340 /// <returns></returns>341 public string SetimageOffsetX(int value)342 {343 if (m_dev == null)344 {345 return "Device is invalID";346 }347 if (!m_dev.IsOpen)348 {349 return "相机未打开";350 }351 double min = GetMinImageOffsetX();352 double max = GetMaxImageOffsetX();353 if (value < min || value > max)354 {355 return string.Format("参数值不在范围内,min:{0},max:{1}",max);356 }357 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageOffsetX])358 {359 p.SetValue(value);360 }361 return "";362 }363 /// <summary>364 /// 设置图像偏移Y365 /// </summary>366 /// <param name="value"></param>367 /// <returns></returns>368 public string SetimageOffsetY(int value)369 {370 if (m_dev == null)371 {372 return "Device is invalID";373 }374 if (!m_dev.IsOpen)375 {376 return "相机未打开";377 }378 double min = GetMinImageOffsetY();379 double max = GetMaxImageOffsetY();380 if (value < min || value > max)381 {382 return string.Format("参数值不在范围内,min:{0},max:{1}",max);383 }384 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageOffsetY])385 {386 p.SetValue(value);387 }388 return "";389 }390 391 #region 获取相机参数的取值范围392 public double GetminexposureTime()393 {394 using (IfloatParameter p = m_dev.ParameterCollection[ParametrizenameSet.ExposureTime])395 {396 return p.GetMinimum();397 }398 }399 public double GetMaxExposureTime()400 {401 using (IfloatParameter p = m_dev.ParameterCollection[ParametrizenameSet.ExposureTime])402 {403 return p.GetMaximum();404 }405 }406 public double GetMinGainRaw()407 {408 using (IfloatParameter p = m_dev.ParameterCollection[ParametrizenameSet.GainRaw])409 {410 return p.GetMinimum();411 }412 }413 public double GetMaxGainRaw()414 {415 using (IfloatParameter p = m_dev.ParameterCollection[ParametrizenameSet.GainRaw])416 {417 return p.GetMaximum();418 }419 }420 public long GetMinImageWIDth()421 {422 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageWIDth])423 {424 return p.GetMinimum();425 }426 }427 public long GetMaxImageWIDth()428 {429 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageWIDth])430 {431 return p.GetMaximum();432 }433 }434 public long GetMinImageHeight()435 {436 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageHeight])437 {438 return p.GetMinimum();439 }440 }441 public long GetMaxImageHeight()442 {443 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageHeight])444 {445 return p.GetMaximum();446 }447 }448 public long GetMinImageOffsetX()449 {450 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageOffsetX])451 {452 return p.GetMinimum();453 }454 }455 public long GetMaxImageOffsetX()456 {457 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageOffsetX])458 {459 return p.GetMaximum();460 }461 }462 public long GetMinImageOffsetY()463 {464 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageOffsetY])465 {466 return p.GetMinimum();467 }468 }469 public long GetMaxImageOffsetY()470 {471 using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizenameSet.ImageOffsetY])472 {473 return p.GetMaximum();474 }475 }476 #endregion477 478 /// <summary>479 /// bitmap图像转HObject480 /// </summary>481 /// <param name="bmp"></param>482 /// <returns></returns>483 //private HObject Bitmap2HObjectBpp24(Bitmap bmp)484 //{485 // HObject Hobj;486 // HOperatorSet.GenEmptyObj(out Hobj);487 // try488 // {489 // Rectangle rect = new Rectangle(0,bmp.WIDth,bmp.Height);490 491 // BitmapData srcBmpData = bmp.LockBits(rect,ImageLockMode.Readonly,PixelFormat.Format24bppRgb);492 // HOperatorSet.GenImageInterleaved(out Hobj,srcBmpData.Scan0,"bgr",bmp.Height,"byte",-1,0);493 // bmp.UnlockBits(srcBmpData);494 // }495 // catch (Exception e)496 // {497 498 // }499 // return Hobj;500 //}501 }总结
以上是内存溢出为你收集整理的c# 大华相机采集类全部内容,希望文章能够帮你解决c# 大华相机采集类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)