|
#include "widget.h"#include "./ui_widget.h"
Widget::Widget(QLabel *parent) : QLabel(parent) , ui(new Ui::Widget){ // 初始化变量 mouse_press = false; ui->setupUi(this); //this->setWindowTitle(" "); timer = new QTimer(this); // 初始化定时器 image = new QImage(); // 初始化QImage recordTimer = new QTimer(this); //this->setFixedSize(this->width(),this->height()); // 固定大小 this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); // 隐藏顶栏 this->setMouseTracking(true); // 打开鼠标追踪
//connect(ui->menuExitButton,SIGNAL(clicked()),this,SLOT(close())); // connect(ui->menuSmallButton,SIGNAL(clicked()),this,SLOT(uiShowMinimized())); // connect(ui->exitButton,SIGNAL(clicked()),this,SLOT(endCapture())); // 停止捕捉
// 摄像头操作 connect(timer,SIGNAL(timeout()),this,SLOT(readFrame())); // 定时器操作,时间到开始buffer并显示每一帧 //connect(ui->startCaptureButton,SIGNAL(clicked()),this,SLOT(startCapture())); // 开始捕捉
// 录制和抓拍 // connect(ui->takePhotosButton,SIGNAL(clicked(bool)),this,SLOT(takePhotos())); // 按钮操作,按下开始抓拍一帧并写入文件 connect(recordTimer,SIGNAL(timeout()),this,SLOT(recordStart_timerSlot())); // 定时器操作,时间到则写入一帧至文件 //connect(ui->recordButton,SIGNAL(clicked(bool)),this,SLOT(startRecord())); // 按钮操作,按下录制时器运作 //connect(ui->endRecordBtn,SIGNAL(clicked(bool)),this,SLOT(endRecord())); // 按钮操作,按下停止录制,不过不确定会不会有丢帧的现象
//connect(ui->replayButton,SIGNAL(clicked(bool)),this,SLOT(showReplay())); // 打开回放界面
#if 0 // load qss QFile qss("../recorder-opencv/uiComponents/scrollbar.qss"); qss.open(QFile::ReadOnly); QString style = qss.readAll(); ui->textBrowser->verticalScrollBar()->setStyleSheet(style); qss.close();
ui->textBrowser->insertPlainText(" Author: 825772667@qq.com \n");#endif
outputMessage("本软件可录制电脑上的摄像头视频,包括电脑自带的摄像头和USB外接摄像头。\n事先准备:\n确保摄像头可用。如果要使用USB摄像头,请先将摄像头的USB线插入电脑。\n录像步骤:\n1.打开摄像头 2. 开始录制; 3. 录像完毕后,停止录制; 录像文件在exe同目录下\n注意:电脑上有多个摄像头时请选择第几个摄像头,如选错请重启软件重选\n");
// 抓拍和视频文件的个数 screenshotNum = 0; videoNum = 0;
recordStartOrStop_UI(false);
startCapture(this->width(),this->height(),0);}
Widget::~Widget(){ delete ui;}
/* * 输出信息*/void Widget::outputMessage(QString Message){
//ui->textBrowser->insertPlainText(Message); //ui->textBrowser->moveCursor(QTextCursor::End);}
/* * 最小化*/void Widget::uiShowMinimized(){ this->showMinimized();}
/* * 按下鼠标事件*/void Widget::mousePressEvent(QMouseEvent *event){ if(event->button() == Qt::LeftButton) { mouse_press = true;
//鼠标相对于窗体的位置 move_point = event->pos();
}
}
/* * 移动鼠标事件*/void Widget::mouseMoveEvent(QMouseEvent *event){ //若鼠标左键被按下 if(mouse_press) { //鼠标相对于屏幕的位置 QPoint move_pos = event->globalPos();
//移动主窗体位置 this->move(move_pos - move_point); }
}
/* * 松开鼠标左键事件*/void Widget::mouseReleaseEvent(QMouseEvent *event){ //设置鼠标为未被按下 mouse_press = false; Q_UNUSED(event);}
/* * 开始捕捉*/void Widget::startCapture(int captureWidth, int captureHeight,QString deviceName){ Q_UNUSED(deviceName);
cap.set(cv::CAP_PROP_FRAME_WIDTH,captureWidth); cap.set(cv::CAP_PROP_FRAME_HEIGHT,captureHeight); int CamID; CamID = 0; //ui->comboBoxCamID->currentIndex(); cap.open(CamID); /* cv::VideoCapture既支持视频文件的读取,也支持从摄像机中视频的读取。cv::VideoCapture对象的创建方式有以下三种:
cv::VideoCapture capture( const string& filename, // 输入文件名 ); cv::VideoCapture capture( int device // 视频捕捉设备 id ); cv::VideoCapture capture(); 第三种方式仅仅创建一个捕获对象,而不提供任何关于打开的信息。创建以后通过成员函数open()来设定打开的信息。open()操作也有以上两种方式。 */ if(!cap.isOpened()){ outputMessage("发生了错误,无法打开摄像头!\n"); return ; } timer->start(33);
//ui->startCaptureButton->setDisabled(true);
}
/* * 退出/结束捕捉 * 并清空label_2*/void Widget::endCapture(){ timer->stop(); cap.release(); this->clear(); outputMessage("停止摄像头完成...\n");}
/* * 读取一帧并返回 * 此帧显示在label_2上*/void Widget::readFrame(){ cap.read(frame); /* 将视频帧读取到cv::Mat矩阵中,有两种方式:一种是read()操作;另一种是 “>>”操作。
cv::Mat frame; cap.read(frame); //读取方式一 cap >> frame; //读取方式二 */ // cv::putText(frame,getLocalTime(),cv:oint(0,20),cv::FONT_HERSHEY_COMPLEX,0.8,cv::Scalar(0,255,0),1,8); // 绘制时间 QImage img = MatImageToQt(frame);
//cv::VideoCapture cap; //cv::imshow("aa", cv::InputArray(frame));
QImage img_resize; int h= this->height(); int w= this->width();
if((img.height()!=h)||(img.width()!=w)) { img_resize =img.scaled(w,h); } else { img_resize =img; }
this->setPixmap(QPixmap::fromImage(img_resize));}
|
|