~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to src/cameramanager.cpp

  • Committer: lixiang
  • Date: 2018-03-06 03:13:06 UTC
  • Revision ID: lixiang@kylinos.cn-20180306031306-fd7qnru3vm4a1xjd
Rewrite with Qt5, and add system monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 ~ 2015 National University of Defense Technology(NUDT) & Kylin Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Kobe Lee    xiangli@ubuntukylin.com/kobe24_lixiang@126.com
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; version 3.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "cameramanager.h"
 
21
#include "mainwindow.h"
 
22
#include <QDebug>
 
23
#include <QVBoxLayout>
 
24
#include "../dbusproxy/youkersessiondbus.h"
 
25
#include "../component/toolbutton.h"
 
26
 
 
27
//#include <QKeyEvent>
 
28
#include <QDesktopServices>
 
29
#include <QUrl>
 
30
 
 
31
CameraManager::CameraManager(QWidget *parent, SessionDispatcher *proxy, QString skin)
 
32
:QDialog(parent),sessionproxy(proxy)
 
33
{
 
34
    this->setStyleSheet("QDialog{border: 1px solid white;border-radius:2px}");
 
35
    this->setFixedSize(622, 480);
 
36
    this->setWindowFlags(Qt::FramelessWindowHint);
 
37
    this->setAutoFillBackground(true);
 
38
    QPalette palette;
 
39
    palette.setBrush(QPalette::Window, QBrush(Qt::white));
 
40
    this->setPalette(palette);
 
41
 
 
42
    timer = NULL;
 
43
    imag = NULL;
 
44
    cam = NULL;
 
45
    frame = NULL;
 
46
 
 
47
    title_bar = new KylinTitleBar(this);
 
48
    initTitleBar(skin);
 
49
 
 
50
    camera_label = new QLabel(this);
 
51
    camera_label->setGeometry(QRect(0,32,622,340));
 
52
    camera_label->hide();
 
53
    baseWidget = new QWidget(this);
 
54
    baseWidget->setStyleSheet("QWidget{border: none;background-color: #a6b6c1;}");//background:transparent;
 
55
    baseWidget->setGeometry(QRect(0,372,622,108));
 
56
    catch_label = new QLabel(baseWidget);
 
57
    catch_label->setStyleSheet("QLabel{border: 1px solid white;}");
 
58
    catch_label->setGeometry(QRect(10,11,145,86));
 
59
    QImage image("://res/camera-default.png");
 
60
    if (!image.isNull()) {
 
61
        catch_label->setPixmap(QPixmap::fromImage(image).scaled(145, 86, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
 
62
    }
 
63
 
 
64
    disable_icon = new QLabel(baseWidget);
 
65
    disable_icon->setGeometry(QRect(263,7,93,93));
 
66
    disable_icon->setStyleSheet("QLabel{background-image:url(':/res/catch-disable.png')}");
 
67
    disable_icon->hide();
 
68
 
 
69
    okBtn = new QPushButton(baseWidget);
 
70
    okBtn->setGeometry(QRect(263,7,93,93));
 
71
    okBtn->hide();
 
72
    okBtn->setCursor(Qt::PointingHandCursor);
 
73
    okBtn->setFocusPolicy(Qt::NoFocus);
 
74
    okBtn->setIconSize(QSize(93, 93));
 
75
    okBtn->setFlat(true);
 
76
    okBtn->setStyleSheet("QPushButton{background:transparent;background-image:url('://res/catch.png');}QPushButton:hover{background:url('://res/catch-hover.png');}");
 
77
    viewBtn = new ToolButton(baseWidget, true);
 
78
    viewBtn->setFocusPolicy(Qt::NoFocus);
 
79
    viewBtn->setCursor(Qt::PointingHandCursor);
 
80
    viewBtn->setIcon("://res/view.png");
 
81
    viewBtn->setText(tr("View Photos"));
 
82
    viewBtn->setGeometry(QRect(500,16,50,60));
 
83
 
 
84
    error_icon = new QLabel(this);
 
85
    error_icon->setGeometry(QRect(0,32,622,340));
 
86
    error_icon->setStyleSheet("QLabel{background-image:url('://res/camera-error.png')}");
 
87
    error_label = new QLabel(this);
 
88
    error_label->setGeometry(QRect(10, 280, 600, 20));
 
89
    error_label->setWordWrap(true);
 
90
    error_label->setStyleSheet("QLabel{color:#90a5b1;font-family: 方正黑体_GBK;font-size: 14px;text-align: center;}");
 
91
    error_label->setAlignment(Qt::AlignCenter);
 
92
    error_label->setText(tr("The camera is out of question, <a style='color: #3f96e4;' href = http://www.ubuntukylin.com> Refresh</a> to call it out ~"));
 
93
 
 
94
    this->initCamera();
 
95
    this->initConnect();
 
96
 
 
97
}
 
98
 
 
99
//CameraManager::~CameraManager()
 
100
//{
 
101
//    if (timer != NULL) {
 
102
//            disconnect(timer,SIGNAL(timeout()),this,SLOT(readFarme()));
 
103
//            if(timer->isActive())
 
104
//                timer->stop();
 
105
//            delete timer;
 
106
//            timer = NULL;
 
107
//    }
 
108
//    if (imag != NULL) {
 
109
//        delete imag;
 
110
//        imag = NULL;
 
111
//    }
 
112
//    if (cam != NULL) {
 
113
//        delete cam;
 
114
//        cam = NULL;
 
115
//    }
 
116
//}
 
117
 
 
118
void CameraManager::initCamera()
 
119
{
 
120
    timer  = new QTimer(this);
 
121
    imag = new QImage();
 
122
    int number = this->countCamaras();//获取摄像头的个数
 
123
//    qDebug()  << "camara number:" << number;
 
124
//    if (cam != NULL)
 
125
//        return;
 
126
    if(number < 1) {
 
127
        this->setOKButtonEnable(false);
 
128
        return;
 
129
    }
 
130
    else {
 
131
        this->setOKButtonEnable(true);
 
132
    }
 
133
    //0表示内置摄像头 1表示外界摄像头
 
134
   cam = cvCreateCameraCapture(0);//打开摄像头,从摄像头中获取视频
 
135
//   assert(NULL != cam);
 
136
   if(cam == NULL) {
 
137
       this->setOKButtonEnable(false);
 
138
       return;
 
139
   }
 
140
   cvSetCaptureProperty(cam, CV_CAP_PROP_FPS, 30);//帧率(只对摄像头有效)
 
141
   //default :640*480
 
142
//   cvSetCaptureProperty(cam, CV_CAP_PROP_FRAME_WIDTH, 1280);//视频流的帧宽度(只对摄像头有效)
 
143
//   cvSetCaptureProperty(cam, CV_CAP_PROP_FRAME_HEIGHT, 720);//视频流的帧高度(只对摄像头有效)
 
144
   timer->start(33);// 开始计时,超时则发出timeout()信号
 
145
}
 
146
 
 
147
QString CameraManager::getCurrentDateTime() {
 
148
    QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
 
149
    QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); //设置显示格式
 
150
    return str;
 
151
}
 
152
 
 
153
QString CameraManager::getHomePath() {
 
154
    QString homepath = QDir::homePath();
 
155
    return homepath;
 
156
}
 
157
 
 
158
int CameraManager::countCamaras() {
 
159
    cv::VideoCapture temp_camera;
 
160
    int maxTested = 10;
 
161
    for (int i = 0; i < maxTested; i++) {
 
162
//        try {
 
163
//            cv::VideoCapture temp_camera(i);
 
164
//        }
 
165
//        catch(cv::Exception& e) {
 
166
//            qDebug() << e.what();
 
167
//        }
 
168
        cv::VideoCapture temp_camera(i);
 
169
        bool res = (!temp_camera.isOpened());
 
170
        temp_camera.release();
 
171
        if (res)
 
172
        {
 
173
            return i;
 
174
        }
 
175
    }
 
176
    return maxTested;
 
177
}
 
178
 
 
179
void CameraManager::setOKButtonEnable(bool enable)
 
180
{
 
181
    if(enable)
 
182
    {
 
183
        error_icon->hide();
 
184
        error_label->hide();
 
185
        disable_icon->hide();
 
186
        camera_label->show();
 
187
        okBtn->show();
 
188
//        okBtn->setEnabled(true);
 
189
    }
 
190
    else
 
191
    {
 
192
        camera_label->hide();
 
193
//        okBtn->setEnabled(false);
 
194
        okBtn->hide();
 
195
        error_icon->show();
 
196
        error_label->show();
 
197
        disable_icon->show();
 
198
    }
 
199
}
 
200
 
 
201
void CameraManager::onOKButtonClicked()
 
202
{
 
203
    frame = cvQueryFrame(cam);
 
204
//    QImage image = QImage((const uchar*)frame->imageData, frame->width, frame->height, QImage::Format_RGB888).rgbSwapped();
 
205
    QImage image = QImage((const uchar*)frame->imageData, frame->width, frame->height, QImage::Format_RGB888).rgbSwapped();
 
206
    catch_label->setPixmap(QPixmap::fromImage(image).scaled(145, 86, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
 
207
    IplImage* outImage = cvCreateImage(cvGetSize(frame),frame->depth,frame->nChannels);
 
208
    cvCopy(frame,outImage,NULL);
 
209
 //    outImage = this->DoPyrDown(outImage);//图片缩放
 
210
    QString outImageName = this->getHomePath() + "/" + this->getCurrentDateTime() + ".jpg";
 
211
    char* ch;
 
212
    QByteArray ba = outImageName.toUtf8();
 
213
    ch=ba.data();
 
214
    cvSaveImage(ch,outImage);
 
215
    cvReleaseImage(&outImage);
 
216
}
 
217
 
 
218
void CameraManager::readFarme()
 
219
{
 
220
    frame = cvQueryFrame(cam);
 
221
    if(!frame)
 
222
        return;
 
223
   QImage image = QImage((const uchar*)frame->imageData, frame->width, frame->height, QImage::Format_RGB888).rgbSwapped();
 
224
   camera_label->setPixmap(QPixmap::fromImage(image));
 
225
}
 
226
 
 
227
 
 
228
// 实现将拍下来的图片缩放一倍
 
229
IplImage* CameraManager::DoPyrDown(IplImage* image, int filter = IPL_GAUSSIAN_5x5)
 
230
//IplImage* CameraManager::DoPyrDown(IplImage* image, int filter = CV_GAUSSIAN_5x5)
 
231
{
 
232
    CvSize size = cvSize(image->width / 2,image->height / 2);
 
233
    IplImage* outImage = cvCreateImage(size,image->depth,image->nChannels);
 
234
    cvPyrDown(image,outImage);
 
235
    cvReleaseImage(&image);
 
236
    return outImage;
 
237
}
 
238
 
 
239
void CameraManager::onViewButtonClicked()
 
240
{
 
241
//    sessionproxy->open_folder_qt(sessionproxy->getHomePath());
 
242
    QDesktopServices::openUrl(QUrl(QString("file:%1").arg(this->getHomePath()), QUrl::TolerantMode));
 
243
}
 
244
 
 
245
void CameraManager::initConnect()
 
246
{
 
247
    connect(title_bar,SIGNAL(closeDialog()), this, SLOT(onCloseButtonClicked()));
 
248
    connect(okBtn,SIGNAL(clicked()), this, SLOT(onOKButtonClicked()));
 
249
    connect(viewBtn, SIGNAL(clicked()), this, SLOT(onViewButtonClicked()));
 
250
    connect(error_label,SIGNAL(linkActivated(QString)),this,SLOT(refreshCamera()));
 
251
    connect(timer, SIGNAL(timeout()), this, SLOT(readFarme()));  // 时间到,读取当前摄像头信息
 
252
}
 
253
 
 
254
void CameraManager::refreshCamera()
 
255
{
 
256
    int number = this->countCamaras();//获取摄像头的个数
 
257
    if(number < 1) {
 
258
        this->setOKButtonEnable(false);
 
259
        return;
 
260
    }
 
261
    else {
 
262
        this->setOKButtonEnable(true);
 
263
    }
 
264
    //0表示内置摄像头 1表示外界摄像头
 
265
   cam = cvCreateCameraCapture(0);//打开摄像头,从摄像头中获取视频
 
266
   if(cam == NULL) {
 
267
       this->setOKButtonEnable(false);
 
268
       return;
 
269
   }
 
270
   cvSetCaptureProperty(cam, CV_CAP_PROP_FPS, 30);//帧率(只对摄像头有效)
 
271
   if(timer != NULL) {
 
272
       if(timer->isActive())
 
273
        timer->stop();
 
274
       timer->start(33);// 开始计时,超时则发出timeout()信号
 
275
   }
 
276
}
 
277
 
 
278
void CameraManager::onCloseButtonClicked()
 
279
{
 
280
    cvReleaseCapture(&cam);
 
281
    if (timer != NULL) {
 
282
            disconnect(timer,SIGNAL(timeout()),this,SLOT(readFarme()));
 
283
            if(timer->isActive())
 
284
                timer->stop();
 
285
            delete timer;
 
286
            timer = NULL;
 
287
    }
 
288
    if (imag != NULL) {
 
289
        delete imag;
 
290
        imag = NULL;
 
291
    }
 
292
    if (cam != NULL) {
 
293
        delete cam;
 
294
        cam = NULL;
 
295
    }
 
296
    this->close();
 
297
}
 
298
 
 
299
void CameraManager::initTitleBar(QString skin)
 
300
{
 
301
    title_bar->setTitleWidth(622);
 
302
    title_bar->setTitleName(tr("Camera"));
 
303
    title_bar->setTitleBackgound(skin);
 
304
}
 
305
 
 
306
void CameraManager::resetTitleSkin(QString skin)
 
307
{
 
308
    title_bar->resetBackground(skin);
 
309
}
 
310
 
 
311
//void CameraManager::keyPressEvent(QKeyEvent *k)
 
312
//{
 
313
//    if (k->key() == Qt::Key_Escape) {
 
314
//        qDebug() << "prssss. esc.....";
 
315
//        if (cam != NULL) {
 
316
//            qDebug() << "cam not null";
 
317
//            this->onCloseButtonClicked();
 
318
//        }
 
319
//        else
 
320
//            qDebug() << "cam null";
 
321
//    }
 
322
//    else if (k->key() == Qt::Key_Enter || k->key() == Qt::Key_Return)
 
323
//    {
 
324
//        qDebug() << "prssss. enter.....";
 
325
//        if (cam != NULL)
 
326
//            this->onPicButtonClicked();
 
327
//    }
 
328
//}