如何用Qt完成录像功能
(Liuting - - →代码下载) |
(Liuting - - →相关链接) |
||
| Line 172: | Line 172: | ||
[[File:Camera.zip]] | [[File:Camera.zip]] | ||
| + | [[Category:Qt]][[Category:Qt Mobility]][[Category:Multimedia]] | ||
== 相关链接 == | == 相关链接 == | ||
| + | *[[Qt 开发]] | ||
| + | *[[多媒体开发]] | ||
''Add categories below. Remove Category:Draft when the page is complete or near complete'' | ''Add categories below. Remove Category:Draft when the page is complete or near complete'' | ||
Revision as of 13:38, 25 April 2012
Delete instructional text in italic
This article explains how to ... Replace the abstract text with a short paragraph (or sentence) describing what the topic covers.
Enter article metadata as described below. Note that this template can be placed anywhere in the article. Do not remove parameters that you do not use
Article Metadata
Contents |
介绍
本文介绍如何使用Qt mobility 的 QCamera 和 QMediaRecorder 完成录像功能
代码实现
.h file
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QCamera>
#include <QCameraViewfinder>
#include <QMediaRecorder>
#include <QPushButton>
#include <QLabel>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
public slots:
void recordVideo();
void changeDuration(qint64 seconds);
void pause();
void stop();
private:
Ui::Dialog *ui;
QCamera* camera;
QCameraViewfinder* viewfinder;
QMediaRecorder* mediaRecorder;
QPushButton* recordButton ;
QPushButton* pauseButton ;
QPushButton* stopButton;
QPushButton* exitButton;
QLabel* timeDisplay;
};
#endif // DIALOG_H
.cpp file
#include "dialog.h"
#include "ui_dialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
// ui->setupUi(this);
QByteArray cameraDevice = QCamera::availableDevices()[0];
camera = new QCamera(cameraDevice);
QHBoxLayout* layout = new QHBoxLayout;
viewfinder = new QCameraViewfinder(this);
QVBoxLayout* vLayout = new QVBoxLayout ;
recordButton = new QPushButton("record", this);
pauseButton = new QPushButton("pause", this);
stopButton = new QPushButton("stop", this);
exitButton = new QPushButton("exit", this);
timeDisplay = new QLabel(this);
vLayout->addWidget(recordButton);
vLayout->addWidget(pauseButton);
vLayout->addWidget(stopButton);
vLayout->addWidget(timeDisplay);
vLayout->addWidget(exitButton);
connect(recordButton, SIGNAL(clicked()), this, SLOT(recordVideo()));
connect(pauseButton,SIGNAL(clicked()),this, SLOT(pause()));
connect(stopButton,SIGNAL(clicked()), this,SLOT(stop()));
connect(exitButton,SIGNAL(clicked()), this, SLOT(close()));
layout->addWidget(viewfinder);
layout->addLayout(vLayout);
camera->setViewfinder(viewfinder);
camera->setCaptureMode(QCamera::CaptureVideo);
camera->start();
mediaRecorder = new QMediaRecorder(camera);
connect(mediaRecorder, SIGNAL(durationChanged(qint64)), this, SLOT(changeDuration(qint64)));
setLayout(layout);
}
Dialog::~Dialog()
{
delete mediaRecorder;
delete camera;
}
void Dialog:: recordVideo()
{
qDebug() << "record video";
mediaRecorder->record();
//QString str = QString("Recorded %1 sec").arg(mediaRecorder->duration());
//timeLabel->setText(str);
}
void Dialog:: changeDuration(qint64 seconds)
{
qDebug() << seconds;
//QString str = QString("Recorded %1 sec").arg(seconds);
timeDisplay->setNum(int(seconds));
}
void Dialog::pause()
{
qDebug() << "pause";
mediaRecorder->pause();
}
void Dialog::stop()
{
mediaRecorder->stop();
}
代码下载
相关链接
Add categories below. Remove Category:Draft when the page is complete or near complete

