147 lines
3.8 KiB
C++
147 lines
3.8 KiB
C++
#include <qimage.h>
|
|
#include <qpixmap.h>
|
|
#include <opencv.hpp>
|
|
#include <qdebug.h>
|
|
#include <memory>
|
|
|
|
#pragma execution_character_set("utf-8")
|
|
#define sColor_blue "51, 56, 69"
|
|
#define sColor_blueWithWhite "64, 68, 81"
|
|
#define sColor_blueWithBlack "46, 50, 52"
|
|
|
|
#define sColor_Red "232, 17, 35"
|
|
#define sColor_RedWithWhite "241, 112, 122"
|
|
|
|
#define sColor_Green "17, 232, 35"
|
|
#define sColor_GreenWithWhite "112, 241, 122"
|
|
|
|
#define sColor_GoldWithBlack "200, 165, 100"
|
|
#define sColor_Gold "223, 185, 146"
|
|
#define sColor_GoldWithWhite "250, 222, 189"
|
|
|
|
#define sColor_Black "22, 22, 22"
|
|
#define sColor_BlackWithWhite "45, 45, 45"
|
|
|
|
//窗口设置
|
|
#define windowMoveFreshRate 100//窗口移动每秒的刷新率
|
|
|
|
|
|
|
|
enum backEndError
|
|
{
|
|
dllNotFound,
|
|
triggerInitFailure,
|
|
triggerConnectionError,
|
|
camerInitFailure
|
|
};
|
|
enum initStatus
|
|
{
|
|
NotInit,
|
|
InitFailure,
|
|
InitOK
|
|
};
|
|
enum imgWidgetShowType
|
|
{
|
|
imgWidgetOri,
|
|
imgWidgetRes
|
|
};
|
|
struct showImgPack
|
|
{
|
|
QPixmap img;
|
|
imgWidgetShowType type;
|
|
int index = -1;
|
|
QString name = "";
|
|
};
|
|
struct historyPack
|
|
{
|
|
QPixmap img;
|
|
std::shared_ptr<std::string>filePath;
|
|
std::vector<std::string>text;
|
|
};
|
|
static QImage cvMatToQImage(cv::Mat& src)
|
|
{
|
|
if (src.channels() == 1) { // if grayscale image
|
|
return QImage((uchar*)src.data, src.cols, src.rows, static_cast<int>(src.step), QImage::Format_Grayscale8).copy();
|
|
}
|
|
if (src.channels() == 3) { // if 3 channel color image
|
|
cv::Mat rgbMat;
|
|
cv::cvtColor(src, rgbMat, cv::COLOR_BGR2RGB); // invert BGR to RGB
|
|
return QImage((uchar*)rgbMat.data, src.cols, src.rows, static_cast<int>(src.step), QImage::Format_RGB888).copy();
|
|
}
|
|
return QImage();
|
|
}
|
|
static cv::Mat qImageToCvMat(const QImage& inImage)
|
|
{
|
|
switch (inImage.format())
|
|
{
|
|
case QImage::Format_RGB32:
|
|
case QImage::Format_RGB888:
|
|
{
|
|
|
|
QImage swapped = inImage;
|
|
if (inImage.format() == QImage::Format_RGB32)
|
|
{
|
|
swapped = swapped.convertToFormat(QImage::Format_RGB888);
|
|
}
|
|
cv::Mat matImg = cv::Mat(swapped.height(), swapped.width(),
|
|
CV_8UC3,
|
|
const_cast<uchar*>(swapped.bits()),
|
|
static_cast<size_t>(swapped.bytesPerLine())
|
|
);
|
|
cv::cvtColor(matImg, matImg, cv::COLOR_RGB2BGR);
|
|
return matImg.clone();
|
|
}
|
|
case QImage::Format_Indexed8:
|
|
{
|
|
cv::Mat mat(inImage.height(), inImage.width(),
|
|
CV_8UC1,
|
|
const_cast<uchar*>(inImage.bits()),
|
|
static_cast<size_t>(inImage.bytesPerLine())
|
|
);
|
|
return mat.clone();
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
return cv::Mat();
|
|
}
|
|
template<typename T>
|
|
T getMax(const T& a, const T& b) {
|
|
return (a > b) ? a : b;
|
|
}
|
|
template<typename T>
|
|
T getMin(const T& a, const T& b) {
|
|
return (a < b) ? a : b;
|
|
}
|
|
template<typename T>
|
|
T getAbs(const T& value) {
|
|
return (value < 0) ? -value : value;
|
|
}
|
|
static void cvMatToQPixMap(cv::Mat& src,QPixmap& pixmap)
|
|
{
|
|
pixmap = QPixmap::fromImage(cvMatToQImage(src));
|
|
}
|
|
static QPixmap cvMatToQPixMap(cv::Mat& src)
|
|
{
|
|
return QPixmap::fromImage(cvMatToQImage(src));
|
|
}
|
|
static std::string getCurrentDateTime() {
|
|
|
|
// 获取当前时间点
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
// 转换为字符串
|
|
auto currentTime = std::chrono::system_clock::to_time_t(now);
|
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 100;
|
|
std::tm* localTime = std::localtime(¤tTime);
|
|
std::stringstream ss;
|
|
ss << std::put_time(localTime, "%H-%M-%S") << "-" << std::setfill('0') << std::setw(2) << ms.count();
|
|
return ss.str();
|
|
}
|
|
Q_DECLARE_METATYPE(imgWidgetShowType)
|
|
Q_DECLARE_METATYPE(cv::Mat*)
|
|
Q_DECLARE_METATYPE(cv::Mat)
|
|
Q_DECLARE_METATYPE(std::string)
|
|
Q_DECLARE_METATYPE(historyPack)
|
|
Q_DECLARE_METATYPE(Qt::GlobalColor)
|
|
#pragma once |