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

274 lines
7.6 KiB
C++

#include <cameraManager.h>
#include <backEndManager.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;
}
camera::camera(cameraModule cameraModulePtrIn, backEndManager* backEndManagerIn, 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)
{
//getCameraImg(camera_mat);
//while(camera_mat.size()>0)
//{
// int size = imgQueue.size();
// for (auto img : camera_mat)
// {
// imgQueue.enqueue(img);
// 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);
// }
// else
// {
// showLost++;
// qDebug() << cameraModulePtr.moduleName << "Lost show";
// }
// camera_mat.clear();
// }
// //防止过大
// while (imgQueue.size() > 5)
// {
// imgQueue.dequeue();
// imgLost++;
// qDebug() << cameraModulePtr.moduleName << "Lost img";
// }
//
// getCameraImg(camera_mat);
//}
int queueSize = requestQueue.size() ;
if (queueSize >0)
{
mutex.lock();
std::shared_ptr <detData>data = requestQueue.dequeue();
trigger();
data->imgMutex.lock();
const unsigned long long timeCode = data->timeCode;
while(1)
{
getCameraImg(data->imgs);
if (data->imgs.size() >= data->imgAmountMin)
{
qDebug() << data->timeCode << " Min:" << data->imgAmountMin << " Get:" << data->imgs.size();
if (lastUpdateTime.elapsed() > 200)//显示在界面
{
lastUpdateTime.start(); // 记录本次更新时间
QPixmap pixmap;
cvMatToQPixMap(data->imgs[0], pixmap);
showImgPack* pack = new showImgPack();
pack->img = pixmap;
pack->type = imgWidgetOri;
pack->index = index;
emit S1(pack);
}
else
{
showLost++;
qDebug() << cameraModulePtr.moduleName << "Lost show";
}
data->imgMutex.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::init(backEndManager* input)
{
_backEndManager = 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))
{
camera* _camera = new camera(m_module,_backEndManager, cameras.size());
QObject::connect(_camera, &camera::S1, this, &cameraManager::R1);
cameras.insert(std::make_pair(m_module.key.toStdString(), _camera));
emit _backEndManager->sendInitString("相机"+m_module.key + "初始化成功");
qDebug() << "[INFO] " << " initParas success!";
}
else
{
QByteArray errorStr("getStatus");
m_module.module_ptr->sendData(vec_mat,errorStr);
isInit = false;
qCritical() << "[Error] " << " moduleName:" << m_module.moduleName << " initParas failed!";
emit _backEndManager->sendInitString("相机" + m_module.key + "初始化失败->"+ errorStr);
}
}
this->start();
}
bool cameraManager::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;
}
void cameraManager::R1(showImgPack* pack)
{
emit _backEndManager->sendPixmapImageWidget(pack);
}