//抓取图像 public override ImageAll GrabImageAll() { g_MtGrabImage.WaitOne(); ImageAll imageAll = new ImageAll(); UInt32 nDataLen = 0; int waitTime = 1000; try { if (g_BaseParCamera.BlUsingTrigger && TriggerSource_e != TriggerSourceCamera_enum.Software)//使用软触发 { if (ImageAll_Q.Count > 0) { ImageAll im = ImageAll_Q.Peek(); im.BitmapSource = ConvertImageBitmap(PFrameInfo_Bit, PData_Bit); ImageAll_Q.Dequeue();//出站 if (ImageAll_Q.Count > 20)//认为溢出 { ImageAll_Q.Clear(); } im.Time = g_PathDirectory.GetTimeName(); im.TimeShort = g_PathDirectory.GetShortTimeName();//图像抓取短时间 return im; } else { return null; } } else { #if(SDK) IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(g_PBufForDriver, 0); MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX(); //获取一帧,超时时间为1秒,此处单位为ms int nRet = GetOneFrameTimeout(pData, ref nDataLen, g_BufSizeForDriver, ref pFrameInfo, waitTime); imageAll.Ho_Image = ConvertHobject(pFrameInfo, pData); imageAll.BitmapSource = ConvertImageBitmap(pFrameInfo, pData); imageAll.Time = g_PathDirectory.GetTimeName(); imageAll.TimeShort = g_PathDirectory.GetShortTimeName();//图像抓取短时间 return imageAll; } #endif return null; } catch { return null; } finally { g_MtGrabImage.ReleaseMutex(); } }
/// <summary> /// 抓取相机的图像,输出所有的格式 /// </summary> public override ImageAll GrabImageAll() { ImageAll imageAll = new ImageAll(); try { if (!BlOpen || g_BaseParCamera.BlUsingTrigger) //外触发模式不进行All抓取 { return(null); } #if (SDK) if (g_Camera != null && g_Camera.IsOpen) { if (g_BaseParCamera.BlUsingTrigger && g_Camera.StreamGrabber.IsGrabbing) { g_Camera.ExecuteSoftwareTrigger(); g_AutoResetEvent.WaitOne(500); } else { try { g_Camera.StreamGrabber.Start(); g_GrabResult = g_Camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); //The stream grabber is already grabbing.说明取图太快,或者两个线程取图冲突 } } } using (g_GrabResult) { if (g_GrabResult != null && g_GrabResult.GrabSucceeded) { imageAll.Ho_Image = ConvertHobject(g_GrabResult); imageAll.BitmapSource = ConvertImageBitmap(g_GrabResult); PathDirectory p = new PathDirectory(); imageAll.TimeShort = p.GetShortTimeName();//图像抓取短时间 imageAll.Time = p.GetTimeName(); } } g_Camera.StreamGrabber.Stop(); #endif return(imageAll); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(null); } }
/// <summary> /// 抓取相机的图像,输出Halcon格式 /// </summary> public override ImageAll GrabImageHal() { g_MtGrabImage.WaitOne(); ImageAll imageAll = new ImageAll(); try { if (!BlOpen)//如果相机打开失败 { return(null); } #if (SDK) try { SetTrriggerMode();//设置软触发 g_ManagedCameraBase.RetrieveBuffer(g_RawImage); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); try { if (g_BaseParCamera.BlUsingTrigger) //使用软触发 { g_ManagedCameraBase.WriteRegister(0x62C, 0x80000000); //发送软触发指令 } g_ManagedCameraBase.RetrieveBuffer(g_RawImage); } catch { if (g_BaseParCamera.BlUsingTrigger) //使用软触发 { g_ManagedCameraBase.WriteRegister(0x62C, 0x80000000); //发送软触发指令 } g_ManagedCameraBase.RetrieveBuffer(g_RawImage); } } //将图像转换为Halcon格式 imageAll.Ho_Image = ConvertHobject(); imageAll.BitmapSource = null; PathDirectory p = new PathDirectory(); imageAll.Time = p.GetShortTimeName(); #endif return(imageAll); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(null); } finally { g_MtGrabImage.ReleaseMutex(); } }
/// <summary> /// 抓取相机的图像,输出Halcon格式 /// </summary> public override ImageAll GrabImageHal() { ImageAll imageAll = new ImageAll(); try { if (!BlOpen) { return(null); } #if (SDK) //★★★ 确保相机当前是采集状态 && 确保相机可以接收软件触发指令 if (g_Camera != null && g_Camera.IsOpen) { if (g_BaseParCamera.BlUsingTrigger)//使用软触发 { if (ImageAll_Q.Count > 0) { ImageAll im = ImageAll_Q.Peek(); ImageAll_Q.Dequeue(); //出站 if (ImageAll_Q.Count > 20) //认为溢出 { ImageAll_Q.Clear(); Log.L_I.WriteError(NameClass, "图像外触发溢出"); } return(im); } else { return(null); } } else { try { g_Camera.StreamGrabber.Start(); g_GrabResult = g_Camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); } } } using (g_GrabResult) { if (g_GrabResult != null && g_GrabResult.GrabSucceeded) { imageAll.Ho_Image = ConvertHobject(g_GrabResult); imageAll.BitmapSource = null; PathDirectory p = new PathDirectory(); imageAll.Time = p.GetTimeName(); imageAll.TimeShort = p.GetShortTimeName(); } } g_Camera.StreamGrabber.Stop(); #endif return(imageAll); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(null); } }