#include #include #include #include #include #include static QByteArray MV_CC_DeviceInfoListToQByteArray(const MV_CC_DEVICE_INFO_LIST& deviceInfoList) { QByteArray byteArray; byteArray.resize(sizeof(MV_CC_DEVICE_INFO_LIST)); memcpy(byteArray.data(), &deviceInfoList, sizeof(MV_CC_DEVICE_INFO_LIST)); return byteArray; } static MV_CC_DEVICE_INFO_LIST QByteArrayToMV_CC_DeviceInfoList(const QByteArray& byteArray) { MV_CC_DEVICE_INFO_LIST deviceInfoList; if (byteArray.size() == sizeof(MV_CC_DEVICE_INFO_LIST)) { memcpy(&deviceInfoList, byteArray.constData(), sizeof(MV_CC_DEVICE_INFO_LIST)); } else { qDebug() << "Invalid size of QByteArray for MV_CC_DEVICE_INFO_LIST!"; } return deviceInfoList; } static MV_CC_DEVICE_INFO_LIST searchMvDevice() { MV_CC_DEVICE_INFO_LIST m_stDevList; memset(&m_stDevList, 0, sizeof(MV_CC_DEVICE_INFO_LIST)); int nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE || MV_USB_DEVICE, &m_stDevList); return m_stDevList; } camera::camera(cameraModule cameraModulePtrIn, int indexIn) { cameraModulePtr = cameraModulePtrIn; cameraName = cameraModulePtr.key; index = indexIn; lastUpdateTime.start(); this->start(); } void camera::run() { QThread::msleep(2000); std::vectorcamera_mat; while (runFlag) { int queueSize = requestQueue.size() ; if (queueSize >0) { mutex.lock(); std::shared_ptr data = requestQueue.dequeue(); trigger(); data->oriImgMutex.lock(); const unsigned long long timeCode = data->timeCode; while(1) { getCameraImg(data->oriImgs); if (data->oriImgs.size() >= data->imgAmountMin) { qDebug() << data->timeCode << " Min:" << data->imgAmountMin << " Get:" << data->oriImgs.size(); if (lastUpdateTime.elapsed() > 200)//显示在界面 { lastUpdateTime.start(); // 记录本次更新时间 QPixmap pixmap; cvMatToQPixMap(data->oriImgs[0], pixmap); showImgPack* pack = new showImgPack(); pack->img = pixmap; pack->type = imgWidgetOri; pack->index = data->oriShowIndex; emit _core._qtFunc.sendPixmapImageWidget(pack); } else { showLost++; qDebug() << cameraModulePtr.moduleName << "Lost show"; } data->oriImgMutex.unlock(); mutex.unlock(); break; } QThread::msleep(1); } } QThread::msleep(100); } } cameraManager::~cameraManager() { for (auto camera : cameras) { delete camera.second; } for (auto module : camerasVec) { Base* base_ptr = module.module_ptr; delete base_ptr; } } void cameraManager::initialization() { #pragma region 加载相机dll for (auto val = 0; val < _core._appConfig._cameraStructs.size(); val++) { QString camera_key = QString::fromStdString(_core._appConfig._cameraStructs[val].connectionName); QString moduleName = QString::fromStdString(_core._appConfig._cameraStructs[val].moduleName);// 相机模块名 QString triggerMode = QString::fromStdString(_core._appConfig._cameraStructs[val].triggerMode);// 相机模块名 QVector surfaceVec; QByteArray init_Byte; // 加载相机模块 if (!loadModule(camera_key, moduleName, triggerMode, init_Byte)) { qCritical() << "[Error] " << " moduleName:" << moduleName << " load camera module failed! "; return; } } #pragma endregion //非海康相机可跳过 MV_CC_DEVICE_INFO_LIST m_stDevList = searchMvDevice(); QByteArray mvDevices = MV_CC_DeviceInfoListToQByteArray(m_stDevList); for (auto i = 0; i < camerasVec.size(); i++) { cameraModule m_module = camerasVec.at(i); QString moduleName = m_module.moduleName; QString key = m_module.key; //传递初始化参数 QByteArray initByte = m_module.m_initByte; QByteArray m_keyByte = key.toUtf8(); //遍历是否有相同key的相机,如果有直接复制指针 for (int count = 0; count < camerasVec.size(); count++) if (!camerasVec[count].key.compare(m_module.key) && count < i) m_module.module_ptr = camerasVec[count].module_ptr; m_module.module_ptr->reciveData(vec_mat, m_keyByte); //初始化相机 //非海康相机可跳过 for (int i = 0; i < m_stDevList.nDeviceNum; i++) { std::string userDefineName = static_cast((LPCSTR)m_stDevList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.chUserDefinedName); std::string serialNumber = static_cast((LPCSTR)m_stDevList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.chSerialNumber); if (!serialNumber.compare(m_module.key.toStdString()) || !userDefineName.compare(m_module.key.toStdString())) { m_module.module_ptr->reciveData(vec_mat, mvDevices); break; } } } } void cameraManager::run() { if (initFlag == true) return; bool initReuslt = true; for (auto i = 0; i < camerasVec.size(); i++) { cameraModule m_module = camerasVec.at(i); if (m_module.init == true) continue; QByteArray initByte = m_module.m_initByte; if (m_module.module_ptr->initParas(initByte)) { camera* _camera = new camera(m_module, cameras.size()); cameras.insert(std::make_pair(m_module.key.toStdString(), _camera)); emit _core._qtFunc.sendInitString("相机"+m_module.key + "初始化成功"); qDebug() << "[INFO] " << " initParas success!"; m_module.init = true; } else { QByteArray errorStr("getStatus"); m_module.module_ptr->sendData(vec_mat, errorStr); initReuslt = false; qCritical() << "[Error] " << " moduleName:" << m_module.moduleName << " initParas failed!"; emit _core._qtFunc.sendInitString("相机" + m_module.key + "初始化失败->"+ errorStr); } } if (initReuslt == true) { initFlag = true; status = InitOK; } else status = InitFailure; } bool cameraManager::loadModule( QString keyName, QString moduleName, QString triggerMode, QByteArray initByte) { QString task_thread_index = 0; try { QString module_path = "./" + moduleName; qDebug() << "[INFO] " << " module_path:" << module_path; /////// 自动拷贝重用dll if (QString(module_path.at(module_path.size() - 1)).toInt() > 0) { //命名格式: 模块名+1/2/3... QString path_model = module_path.mid(0, module_path.size() - 1); if (QFile::exists(path_model + ".dll")) { QFile::remove(module_path + ".dll"); QFile::copy(path_model + ".dll", module_path + ".dll"); qDebug() << "[INFO] " << "copy dll " << module_path; } } /// 动态加载dll QLibrary* m_lib = new QLibrary(module_path); lib_list.append(m_lib); bool flag = m_lib->load(); if (!m_lib->isLoaded()) { qDebug() << "[Error] " << " Loaded error:" << m_lib->errorString(); return false; } CreateFunction createFun = (CreateFunction)m_lib->resolve("create"); ReleaseFunction releaseFun = (ReleaseFunction)m_lib->resolve("destory"); if (createFun && releaseFun) { cameraModule m_module; m_module.key = keyName; m_module.m_initByte = initByte; m_module.moduleName = moduleName; m_module.triggerMode = moduleName; m_module.module_ptr = createFun(); m_module.destory_ptr = releaseFun; if (!task_thread_index.isEmpty()) m_module.task_thread_index = task_thread_index; camerasVec.push_back(m_module); } else { delete m_lib; m_lib = nullptr; return false; } } catch (QString event) { qCritical() << "[Error] " << " event:" << event; return false; } return true; }