根本的问题是格式
前言
在阅读此文之前,你需要了解 Qt 如何获取设备的每一帧,需提前了解这些类 :
- QAbstractVideoSurface
- 成员函数 present
- 成员函数 supportedPixelFormats
- QVIdeoWidget 或 QCameraViewfinder
可以参考这篇文章 : Qt获取摄像头画面的每一帧数据
如果使用了上述方法,在主机上可以正常显示/绘制帧,但部署到 Android 上时为空白, 无需修改其他的文件,请尝试在你 QAbstractVideoSurface::supportedPixelFormats 的实现中,添加这些格式 :
QVideoFrame::Format_NV21
QVideoFrame::Format_YUV420P
可以参考这篇文章 : Android读取摄像头的格式
这是相关文件的完整代码 :
myvideosurface.cpp
#include "myvideosurface.h"
MyVideoSurface::MyVideoSurface() { }
bool MyVideoSurface::present( const QVideoFrame &frame ) {
if ( frame.isValid() ) {
QVideoFrame cframe(frame) ;
emit availableFrame( cframe ) ;
return true ;
}
return false ;
}
QList<QVideoFrame::PixelFormat> MyVideoSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const {
if ( type == QAbstractVideoBuffer::NoHandle ) {
return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_NV21 // 添加的格式其一
<< QVideoFrame::Format_YUV420P // 添加的格式其二
<< QVideoFrame::Format_RGB32
<< QVideoFrame::Format_ARGB32
<< QVideoFrame::Format_ARGB32_Premultiplied
<< QVideoFrame::Format_BGR565
<< QVideoFrame::Format_BGR555 ;
} else return QList<QVideoFrame::PixelFormat>() ;
}