How to grab video frames directly from QCamera

来源:互联网 发布:军工人工智能概念股300 编辑:程序博客网 时间:2024/05/21 11:45

How to grab video frames directly from QCamera

posted on October 3, 2014 by jacob in Free Software, Programming

I have struggled a lot to simply obtain a continous video stream from QCamera without QtGui as single QImages. After a lot of searching, I finally got to a very short and elegant solution. The Qt documentation is really lacking about this. The obvious (and imo correct) solution would be to use QVideoProbe and connect to the videoFrameProbed signal, but this does only work on a couple of platforms and in very exotic constellations (for example, as of now QCamera only works with with QVideoProbe on Android).

The solution is to subclass QAbstractVideoSurface and override its present-method. This is how it looks like:

#include "cameraframegrabber.h"CameraFrameGrabber::CameraFrameGrabber(QObject *parent) :    QAbstractVideoSurface(parent){}QList<QVideoFrame::PixelFormat> CameraFrameGrabber::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 CameraFrameGrabber::present(const QVideoFrame &frame){    if (frame.isValid()) {        QVideoFrame cloneFrame(frame);        cloneFrame.map(QAbstractVideoBuffer::ReadOnly);        const QImage image(cloneFrame.bits(),                           cloneFrame.width(),                           cloneFrame.height(),                           QVideoFrame::imageFormatFromPixelFormat(cloneFrame .pixelFormat()));        emit frameAvailable(image);        cloneFrame.unmap();        return true;    }    return false;}
#ifndef CAMERAFRAMEGRABBER_H#define CAMERAFRAMEGRABBER_H// Qt includes#include <QAbstractVideoSurface>#include <QList>class CameraFrameGrabber : public QAbstractVideoSurface{    Q_OBJECTpublic:    explicit CameraFrameGrabber(QObject *parent = 0);    QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const;    bool present(const QVideoFrame &frame);signals:    void frameAvailable(QImage frame);public slots:};#endif // CAMERAFRAMEGRABBER_H

This is how you would use it in your Qt application:

_camera = new QCamera();_cameraFrameGrabber = new CameraFrameGrabber();_camera->setViewfinder(_cameraFrameGrabber);connect(_cameraFrameGrabber, SIGNAL(frameAvailable(QImage)), this, SLOT(handleFrame(QImage)));_camera->start();
0 0
原创粉丝点击