代码改变世界

从摄像头录制视频实现

2015-01-15 00:49  winemike  阅读(2453)  评论(0编辑  收藏  举报

收藏两个头录视频demo之一:基于opencv实现

QT PRO文件需要加入:LIBS +=   -lopencv_core -lopencv_highgui,或者使用g++ 利用这个参数来编译非QT环境的代码。

#include <QCoreApplication>

#include <QtGui/QCloseEvent>

#include<opencv/cv.h>

#include<opencv/highgui.h>

void closeEvent(QCloseEvent *event);

int main(int argc, char *argv[])

{

    QCoreApplication a(argc, argv);

    cvNamedWindow("vedio");

    CvCapture* capture = 0;

    boolIsclose=true;

    // 首先是要通过摄像设备来得到一个CvCapture对象

    if(1 == argc)

    {   // 从摄像头获取初始化对象CvCapture

        capture = cvCreateCameraCapture(0);

    }

    else

    {   // 从视频文件中获取初始化对象CvCapture

        capture = cvCreateCameraCapture(atoi(argv[1]));

    }

    // 如果没有获取到有效的CvCapture对象,则返回 -1 终止程序运行

    if(!capture)

    {

        return -1;

    }

    // QCloseEvent *closeEvent;

    IplImage* frame;

    //指定视频中每一帧的大小(我的摄像头拍摄下的图片均是160*120的)

    CvSize size = cvSize(640,480);

    //需要初始化一个写视频文件的对象,这里注意使用的编解码器格式是MJPG  帧率设置为5

    CvVideoWriter* videoWriter =

            cvCreateVideoWriter("my.avi",CV_FOURCC('M','J','P','G'),5,size);

    char keyCode;

    //每隔10ms,从摄像头中取出一帧

    while((keyCode = cvWaitKey(10))&&Isclose)

    {

        if(keyCode == 27)

        {

            break;

        }

        //得到从摄像头中获取的帧

        frame = cvQueryFrame(capture);

        //将帧写入视频文件中

        cvWriteFrame(videoWriter,frame);

        cvShowImage("vedio",frame);

        if(a.closingDown())

            Isclose=false;

    }

    cvReleaseVideoWriter(&videoWriter);

    cvReleaseImage(&frame);

    cvDestroyWindow("video");

    returna.exec();

}

 

效果如下:

这个图我就不传了,里面是我的头像。长得丑,怕吓到人。(Linux)

收藏两个摄像头录制视频的demo之二:基于QT实现

 

 

QT PRO文件需要加入如下:

 

QT       += multimedia multimediawidgets

 

程序我改为QT5.0.2版本

 

/*dialog.h*/

 

#ifndef DIALOG_H

 

#defineDIALOG_H

 

#include<QtWidgets/QDialog>

 

#include<QtMultimedia/QCamera>

 

#include<QtMultimediaWidgets/QCameraViewfinder>

 

#include<QtMultimedia/QMediaRecorder>

 

#include<QtWidgets/QPushButton>

 

#include <QtWidgets/QLabel>

 

namespace Ui {

 

class Dialog;

 

}

 

class Dialog : public QDialog

 

{

 

    Q_OBJECT

 

   

 

public:

 

    explicit Dialog(QWidget *parent = 0);

 

    ~Dialog();

 

publicslots:

 

   voidrecordVideo();

 

   voidchangeDuration(qint64 seconds);

 

   voidpause();

 

   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

 

/*dialog.cpp*/

 

#include "dialog.h"

 

#include "ui/ui_dialog.h"

 

#include<QtWidgets/QHBoxLayout>

 

#include <QtWidgets/QVBoxLayout>

 

#include <QtWidgets/QPushButton>

 

#include<QUrl>

 

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);

 

    viewfinder->resize(640,480);

 

    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()

 

{

 

    deletemediaRecorder;

 

    delete camera;

 

}

 

void Dialog:: recordVideo()

 

{

 

    qDebug() << "record video";

 

    mediaRecorder->setOutputLocation(QUrl("/home/zhanghaijun/testclip"));

 

    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));

 

}

 

voidDialog::pause()

 

{

 

    qDebug() << "pause";

 

    mediaRecorder->pause();

 

}

 

void Dialog::stop()

 

{

 

    mediaRecorder->stop();

 

}

 

效果图如下:

这个也不传。

两个对比了下,个人感觉QT的类库做的比Opencv效果处理会更好点,在相同的灯光下会明显感觉清晰。本来还有个基于v4l2的demo,一直没调试出来。以后有时间再学习下。这两个都实现的是录像功能,但是在视频内加入声音还没有实现,这也等以后有时间来实现下。