123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613 |
- #include "EntityCamera.h"
- /// 初始化相机环境
- bool ICamera::initEnv()
- {
- return true;
- }
- /// 关闭相机环境
- bool ICamera::closeEnv()
- {
- return true;
- }
- /// 获取所有相机序列号
- QVector<QString> ICamera::cameraSerial()
- {
- QVector<QString> Seriallist;
- Seriallist.clear();
- MV_CC_DEVICE_INFO_LIST m_stDevList;
- memset(&m_stDevList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
- // ch:枚举子网内所有设备 | en:Enumerate all devices within subnet
- int nRet = CMyCamera::EnumDevices(&m_stDevList);
- if (MV_OK != nRet)
- {
- return Seriallist;
- }
- int g_serverCount = Seriallist.size(); //得到采集设备的数量
- for (size_t i = 0; i < m_stDevList.nDeviceNum; i++)
- {
- MV_CC_DEVICE_INFO* pDeviceInfo = m_stDevList.pDeviceInfo[i];
- QString strSerialNumber = QString(QLatin1String((char*)pDeviceInfo->SpecialInfo.stGigEInfo.chSerialNumber));
- QString strUserName = QString(QLatin1String((char*)pDeviceInfo->SpecialInfo.stGigEInfo.chUserDefinedName));
-
- QString str = strSerialNumber;
- if (!strUserName.isEmpty())
- {
- str = "(" + strUserName + ")" + strSerialNumber;
- }
-
- Seriallist.push_back(str);
-
- }
- return Seriallist;
- }
- EntityCamera::EntityCamera() :
- m_pCamParamConfig(nullptr),
- m_pDialogCamPasram(nullptr)
- {
- m_nImageChannels = 1;
- //////////////////////////////////////////////////////////////////////////
- m_pcMyCamera = nullptr;
-
- }
- EntityCamera::~EntityCamera(void)
- {
- this->stopWork();
- if (m_pcMyCamera != NULL)
- {
- delete m_pcMyCamera;
- }
- bool bcloseCamera = this->closeCamera();
- if (nullptr != m_pDialogCamPasram)
- {
- delete m_pDialogCamPasram;
- m_pDialogCamPasram = NULL;
- }
- if (nullptr != m_pCamParamConfig)
- {
- delete m_pCamParamConfig;
- m_pCamParamConfig = nullptr;
- }
- }
- /// 创建ENTITY相机
- EntityCamera* EntityCamera::createCamera(QString serial)
- {
- EntityCamera* camera = nullptr;
- try
- {
- camera = new EntityCamera();
- if (camera)
- {
- /// 打开相机
- if (true == camera->openCamera(serial))
- {
- if (false == camera->initialize())
- {
- delete camera;
- camera = NULL;
- }
- }
- else
- {
- delete camera;
- camera = NULL;
- }
- }
-
- }
- catch (...)
- {
- delete camera;
- camera = NULL;
- }
- return camera;
- }
- /// 打开相机
- bool EntityCamera::openCamera(QString serial)
- {
- bool bret = true;
- m_strSerial = serial;
- MV_CC_DEVICE_INFO_LIST m_stDevList; // ch:设备信息列表结构体变量,用来存储设备列表 // ch:初始化设备信息列表 | en:Device Information List Initialization
- memset(&m_stDevList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
- // ch:枚举子网内所有设备 | en:Enumerate all devices within subnet
- int nRet = CMyCamera::EnumDevices(&m_stDevList);
- if (MV_OK != nRet)
- {
- return false;
- }
- QString str;
- MV_CC_DEVICE_INFO* pDeviceInfo = NULL;
- for (size_t i = 0; i < m_stDevList.nDeviceNum; i++)
- {
- pDeviceInfo = m_stDevList.pDeviceInfo[i];
- QString strSerialNumber = QString(QLatin1String((char*)pDeviceInfo->SpecialInfo.stGigEInfo.chSerialNumber));
- QString strUserName = QString(QLatin1String((char*)pDeviceInfo->SpecialInfo.stGigEInfo.chUserDefinedName));
- QString str = strSerialNumber;
- if (!strUserName.isEmpty())
- {
- str = "(" + strUserName + ")" + strSerialNumber;
- }
- if (serial.indexOf(str) != -1)
- {
- break;
- }
- }
- if (m_pcMyCamera == NULL)
- {
- m_pcMyCamera = new CMyCamera;
- }
- if (m_pcMyCamera != NULL)
- {
- int nRet = m_pcMyCamera->Open(pDeviceInfo);
- if (MV_OK != nRet)
- {
- delete m_pcMyCamera;
- m_pcMyCamera = NULL;
- return false;
- }
- }
- // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
- if (pDeviceInfo->nTLayerType == MV_GIGE_DEVICE)
- {
- int nPacketSize = m_pcMyCamera->GetOptimalPacketSize();
- if (nPacketSize > 0)
- {
- nRet = m_pcMyCamera->SetIntValue("GevSCPSPacketSize", nPacketSize);
- if (nRet != MV_OK)
- {
- //MessageBox(_T("Warning: Set Packet Size fail!"));
- }
- }
- }
- imageHeight();
- imageWidth();
- imageChannels();
- // ch:注册抓图回调 | en:Register image callback
- m_pcMyCamera->RegisterImageCallBack(EntityCamera::ImageCallBackEx, this);
- return bret;
- }
- /// 关闭相机
- bool EntityCamera::closeCamera()
- {
- bool bret = true;
- if (NULL != m_pcMyCamera)
- {
- //断开设备
- m_pcMyCamera->Close();
- // delete m_pcMyCamera;
- // m_pcMyCamera = NULL;
- }
- return bret;
- }
- /// 初始化
- bool EntityCamera::initialize()
- {
- if (NULL == m_pDialogCamPasram)
- {
- m_pDialogCamPasram = new DialogCamPasram(this);
- }
- // if (imageChannels() == 3)
- // {
- // m_pColorconv->Enable(true, false);
- // m_pColorconv->SetOutputFormat(SapFormatRGB888);
- // m_pColorconv->SetAlign(SapColorConversion::AlignRGGB);
- // }
- return true;
- }
- /// 加载参数
- void EntityCamera::loadCamParam()
- {
- }
- /// 保存参数
- void EntityCamera::saveCamParam()
- {
- }
- /// 设置相机工作模式
- bool EntityCamera::setWorkMode(IBaseCamCommon::CamWorkMode mode, bool Running )
- {
- bool bret = true;
- this->stopWork();
-
- switch (mode)
- {
- case IBaseCamCommon::eContinu:
- //连续采集模式 相机一直工作采图
- this->setToFreeRunMode();
- break;
- case IBaseCamCommon::eHardware:
- //中断采集模式 相机接收到硬件中断中断命令采集图像
- this->setToHardwareTrigerMode();
- break;
- case IBaseCamCommon::eSoftware:
- //中断采集模式 相机接收到软件中断命令采集图像
- bret = this->setToSoftwareTrigerMode();
- break;
- case IBaseCamCommon::eSuspendCou:
- break;
- default:
- break;
- }
- mWorkMode = mode; //工作模式
-
- return bret;
- }
- /// 相机工作模式
- IBaseCamCommon::CamWorkMode EntityCamera::workMode()
- {
- return mWorkMode;
- }
- /// 相机开始工作
- bool EntityCamera::startWork()
- {
- bool bret = true;
- int nRet = MV_OK;
- if (NULL != m_pcMyCamera)
- {
- nRet = m_pcMyCamera->StartGrabbing();
- if (nRet == MV_OK)
- {
- }
- }
- return bret;
- }
- /// 相机停止工作
- bool EntityCamera::stopWork()
- {
- bool bret = true;
- int nRet = MV_OK;
- if (NULL != m_pcMyCamera)
- {
- nRet = m_pcMyCamera->StopGrabbing();
- }
- else
- {
- return false;
- }
- if (MV_OK != nRet)
- {
- return false;
- }
- return bret;
- }
- /// 相机序列号
- QString EntityCamera::serial()
- {
- return m_strSerial;
- }
- /// 重启相机
- bool EntityCamera::restartCamera()
- {
- return true;
- }
- /// 获取一桢图像
- bool EntityCamera::captureImage(void** buf, int size)
- {
- return true;
- }
- /// 获取图像宽
- int EntityCamera::imageWidth(void)
- {
- return 0;
- }
- /// 获取图像高
- int EntityCamera::imageHeight(void)
- {
- return 0;
- }
- /// 获取图像通道数
- int EntityCamera::imageChannels(void)
- {
- return m_nImageChannels;
- }
- /// 获取配置参数类
- IBaseCamParamConfig* EntityCamera::camParamConfig()
- {
- CamParamConfig* paramConfig = nullptr; //配置参数类
- if (nullptr == m_pCamParamConfig)
- {
- m_pCamParamConfig = new CamParamConfig(this);
- }
- paramConfig = m_pCamParamConfig;
- return paramConfig;
- }
- /// 显示设置参数窗口页面
- void EntityCamera::showParamConfigWindow(int mode )
- {
- if (m_pDialogCamPasram)
- {
- m_pDialogCamPasram->setModal(true);
- m_pDialogCamPasram->show();
- }
- }
- /// 获取相机最大分辨率
- bool EntityCamera::getMaxSize(int& w, int& h)
- {
- return true;
- }
- /// 设置相机分辨率
- bool EntityCamera::setResolution(int w, int h)
- {
- return true;
- }
- /// 获取已注册图像回调的数量
- int EntityCamera::getReceCallBackSIze()
- {
- return m_pImgCallBack.size();
- }
- ///设置接收图像数据回调函数
- void EntityCamera::setReceCallBack(IBaseCamCommon::receImgCallBack callBack, long long userParam, QString userID)
- {
- //this->m_pReceImgFunction = callBack;
- //this->mUserParam = userParam;
- IBaseCamCommon::ImgCallBack* pImgCallBack = new IBaseCamCommon::ImgCallBack;
- pImgCallBack->pReceFunction = callBack;
- pImgCallBack->pUserParam = userParam;
- pImgCallBack->strUserID = userID;
- m_pImgCallBack.push_back(pImgCallBack);
- }
- ///设置接收Log数据回调函数
- void EntityCamera::setReceLogCallBack(IBaseCamCommon::receLogCallBack callBack, long long userParam)
- {
- this->m_pReceLogFunction = callBack;
- this->mLogUserParam = userParam;
- }
- /// 执行回调函数
- void EntityCamera::execCallFunction(IBaseCamCommon::CamImgData* img, int frameCount)
- {
- for (int i = 0; i < m_pImgCallBack.size(); i++)
- {
- if (m_pImgCallBack[i]->pReceFunction != nullptr)
- {
- if (m_pImgCallBack[i]->bActivation || (m_pImgCallBack[i]->strUserID == "main"))
- {
- m_pImgCallBack[i]->pReceFunction(img, frameCount, m_strSerial, m_pImgCallBack[i]->pUserParam);
-
- //qDebug() << "execCallFunction " << m_pImgCallBack[i]->strUserID;
- }
- }
- }
- }
- /// 执行Log回调函数
- void EntityCamera::execLogCallFunction(QString serial, QString Massage, long long userParam)
- {
- // printf( "执行回调函数\n" );
- if (this->m_pReceLogFunction != NULL)
- {
- this->m_pReceLogFunction(serial, Massage, userParam);
- }
- }
- /// 软件触发采图
- void EntityCamera::softTriggerCap(QString userID)
- {
- int nRet = m_pcMyCamera->CommandExecute("TriggerSoftware");
- if (MV_OK != nRet)
- {
- qWarning() << "CommandExecute(TriggerSoftware) Error";
- }
- for (int i = 0; i < m_pImgCallBack.size(); i++)
- {
- if (m_pImgCallBack[i]->strUserID == userID)
- {
- m_pImgCallBack[i]->bActivation = true;
- }
- else
- {
- m_pImgCallBack[i]->bActivation = false;
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- int RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight)
- {
- if (NULL == pRgbData)
- {
- return MV_E_PARAMETER;
- }
- for (unsigned int j = 0; j < nHeight; j++)
- {
- for (unsigned int i = 0; i < nWidth; i++)
- {
- unsigned char red = pRgbData[j * (nWidth * 3) + i * 3];
- pRgbData[j * (nWidth * 3) + i * 3] = pRgbData[j * (nWidth * 3) + i * 3 + 2];
- pRgbData[j * (nWidth * 3) + i * 3 + 2] = red;
- }
- }
- return MV_OK;
- }
- void __stdcall EntityCamera::ImageCallBackEx(unsigned char* pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
- {
- EntityCamera* pDlg = (EntityCamera*)pUser;
- int m_nImageWidth = pFrameInfo->nWidth;
- int m_nImageHeight = pFrameInfo->nHeight;
- if (pFrameInfo->enPixelType == PixelType_Gvsp_Mono8)
- {
- pDlg->mImgCapBuffer.channels = 1;
- ;
- pDlg->mImgCapBuffer.count = 1;
- pDlg->mImgCapBuffer.width = m_nImageWidth;
- pDlg->mImgCapBuffer.height = m_nImageHeight;
-
- pDlg->mImgCapBuffer.imgData = pData;
-
- pDlg->mImgCapBuffer.nTriggerIndex = pFrameInfo->nTriggerIndex;
- pDlg->mImgCapBuffer.nLostPacket = pFrameInfo->nLostPacket;
- pDlg->mImgCapBuffer.nFrameNum = pFrameInfo->nFrameNum;
- }
- else if (pFrameInfo->enPixelType == PixelType_Gvsp_RGB8_Packed)
- {
- RGB2BGR(pData, pFrameInfo->nWidth, pFrameInfo->nHeight);
- pDlg->mImgCapBuffer.count = 1;
- pDlg->mImgCapBuffer.channels = 3;
- pDlg->mImgCapBuffer.width = m_nImageWidth;
- pDlg->mImgCapBuffer.height = m_nImageHeight;
- pDlg->mImgCapBuffer.imgData = pData;
- }
- pDlg->execCallFunction(&pDlg->mImgCapBuffer, pDlg->mImgCapBuffer.count);
- }
- /// 设置连续采集模式
- bool EntityCamera::setToFreeRunMode()
- {
- int nRet = m_pcMyCamera->SetEnumValue("TriggerMode", 0);
- if (MV_OK != nRet)
- {
- return nRet;
- }
- return true;
- }
- /// 设置软件触发模式
- bool EntityCamera::setToSoftwareTrigerMode()
- {
- // 获取TriggerMode节点对象
- int nRet = m_pcMyCamera->SetEnumValue("TriggerMode", 1);
- if (MV_OK != nRet)
- {
- //return nRet;
- }
- nRet = m_pcMyCamera->SetEnumValue("TriggerSource", MV_TRIGGER_SOURCE_SOFTWARE);
- if (MV_OK != nRet)
- {
- return nRet;
- }
- return true;
- }
- /// 设置硬件触发模式
- bool EntityCamera::setToHardwareTrigerMode()
- {
- int nRet = m_pcMyCamera->SetEnumValue("TriggerMode", 1);
- if (MV_OK != nRet)
- {
- //return nRet;
- }
- nRet = m_pcMyCamera->SetEnumValue("TriggerSource", MV_TRIGGER_SOURCE_LINE0);
- if (MV_OK != nRet)
- {
- return nRet;
- }
- return true;
- }
|