Recording video from the camera implementation

Source: Internet
Author: User

Collection of two Photography like Head Record system Video of the Demo One: Based on OpenCV Implement

QT Pro files need to be added to: LIBS + =-lopencv_core-lopencv_highgui, or use g++ to compile non-QT environment code using this parameter.

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

The first thing is to get a Cvcapture object through a camera device.

if (1 = = ARGC)

{//Get initialization object from camera cvcapture

Capture = cvcreatecameracapture (0);

}

Else

{//Get initialization object from video file Cvcapture

Capture = Cvcreatecameracapture (Atoi (argv[1));

}

If a valid Cvcapture object is not obtained, the return-1 Terminator runs

if (!capture)

{

return-1;

}

Qcloseevent *closeevent;

iplimage* frame;

Specify the size of each frame in the video (the picture under my camera is 160*120)

Cvsize size = cvsize (640,480);

Need to initialize an object to write a video file, note here that the codec format used is mjpg frame rate set to 5

cvvideowriter* Videowriter =

Cvcreatevideowriter ("My.avi", CV_FOURCC (' M ', ' J ', ' P ', ' G '), 5,size);

Char keycode;

Remove a frame from the camera every 10ms

while (keycode = Cvwaitkey (Ten)) &&isclose)

{

if (keycode = = 27)

{

Break

}

Get frames taken from the camera

frame = Cvqueryframe (capture);

To write a frame to a video file

Cvwriteframe (Videowriter,frame);

Cvshowimage ("Vedio", frame);

if (A.closingdown ())

Isclose=false;

}

Cvreleasevideowriter (&videowriter);

Cvreleaseimage (&frame);

Cvdestroywindow ("video");

Returna.exec ();

}

The effect is as follows:

I'm not going to pass this picture, it's my avatar. Grow ugly, afraid to frighten people. (Linux)

Collection of two camera recording video Demo II: Based on QT implementation

QT Pro files need to be added as follows:

QT + = Multimedia Multimediawidgets

Program I changed to QT5.0.2 version

/*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::D ialog *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::D ialog (Qwidget *parent):

Qdialog (parent),

UI (New UI::D ialog)

{

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::p ause ()

{

Qdebug () << "Pause";

Mediarecorder->pause ();

}

void Dialog::stop ()

{

Mediarecorder->stop ();

}

As follows:

It's not a pass.

Two contrast, the personal feeling QT class library do more than the OPENCV effect processing will be better, in the same light will clearly feel clear. There was also a demo based on V4L2, which has not been debugged. Have time to learn later. Both of these implementations are video functions, but the addition of sound in the video has not been implemented, which is also waiting for time to be realized later.

Recording video from the camera implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.