模块
QT += multimediaQT += multimediawidgets
重载QAbstractVideoSurface类
.h
#ifndef MYVIDEOSURFACE_H#define MYVIDEOSURFACE_H#include <QAbstractVideoSurface>#include <QVideoFrame>class MyVideoSurface : public QAbstractVideoSurface{Q_OBJECTpublic:explicit MyVideoSurface(QObject *parent = nullptr);//重载 用于设置程序支持原始视频格式virtual QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType handleTyle) const;bool present(const QVideoFrame &frame);signals:void videoChanged(QVideoFrame);public slots:};#endif // MYVIDEOSURFACE_H
.cpp
#include "myvideosurface.h"MyVideoSurface::MyVideoSurface(QObject *parent) : QAbstractVideoSurface(parent){}QList<QVideoFrame::PixelFormat> MyVideoSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleTyle) const{//返回图形的处理格式,window下转换的图片格式是Format_RGB32,苹果下的则是Format_ARGB32return QList<QVideoFrame::PixelFormat>()<<QVideoFrame::Format_RGB32;}//重载基类中的函数,用于获取当前帧的视频源数据bool MyVideoSurface::present(const QVideoFrame &frame){emit videoChanged(frame);return true;}
使用
.h
private slots:void OnVideoChanged(QVideoFrame curFrame);void on_startButton_clicked();void on_closeButton_clicked();void on_saveButton_clicked();private:Ui::CameraTest *ui;QCamera *m_pCamera;QCameraImageCapture *m_pImageCapture; //截图部件MyVideoSurface *m_pVf;QLabel *m_label_camera_show;
.cpp
CameraTest::CameraTest(QWidget *parent): QMainWindow(parent), ui(new Ui::CameraTest){ui->setupUi(this);//获取电脑的摄像头名称QCameraInfo info = QCameraInfo::defaultCamera();//将设备名加入摄像头类中m_pCamera = new QCamera(info);m_pImageCapture = new QCameraImageCapture(m_pCamera);m_pImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);m_pVf = new MyVideoSurface(this);m_pCamera->setCaptureMode(QCamera::CaptureStillImage);//捕获视频流m_pCamera->setViewfinder(m_pVf);//获取摄像头支持的分辨率、帧率等参数// m_pCamera->start();// QList<QCameraViewfinderSettings > ViewSets = m_pCamera->supportedViewfinderSettings();// int i = 0;// qDebug() << "viewfinderResolutions sizes.len = " << ViewSets.length();// foreach (QCameraViewfinderSettings ViewSet, ViewSets) {// qDebug() << i++ <<" max rate = " << ViewSet.maximumFrameRate() << "min rate = "<< ViewSet.minimumFrameRate() << "resolution "<<ViewSet.resolution()<<\// "Format="<<ViewSet.pixelFormat()<<""<<ViewSet.pixelAspectRatio();// }// //设置摄像头参数// m_pCamera->setViewfinderSettings(ViewSets[1]);connect(m_pVf, SIGNAL(videoChanged(QVideoFrame)), this, SLOT(OnVideoChanged(QVideoFrame)));}CameraTest::~CameraTest(){delete ui;}//捕捉摄像头的图片void CameraTest::OnVideoChanged(QVideoFrame curFrame){QVideoFrame frame(curFrame);frame.map(QAbstractVideoBuffer::ReadOnly);//将QVideoFrame对象构建QImage对象QImage image(frame.bits(), frame.width(), frame.height(),QVideoFrame::imageFormatFromPixelFormat(frame.pixelFormat()));//设置图片旋转180°,摄像头图片默认的是倒影QMatrix matrix;matrix.rotate(180);image = image.transformed(matrix);//将QImage格式转化成QPixmap格式显示图片QPixmap pixmap = QPixmap::fromImage(image);pixmap = pixmap.scaled(ui->label->size());ui->label->setPixmap(pixmap);}//截图void CameraTest::saveImage(int,QImage image){image=image.convertToFormat(QImage::Format_RGB888);// m_label_camera_show->setPixmap(QPixmap::fromImage(image));// const QPixmap *pixmap = m_label_camera_show->pixmap();QPixmap pixmap = QPixmap::fromImage(image);if(!pixmap.isNull()){pixmap.save("E:\\1.jpg");}}void CameraTest::on_startButton_clicked(){//m_pCamera->setCaptureMode(QCamera::CaptureStillImage);m_pCamera->start();}void CameraTest::on_closeButton_clicked(){m_pCamera->stop();ui->label->clear();}void CameraTest::on_saveButton_clicked(){m_pImageCapture->capture();}
注意
由于QLabel刷新频率较高,导致cpu的内存占有率达到10%以上;可使用GPU代替(qt使用QOpenGLWidget相关类)。
