将QVideoFrame直接转换为QPixmap

时间:2014-09-21 10:17:37

标签: android qt camera qpixmap

我使用QVideoProbe来访问相机框架。我的平台是Android。 我已将每个相机帧转换为QImage,然后将像素图转换为QLabel。 我的问题是这个过程很慢。 帧显示得非常慢。 我可以将QVideoFrame直接转换为QPixmap或其他更快的方式来显示相机帧吗? 这是我的代码:

    QCamera *camera = new QCamera(this);

    camera->setCaptureMode(QCamera::CaptureViewfinder);

    QVideoProbe *videoProbe = new QVideoProbe(this);

    bool ret = videoProbe->setSource(camera);
    qDebug() <<"videoProbe->setSource(camera):" << ret;

    if (ret) {
          connect(videoProbe, SIGNAL(videoFrameProbed(const QVideoFrame &)),
                this, SLOT(present(const QVideoFrame &)));

    }

    camera->start();
...
...


bool MainWindow::present(const QVideoFrame &frame)
{
    qDebug() <<"counter:" << ++counter;

    QVideoFrame cloneFrame(frame);
    if(cloneFrame.map(QAbstractVideoBuffer::ReadOnly))
    {
        QImage img(
                cloneFrame.size(), QImage::Format_ARGB32);
                qt_convert_NV21_to_ARGB32(cloneFrame.bits(),
                (quint32 *)img.bits(),
                cloneFrame.width(),
                cloneFrame.height());

        label->setPixmap(QPixmap::fromImage(img));

        cloneFrame.unmap();
    }

    return true;
}

1 个答案:

答案 0 :(得分:1)

1.要从视频帧转换为QImage,我使用qt内部方法:

//Somewhere before using
extern QImage qt_imageFromVideoFrame(const QVideoFrame &f);
    ...
//using
QImage imgbuf=qt_imageFromVideoFrame(frame);
  1. 您需要跳过一些框架并仅显示一些框架。它将允许您以最大可能的速度处理流。我使用以下代码执行此操作:

    void MyVideoHandler::videoFrameProbed(const QVideoFrame &frame)
    {
        if(!started)
            return;
        if(!frameProcessor)
            return;
        if(m_isBusy)
        {
            //qDebug() << "Video frame dropped";
            return;
        }
        m_isBusy = true;
        qDebug() << "videoFrameProbed";
        QMetaObject::invokeMethod(frameProcessor, "processFrame", Qt::QueuedConnection,
            Q_ARG(QVideoFrame, frame),
            Q_ARG(bool, param1),
            Q_ARG(bool, param2),
            Q_ARG(bool, param3),
            Q_ARG(bool, param4));
        //qDebug() << "processFrame invoked";
    }
    
  2. 我通过invokeMethod调用它只是因为frameProcessor存在于不同的线程中,但它不是你的情况,因为你需要在UI线程中显示它。另一方面你可以在线程中转换为QImage(或QPixmap)然后将结果发送到UI线程。所以这里是代码如何做到这一点:

    frameProcessor=new MyVideoFrameProcessor();
    frameProcessor->moveToThread(&videoStreamThread);
    

    啊,我也必须说MyVideoFrameProcessor在完成处理时生成事件,MyVideoHandler将m_isBusy切换为false。