Files
handleaiuinew/HanddleAI/HandleAiUi-new/back/cameraManager.cpp
T

257 lines
7.1 KiB
C++
Raw Normal View History

2024-05-21 23:54:22 +08:00
#include <cameraManager.h>
#include <qlibrary.h>
#include <windows.h>
#include <MvCameraControl.h>
#include <CameraParams.h>
2024-08-07 09:16:35 +00:00
#include <appCore.hpp>
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;
}
2024-08-07 09:16:35 +00:00
camera::camera(cameraModule cameraModulePtrIn, int indexIn)
{
cameraModulePtr = cameraModulePtrIn;
cameraName = cameraModulePtr.key;
index = indexIn;
lastUpdateTime.start();
this->start();
}
void camera::run()
{
QThread::msleep(2000);
std::vector<cv::Mat>camera_mat;
while (runFlag)
{
2024-05-22 01:32:43 +08:00
int queueSize = requestQueue.size() ;
if (queueSize >0)
2024-05-21 18:11:13 +08:00
{
2024-05-22 01:32:43 +08:00
mutex.lock();
2024-05-22 16:47:14 +08:00
std::shared_ptr <detData>data = requestQueue.dequeue();
2024-05-22 01:32:43 +08:00
trigger();
2024-05-24 18:05:29 +08:00
data->oriImgMutex.lock();
2024-05-22 01:32:43 +08:00
const unsigned long long timeCode = data->timeCode;
2024-05-21 23:54:22 +08:00
while(1)
2024-05-21 18:11:13 +08:00
{
2024-05-24 18:05:29 +08:00
getCameraImg(data->oriImgs);
if (data->oriImgs.size() >= data->imgAmountMin)
2024-05-21 18:11:13 +08:00
{
2024-05-24 18:05:29 +08:00
qDebug() << data->timeCode << " Min:" << data->imgAmountMin << " Get:" << data->oriImgs.size();
2024-05-22 01:32:43 +08:00
if (lastUpdateTime.elapsed() > 200)//显示在界面
{
lastUpdateTime.start(); // 记录本次更新时间
QPixmap pixmap;
2024-05-24 18:05:29 +08:00
cvMatToQPixMap(data->oriImgs[0], pixmap);
2024-05-22 01:32:43 +08:00
showImgPack* pack = new showImgPack();
pack->img = pixmap;
pack->type = imgWidgetOri;
2024-08-07 09:16:35 +00:00
pack->index = data->oriShowIndex;
emit _core._qtFunc.sendPixmapImageWidget(pack);
2024-05-22 01:32:43 +08:00
}
else
{
showLost++;
qDebug() << cameraModulePtr.moduleName << "Lost show";
}
2024-05-24 18:05:29 +08:00
data->oriImgMutex.unlock();
2024-05-22 01:32:43 +08:00
mutex.unlock();
2024-05-21 23:54:22 +08:00
break;
2024-05-21 18:11:13 +08:00
}
2024-05-22 01:32:43 +08:00
QThread::msleep(1);
2024-05-21 18:11:13 +08:00
}
2024-05-22 01:32:43 +08:00
}
QThread::msleep(100);
}
}
2024-05-21 23:54:22 +08:00
cameraManager::~cameraManager()
{
2024-05-20 18:13:21 +08:00
for (auto camera : cameras)
{
delete camera.second;
}
for (auto module : camerasVec)
{
Base* base_ptr = module.module_ptr;
delete base_ptr;
}
2024-05-22 16:47:14 +08:00
}
2024-08-07 09:16:35 +00:00
void cameraManager::initialization()
{
#pragma region 加载相机dll
2024-08-07 09:16:35 +00:00
for (auto val = 0; val < _core._appConfig._cameraStructs.size(); val++)
{
2024-08-07 09:16:35 +00:00
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<QString> surfaceVec;
QByteArray init_Byte;
// 加载相机模块
2024-08-07 09:16:35 +00:00
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<std::string>((LPCSTR)m_stDevList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.chUserDefinedName);
std::string serialNumber = static_cast<std::string>((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;
}
}
2024-08-07 09:16:35 +00:00
}
}
2024-08-13 18:07:24 +08:00
void cameraManager::run()
2024-08-07 09:16:35 +00:00
{
2024-08-13 18:07:24 +08:00
if (initFlag == true)
return;
2024-08-14 10:38:48 +08:00
status = NotInit;
2024-08-13 18:07:24 +08:00
bool initReuslt = true;
2024-08-07 09:16:35 +00:00
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))
{
2024-08-07 09:16:35 +00:00
camera* _camera = new camera(m_module, cameras.size());
2024-05-22 01:32:43 +08:00
cameras.insert(std::make_pair(m_module.key.toStdString(), _camera));
2024-08-07 09:16:35 +00:00
emit _core._qtFunc.sendInitString("相机"+m_module.key + "初始化成功");
qDebug() << "[INFO] " << " initParas success!";
2024-08-07 09:16:35 +00:00
m_module.init = true;
}
else
2024-05-23 15:06:03 +08:00
{
QByteArray errorStr("getStatus");
2024-08-07 09:16:35 +00:00
m_module.module_ptr->sendData(vec_mat, errorStr);
2024-08-13 18:07:24 +08:00
initReuslt = false;
qCritical() << "[Error] " << " moduleName:" << m_module.moduleName << " initParas failed!";
2024-08-07 09:16:35 +00:00
emit _core._qtFunc.sendInitString("相机" + m_module.key + "初始化失败->"+ errorStr);
2024-05-23 15:06:03 +08:00
}
}
2024-08-13 18:07:24 +08:00
if (initReuslt == true)
{
initFlag = true;
status = InitOK;
}
else
status = InitFailure;
}
2024-08-07 09:16:35 +00:00
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;
2024-08-07 09:16:35 +00:00
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;
2024-08-07 09:16:35 +00:00
}