大恒相机SDK开发

前言

大恒相机sdk适用于大恒相机网口与USB使用,并将采集数据转化为Halcon对象。

程序代码

csharp 复制代码
    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处理模式为OldestFirst
                        m_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;
            }
        }
        
    }
}
相关推荐
luoganttcc11 小时前
BEV感知中如何使用相机内外参?
数码相机
SKYDROID云卓小助手16 小时前
三轴云台之相机技术篇
运维·服务器·网络·数码相机·音视频
越甲八千2 天前
相机的曝光和增益
数码相机
越甲八千2 天前
黑白彩色相机成像原理
数码相机
幻想趾于现实2 天前
机器视觉调试——现场链接相机(解决各种相机链接问题)
数码相机·工业相机
a3158238062 天前
SnapdragonCamera骁龙相机源码解析
android·数码相机·framework·高通
越甲八千2 天前
全局曝光与卷帘曝光
数码相机
博图光电2 天前
短波红外相机应用领域介绍
数码相机
中达瑞和-高光谱·多光谱2 天前
多光谱相机在农业中的应用(农作物长势、病虫害、耕地检测等)
数码相机
千野竹之卫3 天前
2025最新云渲染网渲100渲染农场使用方法,渲染100邀请码1a12
开发语言·前端·javascript·数码相机·3d·3dsmax