溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#如何調(diào)用SDK采集圖像并在Halcon窗口中顯示

發(fā)布時(shí)間:2023-02-27 09:45:22 來(lái)源:億速云 閱讀:265 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“C#如何調(diào)用SDK采集圖像并在Halcon窗口中顯示”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C#如何調(diào)用SDK采集圖像并在Halcon窗口中顯示”吧!

開(kāi)發(fā)環(huán)境    VS2012+C#(32位)  Halcon12

引用動(dòng)態(tài)鏈接庫(kù)

引用Halcon動(dòng)態(tài)鏈接庫(kù)(halcondotnet.dll)

引用海康相機(jī)動(dòng)態(tài)鏈接庫(kù)(MvCameraControl.Net.dll)這個(gè)文件在MVS安裝目錄下MVS\Development\DotNet中,如果你是32位的開(kāi)發(fā)環(huán)境就選擇win32文件夾下的,如果是64位就選擇win64文件夾下的

C#如何調(diào)用SDK采集圖像并在Halcon窗口中顯示

創(chuàng)建相機(jī)類

先在創(chuàng)建的類中編寫方法,之后實(shí)例化相機(jī)類,調(diào)用類中的方法。

鼠標(biāo)右鍵單擊工程項(xiàng)目–添加–類,選擇“類”,輸入類的名稱,例如Hikvision,點(diǎn)擊右下角的“添加”。

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

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

private MyCamera m_pMyCamera;
MyCamera.MV_CC_DEVICE_INFO_LIST m_pDeviceList;//設(shè)備列表
private MyCamera.MVCC_INTVALUE stParam;//用于接收特定的參數(shù)
//為讀取、保存圖像創(chuàng)建的數(shù)組
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];
//要轉(zhuǎn)成的Halcon圖像
HImage image = new HImage();

(1) 查找設(shè)備列表

        //查找設(shè)備
        public void DeviceListAcq()
        {
            int nRet;
            // ch:創(chuàng)建設(shè)備列表 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("查找設(shè)備失敗!");
                return;
            }
 
            // ch:在窗體列表中顯示設(shè)備名 | 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:選擇第一項(xiàng) | en:Select the first item
            if (m_pDeviceList.nDeviceNum != 0)
            {
                cbDeviceList.SelectedIndex = 0;
            }
        }

(2) 打開(kāi)設(shè)備

        //打開(kāi)設(shè)備
        public void OpenDevice()
        {
            if (m_pDeviceList.nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
            {
                MessageBox.Show("未發(fā)現(xiàn)設(shè)備,請(qǐng)選擇");
                return;
            }
            int nRet = -1;
 
            // ch:獲取選擇的設(shè)備信息 | 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:打開(kāi)設(shè)備 | 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("設(shè)備打開(kāi)失敗");
                //ShowErrorMsg("Device open fail!", nRet);
                return;
            }
 
            // ch:探測(cè)網(wǎng)絡(luò)最佳包大小(只對(duì)GigE相機(jī)有效) | 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:設(shè)置采集連續(xù)模式 | en:Set Continues Aquisition Mode
            m_pMyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", 2);// ch:工作在連續(xù)模式 | en:Acquisition On Continuous Mode
            m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0);    // ch:連續(xù)模式 | en:Continuous
        }

(3) 連續(xù)采集

        //連續(xù)采集(也就是實(shí)時(shí)顯示)
        public void ContinuesGrab(PictureBox picBox)
        {
            int nRet;
 
            // ch:開(kāi)始采集 | en:Start Grabbing
            nRet = m_pMyCamera.MV_CC_StartGrabbing_NET();
            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("采集失??!");
                //ShowErrorMsg("Trigger Fail!", nRet);
                return;
            }
 
            //實(shí)時(shí)采集
            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) 關(guān)閉設(shè)備

        //關(guān)閉設(shè)備
        public void CloseDevice()
        {
            // ch:關(guān)閉設(shè)備 | 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) 轉(zhuǎn)成Halcon圖像

        //讀取圖片轉(zhuǎn)換成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);//獲取一幀圖像,超時(shí)時(shí)間設(shè)置為1000
            if (MyCamera.MV_OK != nRet)
            {
                return null;
            }
 
            HImage image = new HImage();
 
            //采集的是黑白圖像,利用Halcon圖像庫(kù)中的GenImage1算子來(lái)構(gòu)建圖像
            image.GenImage1("byte", (int)stFrameInfo.nWidth, (int)stFrameInfo.nHeight, pData);
            return image;
        }

下面是我所做的一個(gè)窗體界面,顯示設(shè)備序列號(hào)的下拉列表做成了一個(gè)用戶控件,將上述的代碼全部放在用戶控件中,在主窗體里調(diào)用用戶控件里的方法來(lái)實(shí)現(xiàn)相機(jī)的連接和采集

具體見(jiàn)示例代碼。

C#如何調(diào)用SDK采集圖像并在Halcon窗口中顯示

示例代碼:

        //查找設(shè)備
        private void btnEnum_Click(object sender, EventArgs e)
        {
            ucDeviceList1.DeviceListAcq();
        }
 
        //打開(kāi)設(shè)備
        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;
        }
 
        //關(guān)閉設(shè)備
        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;
        }
 
        //實(shí)時(shí)顯示
        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;
        }
 
        //關(guān)閉設(shè)備
        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;
        }

注:上述方法是不需要修改相機(jī)IP的,但是要提前用MVS軟件修改相機(jī)的IP

到此,相信大家對(duì)“C#如何調(diào)用SDK采集圖像并在Halcon窗口中顯示”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI