效果图:
//CSDNCamera.pro
QT += core gui
QT += multimediawidgets
QT += multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = CSDNCamera
TEMPLATE = app
SOURCES += main.cpp\
CSDNCamera.cpp
HEADERS += CSDNCamera.h
FORMS += CSDNCamera.ui
//CSDNCamera.h
#ifndef CSDNCAMERA_H
#define CSDNCAMERA_H
#include <QWidget>
#include<QCamera>
#include<QCameraViewfinder>
#include<QCameraImageCapture>
#include<QMediaRecorder>
namespace Ui {
class CSDNCamera;
}
class CSDNCamera : public QWidget
{
Q_OBJECT
public:
explicit CSDNCamera(QWidget *parent = 0);
~CSDNCamera();
void openCamera(QString description=QString());
void enumCamera();
void bindRecoder(QCamera *camera);
private slots:
void on_btn_record_video_clicked();
void on_btn_cut_image_clicked();
void onImageCaptured(int id,QImage img);
void on_btn_open_camera_clicked();
void on_btn_record_video_stop_clicked();
private:
Ui::CSDNCamera *ui;
QCamera* mCamera; //摄像头对象
QCameraViewfinder* mViewfinder; //摄像头取景器
QCameraImageCapture* mImageCapture; //截图对象
QMediaRecorder* mMediaRecorder; //视频录制对象
};
#endif // CSDNCAMERA_H
//CSDNCamera.cpp
#include "CSDNCamera.h"
#include "ui_CSDNCamera.h"
#include<QCameraInfo>
#include<QDebug>
#include<QAudioEncoderSettings>
#include<QUrl>
CSDNCamera::CSDNCamera(QWidget *parent) :
QWidget(parent),
ui(new Ui::CSDNCamera)
{
ui->setupUi(this);
mCamera = NULL;
mViewfinder = new QCameraViewfinder(ui->widget_camrea_view);
mViewfinder->resize(ui->widget_camrea_view->size());
mViewfinder->setAspectRatioMode(Qt::KeepAspectRatio);//设置全视角显示
enumCamera();
}
CSDNCamera::~CSDNCamera()
{
if(mCamera != NULL)
mCamera->stop();
delete ui;
}
void CSDNCamera::openCamera(QString description)
{
if(description.isEmpty()){
QCameraInfo info = QCameraInfo::defaultCamera(); //默认摄像头
mCamera = new QCamera(info,this);
}else{
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
foreach (const QCameraInfo &cameraInfo, cameras){
if(cameraInfo.description() == description){
mCamera = new QCamera(cameraInfo,this);
break;
}
}
}
mCamera->setCaptureMode(QCamera::CaptureVideo);
connect(mCamera, static_cast<void(QCamera::*)(QCamera::Error)>(&QCamera::error),
[=](QCamera::Error value){ qDebug()<<value;});
//设置摄像头取景器
mCamera->setViewfinder(mViewfinder);
//绑定截图对象
mImageCapture = new QCameraImageCapture(mCamera);
connect(mImageCapture,SIGNAL(imageCaptured(int,QImage)),SLOT(onImageCaptured(int,QImage)));
bindRecoder(mCamera);
//开启摄像头
mCamera->start();
}
/*
@brief 枚举摄像头
@return
*/
void CSDNCamera::enumCamera()
{
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
foreach (const QCameraInfo &cameraInfo, cameras){
ui->comboBox_camera->addItem(cameraInfo.description());
}
}
/*
@brief 设置录制格式
@param camera 摄像头对象
@return
windows 下测试无效,其他解决方案 用QtMEL 详情 http://kibsoft.ru/
*/
void CSDNCamera::bindRecoder(QCamera *camera)
{
mMediaRecorder = new QMediaRecorder(camera,this);
connect(mMediaRecorder, static_cast<void(QMediaRecorder::*)(QMediaRecorder::Error)>(&QMediaRecorder::error),
[=](QMediaRecorder::Error error){ qDebug()<<error;});
connect(mMediaRecorder,&QMediaRecorder::statusChanged,[=](QMediaRecorder::Status status){qDebug()<<status;});
//设置音频编码格式
QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/amr");
audioSettings.setQuality(QMultimedia::HighQuality);
//mMediaRecorder->setAudioSettings(audioSettings);
QVideoEncoderSettings settings = mMediaRecorder->videoSettings();
settings.setResolution(640,480);
settings.setQuality(QMultimedia::VeryHighQuality);
settings.setFrameRate(30.0);
settings.setCodec("video/mp4");
mMediaRecorder->setVideoSettings(settings);
mMediaRecorder->setOutputLocation(QUrl(qApp->applicationDirPath() + "/xzp.mp4"));
}
void CSDNCamera::on_btn_record_video_clicked()
{
if(!mCamera) return;
qDebug()<<"start record";
mMediaRecorder->record();
}
void CSDNCamera::on_btn_cut_image_clicked()
{
if(!mCamera) return;
mImageCapture->capture();
}
/*
@brief 显示截图
@param id 截图ID
@param img mImageCapture截图返回的图片信息
@return
*/
void CSDNCamera::onImageCaptured(int id, QImage img)
{
qDebug()<<id;
ui->lab_cutImage->setPixmap(QPixmap::fromImage(img).scaled(ui->lab_cutImage->size()));
}
void CSDNCamera::on_btn_open_camera_clicked()
{
openCamera();//打开默认摄像头
//openCamera(ui->comboBox_camera->currentText()); 打开选择的摄像头
}
void CSDNCamera::on_btn_record_video_stop_clicked()
{
if(!mCamera) return;
mMediaRecorder->stop();
}