模块

  1. QT += multimedia
  2. QT += multimediawidgets

重载QAbstractVideoSurface类

.h

  1. #ifndef MYVIDEOSURFACE_H
  2. #define MYVIDEOSURFACE_H
  3. #include <QAbstractVideoSurface>
  4. #include <QVideoFrame>
  5. class MyVideoSurface : public QAbstractVideoSurface
  6. {
  7. Q_OBJECT
  8. public:
  9. explicit MyVideoSurface(QObject *parent = nullptr);
  10. //重载 用于设置程序支持原始视频格式
  11. virtual QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType handleTyle) const;
  12. bool present(const QVideoFrame &frame);
  13. signals:
  14. void videoChanged(QVideoFrame);
  15. public slots:
  16. };
  17. #endif // MYVIDEOSURFACE_H

.cpp

  1. #include "myvideosurface.h"
  2. MyVideoSurface::MyVideoSurface(QObject *parent) : QAbstractVideoSurface(parent)
  3. {
  4. }
  5. QList<QVideoFrame::PixelFormat> MyVideoSurface::supportedPixelFormats(
  6. QAbstractVideoBuffer::HandleType handleTyle) const
  7. {
  8. //返回图形的处理格式,window下转换的图片格式是Format_RGB32,苹果下的则是Format_ARGB32
  9. return QList<QVideoFrame::PixelFormat>()<<QVideoFrame::Format_RGB32;
  10. }
  11. //重载基类中的函数,用于获取当前帧的视频源数据
  12. bool MyVideoSurface::present(const QVideoFrame &frame)
  13. {
  14. emit videoChanged(frame);
  15. return true;
  16. }

使用

.h

  1. private slots:
  2. void OnVideoChanged(QVideoFrame curFrame);
  3. void on_startButton_clicked();
  4. void on_closeButton_clicked();
  5. void on_saveButton_clicked();
  6. private:
  7. Ui::CameraTest *ui;
  8. QCamera *m_pCamera;
  9. QCameraImageCapture *m_pImageCapture; //截图部件
  10. MyVideoSurface *m_pVf;
  11. QLabel *m_label_camera_show;

.cpp

  1. CameraTest::CameraTest(QWidget *parent)
  2. : QMainWindow(parent)
  3. , ui(new Ui::CameraTest)
  4. {
  5. ui->setupUi(this);
  6. //获取电脑的摄像头名称
  7. QCameraInfo info = QCameraInfo::defaultCamera();
  8. //将设备名加入摄像头类中
  9. m_pCamera = new QCamera(info);
  10. m_pImageCapture = new QCameraImageCapture(m_pCamera);
  11. m_pImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
  12. m_pVf = new MyVideoSurface(this);
  13. m_pCamera->setCaptureMode(QCamera::CaptureStillImage);
  14. //捕获视频流
  15. m_pCamera->setViewfinder(m_pVf);
  16. //获取摄像头支持的分辨率、帧率等参数
  17. // m_pCamera->start();
  18. // QList<QCameraViewfinderSettings > ViewSets = m_pCamera->supportedViewfinderSettings();
  19. // int i = 0;
  20. // qDebug() << "viewfinderResolutions sizes.len = " << ViewSets.length();
  21. // foreach (QCameraViewfinderSettings ViewSet, ViewSets) {
  22. // qDebug() << i++ <<" max rate = " << ViewSet.maximumFrameRate() << "min rate = "<< ViewSet.minimumFrameRate() << "resolution "<<ViewSet.resolution()<<\
  23. // "Format="<<ViewSet.pixelFormat()<<""<<ViewSet.pixelAspectRatio();
  24. // }
  25. // //设置摄像头参数
  26. // m_pCamera->setViewfinderSettings(ViewSets[1]);
  27. connect(m_pVf, SIGNAL(videoChanged(QVideoFrame)), this, SLOT(OnVideoChanged(QVideoFrame)));
  28. }
  29. CameraTest::~CameraTest()
  30. {
  31. delete ui;
  32. }
  33. //捕捉摄像头的图片
  34. void CameraTest::OnVideoChanged(QVideoFrame curFrame)
  35. {
  36. QVideoFrame frame(curFrame);
  37. frame.map(QAbstractVideoBuffer::ReadOnly);
  38. //将QVideoFrame对象构建QImage对象
  39. QImage image(frame.bits(), frame.width(), frame.height(),
  40. QVideoFrame::imageFormatFromPixelFormat(frame.pixelFormat()));
  41. //设置图片旋转180°,摄像头图片默认的是倒影
  42. QMatrix matrix;
  43. matrix.rotate(180);
  44. image = image.transformed(matrix);
  45. //将QImage格式转化成QPixmap格式显示图片
  46. QPixmap pixmap = QPixmap::fromImage(image);
  47. pixmap = pixmap.scaled(ui->label->size());
  48. ui->label->setPixmap(pixmap);
  49. }
  50. //截图
  51. void CameraTest::saveImage(int,QImage image)
  52. {
  53. image=image.convertToFormat(QImage::Format_RGB888);
  54. // m_label_camera_show->setPixmap(QPixmap::fromImage(image));
  55. // const QPixmap *pixmap = m_label_camera_show->pixmap();
  56. QPixmap pixmap = QPixmap::fromImage(image);
  57. if(!pixmap.isNull())
  58. {
  59. pixmap.save("E:\\1.jpg");
  60. }
  61. }
  62. void CameraTest::on_startButton_clicked()
  63. {
  64. //m_pCamera->setCaptureMode(QCamera::CaptureStillImage);
  65. m_pCamera->start();
  66. }
  67. void CameraTest::on_closeButton_clicked()
  68. {
  69. m_pCamera->stop();
  70. ui->label->clear();
  71. }
  72. void CameraTest::on_saveButton_clicked()
  73. {
  74. m_pImageCapture->capture();
  75. }

注意

由于QLabel刷新频率较高,导致cpu的内存占有率达到10%以上;可使用GPU代替(qt使用QOpenGLWidget相关类)。