在网上找了摄像头拍照的demo,自己敲了一遍,并加了注释,仅作为学习记录。
头文件:
#ifndef TEST_CAPTURE_H
#define TEST_CAPTURE_H
#include <QDialog>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QLabel>
#include <QPushButton>
namespace Ui {
class test_capture;
}
class test_capture : public QDialog
{
Q_OBJECT
public:
test_capture(QWidget *parent = 0);
~test_capture();
private:
QCamera *camera;//系统摄像头设备
QCameraViewfinder *cameraViewFinder;//摄像头取景器部件
QCameraImageCapture *cameraImageCapture;//截图部件
QPushButton *captureBtn;
QPushButton *saveBtn;
QPushButton *exitBtn;
QLabel *displayLabel;
void translateLanguage();
private slots:
void captureBtnResponded();
void saveBtnResponded();
void exitBtnResponded();
void cameraImageCaptured(int id, QImage image);
};
#endif // TEST_CAPTURE_H
cpp文件:
#include "test_capture.h"
#include "ui_test_capture.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
test_capture::test_capture(QWidget *parent) :
QDialog(parent)
{
this->resize(600, 400);
camera = new QCamera();
cameraViewFinder = new QCameraViewfinder();
cameraImageCapture = new QCameraImageCapture(camera);
captureBtn = new QPushButton();
saveBtn = new QPushButton();
exitBtn = new QPushButton();
displayLabel = new QLabel();
displayLabel->setFixedSize(160, 120);
//打开自动平衡收放图片,显示图像大小,自动调节为QLabel的大小。
displayLabel->setScaledContents(true);
//部件垂直布局
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(displayLabel);
rightLayout->addStretch();
rightLayout->addWidget(captureBtn);
rightLayout->addWidget(saveBtn);
rightLayout->addWidget(exitBtn);
//部件水平布局
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(cameraViewFinder);
mainLayout->addLayout(rightLayout);
connect(captureBtn, SIGNAL(clicked()), this, SLOT(captureBtnResponded()));
connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveBtnResponded()));
connect(exitBtn, SIGNAL(clicked()), this, SLOT(exitBtnResponded()));
connect(cameraImageCapture, SIGNAL(imageCaptured(int,QImage)), this,
SLOT(cameraImageCaptured(int,QImage)));
cameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
camera->setCaptureMode(QCamera::CaptureStillImage);
camera->setViewfinder(cameraViewFinder);
camera->start();//启动摄像头
/*QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(formWidget);
setLayout(layout);
*/
this->setLayout(mainLayout);/*必须执行这句代码才显现出布局*/
this->translateLanguage();
}
test_capture::~test_capture()
{
}
void test_capture::translateLanguage()
{
this->setWindowTitle(tr("testCapture"));
captureBtn->setText(tr("capture"));
saveBtn->setText(tr("save"));
exitBtn->setText(tr("exit"));
}
void test_capture::captureBtnResponded()
{
cameraImageCapture->capture();
}
void test_capture::saveBtnResponded()
{
const QPixmap *pixmap = displayLabel->pixmap();
if(pixmap) {pixmap->save("D:\\a.jpg");}
}
void test_capture::exitBtnResponded()
{
camera->stop();
this->close();
}
void test_capture::cameraImageCaptured(int id, QImage image)
{
displayLabel->setPixmap(QPixmap::fromImage(image));
}
亲测能用。
来自Qt大神 一去二三里的博客: http://blog.sina.com.cn/s/blog_a6fb6cc90101g2mg.html