culturered_client/ProjectorDisplay/WhiteBlackWidget.cpp

74 lines
2.2 KiB
C++
Raw Normal View History

2024-09-07 03:34:44 +00:00
#include "WhiteBlackWidget.h"
#include <QApplication>
#include <QImage>
#include <QHBoxLayout>
#include <QResizeEvent>
#include <QPainter>
//#include <opencv2/opencv.hpp>
#include "DisplayMovieWidget.h"
WhiteBlackWidget::WhiteBlackWidget(QWidget* parent/* = nullptr*/) : MovieWidget(parent) {
InitImage();
}
void WhiteBlackWidget::OnTimeout() {
int maskCount = DisplayMovieWidget::Get().GetMaskCount();
m_current = (m_current + 1) % maskCount;
update();
}
void WhiteBlackWidget::InitImage() {
const QString imagePath = QApplication::applicationDirPath() + "/ProjectDisplay/1/1.png";
QImage image(imagePath);
QImage grayImage = image.convertToFormat(QImage::Format_Grayscale8);
m_currentPixmap = QPixmap::fromImage(grayImage);
//m_display->setPixmap(pixmap);
}
void WhiteBlackWidget::paintEvent(QPaintEvent* evnet) {
if (m_currentPixmap.isNull() || DisplayMovieWidget::Get().IsMaskEmpty()) {
return;
}
int x = (width() - m_currentPixmap.width()) * 0.5;
int y = (height() - m_currentPixmap.height()) * 0.5;
QPainter painter(this);
// 加载遮罩图像
const QPixmap maskPixmap = DisplayMovieWidget::Get().GetMaskPixmap(m_current); // 替换为您的遮罩图像路径
QImage maskImage = maskPixmap.toImage();
//cv::Mat cvImage(maskImage.height(), maskImage.width(), CV_8UC4, maskImage.bits(), maskImage.bytesPerLine());
// 指定缩放的目标大小
int targetWidth = 3700;
int targetHeight = 737;
//cv::cvtColor(cvImage, cvImage, cv::COLOR_RGB2RGBA);
// cv::threshold(cvImage, cvImage, 0, 255, cv2.THRESH_BINARY)
// 计算缩放比例
//double scaleFactor = std::min((double)targetWidth / cvImage.cols, (double)targetHeight / cvImage.rows);
//// 计算新的宽度和高度
//int newWidth = 3700; // cvImage.cols * scaleFactor;
//int newHeight = 737; // cvImage.rows * scaleFactor;
//cv::Mat resizedImage;
//cv::resize(cvImage, resizedImage, cv::Size(newWidth, newHeight));
//
//QImage img(resizedImage.data, resizedImage.cols, resizedImage.rows, resizedImage.step, QImage::Format_ARGB32);
//
//int y1 = (height() - 737) / 2;
//painter.drawImage(0, y1, img);
}