当前位置:   article > 正文

Qt使用opencv,进行视频录制,功能打开、关闭摄像头,开始、结束录制视频,暂停、继续录制,并保存视频文件_qt+opencv摄像头录制视频并保存

qt+opencv摄像头录制视频并保存

1.效果图

2 代码实现

2.1 .h文件

  1. #ifndef VIDEORECORDWIDGET_H
  2. #define VIDEORECORDWIDGET_H
  3. #include <QWidget>
  4. #include<QFileDialog>
  5. #include <QImage>
  6. #include <QLabel>
  7. #include <QTimer>
  8. #include <opencv2/opencv.hpp>
  9. using namespace cv;
  10. namespace Ui {
  11. class VideoRecordWidget;
  12. }
  13. class VideoRecordWidget : public QWidget
  14. {
  15. Q_OBJECT
  16. public:
  17. explicit VideoRecordWidget(QWidget *parent = nullptr);
  18. ~VideoRecordWidget();
  19. private slots:
  20. void on_pushButton_clicked();
  21. void on_pushButton_2_clicked();
  22. void on_pushButton_3_clicked();
  23. void on_pushButton_5_clicked();
  24. void on_pushButton_4_clicked();
  25. void updateFrame();
  26. void on_pushButton_6_clicked();
  27. private:
  28. Ui::VideoRecordWidget *ui;
  29. VideoCapture capture;
  30. // QTimer timer;
  31. VideoWriter videoWriter;
  32. bool m_video_open;
  33. bool m_video_record;
  34. QTimer timer_open;
  35. QTimer timer_record;
  36. String recordViedo_fileName;
  37. };
  38. #endif // VIDEORECORDWIDGET_H

2.2 .cpp文件

  1. #include "videorecordwidget.h"
  2. #include "ui_videorecordwidget.h"
  3. VideoRecordWidget::VideoRecordWidget(QWidget *parent)
  4. : QWidget(parent)
  5. , ui(new Ui::VideoRecordWidget)
  6. {
  7. ui->setupUi(this);
  8. m_video_open=false;
  9. m_video_record=false;
  10. // connect(&timer, &QTimer::timeout, this, &VideoRecordWidget::updateFrame);
  11. }
  12. VideoRecordWidget::~VideoRecordWidget()
  13. {
  14. delete ui;
  15. }
  16. //开启摄像头
  17. void VideoRecordWidget::on_pushButton_clicked()
  18. {
  19. // 打开摄像头
  20. capture.open(0);
  21. if (!capture.isOpened()) {
  22. qDebug("Failed to open camera.");
  23. return;
  24. }
  25. qDebug()<<"摄像头开启中";
  26. // 开始定时器,以固定间隔刷新显示视频图像
  27. timer_open.start(33); // 控制帧率为30fps
  28. m_video_open=true;
  29. connect(&timer_open, &QTimer::timeout, this, &VideoRecordWidget::updateFrame);
  30. }
  31. //关闭摄像头
  32. void VideoRecordWidget::on_pushButton_2_clicked()
  33. {
  34. // 关闭摄像头
  35. qDebug()<<"摄像头关闭中";
  36. capture.release();
  37. ui->label->clear();
  38. ui->label->setText("视频录制器");
  39. timer_open.stop();
  40. m_video_open=false;
  41. if(m_video_record){
  42. qDebug()<<"结束录制";
  43. m_video_record=false;
  44. timer_record.stop();
  45. videoWriter.release();
  46. }
  47. }
  48. //开始录制
  49. void VideoRecordWidget::on_pushButton_3_clicked()
  50. {
  51. if(m_video_open){
  52. if(videoWriter.isOpened()){
  53. qDebug()<<"已经有录制项目:"<<recordViedo_fileName<<"请先结束录制,再操作";
  54. return;
  55. }
  56. // 获取当前时间作为视频文件名
  57. std::time_t time = std::time(0);
  58. std::ostringstream oss;
  59. oss << "video_" << time << ".avi";
  60. recordViedo_fileName=oss.str();
  61. // std::string filename = oss.str();
  62. ui->lineEdit->setText(recordViedo_fileName.c_str());
  63. qDebug()<<"摄像头开启中-并进行录制,文件名:"<<recordViedo_fileName;
  64. timer_record.start(1000/25); // 控制帧率为30fps
  65. m_video_record=true;
  66. cv::Mat frame;
  67. capture >> frame; // 从视频流中捕获当前帧
  68. int codec = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
  69. double fps = 25.0;
  70. cv::Size frameSize(frame.cols, frame.rows);
  71. // if(videoWriter.isOpened()){
  72. // videoWriter.write(frame);
  73. // return;
  74. // }
  75. videoWriter.open(recordViedo_fileName, codec, fps, frameSize);
  76. connect(&timer_record, &QTimer::timeout, this, &VideoRecordWidget::updateFrame);
  77. }else{
  78. qDebug()<<"请先打开摄像头";
  79. }
  80. }
  81. //暂停录制
  82. void VideoRecordWidget::on_pushButton_5_clicked()
  83. {
  84. qDebug()<<"暂停录制";
  85. m_video_record=false;
  86. }
  87. //结束录制
  88. void VideoRecordWidget::on_pushButton_4_clicked()
  89. {
  90. qDebug()<<"结束录制";
  91. m_video_record=false;
  92. timer_record.stop();
  93. videoWriter.release();
  94. }
  95. void VideoRecordWidget::updateFrame()
  96. {
  97. if(m_video_open){
  98. cv::Mat frame;
  99. capture >> frame; // 从视频流中捕获当前帧
  100. if (frame.empty()) {
  101. return;
  102. }
  103. // 将OpenCV的Mat图像转换为Qt的QImage
  104. QImage qimage(frame.data, frame.cols, frame.rows, static_cast<int>(frame.step), QImage::Format_BGR888);
  105. QPixmap pixmap = QPixmap::fromImage(qimage);
  106. // 设置QLabel显示图像
  107. ui->label->setPixmap(pixmap.scaled(ui->label->size(), Qt::KeepAspectRatio));
  108. if(m_video_record){
  109. qDebug()<<"录制中";
  110. // 创建 VideoWriter 对象
  111. // 检查是否成功打开视频文件
  112. if (!videoWriter.isOpened())
  113. {
  114. qDebug() << "无法打开视频文件.";
  115. return;
  116. }
  117. videoWriter.write(frame);
  118. }
  119. }
  120. }
  121. //继续录制
  122. void VideoRecordWidget::on_pushButton_6_clicked()
  123. {
  124. qDebug()<<"继续录制";
  125. m_video_record=true;
  126. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/630125
推荐阅读
相关标签
  

闽ICP备14008679号