Hikvision industrial camera SDK connection demo, Qt interface display

foreword

In the process of machine vision development, industrial cameras are often used to collect images. The following uses the SDK provided by Hikvision Robotics to develop demos, using the language C++, and using Qt for interface display. The SDK comes from the Development folder attached to the MVS 3.1 version. The complete project link address is:
Demo source code C++ version of Hikvision Industrial Camera SDK
Appendix Naming method of Hikvision Industrial Camera:
insert image description here
The development process of Hikvision Industrial Camera is generally as follows:
insert image description here

Detailed development process main code

1. Search and select Camera. MV_CC_DEVICE_INFO_LIST is the device structure. Hikvision's device structure is separate from the image structure. One is related to hardware devices, and the other is related to image information.

MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList,0,sizeof(MV_CC_DEVICE_INFO_LIST));
//----------------1.列举设备---------------------
iRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE ,&stDeviceList);
if(MV_OK != iRet)
{
    
    
   return;
}
//相机个数
int iNumber = stDeviceList.nDeviceNum;
MV_CC_DEVICE_INFO *pDeviceInfo = new MV_CC_DEVICE_INFO;
if(stDeviceList.nDeviceNum>0)
{
    
    
   for(unsigned int i =0;i<stDeviceList.nDeviceNum;i++)
   {
    
    
       pDeviceInfo = stDeviceList.pDeviceInfo[i];
       if(NULL == pDeviceInfo)
       {
    
    
           break;
       }
   }
}
else
{
    
    
   return;
}
'C' == pDeviceInfo->SpecialInfo.stUsb3VInfo.chModelName[12] ? isColor = true : isColor = false;
//---------------2.选择设备并创建句柄---------------
//选择第一个设备
iRet = MV_CC_CreateHandle(&cameraHandle,stDeviceList.pDeviceInfo[0]);
if(MV_OK != iRet)
{
    
    
    return;
}

2. Open the camera and create a handle

//------------------3.打开相机----------------------
iRet = MV_CC_OpenDevice(cameraHandle);
//------------------4.获取图像宽和高----------------
MVCC_INTVALUE ptValue;
MV_CC_GetWidth(cameraHandle,&ptValue);
imageWidth = ptValue.nCurValue;
MV_CC_GetHeight(cameraHandle,&ptValue);
imageHeight = ptValue.nCurValue;

3. Set the acquisition mode. In the field of industrial cameras, trigger modes are divided into internal and external triggers. By default, the trigger mode is turned off, and the camera outputs a video stream at this time; the soft trigger can be understood as taking a photo, clicking it, and collecting a photo; the hard trigger is an external trigger, when the external trigger signal is sent, the camera captures a photo image;

//------------------5.设置采集模式--------------
QString qsAcquisitionMode = ui.acquisitionModeCombo ->currentText();
if("连续采集模式"==qsAcquisitionMode)
{
    
    
     //开启连续采集模式,即触发模式关闭,连续采集
     iRet = MV_CC_SetEnumValue(cameraHandle,"TriggerMode",MV_TRIGGER_MODE_OFF);
}
else if("触发模式" == qsAcquisitionMode)
{
    
    
   //开启触发模式,即触发模式开启
   MV_CC_SetEnumValue(cameraHandle,"TriggerMode",MV_TRIGGER_MODE_OFF);
}

4. Set the trigger source

//---------------6.设置触发源-----------------
QString qsTriggerSource = ui.triggerSourceCombo->currentText();
if("软触发"==qsTriggerSource)
{
    
    
   //开启软触发模式
   iRet = MV_CC_SetEnumValue(cameraHandle,"TriggerSource",7);
}
else
{
    
     
   //其余触发模式暂不处理
   return;
}

5. Modify the camera parameters (the parameters must be modified after the camera is turned on)

//------------7.设置相机曝光,增益等相关参数---------
int iExposureTime = ui.exposureEdit->text().toInt();
iRet = MV_CC_SetEnumValue(cameraHandle,"ExposureTime",iExposureTime);
int iGain = ui.gainEdit->text().toInt();
iRet = MV_CC_SetEnumValue(cameraHandle,"Gain",iExposureTime);

6. Start the acquisition (you can set the image display thread and display pixel format by yourself)

//开启采集
if(isGrabing)
{
    
    
   isGrabing = true;
   iRet = MV_CC_StartGrabbing(cameraHandle);
}

7. Turn off the acquisition, that is, turn off the video stream

if(isGrabing)
{
    
    
   iRet = MV_CC_StopGrabbing(cameraHandle);
}
isGrabing = false;

8. Turn off the camera, here you need to release the relevant memory after turning off the camera

//---------------最后一步,关闭相机------------------
//关闭设备
iRet = MV_CC_CloseDevice(cameraHandle);
//释放句柄
iRet = MV_CC_DestroyHandle(cameraHandle);

The software interface is
insert image description here

Guess you like

Origin blog.csdn.net/qq_43376782/article/details/129882105
Recommended