簡體   English   中英

來自QAbstractVideoSurface的QT QLabel setPixmap

[英]QT QLabel setPixmap from QAbstractVideoSurface

我正在實現自己的類,以在Windows下從QCamera探測視頻幀。 它是QAbstractVideoSurface的子類。 因此,我的探針生成了QPixmap,我試圖在QLabel上繪制(作為取景器)。 我在QLabel setPixmap調用上出現了分段錯誤。

我確定我的Qpixmap制作正確,因為我可以使用save()將其保存在磁盤上。 我的QLabel已初始化並且運行良好,因為我可以從磁盤加載QPixmap並將其設置為QLabel。 我猜有一個像素圖的格式有問題,但不知道如何糾正它:(

我的密碼

框架探針

#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格式。

解:

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,無效* cleanupInfo = Q_NULLPTR)

第一個參數data是從cloneFrame設置的,它將在cloneFrame.unmap();之后釋放cloneFrame.unmap(); dataQLabel::paintEvent堆棧中將不存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM