-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
160 lines (137 loc) · 4.79 KB
/
mainwindow.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
#include "mainwindow.h"
#include <QtDebug>
#include <QInputDialog>
//#include "ui_mainwindow.h"
//#include "receiver.cpp"
//#include "send_thread.h"
//#include "send_thread.h"
//#include "sender.h"
//#include "receiver.h"
// for debug
void print(const char* s) {
std::cout << s << std::endl;
}
// end
Chat::Chat(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Chat),
sender_started(false),
ready(false)
{
ui->setupUi(this);
connect(&theTimer, &QTimer::timeout, this, &Chat::updataImage);
// cv::Mat frame = get_frame();
if(cap.open(0)) {
srcImg_local = cv::Mat::zeros(cap.get(cv::CAP_PROP_FRAME_HEIGHT), cap.get(cv::CAP_PROP_FRAME_WIDTH), CV_8UC3);
// if(true) {
// srcImg = get_frame();
// if(sender_started) {
// theTimer.start(33);
}
ImgLabel = new QLabel(this);
ui->verticalLayout->addWidget(ImgLabel);
sender_thread.start();
theTimer.start(33); // 30 per s
connect(ui->inputIP_btn, &QPushButton::clicked, this, &Chat::get_ip);
connect(ui->set_frame_rate_btn, &QPushButton::clicked, this, &Chat::set_frame_rate);
connect(this, &Chat::got_ip, this, &Chat::send_thread_start);
sharedMemory = new QSharedMemory("frame_rate", this);
}
void Chat::send_thread_start() {
// sender_thread.start();
}
void Chat::paintEvent(QPaintEvent * /*e */) {
// method 1
QPainter painter(this);
QImage image1 = QImage((uchar*)(srcImg.data), srcImg.cols, srcImg.rows, QImage::Format_RGB888);
painter.drawImage(QPoint(100,100), image1);
// QPainter painter2(this);
// 绘制本地摄像头的图像
QImage image2 = QImage((uchar*)(srcImg_local.data), srcImg_local.cols, srcImg_local.rows, QImage::Format_RGB888);
painter.drawImage(QPoint(900,100), image2);
// method 2
// ImgLabel->setPixmap(QPixmap::fromImage(image1));
// ImgLabel->resize(image1.size());
// ImgLabel->show();
}
void Chat::updataImage() {
if(!ready) return;
cap >> srcImg_local;
// std::cout << "u";
srcImg = demo.get_frame();
if(srcImg.data) {
cvtColor(srcImg, srcImg, CV_BGR2RGB);
cvtColor(srcImg_local, srcImg_local, CV_BGR2RGB);
this->update();
}
}
void Chat::get_ip() {
bool ok;
QString name = QInputDialog::getText(this,
tr("IP"),
tr("输入对方IP"),
QLineEdit::Normal,
tr("127.0.0.1"),
&ok);
qDebug() << "get ip";
target_ip = name.toStdString();
sender_started = true;
ready = true;
// sender_thread.start();
emit got_ip();
// theTimer.start(33);
QString sharedMessage(name);
QByteArray sharedData = sharedMessage.toLatin1();
QSharedMemory* sharedMemory = new QSharedMemory("sharedMemoryKey", this);
sharedMemory->create(sharedMessage.size());
sharedMemory->lock();
memcpy(sharedMemory->data(), sharedData.data(), sharedData.size());
sharedMemory->unlock();
return;
}
void Chat::set_frame_rate() {
bool ok;
QString frame_rate = QInputDialog::getText(this,
tr("frame rate"),
tr("输入帧率"),
QLineEdit::Normal,
tr("12"),
&ok);
QString sharedMessage(frame_rate);
QByteArray sharedData = sharedMessage.toLatin1();
// QSharedMemory* sharedMemory = new QSharedMemory("frame_rate", this);
if(sharedMemory->isAttached()) {
sharedMemory->detach();
}
sharedMemory->create(sharedMessage.size());
sharedMemory->lock();
memcpy(sharedMemory->data(), sharedData.data(), sharedData.size());
sharedMemory->unlock();
}
//void Chat::on_pushButton_clicked() {
// srcImg = imread("E:/qt/chat/023.jpg");
// cvtColor(srcImg, grayImg, CV_BGR2GRAY);
// Mat tmp;
// QImage Qtmp;
// if(!isGray) {
// cvtColor(srcImg, tmp, CV_BGR2RGB);
// Qtmp = QImage((const unsigned char*)(tmp.data), tmp.cols, tmp.rows, tmp.step, QImage::Format_RGB888);
// } else {
// cvtColor(grayImg, tmp, CV_GRAY2RGB);
// Qtmp = QImage((const unsigned char*)(tmp.data), tmp.cols, tmp.rows, tmp.step, QImage::Format_RGB888);
// }
// ui -> label -> setPixmap(QPixmap::fromImage(Qtmp));
// ui -> label -> resize(Qtmp.size());
// ui -> label -> show();
//}
//void Chat::on_checkBox_clicked() {
// if(ui->checkBox -> isChecked()) {
// isGray = 1;
// } else {
// isGray = 0;
// }
//}
Chat::~Chat()
{
// delete ui;
}