-
Notifications
You must be signed in to change notification settings - Fork 10
/
videocap.cpp
56 lines (47 loc) · 991 Bytes
/
videocap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "videocap.h"
#include <QTime>
#include <QTimer>
#include <QDebug>
#include <QMutexLocker>
VideoCap::VideoCap(double fps, QObject *parent)
: QObject( parent )
, m_delay( 1000 / fps )
{
moveToThread( &m_thread );
m_thread.start();
QMetaObject::invokeMethod(this,"nextFrame");
}
VideoCap::~VideoCap()
{
setFps(0);
m_thread.quit();
m_thread.wait();
}
void VideoCap::setFps(double fps)
{
QMutexLocker locker(&m_mutex);
m_delay = 1000 / fps;
}
void VideoCap::nextFrame()
{
if ( 0 == m_delay ) { return; }
m_timer.restart();
QImage frame;
doGrabFrame( frame );
if ( ! frame.isNull() )
{
m_mutex.lock();
m_frame = frame;
m_mutex.unlock();
emit readyRead();
}
if ( 0 != m_delay )
{
QTimer::singleShot( qMax( 0.0, m_delay - m_timer.elapsed() ), this, SLOT(nextFrame()) );
}
}
QImage VideoCap::grabFrame()
{
QMutexLocker locker(&m_mutex);
return m_frame.copy();
}