代码:
GraphViewTest::GraphViewTest(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
m_bll = BllData::getInstance();
connect(m_bll, &BllData::returnImgDataSignal, this, &GraphViewTest::returnImgDataSlot);
ui.graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui.graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// 设置场景范围
ui.graphicsView->resize(1440, 1120);
ui.graphicsView->setSceneRect(INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX);
// 反锯齿
ui.graphicsView->setRenderHints(QPainter::Antialiasing);
m_scene.setBackgroundBrush(Qt::gray);
ui.graphicsView->setScene(&m_scene);
}
GraphViewTest::~GraphViewTest()
{
m_bll->~BllData();
}
void GraphViewTest::on_startBtn_clicked() {
m_bll->startAcq();
}
void GraphViewTest::on_stopBtn_clicked() {
m_bll->stopAcq();
}
void GraphViewTest::on_openBtn_clicked() {
QString str = "打开文件";
QString filepath = QFileDialog::getOpenFileName(this, str);
qDebug() << "---------------filepath: " << filepath;
QByteArray cdata = filepath.toLocal8Bit();
QStringList listfile = filepath.split('.');
QString hou = listfile.takeLast();
if (hou == "tif" || hou == "tiff" || hou == "jpg" || hou == "png") {
cv::Mat srcimg = cv::imread(std::string(cdata), cv::IMREAD_UNCHANGED);
int rows = srcimg.rows;
int cols = srcimg.cols;
if (rows == BllData::ImageSizeRow && cols == BllData::ImageSizeColumn) {
quint16* img = BllData::getSingleImg();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
img[i * cols + j] = srcimg.at<ushort>(i, j);
}
}
static int cnt = 0;
cnt++;
if (cnt == 1) {
ImageItemObj* imgdata = new ImageItemObj;
connect(m_bll, &BllData::updateImgDataSignal, imgdata, &ImageItemObj::updateImgDataSlot);
m_scene.addItem(imgdata);
}
m_bll->updateImgData();
}
}
}
void GraphViewTest::returnImgDataSlot() {
static int cnt = 0;
cnt++;
if (cnt == 1) {
imgdata = new ImageItemObj;
connect(m_bll, &BllData::updateImgDataSignal, imgdata, &ImageItemObj::updateImgDataSlot);
m_scene.addItem(imgdata);
}
m_bll->updateImgData();
m_scene.update();
}
ImageItemObj::ImageItemObj()
: QGraphicsPixmapItem()
{
m_bll = BllData::getInstance();
}
ImageItemObj::~ImageItemObj()
{}
QRectF ImageItemObj::boundingRect() const {
return QRectF(-4, -4, 8, 8);
}
void ImageItemObj::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
Q_UNUSED(option);
Q_UNUSED(widget);
qDebug() << "ImageItemObj------------paint----------";
/**/
quint16* img = BllData::getSingleImg();
uchar* imgbit8 = BllData::getImg8bit();
BllData::converToGray(img, imgbit8, m_width, m_center);
QRect target(-720, -560, 1440, 1120);
QImage image(imgbit8, BllData::ImageSizeColumn, BllData::ImageSizeRow, QImage::Format_Grayscale8);
painter->drawImage(target, image);
}
void ImageItemObj::updateImgDataSlot() {
quint16* img = BllData::getSingleImg();
qDebug() << "updateImgDataSlot--------------------data: "<< img[50*50];
int min, max;
BllData::getMinMaxData(img, min, max);
m_width = max - min;
m_center = (max + min) / 2;
update();
}
ProduceImgObj::ProduceImgObj(QObject *parent)
: QObject(parent)
{
m_acqEnable = false;
m_path = "";
}
ProduceImgObj::~ProduceImgObj()
{
qDebug() << "~ProduceImgObj()------------------start-----";
if (m_timer->isActive())
{
m_timer->stop();
delete m_timer;
}
qDebug() << "~ProduceImgObj()------------------end-----";
}
void ProduceImgObj::iniDetSlot() {
m_path = "bga";
m_timer = new QTimer;
m_imgdata = new quint16[ImageSizeRow * ImageSizeColumn];
memset(m_imgdata, 0, sizeof(quint16) * ImageSizeRow * ImageSizeColumn);
connect(m_timer, SIGNAL(timeout()), this, SLOT(timeSlot()));
}
void ProduceImgObj::startAcqDataSlot() {
m_acqEnable = true;
m_timer->start(16);
}
void ProduceImgObj::stopAcqDataSlot() {
m_acqEnable = false;
}
void ProduceImgObj::timeSlot() {
if (!m_acqEnable)
return;
QString based = m_path;
int rnum = qrand() % 35 + 1;
based.append("/").append(QString::number(rnum)).append(".tiff");
qDebug() << "filepath: " << based;
QByteArray cdata = based.toLocal8Bit();
cv::Mat imgm = imread(std::string(cdata), cv::IMREAD_UNCHANGED);
if (imgm.empty())
return;
if (imgm.depth() > 0) {
qDebug() << "imgm.rows: " << imgm.rows;
for (int i = 0; i < imgm.rows; i++) {
for (int j = 0; j < imgm.cols; j++) {
int data = imgm.at<ushort>(i, j);
m_imgdata[i * imgm.cols + j] = data;
}
}
}
emit returnImgDataSignal(m_imgdata);
}
BllData::BllData(QObject *parent)
: QObject(parent)
{
m_produce = new ProduceImgObj;
m_thread = new QThread;
m_produce->moveToThread(m_thread);
connect(m_thread, &QThread::finished, m_produce, &ProduceImgObj::deleteLater);
connect(this, &BllData::iniDataSignal, m_produce, &ProduceImgObj::iniDetSlot);
connect(this, &BllData::startAcqSignal, m_produce, &ProduceImgObj::startAcqDataSlot);
connect(this, &BllData::stopAcqSignal, m_produce, &ProduceImgObj::stopAcqDataSlot);
connect(m_produce, &ProduceImgObj::returnImgDataSignal, this, &BllData::returnImgDataSlot);
emit iniDataSignal();
m_thread->start();
}
BllData::~BllData()
{
if (m_thread) {
if (m_thread->isRunning()) {
m_thread->quit();
m_thread->wait();
}
delete m_thread;
m_thread = NULL;
}
}
BllData* BllData::m_instance = 0;
quint16* BllData::m_img = 0;
uchar* BllData::m_img8bit = 0;
void BllData::updateImgData() {
emit updateImgDataSignal();
}
void BllData::startAcq() {
if (!m_thread->isRunning())
m_thread->start();
emit startAcqSignal();
}
void BllData::stopAcq() {
emit stopAcqSignal();
}
void BllData::returnImgDataSlot(quint16* img) {
quint16* mimg = BllData::getSingleImg();
memcpy(mimg, img, ImageSizeRow * ImageSizeColumn * sizeof(quint16));
emit returnImgDataSignal();
}