大恒相机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;
            }
        }
        
    }
}
相关推荐
lrlianmengba13 小时前
推荐一款可视化和检查原始数据的工具:RawDigger
人工智能·数码相机·计算机视觉
CV-X.WANG17 小时前
【详细 工程向】基于Smart3D的五镜头相机三维重建
数码相机·3d
小负不负1 天前
使用kalibr_calibration标定相机(realsense)和imu(h7min)
数码相机·opencv·计算机视觉
xm一点不soso2 天前
树莓派基本设置--10.使用MIPI摄像头
人工智能·数码相机·计算机视觉
古月居GYH2 天前
【图像与点云融合教程(五)】海康相机 ROS2 多机分布式实时通信功能包
分布式·数码相机
顾北川_野3 天前
Android 解决MTK相机前摄镜像问题
数码相机
创小董6 天前
履带式排爆演习训练机器人技术详解
数码相机·机器人
shuxianshrng7 天前
违停拍照和闯红灯拍照有什么区别吗
人工智能·数码相机·计算机视觉·视觉检测
图灵追慕者7 天前
机器视觉中光源镜头和相机的关系
数码相机·相机·机器视觉·光源·镜头
博图光电7 天前
UV紫外相机
数码相机·uv·紫外相机