How to grab video frames directly from QCamera
2017-04-01 09:06
513 查看
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:
This is how you would use it in your Qt application:
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_OBJECT public: 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();
相关文章推荐
- ios学习--How to capture video frames from the camera as images using AV Foundation
- How to capture video frames from the camera as images using AV Foundation
- How to capture video frames from the camera as images using AV Foundation on iOS
- How to capture video frames from the camera as images using AV Foundation
- (转载).net中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net)
- How to convert from the color camera space to the depth camera space in Kinect For Windows
- How to download streaming audio or video media from the internet using the MMS protocol?
- net 中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net)
- .net中捕获摄像头视频的方式及对比(How to Capture Camera Video via(转)
- A class to grab pictures from your camera
- How to save a picture (take screenshot) from a camera
- ffmpeg示例一:how to use libavformat and libavcodec to read video from a file.
- How to identify video formats from command line on Linux
- .net中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net)
- .net中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net)
- Using Live555 to Stream Live Video from an IP camera connected to an H264 encoder
- .net中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net)
- net 中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net) (转)
- .net中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net)
- How to use OpenCV to capture and display images from a camera