×
Namespaces

Variants
Actions
(Difference between revisions)

如何用Qt完成录像功能

From Nokia Developer Wiki
Jump to: navigation, search
liuting (Talk | contribs)
(Liuting - - 介绍)
hamishwillee (Talk | contribs)
m (Text replace - "Category:Multimedia" to "")
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Draft]]
+
[[Category:Qt]][[Category:Qt Mobility]][[Category:Camera]][[Category:Imaging]][[Category:Lang-Chinese]][[Category:Symbian]][[Category:MeeGo Harmattan]][[Category:Code Examples]]
''Delete instructional text in italic''
+
 
+
{{Abstract|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''
+
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
+
|sourcecode= [[Media:Camera.zip]]  
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
Line 12: Line 7:
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
+
|language= Lang-Chinese
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
|translated-from-title= <!-- Title only -->  
+
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-by= <!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
|creationdate= <!-- Format YYYYMMDD -->
+
|creationdate= 20120425
|author= <!-- Display as link [[User:username]] -->
+
|author= [[User:Liuting]]
 
}}
 
}}
  
Line 32: Line 27:
  
 
== 代码实现==
 
== 代码实现==
 +
.h file
 +
<code>
 +
#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
 +
</code>
 +
.cpp file
 +
<code>
 +
#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();
 +
}
 +
 +
</code>
  
 
== 代码下载==
 
== 代码下载==
 +
[[File:Camera.zip]]
 +
  
 
== 相关链接 ==
 
== 相关链接 ==
 +
*[[Portal:Qt (Chinese)]]
 +
*[[多媒体开发]]
  
 
''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''

Latest revision as of 08:00, 14 June 2013

Article Metadata

代码示例
源文件: Media:Camera.zip

兼容于
平台:
Symbian

文章
liuting 在 25 Apr 2012 创建
最后由 hamishwillee 在 14 Jun 2013 编辑

Contents

[edit] 介绍

本文介绍如何使用Qt mobility 的 QCamera 和 QMediaRecorder 完成录像功能

[edit] 代码实现

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

[edit] 代码下载

File:Camera.zip


[edit] 相关链接

Add categories below. Remove Category:Draft when the page is complete or near complete

This page was last modified on 14 June 2013, at 08:00.
165 page views in the last 30 days.
×