(DEMO代码)项目名称:myCamera
myCamera.pro
QT += multimedia
QT += multimediawidgets
mycameradialog.h
#ifndef MYCAMERADIALOG_H
#define MYCAMERADIALOG_H
#include <QDialog>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QLabel>
#include <QPushButton>
class MyCameraDialog : public QDialog
{
Q_OBJECT
public:
MyCameraDialog(QWidget *parent = 0);
~MyCameraDialog();
private slots:
void captureBtnResponded();
void saveBtnResponded();
void exitBtnResponded();
void cameraImageCaptured(int id, QImage image);
private:
// 系统摄像设备(摄像头)
QCamera *camera;
// 摄像取景器部件
QCameraViewfinder *cameraViewFinder;
// 截图部件
QCameraImageCapture *cameraImageCapture;
QPushButton *captureBtn;
QPushButton *saveBtn;
QPushButton *exitBtn;
QLabel *displayLabel;
void translateLanguage();
};
#endif // MYCAMERADIALOG_H
mycameradialog.cpp
#include "mycameradialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
MyCameraDialog::MyCameraDialog(QWidget *parent)
: QDialog(parent)
{
this->setFixedSize(700, 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);
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();
this->setLayout(mainLayout);
this->translateLanguage();
}
MyCameraDialog::~MyCameraDialog()
{
}
void MyCameraDialog::translateLanguage()
{
this->setWindowTitle(tr("testCapture"));
captureBtn->setText(tr("capture"));
saveBtn->setText(tr("save"));
exitBtn->setText(tr("exit"));
}
void MyCameraDialog::captureBtnResponded()
{
cameraImageCapture->capture();
}
void MyCameraDialog::saveBtnResponded()
{
const QPixmap *pixmap = displayLabel->pixmap();
if(pixmap) {pixmap->save("D:/workspace/Qt_Project/test/camera/a.jpg");}
}
void MyCameraDialog::exitBtnResponded()
{
camera->stop();
this->close();
}
void MyCameraDialog::cameraImageCaptured(int id, QImage image)
{
displayLabel->setPixmap(QPixmap::fromImage(image));
}