For QCamera output, this method does not always work. In particular, QVideoFrame :: imageFormatFromPixelFormat () returns QImage :: Format_Invalid when QVideoFrame :: Format_Jpeg is set, which is what comes out of my QCamera. But it 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 { // eg JPEG int nbytes = frame.mappedBytes(); img = QImage::fromData(frame.bits(), nbytes); } frame.unmap(); return img; }
source share