在多个小部件中显示视频的一种方法是使用自定义视频表面类,并使用它们为您生成一系列QImage
,并以您喜欢的方式处理/显示这些图像。自定义影像面的
例子:
/* Here is our custom video surface, */
class VideoSurface : public QAbstractVideoSurface
{
Q_OBJECT
public:
VideoSurface(QObject *parent = 0) : QAbstractVideoSurface(parent)
{
}
QList<QVideoFrame::PixelFormat>
supportedPixelFormats(QAbstractVideoBuffer::HandleType) const
{
return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_RGB32;
}
/* this will get the QVideoFrame and convert to QImage. */
bool present(const QVideoFrame& frame)
{
if (frame.isValid())
{
QVideoFrame cloneFrame(frame);
cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
const QImage img = QImage(cloneFrame.bits(),
cloneFrame.width(),
cloneFrame.height(),
QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
cloneFrame.unmap();
emit readyRead(img);
return true;
}
return false;
}
signals:
void readyRead(QImage);
};
这里有一个样本项目:
Double View project
截图:

希望帮助!
这是一个巨大的帮助!现在,在一些测试视频中,我收到了一些缺少GStreamer插件的错误。我打算用OpenCV抓取和处理这些帧,你认为这会是一个问题吗? – Hugo 2015-02-16 17:37:42
关于您的“缺少GStreamer”,可能会发生这种情况,因为QMediaPlayer不支持所有视频格式,但我不确定,因为我用一个视频试了一下:),我不相信使用OpenCV抓取会是一个问题,您只需要将QImage转换为相应的OpenCV图像,如下例所示:[OpenCV和QImage](http://asmaloney.com/2013/11/code/converting-between-cvmat-and-qimage -or-qpixmap /) – 2015-02-16 23:25:28
好的,谢谢你的帮助。我会为此工作。 – Hugo 2015-02-17 11:19:52