大恒相机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;
            }
        }
        
    }
}
相关推荐
CES_Asia1 天前
工信部“人工智能+”制造行动点亮CES Asia 2025
人工智能·科技·数码相机·制造·智能音箱·智能手表
只待花开2 天前
ROS2 python编写 intel realsense D405相机节点通过launch.py启动多个相机并发送图像话题,基于pyrealsense2库
数码相机
KeyPan2 天前
【ORB-SLAM3:相机针孔模型和相机K8模型】
数码相机
千穹凌帝2 天前
基于深度学习多图像融合的屏幕缺陷检测方案
人工智能·深度学习·数码相机
传说故事3 天前
相机内外参知识
数码相机·相机·相机参数
妄想出头的工业炼药师3 天前
imu相机EKF
数码相机
合方圆~小文3 天前
工业摄像机基于电荷耦合器件的相机
人工智能·深度学习·数码相机·目标检测
资源补给站4 天前
大恒相机开发(1)—Python调用采集彩色图像并另存为本地
开发语言·python·数码相机
OAK中国_官方4 天前
四相机设计实现全向视觉感知的开源空中机器人无人机
数码相机·机器人·无人机
s_daqing4 天前
解锁BL后的K40降级
数码相机