-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCaptureVideoFilter.cpp
80 lines (64 loc) · 1.86 KB
/
CaptureVideoFilter.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "CaptureVideoFilter.h"
#include "QVideoFrameToQImage.h"
#include "QVideoFrameBits.h"
#include <QBuffer>
#include <QByteArray>
CaptureVideoFilter::CaptureVideoFilter( QObject* parent )
: QAbstractVideoFilter( parent ),
m_VideoOutputOrientation( 0 ),
m_Capturing( false )
{
}
QVideoFilterRunnable* CaptureVideoFilter::createFilterRunnable()
{
return new CaptureVideoFilterRunnable( this );
}
void CaptureVideoFilter::capture()
{
setCapturing( true );
}
void CaptureVideoFilter::emitCaptured( const QUrl &imageUrl )
{
emit captured( imageUrl );
}
CaptureVideoFilterRunnable::CaptureVideoFilterRunnable( CaptureVideoFilter* filter ) :
QVideoFilterRunnable(),
m_Filter( filter )
{
}
QVideoFrame CaptureVideoFilterRunnable::run( QVideoFrame *input, const QVideoSurfaceFormat &surfaceFormat, RunFlags flags )
{
Q_UNUSED( surfaceFormat )
Q_UNUSED( flags )
if ( !input )
{
return QVideoFrame();
}
if ( !m_Filter || !m_Filter->capturing() )
{
return *input;
}
QImage image = QVideoFrameToQImage( *input );
QVideoFrameBits frameBits( &image, input, surfaceFormat, m_Filter->videoOutputOrientation() );
QImage newImage( frameBits.width(), frameBits.height(), QImage::Format_ARGB32 );
for ( int y = 0; y < frameBits.height(); y++ )
{
frameBits.getScanLine( y, newImage.scanLine( y ) );
}
QByteArray byteArray;
QBuffer buffer( &byteArray );
newImage.save( &buffer, "PNG" );
QString dataURI = QString( "data:image/png;base64," ) + QString::fromUtf8( byteArray.toBase64() );
m_Filter->emitCaptured( QUrl( dataURI ) );
m_Filter->setCapturing( false );
return *input;
}
void CaptureVideoFilter::setCapturing( bool capturing )
{
if ( m_Capturing == capturing )
{
return;
}
m_Capturing = capturing;
emit capturingChanged();
}