2024-05-20 01:02:24 +08:00
|
|
|
#include <cameraController.h>
|
|
|
|
|
#include <backEndController.h>
|
|
|
|
|
#include <jsonParsing.h>
|
|
|
|
|
#include <qlibrary.h>
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#include <MvCameraControl.h>
|
|
|
|
|
#include <CameraParams.h>
|
|
|
|
|
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-05-21 01:26:00 +08:00
|
|
|
camera::camera(cameraModule cameraModulePtrIn, backEndController* backEndControllerIn, int indexIn)
|
|
|
|
|
{
|
|
|
|
|
cameraModulePtr = cameraModulePtrIn;
|
|
|
|
|
cameraName = cameraModulePtr.key;
|
|
|
|
|
index = indexIn;
|
2024-05-21 01:42:28 +08:00
|
|
|
lastUpdateTime.start();
|
2024-05-21 01:26:00 +08:00
|
|
|
this->start();
|
|
|
|
|
}
|
|
|
|
|
void camera::run()
|
|
|
|
|
{
|
|
|
|
|
QThread::msleep(2000);
|
|
|
|
|
std::vector<cv::Mat>camera_mat;
|
|
|
|
|
while (runFlag)
|
|
|
|
|
{
|
|
|
|
|
getCameraImg(camera_mat);
|
|
|
|
|
if (camera_mat.size() > 0)
|
|
|
|
|
{
|
|
|
|
|
int size = imgQueue.size();
|
|
|
|
|
for (auto img : camera_mat)
|
|
|
|
|
{
|
|
|
|
|
imgQueue.enqueue(img);
|
|
|
|
|
//emit _backEndController->sendPixmapImageWidgetByIndex(cvMatToQPixMap(img), imgWidgetOri, index);
|
2024-05-21 01:42:28 +08:00
|
|
|
if (lastUpdateTime.elapsed() > 200)
|
|
|
|
|
{
|
|
|
|
|
lastUpdateTime.start(); // 记录本次更新时间
|
|
|
|
|
QPixmap pixmap;
|
|
|
|
|
cvMatToQPixMap(img, pixmap);
|
|
|
|
|
showImgPack* pack = new showImgPack();
|
|
|
|
|
pack->img = pixmap;
|
|
|
|
|
pack->type = imgWidgetOri;
|
|
|
|
|
pack->index = index;
|
|
|
|
|
emit S1(pack);
|
|
|
|
|
}
|
2024-05-21 01:26:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//防止过大
|
|
|
|
|
//while (imgQueue.size() > 0)
|
|
|
|
|
//{
|
|
|
|
|
// imgQueue.dequeue();
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
QThread::msleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 01:02:24 +08:00
|
|
|
cameraController::~cameraController()
|
|
|
|
|
{
|
2024-05-20 18:13:21 +08:00
|
|
|
for (auto camera : cameras)
|
|
|
|
|
{
|
|
|
|
|
delete camera.second;
|
|
|
|
|
}
|
2024-05-20 01:02:24 +08:00
|
|
|
for (auto module : camerasVec)
|
|
|
|
|
{
|
|
|
|
|
Base* base_ptr = module.module_ptr;
|
|
|
|
|
delete base_ptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void cameraController::init(backEndController* input)
|
|
|
|
|
{
|
|
|
|
|
_backEndController = input;
|
|
|
|
|
#pragma region 加载相机dll
|
|
|
|
|
for (auto val = 0; val < input->_jsonConfiguration->camera.cameraList.size(); val++)
|
|
|
|
|
{
|
|
|
|
|
QString camera_key = input->_jsonConfiguration->camera.cameraList[val].connectionName;
|
|
|
|
|
QString moduleName = input->_jsonConfiguration->camera.cameraList[val].moduleName; // 相机模块名
|
|
|
|
|
QVector<QString> surfaceVec;
|
|
|
|
|
QByteArray init_Byte;
|
|
|
|
|
// 加载相机模块
|
|
|
|
|
if (!loadModule(camera_key, moduleName, 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_module.module_ptr->initParas(initByte))
|
|
|
|
|
{
|
2024-05-21 01:26:00 +08:00
|
|
|
camera* _camera = new camera(m_module,_backEndController, cameras.size());
|
|
|
|
|
QObject::connect(_camera, &camera::S1, this, &cameraController::R1);
|
2024-05-20 01:02:24 +08:00
|
|
|
cameras.insert(std::make_pair(m_module.key, _camera));
|
|
|
|
|
qDebug() << "[INFO] " << " initParas success!";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
qCritical() << "[Error] " << " moduleName:" << m_module.moduleName << " initParas failed!";
|
|
|
|
|
}
|
2024-05-21 01:26:00 +08:00
|
|
|
|
|
|
|
|
this->start();
|
2024-05-20 01:02:24 +08:00
|
|
|
}
|
|
|
|
|
bool cameraController::loadModule(QString keyName, QString moduleName, 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.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-05-21 01:26:00 +08:00
|
|
|
void cameraController::R1(showImgPack* pack)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
emit _backEndController->sendPixmapImageWidget(pack);
|
|
|
|
|
}
|