首頁 > 軟體

C#呼叫海康工業相機SDK採集影象並在Halcon視窗中顯示方式

2023-02-27 06:00:38

呼叫海康工業相機SDK採集影象並在Halcon視窗中顯示

最近做專案需要對海康相機進行二次開發,現將所學進行整理。

開發環境    VS2012+C#(32位元)  Halcon12

參照動態連結庫

參照Halcon動態連結庫(halcondotnet.dll)

參照海康相機動態連結庫(MvCameraControl.Net.dll)這個檔案在MVS安裝目錄下MVSDevelopmentDotNet中,如果你是32位元的開發環境就選擇win32資料夾下的,如果是64位元就選擇win64資料夾下的

建立相機類

先在建立的類中編寫方法,之後範例化相機類,呼叫類中的方法。

滑鼠右鍵單擊工程專案–新增–類,選擇“類”,輸入類的名稱,例如Hikvision,點選右下角的“新增”。

首先,要引入的名稱空間:using HalconDotNet;     using MvCamCtrl.NET;

其次,需要用到的全部變數:

private MyCamera m_pMyCamera;
MyCamera.MV_CC_DEVICE_INFO_LIST m_pDeviceList;//裝置列表
private MyCamera.MVCC_INTVALUE stParam;//用於接收特定的引數
//為讀取、儲存影象建立的陣列
UInt32 m_nBufSizeForDriver = 3072 * 2048 * 3;
byte[] m_pBufForDriver = new byte[3072 * 2048 * 3];
UInt32 m_nBufSizeForSaveImage = 3072 * 2048 * 3 * 3 + 2048;
byte[] m_pBufForSaveImage = new byte[3072 * 2048 * 3 * 3 + 2048];
//要轉成的Halcon影象
HImage image = new HImage();

(1) 查詢裝置列表

        //查詢裝置
        public void DeviceListAcq()
        {
            int nRet;
            // ch:建立裝置列表 en:Create Device List
            System.GC.Collect();
            cbDeviceList.Items.Clear();
            nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);
            if (0 != nRet)
            {
                MessageBox.Show("查詢裝置失敗!");
                return;
            }
 
            // ch:在表單列表中顯示裝置名 | en:Display device name in the form list
            for (int i = 0; i < m_pDeviceList.nDeviceNum; i++)
            {
                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
                if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                    if (gigeInfo.chUserDefinedName != "")
                    {
                        cbDeviceList.Items.Add("GigE: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        cbDeviceList.Items.Add("GigE: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                }
                else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
                    MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));
                    if (usbInfo.chUserDefinedName != "")
                    {
                        cbDeviceList.Items.Add("USB: " + usbInfo.chUserDefinedName + " (" + usbInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        cbDeviceList.Items.Add("USB: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")");
                    }
                }
            }
 
            // ch:選擇第一項 | en:Select the first item
            if (m_pDeviceList.nDeviceNum != 0)
            {
                cbDeviceList.SelectedIndex = 0;
            }
        }

(2) 開啟裝置

        //開啟裝置
        public void OpenDevice()
        {
            if (m_pDeviceList.nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
            {
                MessageBox.Show("未發現裝置,請選擇");
                return;
            }
            int nRet = -1;
 
            // ch:獲取選擇的裝置資訊 | en:Get selected device information
            MyCamera.MV_CC_DEVICE_INFO device =
                (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[cbDeviceList.SelectedIndex],
                                                              typeof(MyCamera.MV_CC_DEVICE_INFO));
 
            // ch:開啟裝置 | en:Open device
            if (null == m_pMyCamera)
            {
                m_pMyCamera = new MyCamera();
                if (null == m_pMyCamera)
                {
                    return;
                }
            }
 
            nRet = m_pMyCamera.MV_CC_CreateDevice_NET(ref device);
            if (MyCamera.MV_OK != nRet)
            {
                return;
            }
 
            nRet = m_pMyCamera.MV_CC_OpenDevice_NET();
            if (MyCamera.MV_OK != nRet)
            {
                m_pMyCamera.MV_CC_DestroyDevice_NET();
                MessageBox.Show("裝置開啟失敗");
                //ShowErrorMsg("Device open fail!", nRet);
                return;
            }
 
            // ch:探測網路最佳包大小(只對GigE相機有效) | en:Detection network optimal package size(It only works for the GigE camera)
            if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
            {
                int nPacketSize = m_pMyCamera.MV_CC_GetOptimalPacketSize_NET();
                if (nPacketSize > 0)
                {
                    nRet = m_pMyCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
                    if (nRet != MyCamera.MV_OK)
                    {
                        Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet);
                    }
                }
                else
                {
                    Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize);
                }
            }
 
            // ch:設定採集連續模式 | en:Set Continues Aquisition Mode
            m_pMyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", 2);// ch:工作在連續模式 | en:Acquisition On Continuous Mode
            m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0);    // ch:連續模式 | en:Continuous
        }

(3) 連續採集

        //連續採集(也就是實時顯示)
        public void ContinuesGrab(PictureBox picBox)
        {
            int nRet;
 
            // ch:開始採集 | en:Start Grabbing
            nRet = m_pMyCamera.MV_CC_StartGrabbing_NET();
            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("採集失敗!");
                //ShowErrorMsg("Trigger Fail!", nRet);
                return;
            }
 
            //實時採集
            m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0);
 
            // ch:顯示 | en:Display   在PictureBox控制元件中顯示
            nRet = m_pMyCamera.MV_CC_Display_NET(picBox.Handle);
            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("顯示失敗!");
            }
        }

(4) 停止採集

        //停止採集
        public void StopGrab()
        {
            int nRet = -1;
            // ch:停止採集 | en:Stop Grabbing
            nRet = m_pMyCamera.MV_CC_StopGrabbing_NET();
            if (nRet != MyCamera.MV_OK)
            {
                MessageBox.Show("停止採集失敗!");
            }
 
        }

(5) 關閉裝置

        //關閉裝置
        public void CloseDevice()
        {
            // ch:關閉裝置 | en:Close Device
            int nRet;
 
            nRet = m_pMyCamera.MV_CC_CloseDevice_NET();
            if (MyCamera.MV_OK != nRet)
            {
                return;
            }
 
            nRet = m_pMyCamera.MV_CC_DestroyDevice_NET();
            if (MyCamera.MV_OK != nRet)
            {
                return;
            }
        }

(6) 轉成Halcon影象

        //讀取圖片轉換成Halcon影象
        public HImage ReadImage()
        {
            int nRet;
            //MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
            UInt32 nPayloadSize = 0;
            nRet = m_pMyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                return null;
            }
            nPayloadSize = stParam.nCurValue;
            if (nPayloadSize > m_nBufSizeForDriver)
            {
                m_nBufSizeForDriver = nPayloadSize;
                m_pBufForDriver = new byte[m_nBufSizeForDriver];
                m_nBufSizeForSaveImage = m_nBufSizeForDriver * 3 + 2048;
                m_pBufForSaveImage = new byte[m_nBufSizeForSaveImage];
            }
 
            IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForDriver, 0);
            MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
            nRet = m_pMyCamera.MV_CC_GetOneFrameTimeout_NET(pData, m_nBufSizeForDriver, ref stFrameInfo, 1000);//獲取一幀影象,超時時間設定為1000
            if (MyCamera.MV_OK != nRet)
            {
                return null;
            }
 
            HImage image = new HImage();
 
            //採集的是黑白影象,利用Halcon影象庫中的GenImage1運算元來構建影象
            image.GenImage1("byte", (int)stFrameInfo.nWidth, (int)stFrameInfo.nHeight, pData);
            return image;
        }

下面是我所做的一個表單介面,顯示裝置序列號的下拉選單做成了一個使用者控制元件,將上述的程式碼全部放在使用者控制元件中,在主表單裡呼叫使用者控制元件裡的方法來實現相機的連線和採集

具體見範例程式碼。

範例程式碼:

        //查詢裝置
        private void btnEnum_Click(object sender, EventArgs e)
        {
            ucDeviceList1.DeviceListAcq();
        }
 
        //開啟裝置
        private void btnOpen_Click(object sender, EventArgs e)
        {
            ucDeviceList1.OpenDevice();
            btnOpen.Enabled = false;
            btnClose.Enabled = true;
 
            btnContinuesGrab.Enabled = true;
            //btnSingleStep.Enabled = true;
            btnStopGrab.Enabled = true;
        }
 
        //關閉裝置
        private void btnClose_Click(object sender, EventArgs e)
        {
            ucDeviceList1.CloseDevice();
 
            btnOpen.Enabled = true;
            btnClose.Enabled = false;
 
            btnContinuesGrab.Enabled = false;
            btnSingleStep.Enabled = false;
            btnStopGrab.Enabled = false;
        }
 
        //實時顯示
        private void btnContinuesGrab_Click(object sender, EventArgs e)
        {
            ucDeviceList1.ContinuesGrab(picboxShowImg);
            btnContinuesGrab.Enabled = false;
            btnSingleStep.Enabled = true;
            btnStopGrab.Enabled = true;
        }
 
        //單步採集
        private void btnSingleStep_Click(object sender, EventArgs e)
        {
            // 將採集到的影象在Halcon視窗中顯示
            HTuple hWind = hWindowControl1.HalconWindow;
 
            HTuple width, height;
 
            HObject hv_image;
            HOperatorSet.GenEmptyObj(out hv_image);
            hv_image.Dispose();
            hv_image = ucDeviceList1.ReadImage();
 
            HOperatorSet.GetImageSize(hv_image, out width, out height);
            HOperatorSet.SetPart(hWind, 0, 0, height - 1, width - 1);
            HOperatorSet.DispObj(img, hWind);
 
            btnStopGrab.Enabled = true;
         }
 
        //停止採集
        private void btnStopGrab_Click(object sender, EventArgs e)
        {
            ucDeviceList1.StopGrab();
            btnContinuesGrab.Enabled = true;
            btnStopGrab.Enabled = false;
        }
 
        //關閉裝置
        private void btnClose_Click(object sender, EventArgs e)
        {
            ucDeviceList1.CloseDevice();
 
            btnOpen.Enabled = true;
            btnClose.Enabled = false;
 
            btnContinuesGrab.Enabled = false;
            btnSingleStep.Enabled = false;
            btnStopGrab.Enabled = false;
        }

注:上述方法是不需要修改相機IP的,但是要提前用MVS軟體修改相機的IP

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com