在上一篇文章中,实现了单个摄像头的摄像头读取、视频保存、摄像头关闭等功能;
这一篇实现两个摄像头的读取,在笔记本电脑上插一个USB摄像头,然后让显示两个摄像头的图像并保存
先来个效果展示
话不多说,直接上代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
#include
using namespace cv;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this);
OPEN = true;
/*信号和槽*/
connect(ui->OpenCameraBtn,SIGNAL(click()),this,SLOT(on_OpenCameraBtn_clicked()));//打开摄像头按钮
connect(ui->TakePicBtn,SIGNAL(click()),this,SLOT(on_TakePicBtn_clicked()));//保存视频按钮
connect(ui->CloseCameraBtn,SIGNAL(click()),this,SLOT(on_CloseCameraBtn_clicked()));//关闭摄像头按钮
connect(ui->OpenVideoBtn,SIGNAL(click()),this,SLOT(on_OpenVideoBtn_clicked()));//打开视频按钮
connect(timer,SIGNAL(timeout()),this,SLOT(getFrame()));
}
/******************************
***********打开摄像头***********
*****************************/
void MainWindow::on_OpenCameraBtn_clicked()
{
// 设置摄像头的拍摄属性为 分辨率640x480,帧率30fps
// cam.set(CAP_PROP_FRAME_HEIGHT, 480);
// cam.set(CAP_PROP_FRAME_WIDTH, 640);
// cam.set(CAP_PROP_FPS, 30.0);
//打开摄像头 从摄像头中获取视频
cam0.open(0);
cam1.open(1);
if(cam0.isOpened()&&!cam1.isOpened())
{
cam1 = cam0;
}
if(!cam0.isOpened()&&cam1.isOpened())
{
cam0 = cam1;
}
//开启定时器,每隔0.05秒刷新一次
timer->start(50);
}
void MainWindow::getFrame()
{
//从摄像头中抓取并返回每一帧
if(cam0.isOpened())
{
cam0 >> frame0;
//将抓取到的帧转换成QImage格式
//QImage img0 = QImage((const unsigned char*)frame0.data, frame0.cols, frame0.rows, QImage::Format_RGB888).rgbSwapped();
//ui->label_2->setPixmap(QPixmap::fromImage(img0));
namedWindow("VideoPlay0", WINDOW_NORMAL);
imshow("VideoPlay0", frame0);
waitKey(1000 / 30);//30帧
}
if(cam1.isOpened())
{
cam1 >> frame1;
//将抓取到的帧转换成QImage格式
//QImage img1 = QImage((const unsigned char*)frame1.data, frame1.cols, frame1.rows, QImage::Format_RGB888).rgbSwapped();
//将图片显示到QLabel上
//ui->label->setPixmap(QPixmap::fromImage(img1));
namedWindow("VideoPlay1", WINDOW_NORMAL);
imshow("VideoPlay1", frame1);
waitKey(1000 / 30);//30帧
}
}
/******************************
***********保存视频按钮************
*****************************/
void MainWindow::on_TakePicBtn_clicked()
{
if(!(cam0.isOpened() && cam1.isOpened()))
{
cam0.open(0);
cam1.open(1);
}
write0.open("D:\\cam_0.avi",VideoWriter::fourcc('M', 'J', 'P', 'G'), 30.0, Size(640, 480), true);
write1.open("D:\\cam_1.avi",VideoWriter::fourcc('M', 'J', 'P', 'G'), 30.0, Size(640, 480), true);
while(OPEN)
{
if(cam0.isOpened())
{
cam0 >> frame0;
//if(!frame0) break;
write0.write(frame0);
namedWindow("VideoPlay0", WINDOW_NORMAL);
imshow("VideoPlay0", frame0);
waitKey(1000 / 30);
}
if(cam1.isOpened())
{
cam1 >> frame1;
//if(!frame1) break;
write1.write(frame1);
namedWindow("VideoPlay1", WINDOW_NORMAL);
imshow("VideoPlay1", frame1);
waitKey(1000 / 30);
}
}
}
/******************************
***********关闭摄像头***********
*****************************/
void MainWindow::on_CloseCameraBtn_clicked()
{
timer->stop();
cam0.release();
cam1.release();
OPEN = false;
write0.release();
write1.release();
destroyWindow("VideoPlay0");
destroyWindow("VideoPlay1");
}
/******************************
***********打开视频***********
*****************************/
void MainWindow::on_OpenVideoBtn_clicked()
{
VideoCapture video;
Mat frame2;
video.open("D:\\test.avi");
while (video.isOpened())
{
video >> frame2;
imshow("video", frame2);
if (cvWaitKey(40) == 27) //cvWaitKey的参数相当于多少ms一帧,现在是40ms一帧,1s25帧
break; //按ESC就退出
}
}
MainWindow::~MainWindow()
{
delete ui;
}