步骤:
- 在.pro文件下添加两个模块。如下:
QT += multimedia #这个模块包含<QCamera><QCameraImageCapture> QT += multimediawidgets #包含<QCameraViewfinder>
- widget.h头文件,如下:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QCamera>//系统摄像设备(摄像头) #include <QCameraViewfinder>//摄像取景器部件 #include <QCameraImageCapture>//截图部件 #include <QFileDialog> //保存时候打开文件窗口 namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: //定义槽函数 void captureImage(); void displayImage(int,QImage); void saveImage(); private: Ui::Widget *ui; QCamera *camera; QCameraViewfinder *viewfinder; QCameraImageCapture *imageCapture; }; #endif // WIDGET_H
- widget.cpp文件,如下:
#include "widget.h" #include "ui_widget.h" #include <QPen> #include <QPainter> #include <QDebug> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); //申请空间 camera=new QCamera(this); //系统摄像设备(摄像头) viewfinder=new QCameraViewfinder(this);//摄像取景器部件 imageCapture=new QCameraImageCapture(camera); //截图部件 指定父对象camera 依赖摄像头截图 camera->setViewfinder(viewfinder);//申请好的取景部件给摄像头 camera->start();//运行摄像头 //注意: //ImageView是 horizontalLayout 类的对象 //ImageCapture是Lable 类的对象 ui->ImageView ->addWidget(viewfinder);//摄像头取到的景放入ImageView ui->ImageCapture->setScaledContents(true); //让图片适应ImageCapture的大小 //horizontalLayout 类有捕捉图像的功能 connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage))); connect(ui->buttonCapture, SIGNAL(clicked()), this, SLOT(captureImage())); connect(ui->buttonSave, SIGNAL(clicked()), this, SLOT(saveImage())); connect(ui->buttonQuit, SIGNAL(clicked()), this, SLOT(close())); } Widget::~Widget() { delete ui; } void Widget::captureImage() { imageCapture->capture(); } void Widget::displayImage(int , QImage image) { ui->ImageCapture->setPixmap(QPixmap::fromImage(image)); } void Widget::saveImage() { QString fileName=QFileDialog::getSaveFileName(this, tr("save file"), "../", tr("Images (*.png *.xpm *.jpg)")); //返回的是文件路径字符串 , 第四个参数是设置可以保存的类型 if(fileName.isEmpty()) { return; } const QPixmap* pixmap=ui->ImageCapture->pixmap();//使用一种画布指向图片 if(pixmap) { pixmap->save(fileName); //保存画布 } }