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

相关推荐
mudtools5 小时前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz11 小时前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫13 小时前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务1 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther1 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间1 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec1 天前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
Tiger_shl1 天前
【层面一】C#语言基础和核心语法-02(反射/委托/事件)
开发语言·c#
mudtools1 天前
.NET驾驭Word之力:COM组件二次开发全攻略之连接Word与创建你的第一个自动化文档
后端·c#
王维志1 天前
LiteDB详解
数据库·后端·mongodb·sqlite·c#·json·database