Qt5如何使用攝像頭


(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));
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: