265 lines
7.3 KiB
C++
265 lines
7.3 KiB
C++
#include "camera_thread.h"
|
|
#include <filesystem>
|
|
#include <io.h>
|
|
#include <tchar.h>
|
|
#include <FrontendBackendBridge.h>
|
|
QQueue<dataPack> ImageDataPack;
|
|
MyThread::~MyThread()
|
|
{
|
|
this->setStopRun();
|
|
wait(100);
|
|
exit();
|
|
qDebug() << "[INFO] " << " m_name:" << m_name << " delete thread success! ";
|
|
}
|
|
|
|
//bool MyThread::setModule(QString station_name, QString name, QString type, MyModule module, QJsonObject obj)
|
|
bool MyThread::setModule(QString station_name, QString name, QString type, MyModule module)
|
|
{
|
|
m_name = name;
|
|
m_triggermodel = type;
|
|
m_Module = module;
|
|
// m_moduleobj = obj;
|
|
m_stationName = station_name;
|
|
m_staticName = m_name;
|
|
qCritical() << "[DEBUG] " << " name " << name;
|
|
|
|
//QString image_queue_num = m_moduleobj.value("image_queue_num").toString();
|
|
//picNum = image_queue_num.toInt();
|
|
if (m_triggermodel.isEmpty())
|
|
{
|
|
qCritical() << "[Error] " << " trigger_mod is null ! ";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
void MyThread::run()
|
|
{
|
|
try
|
|
{
|
|
qDebug() << "[INFO] " << " m_type:" << m_type << " module start time: " << "now!";
|
|
if (!runCameraModule())
|
|
{
|
|
qCritical() << "[Error] " << "runCameraModule run failed! ";
|
|
return;
|
|
}
|
|
|
|
}
|
|
catch (QString ev)
|
|
{
|
|
qCritical() << "[Error] " << "catch ev:" << ev;
|
|
return;
|
|
}
|
|
//qDebug() << __FUNCTION__ << " line:" << __LINE__ << " module run end time :" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz") ;
|
|
}
|
|
void MyThread::setMutex(QMutex* mutex)
|
|
{
|
|
m_mutex = mutex;
|
|
}
|
|
bool MyThread::cameraTrigger()
|
|
{
|
|
if (!m_Module.module_ptr->run())
|
|
{
|
|
qCritical() << "[Error] " << " m_name:" << m_name << " run failed!";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
void MyThread::getImageFromCamera(std::vector<cv::Mat>& mats)
|
|
{
|
|
std::vector<cv::Mat> camera_mat;
|
|
QByteArray task_name;
|
|
if (!m_Module.module_ptr->sendData(camera_mat, task_name))
|
|
qCritical() << "[Error] " << " camera_mat is error! ";
|
|
qDebug() << __FUNCTION__ << " line:" << __LINE__ << "camera_mat size:"<< camera_mat.size();
|
|
|
|
for (auto img : camera_mat)
|
|
mats.push_back(img.clone());
|
|
}
|
|
void MyThread::setExposureTime(float value)
|
|
{
|
|
QByteArray task_name("exposure_" + QByteArray::number(value));
|
|
std::vector<cv::Mat> camera_mat;
|
|
if (!m_Module.module_ptr->sendData(camera_mat, task_name))
|
|
qCritical() << "[Error] " << "set exposure error!";
|
|
}
|
|
void MyThread::setUserSet(int value)
|
|
{
|
|
QByteArray task_name("userset_" + QByteArray::number(value));
|
|
std::vector<cv::Mat> camera_mat;
|
|
if (!m_Module.module_ptr->sendData(camera_mat, task_name))
|
|
qCritical() << "[Error] " << "set exposure error!";
|
|
}
|
|
void MyThread::setLightSourceValue(int aLine = -1, int bLine = -1, int cLine = -1, int dLine = -1,QString Name="")
|
|
{
|
|
//ex:"SA0200#SB0100#SC000#"
|
|
std::string aLineStr(std::to_string(aLine)),
|
|
bLineStr(std::to_string(bLine)),
|
|
cLineStr(std::to_string(cLine)),
|
|
dLineStr(std::to_string(dLine)),
|
|
controllCommand("");
|
|
//
|
|
if (aLine != -1)
|
|
{
|
|
controllCommand += "SA0";
|
|
if (aLineStr.length() < 3)
|
|
for (int i = 0; i < 3 - aLineStr.length(); i++)
|
|
controllCommand += "0";
|
|
controllCommand += aLineStr + "#";
|
|
}
|
|
//
|
|
if (bLine != -1)
|
|
{
|
|
controllCommand += "SB0";
|
|
if (bLineStr.length() < 3)
|
|
for (int i = 0; i < 3 - bLineStr.length(); i++)
|
|
controllCommand += "0";
|
|
controllCommand += bLineStr + "#";
|
|
}
|
|
//
|
|
if (cLine != -1)
|
|
{
|
|
controllCommand += "SC0";
|
|
if (cLineStr.length() < 3)
|
|
for (int i = 0; i < 3 - cLineStr.length(); i++)
|
|
controllCommand += "0";
|
|
controllCommand += cLineStr + "#";
|
|
}
|
|
//
|
|
if (dLine != -1)
|
|
{
|
|
controllCommand += "SD0";
|
|
if (dLineStr.length() < 3)
|
|
for (int i = 0; i < 3 - dLineStr.length(); i++)
|
|
controllCommand += "0";
|
|
controllCommand += dLineStr + "#";
|
|
}
|
|
QSerialPort* Port = bridge.back.getSerialPort(Name);
|
|
if (Port != NULL)
|
|
{
|
|
Port->write(controllCommand.c_str());
|
|
Port->waitForBytesWritten();
|
|
}
|
|
}
|
|
void MyThread::takeImg(unsigned long long timeCode, filmMode type, QString name,detData * datas,QString FinsAfter)
|
|
{
|
|
finsAfter = FinsAfter;
|
|
if (currentHash != 0)//currentHash会在完成触发后清零,在runCameraModule()
|
|
qDebug() << "[WARNING]当前模块似乎有没完成的触发,请检查信号是否完整 hashCode:" << currentHash;
|
|
globalMode = type;
|
|
if (name != "")
|
|
{
|
|
m_name = name;
|
|
}
|
|
else
|
|
{
|
|
m_name = m_staticName;
|
|
}
|
|
gloabalMats = &datas->imgs;
|
|
currentHash = timeCode;
|
|
globalDetData = datas;
|
|
this->start();
|
|
}
|
|
bool MyThread::runCameraModule()
|
|
{
|
|
QByteArray task_name;
|
|
std::vector<cv::Mat> camera_mat;
|
|
// 判断触发模式
|
|
if (m_triggermodel.contains("software"))
|
|
{
|
|
camera_mat = cameraModule(globalMode);
|
|
}
|
|
else if (m_triggermodel.contains("hardware")) // 硬触发 hardware_trigger
|
|
{
|
|
camera_mat = cameraModule(hardware_trigger);
|
|
}
|
|
if (finsAfter != "")
|
|
bridge.back.sendFinsMsg(finsAfter);
|
|
if (camera_mat.empty())
|
|
{
|
|
qCritical() << "[Error] " << " camera_mat is null! ";
|
|
return false;
|
|
}
|
|
mutex.lock();
|
|
#if 0 //使用容器 bug
|
|
cameraImgsPtr->insertImg(currentHash, &camera_mat);
|
|
#endif
|
|
#if 1//使用指针
|
|
if (gloabalMats != nullptr)
|
|
{
|
|
if (globalDetData != nullptr)
|
|
{
|
|
//globalDetData->imgsMutex.lock();
|
|
for (auto img : camera_mat)
|
|
gloabalMats->push_back(img);
|
|
//globalDetData->imgsMutex.unlock();
|
|
}
|
|
else
|
|
{
|
|
for (auto img : camera_mat)
|
|
gloabalMats->push_back(img);
|
|
}
|
|
cout << "sucess" << currentHash << endl;
|
|
|
|
|
|
}
|
|
#endif
|
|
globalDetData = nullptr;
|
|
mutex.unlock();
|
|
|
|
camera_mat.clear();
|
|
currentHash = 0;
|
|
|
|
return true;
|
|
}
|
|
std::vector<cv::Mat> MyThread::getImage(int amount)
|
|
{
|
|
std::vector<cv::Mat> mats;
|
|
if (detImgVec.size() < amount)//数量不满足要求
|
|
return mats;
|
|
mutex.lock();
|
|
mats.swap(detImgVec);
|
|
mutex.unlock();
|
|
return mats;
|
|
}
|
|
std::vector<cv::Mat> MyThread::cameraModule(filmMode type)
|
|
{
|
|
std::vector<cv::Mat> mats;
|
|
|
|
QByteArray task_name;
|
|
std::vector<cv::Mat> camera_mat;
|
|
cv::Mat a, b, c;
|
|
switch (type)
|
|
{
|
|
case MyThread::hardware_trigger:
|
|
getImageFromCamera(camera_mat);
|
|
break;
|
|
case MyThread::normal:
|
|
if (!cameraTrigger())
|
|
{
|
|
cv::Mat nullMat = cv::Mat::zeros(cv::Size(500, 500), CV_8UC1);
|
|
camera_mat.push_back(nullMat);
|
|
}
|
|
else
|
|
{
|
|
getImageFromCamera(camera_mat);
|
|
}
|
|
break;
|
|
}
|
|
return camera_mat;
|
|
}
|
|
bool MyThread::sendDataCommond(QString m_type, cv::Mat mat, QByteArray byte)
|
|
{
|
|
QMutexLocker locker(m_mutex); // 加锁
|
|
|
|
dataPack m_pack;
|
|
m_pack.mat = mat;
|
|
m_pack.byte = byte;
|
|
m_pack.retFlag = m_type;
|
|
m_pack.name = m_name;//名字
|
|
m_pack.showIndex = showIndex;//第几个窗口
|
|
// 界面显示包
|
|
ImageDataPack.append(m_pack);
|
|
QMetaObject::invokeMethod(m_obj, "reciveImageData");
|
|
return true;
|
|
} |