Qt : 启动摄像头.

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37233607/article/details/79181513

见识过了QQ等社交软件的视频功能,就想看看Qt是否封装了相关的类.然后做了下面一个小demo,一个简单的摄像头.

这里写图片描述

下面给出代码:由于拖拽的布局,有些人看不太懂,所以我全部用的代码手工实现….觉得烦的朋友不要在意啦~

#include "c.h"

c::c(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    this->setWindowTitle(QString::fromLocal8Bit("摄像头"));

    //创建各成员.
    m_camera = new QCamera();
    m_viewFinder = new QCameraViewfinder(this);
    m_imageCapture = new QCameraImageCapture(m_camera);
    m_firstLabel = new QLabel(this);
    m_secondLabel = new QLabel(this);
    m_grabButton = new QPushButton(QString::fromLocal8Bit("截图"),this);
    m_saveButton = new QPushButton(QString::fromLocal8Bit("保存"),this);
    m_exitButton = new QPushButton(QString::fromLocal8Bit("退出"),this);

    //给相机设置取景器.
    m_camera->setViewfinder(m_viewFinder);

    //设置图像显示的位置.
    m_viewFinder->move(0,0);
    m_viewFinder->setFixedSize(600,600);
    m_viewFinder->show();

    //设置布局.
    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->addWidget(m_grabButton);
    m_grabButton->setFixedSize(100,25);
    hLayout->addWidget(m_saveButton);
    m_saveButton->setFixedSize(100, 25);
    hLayout->addWidget(m_exitButton);
    m_exitButton->setFixedSize(100, 25);
    hLayout->setGeometry(QRect(0,600,1000,100));

    m_firstLabel->setGeometry(600,0,400,400);
    m_firstLabel->setFrameShape(QFrame::StyledPanel);
    m_firstLabel->setScaledContents(true);
    m_secondLabel->setGeometry(700, 400, 200, 200);
    m_secondLabel->setFrameShape(QFrame::Panel);
    m_secondLabel->setScaledContents(true);

    this->setFixedSize(1000,700);

    //启动相机.
    m_camera->start();

    connect(m_imageCapture, SIGNAL(imageCaptured(int, const QImage&)), this, SLOT(showCapturedImageSlot(int,const QImage&)));
    connect(m_grabButton, SIGNAL(clicked()), this, SLOT(grabPictureSlot()));
    connect(m_saveButton, SIGNAL(clicked()), this, SLOT(saveImageSlot()));
    connect(m_exitButton, SIGNAL(clicked()), this, SLOT(close()));
}

c::~c()
{

}
void c::showCapturedImageSlot(int, const QImage& image)
{
    //保存所截取的图片.
    m_image = image;
    //显示截取的图片.
    m_firstLabel->setPixmap(QPixmap::fromImage(m_image));
    m_secondLabel->setPixmap(QPixmap::fromImage(m_image));
}
void c::grabPictureSlot()
{
    m_imageCapture->capture();
}
void c::saveImageSlot()
{
    //判断是否有可存的图片.
    if (m_image.isNull())
    {
        QMessageBox::warning(this, "warning", QString::fromLocal8Bit("没有可存取的图片"));
        return;
    }
    QString savePath = QFileDialog::getSaveFileName(this, "save", QDir::currentPath());
    if (!savePath.isEmpty())
    {
        //保存图片并且提示.
        bool ok = m_image.save(savePath);
        if (ok)
            QMessageBox::information(this, "success", QString::fromLocal8Bit("图片已成功保存在:%1").arg(savePath));
    }
}

“c.h”

#ifndef C_H
#define C_H

#include <QtWidgets/QWidget>
#include "ui_c.h"
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QMessageBox>

class c : public QWidget
{
    Q_OBJECT

public:
    c(QWidget *parent = 0);
    ~c();
private slots:
    void showCapturedImageSlot(int, const QImage&);
    void grabPictureSlot();
    void saveImageSlot();
private:
    Ui::cClass ui;
    QCamera *m_camera;
    QCameraViewfinder *m_viewFinder;
    QCameraImageCapture *m_imageCapture;
    QLabel *m_firstLabel;
    QLabel *m_secondLabel;

    QPushButton *m_grabButton;
    QPushButton *m_saveButton;
    QPushButton *m_exitButton;
    QImage m_image;
};

#endif // C_H

这样一个简单的摄像头功能就实现啦~

展开阅读全文

没有更多推荐了,返回首页