本文主要是介绍大恒相机SDK开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
大恒相机sdk适用于大恒相机网口与USB使用,并将采集数据转化为Halcon对象。
程序代码
internal class DaHeng_SDK{IGXDevice m_objIGXDevice = null; ///<设备对像IGXStream m_objIGXStream = null; ///<流对像IGXFactory m_objIGXFactory = null; ///<Factory对像IGXFeatureControl m_objIGXFeatureControl = null; ///<远端设备属性控制器对像IGXFeatureControl m_objIGXStreamFeatureControl = null; ///<流层属性控制器对象List<IGXDeviceInfo> listGXDeviceInfo = null;bool m_bIsColor = false;public delegate void delegateProcessHImage(HObject Image);public event delegateProcessHImage EventProcessImage;/// <summary>/// 查找相机/// </summary>/// <returns>输出所连接的相机列表</returns>public List<string> FindCamer(){try{m_objIGXFactory = IGXFactory.GetInstance();m_objIGXFactory.Init();List<string> CamerName = new List<string>();CloseCamerStream();CloseCamer();listGXDeviceInfo = new List<IGXDeviceInfo>();m_objIGXFactory.UpdateDeviceList(200, listGXDeviceInfo);m_objIGXFactory.Init();for (int i = 0; i < listGXDeviceInfo.Count; i++){CamerName.Add(listGXDeviceInfo[i].GetSN());}return CamerName;}catch (Exception ex){MessageBox.Show(ex.ToString());return null;}}/// <summary>/// 开启相机/// </summary>/// <param name="输入相机序列号开启"></param>/// <returns>开启失败返回false</returns>public bool OpenCamer(string CamerName){try{// 判断当前连接设备个数if (listGXDeviceInfo.Count <= 0){return false;}// 如果设备已经打开则关闭,保证相机在初始化出错情况下能再次打开if (null != m_objIGXDevice){m_objIGXDevice.Close();m_objIGXDevice = null;}//打开列表指定设备for (int i = 0; i < listGXDeviceInfo.Count; i++){if (CamerName== listGXDeviceInfo[i].GetSN()){m_objIGXDevice = m_objIGXFactory.OpenDeviceBySN(listGXDeviceInfo[i].GetSN(), GX_ACCESS_MODE.GX_ACCESS_EXCLUSIVE);m_objIGXFeatureControl = m_objIGXDevice.GetRemoteFeatureControl();}}//打开流if (null != m_objIGXDevice){m_objIGXStream = m_objIGXDevice.OpenStream(0);m_objIGXStreamFeatureControl = m_objIGXStream.GetFeatureControl();}// 建议用户在打开网络相机之后,根据当前网络环境设置相机的流通道包长值,// 以提高网络相机的采集性能,设置方法参考以下代码。GX_DEVICE_CLASS_LIST objDeviceClass = m_objIGXDevice.GetDeviceInfo().GetDeviceClass();if (GX_DEVICE_CLASS_LIST.GX_DEVICE_CLASS_GEV == objDeviceClass){// 判断设备是否支持流通道数据包功能if (true == m_objIGXFeatureControl.IsImplemented("GevSCPSPacketSize")){// 获取当前网络环境的最优包长值uint nPacketSize = m_objIGXStream.GetOptimalPacketSize();// 将最优包长值设置为当前设备的流通道包长值m_objIGXFeatureControl.GetIntFeature("GevSCPSPacketSize").SetValue(nPacketSize);}}//注册相机回调函数m_objIGXStream.RegisterCaptureCallback(this, __CaptureCallbackPro);__IsSupportColor(ref m_bIsColor);return true;}catch (Exception ex){MessageBox.Show(ex.Message);return false;}}/// <summary>/// 单步采集图像/// </summary>/// <returns>采集失败返回false</returns>public bool OneGrap(){try{if (null != m_objIGXStream){//清空相机数据,防止卡帧m_objIGXStream.FlushQueue();}//发送软触发命令if (null != m_objIGXFeatureControl){//设置触发命令开启m_objIGXFeatureControl.GetEnumFeature("TriggerMode").SetValue("On");//发送软触发命令m_objIGXFeatureControl.GetCommandFeature("TriggerSoftware").Execute();}return true;}catch (Exception ex){MessageBox.Show(ex.ToString());return false;}}//连续触发public bool ContinuousGrap(){//发送开采命令if (null != m_objIGXFeatureControl){m_objIGXFeatureControl.GetEnumFeature("TriggerMode").SetValue("Off");//设置采集模式连续采集m_objIGXFeatureControl.GetEnumFeature("AcquisitionMode").SetValue("Continuous");}return true;}/// <summary>/// 停止采集相机/// </summary>/// <returns>停止采集失败返回false</returns>public bool CloseCamerStream(){//关闭采集流通道if (null != m_objIGXStream){m_objIGXStream.StopGrab();}return true ;}/// <summary>/// 关闭相机/// </summary>/// <returns>关闭失败返回false</returns>public bool CloseCamer(){try{//停止流通道、注销采集回调和关闭流if (null != m_objIGXStream){m_objIGXStream.StopGrab();//注销采集回调函数m_objIGXStream.UnregisterCaptureCallback();m_objIGXStream.Close();m_objIGXStream = null;m_objIGXStreamFeatureControl = null;}//关闭设备if (null != m_objIGXDevice){listGXDeviceInfo = null;m_objIGXFeatureControl = null;m_objIGXDevice.Close();m_objIGXFactory = null;m_objIGXDevice = null;}return true;}catch (Exception ex){MessageBox.Show(ex.ToString());return false;}}/// <summary>/// 获取相机设置/// </summary>/// <param name="ExposureTime">曝光时间</param>/// <param name="Gain">增益</param>/// <returns>获取失败返回false</returns>public bool GetCamerSet(out int ExposureTime, out int Gain){ExposureTime = 0; Gain=0;try{ExposureTime = int.Parse(m_objIGXFeatureControl.GetFloatFeature("ExposureTime").GetValue().ToString());Gain = int.Parse(m_objIGXFeatureControl.GetFloatFeature("Gain").GetValue().ToString());return true;}catch (Exception){return false;}}/// <summary>/// 设置相机参数/// </summary>/// <param name="ExposureTime">曝光时间</param>/// <param name="Gain">增益</param>/// <returns>获取失败返回false</returns>public bool CamerSet(int ExposureTime,int Gain){try{m_objIGXFeatureControl.GetFloatFeature("ExposureTime").SetValue(ExposureTime);m_objIGXFeatureControl.GetFloatFeature("Gain").SetValue(Gain);return true;}catch (Exception){return false;}}/// <summary>/// 图像回调函数/// </summary>/// <param name="objUserParam"></param>/// <param name="objIFrameData">回调结果参数</param>private void __CaptureCallbackPro(object objUserParam, IFrameData objIFrameData){try{HObject hPylonImage = new HObject();HOperatorSet.GenEmptyObj(out hPylonImage);Task.Run(() =>{if (m_bIsColor){//彩色图像HOperatorSet.GenImageInterleaved(out hPylonImage, (HTuple)objIFrameData.GetBuffer(), (HTuple)"rgb", (HTuple)objIFrameData.GetWidth(), (HTuple)objIFrameData.GetHeight(), -1, "byte", 0, 0, 0, 0, -1, 0);}else{//灰度图像HOperatorSet.GenImage1Extern(out hPylonImage, "byte", (HTuple)objIFrameData.GetWidth(), (HTuple)objIFrameData.GetHeight(), (HTuple)objIFrameData.GetBuffer(), IntPtr.Zero);}EventProcessImage(hPylonImage);});}catch (Exception ex){MessageBox.Show(ex.ToString());}}/// <summary>/// 开启相机采集/// </summary>/// <returns>开启失败返回false</returns>public bool OpenCamerSteam(){try{if (null != m_objIGXStreamFeatureControl){try{//设置流层Buffer处理模式为OldestFirstm_objIGXStreamFeatureControl.GetEnumFeature("StreamBufferHandlingMode").SetValue("OldestFirst");}catch (Exception){}}//开启采集流通道if (null != m_objIGXStream){m_objIGXStream.StartGrab();}//发送开采命令if (null != m_objIGXFeatureControl){//设置采集模式连续采集m_objIGXFeatureControl.GetEnumFeature("AcquisitionMode").SetValue("Continuous");//设置触发模式为开m_objIGXFeatureControl.GetEnumFeature("TriggerMode").SetValue("On");//选择触发源为软触发m_objIGXFeatureControl.GetEnumFeature("TriggerSource").SetValue("Software");m_objIGXFeatureControl.GetCommandFeature("AcquisitionStart").Execute();}return true;}catch (Exception ex){MessageBox.Show(ex.ToString());return false;}}/// <summary>/// 判断当前采集数据是否为彩色图像/// </summary>/// <param name="bIsColorFilter"></param>private void __IsSupportColor(ref bool bIsColorFilter){bool bIsImplemented = false;bool bIsMono = false;string strPixelFormat = "";strPixelFormat = m_objIGXDevice.GetRemoteFeatureControl().GetEnumFeature("PixelFormat").GetValue();if (0 == string.Compare(strPixelFormat, 0, "Mono", 0, 4)){bIsMono = true;}else{bIsMono = false;}bIsImplemented = m_objIGXDevice.GetRemoteFeatureControl().IsImplemented("PixelColorFilter");// 若当前为非黑白且支持PixelColorFilter则为彩色if ((!bIsMono) && (bIsImplemented)){bIsColorFilter = true;}else{bIsColorFilter = false;}}}
}
这篇关于大恒相机SDK开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!