260 lines
7.9 KiB
C++
260 lines
7.9 KiB
C++
#include "Hd_MvCameraTL_module.h"
|
|||
|
|
#include <QDebug>
|
||
|
|
|
||
|
|
QQueue<cv::Mat> queuePic;
|
||
|
|
QMutex* n_mutex = new QMutex();
|
||
|
|
//0106 断连提示
|
||
|
|
void _stdcall ExceptionCB(unsigned int nMesgType, void* pUser) {
|
||
|
|
mv_Status = "disconnect";
|
||
|
|
qDebug() << "[Error] " << " MvCameraTL disconnects!";
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
Hd_MvCameraTL_module* create()
|
||
|
|
{
|
||
|
|
qDebug() << "[INFO] " << "Hd_MvCamera_module create success!" ;
|
||
|
|
return new Hd_MvCameraTL_module;
|
||
|
|
}
|
||
|
|
|
||
|
|
void destory(Hd_MvCameraTL_module * ptr)
|
||
|
|
{
|
||
|
|
if(ptr)
|
||
|
|
{
|
||
|
|
delete ptr;
|
||
|
|
ptr = nullptr;
|
||
|
|
}
|
||
|
|
qDebug() << "[INFO] " << "Hd_MvCamera_module destory success!";
|
||
|
|
}
|
||
|
|
void __stdcall ImageCallBackEx(unsigned char* pData0, MV_FRAME_OUT_INFO_EX* pFrameInfo0, void* pUser0)
|
||
|
|
{
|
||
|
|
Q_UNUSED(pUser0)
|
||
|
|
double time_Start = (double)clock();
|
||
|
|
cv::Mat srcImage;
|
||
|
|
//qDebug() << "[info]" << __FUNCTION__ << " line:" << __LINE__ << "--- start";
|
||
|
|
if (pFrameInfo0)
|
||
|
|
{
|
||
|
|
//获取的是单通道灰度图
|
||
|
|
if (pFrameInfo0->enPixelType == PixelType_Gvsp_Mono8)//获取当前采集到的图像格式
|
||
|
|
{
|
||
|
|
srcImage = cv::Mat(pFrameInfo0->nHeight, pFrameInfo0->nWidth, CV_8UC1, pData0);
|
||
|
|
}
|
||
|
|
//获取的是RGB8图
|
||
|
|
else if (pFrameInfo0->enPixelType == PixelType_Gvsp_RGB8_Packed)
|
||
|
|
{
|
||
|
|
srcImage = cv::Mat(pFrameInfo0->nHeight, pFrameInfo0->nWidth, CV_8UC3,pData0);
|
||
|
|
cvtColor(srcImage, srcImage, COLOR_RGB2BGR);
|
||
|
|
}
|
||
|
|
else if (pFrameInfo0->enPixelType == PixelType_Gvsp_BGR8_Packed)
|
||
|
|
{
|
||
|
|
srcImage = cv::Mat(pFrameInfo0->nHeight, pFrameInfo0->nWidth, CV_8UC3, pData0);
|
||
|
|
|
||
|
|
//cvtColor(srcImage, srcImage, COLOR_RGB2BGR);//导致异色
|
||
|
|
}
|
||
|
|
//获取的是yuv或bayer图
|
||
|
|
else if (pFrameInfo0->enPixelType == PixelType_Gvsp_YUV422_Packed || pFrameInfo0->enPixelType == PixelType_Gvsp_BayerRG8)
|
||
|
|
{
|
||
|
|
MvGvspPixelType enDstPixelType = PixelType_Gvsp_Undefined;
|
||
|
|
enDstPixelType = PixelType_Gvsp_BGR8_Packed;
|
||
|
|
unsigned int nConvertDataSize = pFrameInfo0->nWidth * pFrameInfo0->nHeight * 3;
|
||
|
|
unsigned char* pConvertData = (unsigned char*)malloc(nConvertDataSize);
|
||
|
|
if (NULL == pConvertData)
|
||
|
|
{
|
||
|
|
printf("malloc pConvertData1 fail!\n");
|
||
|
|
m_flag = true;
|
||
|
|
srcImage = cv::Mat(5, 5, CV_8UC1).setTo(0);
|
||
|
|
n_mutex->lock();
|
||
|
|
queuePic.append(srcImage);
|
||
|
|
n_mutex->unlock();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
MV_CC_PIXEL_CONVERT_PARAM stConvertParam = { 0 };
|
||
|
|
stConvertParam.nWidth = pFrameInfo0->nWidth;
|
||
|
|
stConvertParam.nHeight = pFrameInfo0->nHeight;
|
||
|
|
stConvertParam.pSrcData = pData0;
|
||
|
|
stConvertParam.nSrcDataLen = pFrameInfo0->nFrameLen;
|
||
|
|
stConvertParam.enSrcPixelType = pFrameInfo0->enPixelType;
|
||
|
|
stConvertParam.enDstPixelType = enDstPixelType;
|
||
|
|
stConvertParam.pDstBuffer = pConvertData;
|
||
|
|
stConvertParam.nDstBufferSize = nConvertDataSize;
|
||
|
|
|
||
|
|
int nRet = MV_CC_ConvertPixelType(handle, &stConvertParam);
|
||
|
|
if (MV_OK == nRet)
|
||
|
|
{
|
||
|
|
srcImage = cv::Mat::zeros(cv::Size(pFrameInfo0->nWidth, pFrameInfo0->nHeight), CV_8UC3);
|
||
|
|
memcpy(srcImage.data, stConvertParam.pDstBuffer, nConvertDataSize);
|
||
|
|
//Mat mysrcImage = cv::Mat(mypFrameInfo->nHeight, mypFrameInfo->nWidth, CV_8UC3, stConvertParam.pDstBuffer);
|
||
|
|
//mysrcImage.copyTo(srcImage);
|
||
|
|
//mysrcImage.release();
|
||
|
|
stConvertParam = { 0 };
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
srcImage = cv::Mat(0, 0, CV_8UC3).setTo(0);
|
||
|
|
}
|
||
|
|
if (pConvertData)
|
||
|
|
{
|
||
|
|
free(pConvertData);
|
||
|
|
pConvertData = NULL;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(!srcImage.empty())
|
||
|
|
{
|
||
|
|
m_flag = true;
|
||
|
|
n_mutex->lock();
|
||
|
|
queuePic.append(srcImage);
|
||
|
|
n_mutex->unlock();
|
||
|
|
//统计图片成像时间
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
srcImage = cv::Mat(5, 5, CV_8UC1).setTo(0);
|
||
|
|
n_mutex->lock();
|
||
|
|
queuePic.append(srcImage);
|
||
|
|
n_mutex->unlock();
|
||
|
|
}
|
||
|
|
srcImage.release();
|
||
|
|
double time_End = (double)clock();
|
||
|
|
qDebug() << "[INFO] " "image pic:" << index++ << " srcImage Exist,Time:" << (time_End - time_Start) << "ms" << "--CameraName:" << QString::fromStdString(Username);
|
||
|
|
|
||
|
|
pData0 = { 0 };
|
||
|
|
}
|
||
|
|
//搜索所有设备
|
||
|
|
int SearchDevice()
|
||
|
|
{
|
||
|
|
memset(&m_stDevList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
|
||
|
|
|
||
|
|
// ch:枚举子网内所有设备 | en:Enumerate all devices within subnet
|
||
|
|
int nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE || MV_USB_DEVICE, &m_stDevList);
|
||
|
|
for(int i=0; i<m_stDevList.nDeviceNum;i++)
|
||
|
|
{
|
||
|
|
unsigned char* name=m_stDevList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.chSerialNumber;
|
||
|
|
string serialNumber = static_cast<string>((LPCSTR)name);
|
||
|
|
if(Username==serialNumber)
|
||
|
|
{
|
||
|
|
index=i;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (MV_OK != nRet)
|
||
|
|
{
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
Hd_MvCameraTL_module::Hd_MvCameraTL_module()
|
||
|
|
{
|
||
|
|
//m_thread = new MyThread;
|
||
|
|
//m_mutex = new QMutex();
|
||
|
|
}
|
||
|
|
|
||
|
|
void CloseDevice()
|
||
|
|
{
|
||
|
|
MV_CC_StopGrabbing(handle);
|
||
|
|
MV_CC_CloseDevice(handle);
|
||
|
|
MV_CC_DestroyHandle(handle);
|
||
|
|
}
|
||
|
|
Hd_MvCameraTL_module::~Hd_MvCameraTL_module()
|
||
|
|
{
|
||
|
|
CloseDevice();
|
||
|
|
if(n_mutex)
|
||
|
|
{
|
||
|
|
delete n_mutex;
|
||
|
|
n_mutex = nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
qDebug() << "[INFO] " << " delete success!";
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Hd_MvCameraTL_module::sendData(std::vector<cv::Mat> &mat,QByteArray &data)
|
||
|
|
{
|
||
|
|
if (data == "connect")
|
||
|
|
{
|
||
|
|
data = mv_Status;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
if(queuePic.empty())
|
||
|
|
{
|
||
|
|
qCritical() << " [Error] " << " srcImage is null";
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
n_mutex->lock();
|
||
|
|
cv::Mat dstMat = queuePic.dequeue();
|
||
|
|
n_mutex->unlock();
|
||
|
|
mat.push_back(dstMat);
|
||
|
|
qCritical() << " [Error] " << " get image";
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
bool Hd_MvCameraTL_module::reciveData(std::vector<cv::Mat> &mat,QByteArray &data)
|
||
|
|
{
|
||
|
|
Q_UNUSED(mat)
|
||
|
|
m_flag = false;
|
||
|
|
Username= data.toStdString();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Hd_MvCameraTL_module::initParas(QByteArray&)
|
||
|
|
{
|
||
|
|
SearchDevice();
|
||
|
|
unsigned char* serialNumber=m_stDevList.pDeviceInfo[index]->SpecialInfo.stGigEInfo.chSerialNumber;
|
||
|
|
string userName = static_cast<string>((LPCSTR)serialNumber);
|
||
|
|
qDebug() << "[INFO] " << " start"<<"--UserName:"<<QString::fromStdString(userName);
|
||
|
|
if (handle != NULL)
|
||
|
|
{
|
||
|
|
MV_CC_StopGrabbing(handle);
|
||
|
|
MV_CC_CloseDevice(handle);
|
||
|
|
MV_CC_DestroyHandle(handle);
|
||
|
|
}
|
||
|
|
|
||
|
|
//打开相机
|
||
|
|
MV_CC_CreateHandle(&handle, m_stDevList.pDeviceInfo[index]);
|
||
|
|
int nRet = MV_CC_OpenDevice(handle);
|
||
|
|
if (MV_OK != nRet)
|
||
|
|
return false;
|
||
|
|
//触发模式
|
||
|
|
//MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_ON);
|
||
|
|
//MV_CC_SetEnumValue(handle, "TriggerSource", MV_TRIGGER_SOURCE_LINE0);
|
||
|
|
//加载用户集
|
||
|
|
nRet = MV_CC_SetEnumValueByString(handle, "UserSetSelector", "UserSet1");
|
||
|
|
if (MV_OK != nRet)
|
||
|
|
{
|
||
|
|
qDebug() << "Set UserSetSelector fail! nRet " << nRet;
|
||
|
|
}
|
||
|
|
// ch:注册抓图回调 | en:Register image callback
|
||
|
|
MV_CC_SetIntValueEx(handle, "GevHeartbeatTimeout", 3000);//心跳日志
|
||
|
|
|
||
|
|
nRet = MV_CC_RegisterImageCallBackEx(handle, ImageCallBackEx, handle);//注册回调
|
||
|
|
if (MV_OK != nRet)
|
||
|
|
return false;
|
||
|
|
nRet = MV_CC_RegisterExceptionCallBack(handle, ExceptionCB, NULL);//注册报警回调
|
||
|
|
if (MV_OK != nRet)
|
||
|
|
return false;
|
||
|
|
nRet = MV_CC_StartGrabbing(handle);
|
||
|
|
if (MV_OK != nRet)
|
||
|
|
return false;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Hd_MvCameraTL_module::run()
|
||
|
|
{
|
||
|
|
bool result = false;
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
n_mutex->lock();
|
||
|
|
if(queuePic.size() > 0)
|
||
|
|
result = true;
|
||
|
|
n_mutex->unlock();
|
||
|
|
}
|
||
|
|
catch (QString ev)
|
||
|
|
{
|
||
|
|
qDebug() << "[Error] " << " ev:" << ev ;
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|