winfrom大恒工业相机SDK二次开发、C#联合halcon开发

一、开发环境

1.在大恒图像官网下载SDK安装包,安装SDK后,打开安装目录找到Samples文件夹,然后找到Samples\CSharp SDK\x64\DoNET\.NET4.0文件夹下找到GxIAPINET.dll,如图:

2.打开VS2019软件,建立winfrom项目,引用GxIAPINET.dll到项目,如下图:

3.将相机连接到PC的USB3.0接口

二、二次开发类

开发代码:相机连接类SvDaHengUSB3包含连接相机、关闭相机连接、修改相机配置和相机采集几个方法,代码如下:

using HalconDotNet;

using GxIAPINET;

复制代码
 /// <summary>
    /// 大恒图像工业相机
    /// </summary>
    public class SvDaHengUSB3
    {     
        IGXFactory U3_IGXFactory = null;//<Factory对像   
        List<IGXDeviceInfo> U3_IGXDeviceInfo = new List<IGXDeviceInfo>();
        IGXDevice Cam = null;
        IGXStream Cam_Stream = null;  //<流对像
        IGXFeatureControl Cam_RemoteControl = null;    //<远端设备属性控制器对像  
        IGXFeatureControl Cam_Control = null;  //<流层属性控制器对象
        /// <summary>
        /// 连接相机
        /// </summary>
        public override void Initial()
        {
            base.Initial();        
            try
            {
                U3_IGXFactory = IGXFactory.GetInstance();
                U3_IGXFactory.Init();
                U3_IGXDeviceInfo.Clear();
                if (null != U3_IGXFactory)
                {
                    U3_IGXFactory.UpdateDeviceList(200, U3_IGXDeviceInfo);
                }                                       
                //通过SN打开设备
                //GX_ACCESS_CONTROL以控制的方式打开相机
                //GX_ACCESS_EXCLUSIVE以独占的方式发开相机
                //GX_ACCESS_READONLY以只读的方式打开相
                Cam = U3_IGXFactory.OpenDeviceBySN(SN, GX_ACCESS_MODE.GX_ACCESS_EXCLUSIVE);
                //获取流通道个数
                uint stream_num = Cam.GetStreamCount();
                //打开设备的流
                Cam_Stream = Cam.OpenStream(0);
                //获取远端设备属性控制器
                Cam_RemoteControl = Cam.GetRemoteFeatureControl();
                //获取本地属性控制器
                Cam_Control = Cam.GetFeatureControl();
                关闭设备
                //Cam.Close();

                if (Cam == null)
                {
                    throw new Exception($"相機{SN}連接失敗");
                }
                //if (Colorful == "黑白")
                //{
                //    Cam_RemoteControl.GetEnumFeature("PixelFormat").SetValue("Mono8");//像素格式
                //    result = 1;
                //}
                //else
                //{
                //    Cam_RemoteControl.GetEnumFeature("PixelFormat").SetValue("BayerBG8");//像素格式
                //    Cam_RemoteControl.GetEnumFeature("BalanceWhiteAuto").SetValue("Off");//自动白平衡
                //    Cam_RemoteControl.GetEnumFeature("BalanceRatioSelector").SetValue("Red");//自动白平衡通道
                //    Cam_RemoteControl.GetEnumFeature("BalanceRatio").SetValue("2f");//白平衡系数
                //    Cam_RemoteControl.GetEnumFeature("BalanceRatioSelector").SetValue("Green");
                //    Cam_RemoteControl.GetEnumFeature("BalanceRatio").SetValue("1f");
                //    Cam_RemoteControl.GetEnumFeature("BalanceRatioSelector").SetValue("Blue");
                //    Cam_RemoteControl.GetEnumFeature("BalanceRatio").SetValue("2f");
                //    result = 1;
                //}

                //Cam_RemoteControl.GetEnumFeature("TriggerMode").SetValue("On");//触发模式
                //Cam_RemoteControl.GetEnumFeature("TriggerSource").SetValue("Software");//触发源
                //Cam_RemoteControl.GetEnumFeature("ExposureAuto").SetValue("Off");//自动曝光
                //Cam_RemoteControl.GetEnumFeature("GainAuto").SetValue("Off");//自动增益
                //Cam_RemoteControl.GetFloatFeature("ExposureTime").SetValue((float)Exposure);//曝光
                //Cam_RemoteControl.GetFloatFeature("Gain").SetValue((float)Gain);//增益                              

                //if (result <= 0)
                //{
                //    throw new Exception($"相機{SN}開啟失敗");
                //}

                Thread.Sleep(500);
                Snap();
            }
            catch (Exception ex)
            {
                SvMaster.Log.WriteError(ex);
            }
        }
        //关闭相机
        public override void Close()
        {          
            try
            {
                // 如果未停采则先停止采集
                if (null != Cam_RemoteControl)
                {
                    Cam_RemoteControl.GetCommandFeature("AcquisitionStop").Execute();
                    Cam_RemoteControl = null;
                }

                //停止流通道、注销采集回调和关闭流
                if (null != Cam_Stream)
                {
                    Cam_Stream.StopGrab();
                    //注销采集回调函数
                    Cam_Stream.UnregisterCaptureCallback();
                    Cam_Stream.Close();
                    Cam_Stream = null;
                    Cam_Control = null;
                }
                //关闭设备
                if (null != Cam)
                {
                    Cam.Close();
                    Cam = null;                   
                }
            }
            catch (Exception ex)
            {
                SvMaster.Log.WriteError(ex);
            }           
        }
        //修改相机配置
        public override void AdaptCamera()
        {
            try
            {
                Cam_RemoteControl.GetFloatFeature("ExposureTime").SetValue((float)Exposure);//曝光
                Cam_RemoteControl.GetFloatFeature("Gain").SetValue((float)Gain);//增益               
            }
            catch (Exception ex)
            {
                SvMaster.Log.WriteError(ex);
            }
        }      
        //相机采集
        public override HObject Snap()
        {
            HObject image = null;
            try
            {           
                //单采集
                Cam_Stream.StartGrab();
                Cam_RemoteControl.GetCommandFeature("AcquisitionStart").Execute();
                IImageData images = Cam_Stream.GetImage(1000);//超时ms               
                if (images.GetStatus() == GX_FRAME_STATUS_LIST.GX_FRAME_STATUS_SUCCESS)
                {
                    HOperatorSet.GenImage1(out image, "byte", images.GetWidth(), images.GetHeight(), images.GetBuffer());
                    image = BayerToRgb(image, "bayer_bg");
                    image = RotateImage(image);
                    //获取到完整帧图像进行图像处理
                    images.Destroy();
                }
                else
                {
                    Cam_RemoteControl.GetCommandFeature("AcquisitionStop").Execute();
                    Cam_RemoteControl = null;
                    Cam_Stream.StopGrab();
                    //注销采集回调函数
                    Cam_Stream.UnregisterCaptureCallback();
                    Cam_Stream.Close();
                    Cam_Stream = null;
                    Cam_Control = null;
                    Cam.Close();
                    Cam = null;
                    SvMaster.Log.WriteError($"相機{SN}取像超时,重新连接");
                }
            }
            catch (Exception ex)
            {
                SvMaster.Log.WriteError(ex);
            }          
            return image;
        }    
    }

说明:代码中SN、Exposure、Gain为外界设置的值

参考文献:WIN大恒工业相机SDK开发-CSDN博客

大恒相机sdk二次开发 _c#从0开始新建winform窗体实现相机基础采集功能_大恒相机康耐视开发实例-CSDN博客

相关推荐
lingxiao1688821 分钟前
WebApi详解+Unity注入--下篇:Unity注入
unity·c#·wpf
格林威1 小时前
传送带上运动模糊图像复原:提升动态成像清晰度的 6 个核心方案,附 OpenCV+Halcon 实战代码!
人工智能·opencv·机器学习·计算机视觉·ai·halcon·工业相机
lingxiao168883 小时前
WebApi详解+Unity注入--中篇:.net core的WebAPI
unity·c#·.netcore
ServBay3 小时前
C# 成为 2025 年的编程语言,7个C#技巧助力开发效率
后端·c#·.net
故事不长丨7 小时前
C#进制转换:从基础原理到实战应用
开发语言·c#·进制转换·16进制·2进制·10进制
liulilittle7 小时前
VEthernet 框架实现 tun2socks 的技术原理
网络·windows·c#·信息与通信·通信
云草桑8 小时前
.net AI API应用 客户发的信息提取对接上下游系统报价
ai·c#·.net·semantickernel·sk
故事不长丨9 小时前
C#File文件操作全解析:从基础用法到异常处理
服务器·开发语言·visualstudio·c#·文件操作·io流·file
工程师00710 小时前
C# 动态编程(基于 dynamic 类型)
开发语言·c#·dynamic·动态编程
用户2986985301410 小时前
C#: 在Word文档中添加或移除可编辑区域
后端·c#