#include "Hd_CameraModule_readImage.h" using namespace std; #include //data == "rest_newProductIn"时要结束run;机台报警,取图超时,复位后重新拍的情况。 bool ifBreak = false; //ifRunning减少ifBreak的判断;"rest_newProductIn"时,Logic_camera_thread框架会结束未处理完的相相机取图 bool ifRunning = false; QString byteArrayToUnicode(const QByteArray array) { // state用于保存转换状态,它的成员invalidChars,可用来判断是否转换成功 // 如果转换成功,则值为0,如果值大于0,则说明转换失败 QTextCodec::ConverterState state; // 先尝试使用utf-8的方式把QByteArray转换成QString QString text = QTextCodec::codecForName("UTF-8")->toUnicode(array.constData(), array.size(), &state); // 如果转换时无效字符数量大于0,说明编码格式不对 if (state.invalidChars > 0) { // 再尝试使用GBK的方式进行转换,一般就能转换正确(当然也可能是其它格式,但比较少见了) text = QTextCodec::codecForName("GBK")->toUnicode(array); } return text; } vector srcALLmat; Hd_CameraModule_readImage* create() { qDebug() << __FUNCTION__ << " line:" << __LINE__ << "Hd_CameraModule_readImage create success!" ; return new Hd_CameraModule_readImage; } void destory(Hd_CameraModule_readImage* ptr) { srcALLmat.clear(); if (ptr) { delete ptr; ptr = nullptr; } qDebug() << __FUNCTION__ << " line:" << __LINE__ << "Hd_CameraModule_readImage destory success!"; } //类创建 Hd_CameraModule_readImage::Hd_CameraModule_readImage() { } Hd_CameraModule_readImage::~Hd_CameraModule_readImage() { localSourceImgsName.clear(); ifFirst = false; imgIndex = 0; maxStartmain = 0; DirPath = ""; qDebug() << __FUNCTION__ << "line:" << __LINE__ << " delete success!"; } //此函数内存上涨?? //从类中读数据到实例对象。 //每次返回一张图片 bool Hd_CameraModule_readImage::readData(std::vector& mat, QByteArray& data) { mat.push_back(cv::Mat(1200, 1300, CV_8UC1)); return true; if (localSourceImgsName.size() == 0) return false; DWORD start1 = timeGetTime(); int reconnect = 0; if (imgIndex >= localSourceImgsName.size()) { //qDebug() << "[info]" << __FUNCTION__ << "line:" << __LINE__ << " sendData finished!!" ; imgIndex = 0; //return true; } if (!ifFirst)maxStartmain = max(maxStartmain, timeGetTime() - startmain); else ifFirst = false; //qDebug() << "[info]" << __FUNCTION__ << "line:" << __LINE__ << " startmain cutTime::" << timeGetTime() - startmain; //qDebug() << "[info]" << __FUNCTION__ << "line:" << __LINE__ << " maxStartmain cutTime::" << maxStartmain; //图片列表 vector srcmat; for (int i = 0; i < 1; i++) { //srcmat .push_back( srcALLmat[imgIndex++]); QString imgName = localSourceImgsName[imgIndex++]; QString qsourcePath = DirPath + "/" + imgName; //qDebug() << qsourcePath; QByteArray cdata = qsourcePath.toLocal8Bit(); //img路径列表 string sourceImgPath = string(cdata); string path = sourceImgPath; cv::Mat srcmat1; srcmat1 = cv::imread(sourceImgPath); srcmat.push_back(srcmat1); srcmat1.release(); //data = imgName.toLocal8Bit(); //imgName.clear(); //qsourcePath.clear(); //cdata.clear(); } if (srcmat.empty()) { //qCritical() << __FUNCTION__ << " line:" <<__LINE__ << " srcImage is null!"; startmain = timeGetTime(); return false; } mat = srcmat; //srcmat.clear(); if (imgIndex == localSourceImgsName.size()) { //qDebug() << "[info]" << __FUNCTION__ << "line:" << __LINE__ << " sendData finished!!"; //qDebug() << "[info]" << __FUNCTION__ << "line:" << __LINE__ << " sendData cutTime::" << timeGetTime() - start; qDebug() << "[info]" << __FUNCTION__ << "line:" << __LINE__ << " sendData finished , allTime::" << allTime; //8个相机模块,16M读图17ms;65M读图50ms; allTime = 0; } int acut = timeGetTime() - start1; //qDebug() << "[info]" << __FUNCTION__ << "line:" << __LINE__ << " sendData cutTime::" << acut; allTime += acut; startmain = timeGetTime(); //qDebug() << "[info]" << __FUNCTION__ << "line:"<< __LINE__ << " sendData mat channels:" << mat.channels(); return true; } #include // 实例对象把数据写入到类。 bool Hd_CameraModule_readImage::writeData(std::vector &mat,QByteArray &data) { //qDebug() << "[info] "<< __FUNCTION__ << " line:" <<__LINE__ << " start"<<"-------------UserName:"< 0) // SetExposureTime(expourseTimeList[0]); // if (gainList.size() > 0) // SetGain(gainList[0]); //} //getImageNum = 0; if (ifRunning == true) { ifBreak = true; Sleep(10); } return true; } else { if (data.contains("SetExposureTime")) { qDebug() << __FUNCTION__ << " line:" << __LINE__ << "data:" << data; } return true; } /*else { m_flag = false; expourseTimeList.clear(); gainList.clear(); }*/ //m_flag = false; /*QVector vector_data; decodeInitData(data,vector_data); if( vector_data.size() != 1 ) { return false; };*/ QString lastName = data; DirPath = "C:/testImg/source"+ lastName[lastName.size()-1];// vector_data[0];// .toStdString(); //读图模块Username,为文件夹路径 start = timeGetTime(); QStringList imgFilters; imgFilters << "*.jpg" << "*.png" << "*.PNG" << "*.gif" << "*.tif" << "*.jpeg" << "*.bmp"; QDir qdir(DirPath); localSourceImgsName.clear(); //返回文件名列表 localSourceImgsName = qdir.entryList(imgFilters, QDir::Files | QDir::Readable, QDir::Name); //qDebug() << "localSourceImgsName.size()::" << localSourceImgsName.size(); qDebug() << __FUNCTION__ << " line:" << __LINE__ << " run start DirPath::" << DirPath; startmain = timeGetTime(); ifFirst = true; return true; } //数据解析, bool Hd_CameraModule_readImage::decodeInitData(QByteArray byteArray, QVector& vector_data) { /*while (!byteArray.isEmpty()) { int type = 0; QByteArray m_type = byteArray.mid(0, 4); memcpy((void*)&type, (void*)m_type.data(), 4); byteArray.remove(0, 4); int len = 0; QByteArray m_len = byteArray.mid(0, 4); memcpy((void*)&len, (void*)m_len.data(), 4); byteArray.remove(0, 4); QByteArray mm_data = byteArray.mid(0, len); vector_data.append(mm_data); byteArray.remove(0, len); }*/ return true; } //递归读取文件夹及子文件夹内所有图片 QStringList findFiles(const QString& startDir, const QStringList& filters) { QStringList names; QDir dir(startDir); const auto files = dir.entryList(filters, QDir::Files); for (const QString& file : files) { names += startDir + "/" + file; } const auto subdirs = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot); for (const QString& subdir : subdirs) { names += findFiles(startDir + "/" + subdir, filters); } return names; } //传入initParas函数,格式为:相机key+参数名1#参数值1#参数值2+参数名2#参数值1... //初始化 bool Hd_CameraModule_readImage::initParas(QByteArray& initData) { QByteArrayList valueList = initData.split('+'); QString lastName = valueList[0]; if (initData.contains("onlySetParameter")) { qDebug() << __FUNCTION__ << " line:" << __LINE__ << "onlySetParameter,lastName:" << lastName; if (valueList.size() > 1) for (int i = 1; i < valueList.size(); i++) { QString valuei = byteArrayToUnicode(valueList[i]); qDebug() << __FUNCTION__ << " line:" << __LINE__ << "onlySetParameter,lastName:" << lastName<<" ,valuei:"<< valuei; } return true; } //DirPath = "C:/testImg/source" + lastName[lastName.size() - 1];// vector_data[0];// .toStdString(); //读图模块Username,为文件夹路径 DirPath = "C:/testImg/" + lastName; start = timeGetTime(); QStringList imgFilters; imgFilters << "*.jpg" << "*.png" << "*.PNG" << "*.gif" << "*.tif" << "*.jpeg" << "*.bmp"; QDir qdir(DirPath); localSourceImgsName.clear(); //返回文件名列表 localSourceImgsName = qdir.entryList(imgFilters, QDir::Files | QDir::Readable, QDir::Name); //qDebug() << "localSourceImgsName.size()::" << localSourceImgsName.size(); qDebug() << __FUNCTION__ << " line:" << __LINE__ << " run start DirPath::" << DirPath; startmain = timeGetTime(); ifFirst = true; ////加载到内存队列 //for (int i = 0; i < localSourceImgsName.size(); i++) //{ // QString imgName = localSourceImgsName[i]; // QString qsourcePath = DirPath + "/" + imgName; // //qDebug() << qsourcePath; // QByteArray cdata = qsourcePath.toLocal8Bit(); // //img路径列表 // string sourceImgPath = string(cdata); // string path = sourceImgPath; // cv::Mat srcmat1; // srcmat1 = cv::imread(sourceImgPath); // srcALLmat.push_back(srcmat1); // srcmat1.release(); //} //QStringList imgFilters; //imgFilters << "*.jpg" << "*.png" << "*.PNG" << "*.gif" << "*.tif" << "*.jpeg" << "*.bmp"; //QDir qdir(DirPath); //localSourceImgsName.clear(); //返回文件名列表 //localSourceImgsName = qdir.entryList(imgFilters, QDir::Files | QDir::Readable, QDir::Name); //需要递归查找子文件夹内图片的方式 //QStringList localSourceImgsName = findFiles(DirPath, QStringList() << "*.BMP" << "*png"); return true; } bool Hd_CameraModule_readImage::run() { //qDebug() << __FUNCTION__ << " line:" << __LINE__ << " run start DirPath::" << DirPath; try { int timeout_flag = 0; /*DWORD start = timeGetTime(); while (true) { DWORD ts = timeGetTime() - start; if ((DWORD)timeout_ms < ts) { timeout_flag = 1; break; } if (m_flag) { break; } Sleep(1); } m_flag = false; if (timeout_flag == 1) { qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ---------- run timeout error! "; }*/ } catch (QString ev) { qDebug() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev; } return true; } //相机连接状态:正常连接或断开 bool Hd_CameraModule_readImage::checkStatus() { return moduleStatus; }