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博客

相关推荐
界面开发小八哥7 分钟前
界面控件DevExpress WinForms中文帮助文档 - 入门指南
ai·c#·.net·devexpress·ui开发·winforms
清风与日月41 分钟前
Yitter.IdGenerator:高性能分布式唯一ID生成器详解
分布式·c#·.net·.netcore
影寂ldy2 小时前
C# WinForm TCP-Socket
网络·tcp/ip·c#
czhc11400756632 小时前
2026-08-25 博客:现场排查的 8 个坑
c#
Z5998178413 小时前
c#软件开发学习笔记--WPF(控件、样式)
笔记·学习·c#
格林威3 小时前
C#高位深相机图像保存:12位16位图像保存方案,OpenCvSharp和Halcon实现
开发语言·人工智能·数码相机·计算机视觉·c#·视觉检测·工业相机
雪隐3 小时前
WPF + MVVM 实战系列03-MVVM 的边界之争,终于有人画了条“差不多”的线
c#
影寂ldy3 小时前
C# WinForm TCP聊天室(服务端+客户端)
windows·tcp/ip·c#
七仔啊4 小时前
C#、.NET 、ASP.NET Core、Visual Studio全系列快速入门
c#·asp.net·.net
李高钢16 小时前
REST vs gRPC 选型对比与迁移实践:从原理到落地
c#·集成·winforms