Qt5.5 调用摄像头拍照

在网上找了摄像头拍照的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 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值