1.由于QVideoFrame::image() const : unsupported pixel format Format_ABGR32 ,在转换时需要做个特殊处理如下,增加了android手机下的特殊格式处理:
if(frame.pixelFormat() == QVideoFrame::Format_ABGR32) 此部分代码
QImage imageFromVideoFrame(QVideoFrame &frame)
{
qDebug() << "resolution:(" << frame.width() << ", " << frame.height() << ")";
QImage image;
if(frame.map(QAbstractVideoBuffer::ReadOnly)){
QImage::Format fmt = QVideoFrame::imageFormatFromPixelFormat(frame.pixelFormat());
if(fmt != QImage::Format_Invalid){
qDebug() << "not invalid format.";
image = QImage(frame.bits(), frame.width(), frame.height(), /*v.bytesPerLine(),*/fmt);
}
else if(frame.pixelFormat() == QVideoFrame::Format_ABGR32){
const auto max = frame.width() * frame.height() * 4;
std::vector< uchar > buf;
buf.reserve( max );
uchar * bits = frame.bits();
static const size_t i1 = ( Q_BYTE_ORDER == Q_LITTLE_ENDIAN ? 2 : 0 );
static const size_t i2 = ( Q_BYTE_ORDER == Q_LITTLE_ENDIAN ? 1 : 3 );
static const size_t i3 = ( Q_BYTE_ORDER == Q_LITTLE_ENDIAN ? 0 : 2 );
static const size_t i4 = ( Q_BYTE_ORDER == Q_LITTLE_ENDIAN ? 3 : 1 );
for( auto i = 0; i < max; )
{
buf.push_back( bits[ i1 ] );
buf.push_back( bits[ i2 ] );
buf.push_back( bits[ i3 ] );
buf.push_back( bits[ i4 ] );
bits += 4;
i += 4;
}
image = QImage( &buf[ 0 ], frame.width(), frame.height(), frame.bytesPerLine(),
QImage::Format_ARGB32 ).copy();
}
else
{
int nbytes = frame.mappedBytes();
qDebug() << "invalid format. nbytes=" << nbytes;
image = QImage::fromData(frame.bits(), nbytes);
//image = frame.image();
}
frame.unmap();
}
return image.mirrored(true, true);
}