结构调整

This commit is contained in:
RCi
2025-07-27 16:12:36 +08:00
parent c19b1b5cd7
commit 64e5d663ca
69 changed files with 24 additions and 1 deletions
@@ -0,0 +1,65 @@
# 获取获取当前目录名
get_filename_component(CURRENT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(TIMESTAMP RUN_TIME)
message( " model start : " ${CURRENT_FOLDER}:${RUN_TIME})
set(DLL_NAMES "opencv")
foreach(child ${DLL_NAMES})
LIST(APPEND ENV_INCLUDE ${${child}_include})
LIST(APPEND ENV_LIB ${${child}_lib})
LIST(APPEND ENV_TARGET ${${child}_target_link})
endforeach()
message(" ENV_INCLUDE : " ${ENV_INCLUDE} )
message(" ENV_LIB : " ${ENV_LIB} )
message(" ENV_TARGET : " ${ENV_TARGET} )
include_directories(
${ENV_INCLUDE}
)
link_directories (
${ENV_LIB}
)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
SET(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
FILE(GLOB HEAD_FILES "./*.h" )
FILE(GLOB SRC_FILES "./*.cpp")
FILE(GLOB UI_FILES "./*.ui")
#find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
add_library(Hd_CameraModule_readImage SHARED
${HEAD_FILES}
${SRC_FILES}
${UI_FILES}
)
set_target_properties(${CURRENT_FOLDER} PROPERTIES LINKER_LANGUAGE C
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}
)
target_link_libraries(Hd_CameraModule_readImage PRIVATE
${qt_target_link}
${MV_target_link}
${opencv_target_link}
Qt${QT_VERSION_MAJOR}::Core
)
string(TIMESTAMP RUN_TIME)
message( " model end : " ${CURRENT_FOLDER}:${RUN_TIME})
@@ -0,0 +1,347 @@
#include "Hd_CameraModule_readImage.h"
using namespace std;
#include <QTextCodec>
//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<cv::Mat> 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<cv::Mat>& mat, QByteArray& data)
{
mat.push_back(cv::Mat(8000, 6300, 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<cv::Mat> 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 <qdir.h>
// 实例对象把数据写入到类。
bool Hd_CameraModule_readImage::writeData(std::vector<cv::Mat> &mat,QByteArray &data)
{
//qDebug() << "[info] "<< __FUNCTION__ << " line:" <<__LINE__ << " start"<<"-------------UserName:"<<data;
Q_UNUSED(mat);
//2024.02.18更新,相机对应的读SN信号时,表示新产品进入,设置初始值
if (data == "rest_newProductIn")
{
////根据实际项目,设置初始值:曝光、增益,ROI区域等
//{
// if (expourseTimeList.size() > 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<QByteArray> 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<QByteArray>& 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;
}
@@ -0,0 +1,75 @@
#ifndef Hd_CameraModule_readImage_H
#define Hd_CameraModule_readImage_H
#include <QtCore/qglobal.h>
#include<opencv2/opencv.hpp>
#include <opencv.hpp>
#include <QByteArray>
#include <QDebug>
#include<iostream>
#include <Windows.h>
#include <time.h>
#include <QScopedPointer>
#include <QThread>
#include "base.h"
using namespace cv;
#pragma comment(lib, "winmm.lib")
#pragma execution_character_set("utf-8")
////static cv::Mat srcImage;
//static vector<cv::Mat> srcmat;
//static int index=0; //相机序号
//static void* handle;
//static bool m_flag = false;
static bool moduleStatus = true;
class Hd_CameraModule_readImage :public Base
{
public:
Hd_CameraModule_readImage();
virtual ~Hd_CameraModule_readImage();
//从类中读数据到实例对象。
bool readData(std::vector<cv::Mat>&,QByteArray&);
// 实例对象把数据写入到类。
bool writeData(std::vector<cv::Mat>&,QByteArray&);
//初始化
bool initParas(QByteArray&);
bool run();
bool decodeInitData(QByteArray byteArray, QVector<QByteArray>& vector_data);
bool checkStatus();
private:
int timeout_ms = 5000;
//计算读图CT
DWORD startmain=0;
//最大读图时间
int maxStartmain=0;
//是否为第一个
bool ifFirst=false;
//睡眠时间
int sleeptime=0;
//文件夹读到的所有图片
QStringList localSourceImgsName;
int imgIndex=0;
DWORD start, stop;
int allTime=0;
//图片文件夹路径
QString DirPath = "";
};
extern "C"
{
Q_DECL_EXPORT Hd_CameraModule_readImage * create();
Q_DECL_EXPORT void destory(Hd_CameraModule_readImage * ptr);
}
#endif // Hd_readImage_module_H
@@ -0,0 +1,31 @@
#ifndef __BASECLASS_H__
#define __BASECLASS_H__
#include <iostream>
#include <QObject>
#include <QByteArray>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
class Base :public QObject
{
Q_OBJECT
public:
bool init_flag; //是否初始化,false,true
bool status = true; //相机状态(判断相机是否断开连接)
std::string paras; //传输数据
cv::Mat m_mat; //传输图片
std::string moduleName; //模块名称
public:
Base() {}
virtual ~Base() {}
virtual bool readData(std::vector<cv::Mat>&,QByteArray&){return true;}
virtual bool writeData(std::vector<cv::Mat>&,QByteArray&){return true;}
virtual bool initParas(QByteArray&){return true;}
virtual bool run() {return true;}
virtual bool checkStatus() { return true; }
};
#endif