Qt5.5 呼叫攝像頭拍照
阿新 • • 發佈:2019-02-13
在網上找了攝像頭拍照的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