dalsa-8k线阵网口相机

 

 //初始化相机类
        SapLocation m_ServerLocation;
        SapAcqDevice m_AcqDevice;
        SapBuffer m_Buffers;
        SapAcqDeviceToBuf m_Xfer;

        /// <summary>
        /// 获取相机名和索引
        /// </summary>
        /// <param name="sCameraName"></param>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public bool GetCameraInfo(out string sCameraName, out int nIndex)
        {
            sCameraName = "";
            nIndex = 0;

            int serverCount = SapManager.GetServerCount();
            int GenieIndex = 0;
            System.Collections.ArrayList listServerNames = new System.Collections.ArrayList();

            bool bFind = false;
            string serverName = "";
            for (int serverIndex = 0; serverIndex < serverCount; serverIndex++)
            {
                if (SapManager.GetResourceCount(serverIndex, SapManager.ResourceType.AcqDevice) != 0)
                {
                    serverName = SapManager.GetServerName(serverIndex);
                    listServerNames.Add(serverName);
                    GenieIndex++;
                    bFind = true;
                }
            }

            int count = 1;
            string deviceName = "";
            foreach (string sName in listServerNames)
            {
                deviceName = SapManager.GetResourceName(sName, SapManager.ResourceType.AcqDevice, 0);
                count++;
            }

            sCameraName = serverName;
            nIndex = GenieIndex;

            return bFind;
        }

        /// <summary>
        /// 创建各对象
        /// </summary>
        /// <returns></returns>
        public bool CreateObjects()
        {
            // Create acquisition object
            if (m_AcqDevice != null && !m_AcqDevice.Initialized)
            {
                if (m_AcqDevice.Create() == false)
                {
                    return false;
                }
            }
            // Create buffer object
            if (m_Buffers != null && !m_Buffers.Initialized)
            {
                if (m_Buffers.Create() == false)
                {
                    return false;
                }
                m_Buffers.Clear();
            }


            if (m_Xfer != null && m_Xfer.Pairs[0] != null)
            {
                m_Xfer.Pairs[0].Cycle = SapXferPair.CycleMode.NextWithTrash;
                if (m_Xfer.Pairs[0].Cycle != SapXferPair.CycleMode.NextWithTrash)
                {
                    return false;
                }
            }

            // Create Xfer object
            if (m_Xfer != null && !m_Xfer.Initialized)
            {
                if (m_Xfer.Create() == false)
                {
                    return false;
                }
            }
            return true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string Name;
            int Index;
            bool RTemp = GetCameraInfo(out Name,out Index);
            MessageBox.Show(Name);

            m_ServerLocation = new SapLocation(Name,0);
            m_AcqDevice = new SapAcqDevice(m_ServerLocation, "C:\\Users\\dell\\Desktop\\dalsa111.ccf");//读取配置文件

            if (SapBuffer.IsBufferTypeSupported(m_ServerLocation, SapBuffer.MemoryType.ScatterGather))
                m_Buffers = new SapBufferWithTrash(2, m_AcqDevice, SapBuffer.MemoryType.ScatterGather);
            else
                m_Buffers = new SapBufferWithTrash(2, m_AcqDevice, SapBuffer.MemoryType.ScatterGatherPhysical);
            m_Xfer = new SapAcqDeviceToBuf(m_AcqDevice, m_Buffers);

            m_Xfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
            m_Xfer.XferNotify += new SapXferNotifyHandler(xfer_XferNotify);
            m_Xfer.XferNotifyContext = this;

            if (!CreateObjects())
            {
                MessageBox.Show("失败");
            }
        }

        IntPtr Data;//图像指针

        /// <summary>
        /// 相机自带回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="argsNotify"></param>
        void xfer_XferNotify(object sender, SapXferNotifyEventArgs argsNotify)
        {
            unsafe
            {
                //fixed (byte* dest = mCamerData.ImgBuffer) //保持内存不被回收
                //{
                    m_Buffers.GetAddress(out Data);
                    HObject ImageTemp = null;
                    HOperatorSet.GenImage1(out ImageTemp, "byte", 8192, 2000, Data);//取内存数据,生成图像,halcon实现

                    hWindowControl1.HalconWindow.SetPart(0, 0, 2000, 8192);
                    HOperatorSet.DispObj(ImageTemp,hWindowControl1.HalconWindow);
                   // HOperatorSet.WriteImage(ImageTemp, "bmp", 0, "C:\\Users\\dell\\Desktop\\3");
                //}
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (m_Xfer.Grab())
            {
                MessageBox.Show("开始采集");
            }
        }

以上是网口相机的采集程序,图像采集到内存后,使用halcon中的genimage函数从内存中直接读取并生成图像。

如果是camlink接口相机,主要要修改GetCameraInfo方法和其他地方,具体参照demo。

猜你喜欢

转载自blog.csdn.net/zxf347085420/article/details/90757911