可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I want to get every frames from a QMediaPlayer and convert it to QImage (or cv::Mat)
so I used videoFrameProbed signal from QVideoProbe:
connect(&video_probe_, &QVideoProbe::videoFrameProbed, [this](const QVideoFrame& currentFrame){ //QImage img = ?? }
But I didn't find any way for getting QImage from QVideoFrame!
How can I convert QVideoFrame to QImage ?!
回答1:
You can use QImage's constructor:
QImage img( currentFrame.bits(), currentFrame.width(), currentFrame.height(), currentFrame.bytesPerLine(), imageFormat);
Where you can get imageFormat from pixelFormat of the QVideoFrame:
QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(currentFrame.pixelFormat());
回答2:
For QCamera output, that method doesn't always work. In particular, QVideoFrame::imageFormatFromPixelFormat() returns QImage::Format_Invalid when given QVideoFrame::Format_Jpeg, which is what's coming out of my QCamera. But this works:
QImage Camera::imageFromVideoFrame(const QVideoFrame& buffer) const { QImage img; QVideoFrame frame(buffer); // make a copy we can call map (non-const) on frame.map(QAbstractVideoBuffer::ReadOnly); QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat( frame.pixelFormat()); // BUT the frame.pixelFormat() is QVideoFrame::Format_Jpeg, and this is // mapped to QImage::Format_Invalid by // QVideoFrame::imageFormatFromPixelFormat if (imageFormat != QImage::Format_Invalid) { img = QImage(frame.bits(), frame.width(), frame.height(), // frame.bytesPerLine(), imageFormat); } else { // e.g. JPEG int nbytes = frame.mappedBytes(); img = QImage::fromData(frame.bits(), nbytes); } frame.unmap(); return img; }