来自QAbstractVideoSurface的QT QLabel setPixmap

时间:2018-07-01 09:41:56

标签: c++ qt qlabel qcamera

我正在实现自己的类,以在Windows下探测来自QCamera的视频帧。它是QAbstractVideoSurface的子类。因此,我的探针生成了QPixmap,我试图在QLabel上绘制(作为取景器)。而且我在QLabel setPixmap调用上出现了分段错误。

我确定我的Qpixmap制作正确,因为我可以使用save()将其保存在磁盘上。我的QLabel已初始化并且运行良好,因为我可以从磁盘加载QPixmap并将其设置为QLabel。我想像素图的格式有问题,但不知道如何纠正它:(

我的代码

frameprobe.h

#ifndef FRAMEPROBE_H
#define FRAMEPROBE_H
#include <QAbstractVideoSurface>
#include <QList>
#include <QPixmap>

class FrameProbe : public QAbstractVideoSurface
{
    Q_OBJECT
    public:
        explicit FrameProbe(QObject *parent = 0);

        QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const;

        bool present(const QVideoFrame &frame);

    signals:
        void frameAvailable(QImage frame);
        void frameReady(QPixmap frame);

    public slots:
};

#endif // FRAMEPROBE_H

frameprobe.cpp

#include "frameprobe.h"

FrameProbe::FrameProbe(QObject *parent) :
    QAbstractVideoSurface(parent)
{
}

QList<QVideoFrame::PixelFormat> FrameProbe::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
    Q_UNUSED(handleType);
    return QList<QVideoFrame::PixelFormat>()
        << QVideoFrame::Format_ARGB32
        << QVideoFrame::Format_ARGB32_Premultiplied
        << QVideoFrame::Format_RGB32
        << QVideoFrame::Format_RGB24
        << QVideoFrame::Format_RGB565
        << QVideoFrame::Format_RGB555
        << QVideoFrame::Format_ARGB8565_Premultiplied
        << QVideoFrame::Format_BGRA32
        << QVideoFrame::Format_BGRA32_Premultiplied
        << QVideoFrame::Format_BGR32
        << QVideoFrame::Format_BGR24
        << QVideoFrame::Format_BGR565
        << QVideoFrame::Format_BGR555
        << QVideoFrame::Format_BGRA5658_Premultiplied
        << QVideoFrame::Format_AYUV444
        << QVideoFrame::Format_AYUV444_Premultiplied
        << QVideoFrame::Format_YUV444
        << QVideoFrame::Format_YUV420P
        << QVideoFrame::Format_YV12
        << QVideoFrame::Format_UYVY
        << QVideoFrame::Format_YUYV
        << QVideoFrame::Format_NV12
        << QVideoFrame::Format_NV21
        << QVideoFrame::Format_IMC1
        << QVideoFrame::Format_IMC2
        << QVideoFrame::Format_IMC3
        << QVideoFrame::Format_IMC4
        << QVideoFrame::Format_Y8
        << QVideoFrame::Format_Y16
        << QVideoFrame::Format_Jpeg
        << QVideoFrame::Format_CameraRaw
        << QVideoFrame::Format_AdobeDng;
}

bool FrameProbe::present(const QVideoFrame &frame)
{
    if (frame.isValid()) {
        QVideoFrame cloneFrame(frame);
        cloneFrame.map(QAbstractVideoBuffer::ReadOnly);

        QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat());

        const QImage image(cloneFrame.bits(),
                           cloneFrame.width(),
                           cloneFrame.height(),
                           imageFormat);
        emit frameAvailable(image);
        emit frameReady(QPixmap::fromImage(image));
        cloneFrame.unmap();
        return true;
    }
    return false;
}

初始化代码

  QCamera * MyCamera= new QCamera(CameraDeviceName);
    MyCamera->setCaptureMode( QCamera::CaptureVideo );

    Label = new QLabel();
    Label->setText("Label");

    FrameProbe * VSurface = new FrameProbe();

    MyCamera->setViewfinder(VSurface);

    connect(VSurface, SIGNAL(frameAvailable(QImage)), this, SLOT(video_probed(QImage)));
    connect(VSurface, SIGNAL(frameReady(QPixmap)), this,SLOT(video_forward(QPixmap)));

    ui->gridLayout->addWidget(Label,0,0);

    MyCamera->start();

和广告位

void MainWindow::video_probed(QImage InVideoFrame){

  FramesProbed++;
  std::cout<<FramesProbed<<std::endl;

}
void MainWindow::video_forward(QPixmap InVideoFrame){

    Label->setPixmap(InVideoFrame); //<<<<<<<<< Segmentation Fault here
}

如果我将video_forward插槽更改为类似的内容

void MainWindow::video_forward(QPixmap InVideoFrame){

    InVideoFrame.save("c:\\temp\\a.jpg",0,-1);
    QPixmap a;
    a.load("c:\\temp\\a.jpg");

    Label->setPixmap(a);
}

它起作用。当然很慢:)但可以工作...

PS:在FrameProbe :: present图像中具有QImage :: Format_RGB32格式。

1 个答案:

答案 0 :(得分:0)

解决方案:

bool FrameProbe::present(const QVideoFrame &frame)
{
   ...
   emit frameAvailable(image.copy());
   ...
}

// slot video_probed
connect(VSurface, &FrameProbe::frameAvailable, [=](QImage a){
    Label->setPixmap(QPixmap::fromImage(a));
});

那为什么崩溃:

因为您正在使用以下结构来构造QImage

  

QImage :: QImage(uchar *数据,整数宽度,整数高度,格式格式,   QImageCleanupFunction cleanupFunction = Q_NULLPTR,void * cleanupInfo =   Q_NULLPTR)

第一个参数data是从cloneFrame设置的,它将在cloneFrame.unmap();之后释放。 data不会在QLabel::paintEvent堆栈中。