1、导入相机SDK(将海康SDK库复制粘贴到项目的根目录)
2、在CameraWidget.h头文件中声明如下变量和函数
3、在CameraWidget.cpp文件中写入如下代码
#include "camerawidget.h" #include "ui_camerawidget.h" #include <QDebug> CameraWidget::CameraWidget(QWidget *parent) : QWidget(parent), ui(new Ui::CameraWidget), handle(NULL), m_isColor(false) { ui->setupUi(this); //step 1: 初始化相机 initCamera(); //step2: 注册取图回调函数 if(m_isColor) { int ret = MV_CC_RegisterImageCallBackForRGB(handle, CameraWidget::getCameraImageCallback, this); if(MV_OK != ret) qDebug() << "=======faild =======" ; } else MV_CC_RegisterImageCallBackEx(handle, CameraWidget::getCameraImageCallback, this); //step 3: 开始取图 MV_CC_StartGrabbing(handle); } CameraWidget::~CameraWidget() { MV_CC_StopGrabbing(handle); //停止取图 int nRet = -1; //关闭设备,释放资源 nRet = MV_CC_CloseDevice(handle); if (MV_OK != nRet) { qDebug() << "error: CloseDevice fail"; } //销毁句柄,释放资源 nRet = MV_CC_DestroyHandle(handle); if (MV_OK != nRet) { qDebug() << "error: DestroyHandle fail"; } delete ui; } void CameraWidget::getCameraImageCallback(unsigned char *pData, MV_FRAME_OUT_INFO_EX *stImageInfo, void *arg) { (void)(stImageInfo); CameraWidget *tmp = (CameraWidget *)arg; memcpy(tmp->mpData,pData,tmp->nDataSize); tmp->displayCameraImage(); //刷新图像 } int CameraWidget::initCamera() { int nRet = -1; MV_CC_DEVICE_INFO_LIST stDeviceList; memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST)); // step 1 : enum device nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList); if (MV_OK != nRet) { qDebug()<<"MV_CC_EnumDevices fail!"; return -1; } unsigned int nIndex = 0; MV_CC_DEVICE_INFO* pDeviceInfo; if (stDeviceList.nDeviceNum > 0) { for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++) { qDebug() <<QString("[device %1]").arg(i); pDeviceInfo = stDeviceList.pDeviceInfo[i]; if (NULL == pDeviceInfo) { break; } } } else { qDebug()<<"Find No Devices!"; return -1; } pDeviceInfo = stDeviceList.pDeviceInfo[0]; //打印相机型号及序列号 qDebug() << QString::fromLocal8Bit(reinterpret_cast<char*>(pDeviceInfo->SpecialInfo.stGigEInfo.chModelName)); qDebug() << QString::fromLocal8Bit(reinterpret_cast<char*>(pDeviceInfo->SpecialInfo.stGigEInfo.chSerialNumber)); //彩色相机判别 if('C' == pDeviceInfo->SpecialInfo.stGigEInfo.chModelName[12]) { m_isColor = true; qDebug() << "==============color================="; } // step2 :select device and create handle nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]); if (MV_OK != nRet) { qDebug()<<"MV_CC_CreateHandle fail!"; return -1; } // step3 : open device nRet = MV_CC_OpenDevice(handle); if (MV_OK != nRet) { qDebug()<<"MV_CC_OpenDevice fail!"; return -1; } //step 4: get camera image width and height MVCC_INTVALUE ptValue; // int w,h; MV_CC_GetWidth(handle, &ptValue); m_imageWidth = ptValue.nCurValue; MV_CC_GetHeight(handle, &ptValue); m_imageHeight = ptValue.nCurValue; if(m_isColor) nDataSize = m_imageWidth * m_imageHeight * 3; else nDataSize = m_imageWidth * m_imageHeight; //step 5: set trigger mode as on nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 1); nRet = MV_CC_SetEnumValue(handle, "TriggerSource", 0); mpData = (unsigned char *)malloc(nDataSize); return 1; } int CameraWidget::displayCameraImage() { QImage image; if(m_isColor) image = QImage((uchar*)mpData,m_imageWidth,m_imageHeight,QImage::Format_RGB888); else image = QImage((uchar*)mpData,m_imageWidth,m_imageHeight,QImage::Format_Indexed8); ui->lb_image->setPixmap(QPixmap::fromImage(image).scaled(560,420)); return 1; }
4、运行结果:
使用12v的电压给相机的输入line0一个信号,出现如下结果: