一、开发环境

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;

 /// /// 大恒图像工业相机/// public class SvDaHengUSB3{ IGXFactory U3_IGXFactory = null;//<Factory对像 List U3_IGXDeviceInfo = new List();IGXDevice Cam = null;IGXStream Cam_Stream = null;//<流对像IGXFeatureControl Cam_RemoteControl = null;//<远端设备属性控制器对像IGXFeatureControl Cam_Control = null;//<流层属性控制器对象/// /// 连接相机/// 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博客