Baumer堡盟相机SDK开发示例

Baumer堡盟相机SDK开发示例(C#调用)

简介
工作中使用到堡盟的彩色相机,在此将调用SDK采集图像相关内容进行整理记录。

开发环境
Windows7
VS2013 64位
halcon12
之所以用到halcon,是因为获取到相机采集的图像后,需要利用halcon图像库进行处理,所以直接将图像转换为halcon库可以处理的格式。

引用库文件
在VS中创建项目,根据自己的环境引用对应的文件。
32-Bit:
\Components\Dev\C_Sharp\Win32\
bgapi2_genicam_dotnet.dll
64-Bit
\Components\Dev
bgapi2_genicam_dotnet.dll

将下列四个文件复制到项目的生产目录中
32-Bit:
\Components\Bin\Win32\目录下
bgapi2_genicam.dll
bgapi2_img.dll
bopfdrvctl.dll
MathParser.dll
64-Bit
\Components\Bin\x64\目录下
bgapi2_genicam.dll
bgapi2_img.dll
bopfdrvctl.dll
MathParser.dll

编写相机类CameraClass

public CameraClass(string id)
{
   this.cameraSN = id;
}
public int connect()//连接相机函数,返回0成功返回-1失败
{
    try
    {
       imgProcessor = BGAPI2.ImageProcessor.Instance;
       if (imgProcessor.NodeList.GetNodePresent("DemosaicingMethod") == true)
       {
          imgProcessor.NodeList["DemosaicingMethod"].Value = "NearestNeighbor"; 
       }
       systemList = BGAPI2.SystemList.Instance;
       systemList.Refresh();//刷新系统列表
       if (systemList.Count == 0)
       {
           //没有找到系统
           return -1;
       }
       foreach (KeyValuePair sys_pair in BGAPI2.SystemList.Instance)
       {
           if (!sys_pair.Value.IsOpen)
           {
               sys_pair.Value.Open();
           }
               sSystemID = sys_pair.Key;
               interfaceList = sys_pair.Value.Interfaces;
               //刷新网卡列表信息
               interfaceList.Refresh(100); // timeout of 100 msec
               foreach (KeyValuePair ifc_pair in interfaceList)
               {
                   if (!ifc_pair.Value.IsOpen)
                   {
                       ifc_pair.Value.Open();
                       sInterfaceID = ifc_pair.Key;
                   }
                        //搜索已经连接到网卡上的所有相机,并刷新设备列表
                        deviceList = ifc_pair.Value.Devices;
                        deviceList.Refresh(100);
                        if (deviceList.Count == 0)
                        {
                            //如果该网卡的设备列表中没有连接相机,则关闭当前网卡连接,在网卡列表中选择下一个网卡
                            ifc_pair.Value.Close();
                            continue;
                        }
                        foreach (KeyValuePair dev_pair in deviceList)
                        {
                        //dev_pair.Value.SerialNumber可返回相机序列号
                            if (cameraSN.Equals(dev_pair.Value.SerialNumber))
                            {
                                dev_pair.Value.Open();
                                sDeviceID = dev_pair.Key;
                                mInterface = interfaceList[sInterfaceID];
                                mDevice = deviceList[dev_pair.Key];
                                sSystemID = sys_pair.Key;
                                mSystem = systemList[sSystemID];
                                mDevice.RemoteNodeList["AcquisitionStop"].Execute();
                                return 0;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
                return -1;
            }
            catch
            {
                return -1;
            }
        }
public int open()//打开相机函数,返回0成功,返回-1失败
{
    if (mDevice == null)
    {
        return -1;
    }
    try
    {
        datastreamList = mDevice.DataStreams;
        datastreamList.Refresh();
        foreach (KeyValuePair dst_pair in datastreamList)
        {
            if (dst_pair.Value.IsOpen == false)
            {
               dst_pair.Value.Open();
            }
            sDataStreamID = dst_pair.Key;
            break;
        }
        if (sDataStreamID == "")
        {
            return -1;
        }
        else
        {
             mDataStream = datastreamList[sDataStreamID];
        }
        bufferList = mDataStream.BufferList;
        bufferList.DiscardAllBuffers();
        for (int i = 0; i < 4; i++)
        {
             mBuffer = new BGAPI2.Buffer();
             bufferList.Add(mBuffer);
        }
        foreach (KeyValuePair buf_pair in bufferList)
        {
             buf_pair.Value.QueueBuffer();
        }
        mDataStream.StartAcquisition();//开启数据流
        //START CAMERA
        mDevice.RemoteNodeList["AcquisitionStart"].Execute();
        return 0;
     }
     catch
     {
       return -11;
     }
  }
public HImage grabImage()//发送软触发并获取转换后图像,成功返回HImage结果,失败返回Null
{
   try
   {
       mDevice.RemoteNodeList["TriggerSoftware"].Execute();//发送软触发命令
       mBufferFilled = null;
       mBufferFilled = mDataStream.GetFilledBuffer(300); // 设置读图超时时间为300ms
       //创建一个堡盟相机内部的图像对象,并由此获取到图像的宽度、高度、格式、数组指针、数组长度等信息
       BGAPI2.Image mTransformImage = null;
       BGAPI2.Image mImage = imgProcessor.CreateImage((uint)mBufferFilled.Width, (uint)mBufferFilled.Height, (string)mBufferFilled.PixelFormat, mBufferFilled.MemPtr, (ulong)mBufferFilled.MemSize);
       double fBytesPerPixel = (double)mImage.NodeList["PixelFormatBytes"].Value;
       if ((string)mImage.PixelFormat.Substring(0, 4) == "Mono")
       {
           //如果采集的图像为黑白图像,则执行下面的代码
           mTransformImage = mImage.TransformImage("Mono8");
           transformImageBufferCopy = new byte[(uint)((uint)mTransformImage.Width * (uint)mTransformImage.Height * 1.0)];
           image = new HImage();
           image.GenImage1("byte", (int)mTransformImage.Width, (int)mTransformImage.Height, mTransformImage.Buffer);
       }
       else
       {
            //如果采集的图像为彩色图像,则执行下面的代码
            mTransformImage = mImage.TransformImage("BGR8");
            transformImageBufferCopy = new byte[(uint)((uint)mTransformImage.Width * (uint)mTransformImage.Height * 3.0)];
            image = new HImage();
            image.GenImageInterleaved(mTransformImage.Buffer, "bgr", (int)mTransformImage.Width, (int)mTransformImage.Height, 0, "byte", (int)mTransformImage.Width, (int)mTransformImage.Height, 0, 0, -1, 0);
        }
        if (mImage != null) mImage.Release();
        if (mTransformImage != null) mTransformImage.Release();
        mBufferFilled.QueueBuffer();
        return image;
     }
     catch
     {
         return null;
     }
  }
}  

实例化相机对象后,通过connect()函数将相机进行连接,利用open()函数打开相机,执行grabImage()函数即可获取采集到的图像。
在grabImage()函数中首先发送了软触发命令,然后再读取图像。所以需要将相机设置为触发模式,触发源为Soft才可以采集到图像。

水平有限,难免有错误和不足之处,恳请批评指正。

你可能感兴趣的:(Baumer,堡盟相机,SDK,C#,halcon)