forked from CodElecCz/CameraUIv1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraViewSingle.cpp
183 lines (152 loc) · 4.93 KB
/
CameraViewSingle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>
#include <time.h>
#include "CameraViewSingle.h"
#include "ui_CameraViewSingle.h"
CameraViewSingle::CameraViewSingle(QWidget *parent, QString name) :
QWidget(parent),
ui(new Ui::CameraViewSingle),
view(new QGraphicsView(this)),
scene(new QGraphicsScene(this)),
toolbox(new QButtonGroup(this)),
cameraName(new QLabel(this)),
cameraInfo(new QLabel(this)),
initFitInView(false)
{
ui->setupUi(this);
QVBoxLayout *centralLayout = new QVBoxLayout(this);
centralLayout->setSpacing(0);
centralLayout->setMargin(0);
//centralLayout->addSpacing(17); //label on top
centralLayout->addWidget(view, 1);
QHBoxLayout *toolboxLayout = new QHBoxLayout;
toolboxLayout->setSpacing(0);
toolboxLayout->setMargin(0);
QPushButton *buttonPlus = new QPushButton(QIcon("://icons/zoom_in.png"), QString(""), this);
buttonPlus->setMaximumWidth(32);
toolboxLayout->addWidget(buttonPlus);
QPushButton *buttonMinus = new QPushButton(QIcon("://icons/zoom_out.png"), QString(""), this);
buttonMinus->setMaximumWidth(32);
toolboxLayout->addWidget(buttonMinus);
QPushButton *buttonFit = new QPushButton(QIcon("://icons/zoom_fit.png"), QString(""), this);
buttonFit->setMaximumWidth(32);
toolboxLayout->addWidget(buttonFit);
toolboxLayout->addStretch();
toolboxLayout->addWidget(cameraName);
toolboxLayout->addWidget(cameraInfo);
toolboxLayout->addStretch();
centralLayout->addLayout(toolboxLayout);
toolbox->addButton(buttonPlus, 0);
toolbox->addButton(buttonMinus, 1);
toolbox->addButton(buttonFit, 2);
connect(toolbox, SIGNAL(buttonClicked(int)), this, SLOT(buttonClicked(int)));
view->setScene(scene);
setViewName(name);
}
CameraViewSingle::~CameraViewSingle()
{
delete ui;
}
void CameraViewSingle::setViewName(QString name)
{
cameraName->setText(name);
}
void CameraViewSingle::setError(QString msg)
{
scene->clear();
view->update();
error = msg;
cameraInfo->setText(" [Error]");
cameraInfo->setToolTip(msg);
}
void CameraViewSingle::updateImage(const QImage& image)
{
//image was resized
if(image.width() != scene->width() || image.height() != scene->height())
initFitInView = false;
//GraphicView resized
static int widthV_old = 0;
static int heightV_old = 0;
int widthV = view->rect().width();
int heightV = view->rect().height();
if(widthV_old != widthV || heightV_old!=heightV)
{
initFitInView = false;
widthV_old = widthV;
heightV_old = heightV;
}
scene->clear();
scene->setSceneRect(image.rect());
pixMap = QPixmap::fromImage(image);
qImage = image.copy();
scene->addPixmap(pixMap);
//ROIs
//scene->addRect(0,0,scene->width(), scene->height(), QPen(QColor("green")), QBrush());
view->update();
//first image adjustment
if(!initFitInView && view->isVisible())
{
initFitInView = true;
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
}
cameraInfo->setText("");
cameraInfo->setToolTip("");
}
void CameraViewSingle::updateImage(QImage *image)
{
static clock_t time = 0;
if(image!=nullptr)
{
scene->clear();
scene->addPixmap(QPixmap::fromImage(*image));
//ROIs
//scene->addRect(20,20,100,100, QPen(QColor("green")), QBrush());
view->update();
delete image;
//first image adjustment
if(!initFitInView)
{
initFitInView = true;
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
}
if(time>0)
{
double dt = static_cast<double>(clock() - time)/1000.0;
if(dt<1.0)
{
//QString stime = QString("; Time: %1 [s]; Framerate: %2").arg(QString::number(dt, 'f', 3), QString::number(1.0/dt, 'f', 0));
QString stime = QString("; %1 [fps]").arg(QString::number(1.0/dt, 'f', 0));
cameraInfo->setText(stime);
}
}
time = clock();
}
}
void CameraViewSingle::buttonClicked(int id)
{
switch(id)
{
case 0: //plus
view->scale(2.0, 2.0);
break;
case 1: //minus
view->scale(0.5, 0.5);
break;
case 2: //minus
view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
break;
default:
break;
}
}
bool CameraViewSingle::saveImage(QString filePath, QString imageFormat, int imageQuality)
{
//bool res = pixMap.save(filePath, imageFormat.toStdString().c_str(), imageQuality);
bool res = qImage.save(filePath, imageFormat.toStdString().c_str(), imageQuality);
if(!res)
{
qDebug() << "File not saved! " + filePath;
}
return res;
}