QVideoFrame::map() 崩溃
QVideoFrame::map() crashes
我正在尝试使用 QCamera 从相机获取图像。
我认为我应该从 QAbstractVideoSurface
派生并实现 present()
,其中 QVideoFrame
表示相机捕获的当前图像作为参数发送。
因为我需要做一些处理,所以我尝试 map()
我的框架,用 bits()
获取数据,做我必须做的任何事情,然后 unmap()
它。但是我在 map()
上崩溃了
这是我得到的错误:
W libTest.so: (null):0 ((null)): Unsupported viewfinder pixel format
D SensorManager: registerListener :: 6, LSM6DSL Acceleration Sensor, 200000, 0,
E libEGL : call to OpenGL ES API with no current context (logged once per thread)
E GLConsumer: [SurfaceTexture-0-546-0] attachToContext: invalid current EGLDisplay
F libc : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x4 in tid 663 (qtMainLoopThrea)
我做错了什么?
这是完整的应用程序代码:
///////////////////////////////////////////////
//main.cpp
///////////////////////////////////////////////
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "camera_engine.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
CameraEngine camEngine;
engine.rootContext()->setContextProperty("cameraEngine", &camEngine);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
///////////////////////////////////////////////
//camera_engine.h
///////////////////////////////////////////////
#ifndef __CAMERA_ENGINE_H__
#define __CAMERA_ENGINE_H__
#include <QCamera>
#include "image_reader.h"
class CameraEngine : public QObject {
Q_OBJECT
public:
explicit CameraEngine(QCamera::Position pos = QCamera::BackFace);
~CameraEngine();
public slots:
void start();
private:
QCamera mCamera;
ImageReader mImageReader;
};
#endif // __CAMERA_ENGINE_H__
///////////////////////////////////////////////
//camera_engine.cpp
///////////////////////////////////////////////
#include "camera_engine.h"
CameraEngine::CameraEngine(QCamera::Position pos) : mCamera(pos)
{
mCamera.setViewfinder(&mImageReader);
QCameraViewfinderSettings viewFinderSettings;
viewFinderSettings.setResolution(640, 480);
viewFinderSettings.setMinimumFrameRate(30);
viewFinderSettings.setMaximumFrameRate(30);
viewFinderSettings.setPixelFormat(QVideoFrame::Format_RGB24);
mCamera.setViewfinderSettings(viewFinderSettings);
}
CameraEngine::~CameraEngine()
{
if (mCamera.state() == QCamera::ActiveState) {
mCamera.stop();
}
}
void CameraEngine::start()
{
mCamera.start();
}
///////////////////////////////////////////////
//image_reader.h
///////////////////////////////////////////////
#ifndef CAMERA_IMAGE_READER_H
#define CAMERA_IMAGE_READER_H
#include <QAbstractVideoSurface>
class ImageReader : public QAbstractVideoSurface {
Q_OBJECT
public:
ImageReader() = default;
~ImageReader() = default;
virtual bool present(const QVideoFrame& frame);
virtual QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const;
};
#endif // CAMERA_IMAGE_READER_H
///////////////////////////////////////////////
//image_reader.cpp
///////////////////////////////////////////////
#include "image_reader.h"
#include <QDebug>
bool ImageReader::present(const QVideoFrame &frame)
{
QVideoFrame currentFrame = frame;
currentFrame.map(QAbstractVideoBuffer::ReadOnly); //crashes here
// Do something
currentFrame.unmap();
return true;
}
QList<QVideoFrame::PixelFormat> ImageReader::supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const
{
Q_UNUSED(type)
return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_RGB24;
}
///////////////////////////////////////////////
//main.qml
///////////////////////////////////////////////
import QtQuick 2.11
import QtQuick.Controls 2.2
ApplicationWindow {
id: window
visible: true
width: 640
height: 480
Component.onCompleted: cameraEngine.start()
}
编辑:所以,我认为这可能是因为我的 QVideoFrame 存储为 OpenGL 纹理,而我的 present()
函数可能不在 OpenGL 线程上 运行,因此找不到 OpenGL ES上下文。
有没有办法确保它 运行 在正确的线程上?
Edit2:我发现了这个:http://doc.qt.io/qt-5/qtquick-scenegraph-openglunderqml-example.html
也许我可以修改此代码以获得有效的 OpenGL ES 上下文。不幸的是我现在没有时间去做。那个星期一我会试试,除非有人有更好的建议,然后告诉你结果。
Edit3:所以,显然我的解决方案不是很好,我在 initializeOpenGLFunctions();
上遇到了崩溃 (SIGSEGV)
我看到了 Antonio Dias 的回答,使用 VideoOutput
和函数 grabToImage
,试了一下,它似乎有效,但是,如果我理解正确的话,grabToImage
"draws" CPU 内存中的 VideoOutput
,我丢失了一些我计划在此过程中使用 QMediaMetaData
获得的元数据。
我也试过直接用NDK,但是相机要求API等级至少24级,而且设置了我找到的所有相关设置后,好像也用不上
Edit4:我实际上不知道我做了什么,但我的程序最终使用了正确的 API 级别,所以我现在将使用 NDK 解决方案。
...unless someone have a better suggestion...
我在 QML 中从 Camera
对象捕获图像的简单方法是调用 grabToImage
并将图像传递给 C++ 进行处理!
此方法不要求您处理活动相机的所有帧,您只有在准备好或需要时才可以抓取!
请记住,尤其是在 android 中,VideoOutput
对象需要可见才能抓取任何东西,例如,您不能在屏幕关闭的情况下抓取图像Android。但是你不需要填满屏幕,你只需要给VideoOutput
屏幕的一些区域并保持它可见并在顶部。
另外,请注意,减小 VideoOutput
的尺寸不会减小您可以捕获的最大尺寸,例如,您可以捕获尺寸较小的 VideoOutput
的 1280x720 图像。
此外,调整大小时必须保留纵横比 VideoOutput
以保持图像的纵横比。
代码:
您可以看到完整的源代码 here!
main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "camerahelper.h"
int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
CameraHelper camera_helper;
camera_helper.start(engine.rootObjects().first());
return app.exec();
}
camerahelper.h:
#ifndef CAMERAHELPER_H
#define CAMERAHELPER_H
#include <QtCore>
#include <QtGui>
#include <QtQuick>
class CameraHelper : public QObject
{
Q_OBJECT
public:
explicit CameraHelper(QObject *parent = nullptr);
signals:
public slots:
void start(QObject *qml_obj);
void grab();
private slots:
void frameReady(const QVariant &frame_variant);
private:
QObject *m_qml_obj;
};
#endif // CAMERAHELPER_H
camerahelper.cpp:
#include "camerahelper.h"
CameraHelper::CameraHelper(QObject *parent) : QObject(parent)
{
}
void CameraHelper::start(QObject *qml_obj)
{
m_qml_obj = qml_obj;
//Connect the QML frameReady SIGNAL to our frameReady SLOT
connect(m_qml_obj, SIGNAL(frameReady(QVariant)), this, SLOT(frameReady(QVariant)));
//Do the first grab
grab();
}
void CameraHelper::grab()
{
//Size of the captured image
QSize size = QSize(320, 240);
//Pass grab size to QML and wait for captured image on the frameReady SIGNAL
QMetaObject::invokeMethod(m_qml_obj, "grab", Q_ARG(QVariant, size.width()), Q_ARG(QVariant, size.height()));
}
void CameraHelper::frameReady(const QVariant &frame_variant)
{
QQuickItemGrabResult *grab_result = qvariant_cast<QQuickItemGrabResult*>(frame_variant); //Cast from QVariant
QImage frame = grab_result->image(); //Get the QImage
grab_result->deleteLater(); //Release QQuickItemGrabResult
//Depending on OS the image can have different formats,
//use convertToFormat to unify all possibles formats to one
frame = frame.convertToFormat(QImage::Format_RGB32);
//Frame is ready to use
grab(); //Do the next frame grab
}
main.qml:
import QtQuick 2.10
import QtQuick.Window 2.10
import QtMultimedia 5.8
Window
{
visible: true
color: "black"
width: 640
height: 480
title: qsTr("Hello World")
signal frameReady(var frame)
Camera
{
id: camera
viewfinder.resolution: "320x240"
}
VideoOutput
{
id: videoOutput
source: camera
autoOrientation: true
visible: true
anchors.fill: parent
}
function grab(grab_width, grab_height)
{
if (!visible)
return
videoOutput.grabToImage(function(frame)
{
frameReady(frame) //Emit frameReady SIGNAL
}, Qt.size(grab_width, grab_height))
}
}
我终于成功了。显然 QCamera
在 Android 上有一些问题,所以我不得不使用 QML Camera
,并用 VideoOutput
显示它,并应用一个过滤器,通过它我可以得到我的图片。
此过滤器由两部分组成:一部分源自 QAbstractVideoFilter
, the other deriving from QVideoFilterRunnable
。
代码如下:
////////////////////////////////////////////////////////////////////
// main.cpp
////////////////////////////////////////////////////////////////////
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "myfilter.hpp"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<MyFilter>("example.myfilter", 1, 0, "MyFilter");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
////////////////////////////////////////////////////////////////////
// myfilter.hpp
////////////////////////////////////////////////////////////////////
#ifndef MYFILTER_H
#define MYFILTER_H
#include <QAbstractVideoFilter>
class MyFilterRunnable : public QVideoFilterRunnable {
public:
QVideoFrame run(QVideoFrame *input, const QVideoSurfaceFormat &surfaceFormat, RunFlags flags);
};
class MyFilter : public QAbstractVideoFilter
{
public:
QVideoFilterRunnable* createFilterRunnable();
};
#endif // MYFILTER_H
////////////////////////////////////////////////////////////////////
// myfilter.cpp
////////////////////////////////////////////////////////////////////
#include "myfilter.hpp"
#include <QOpenGLContext>
#include <QOpenGLFunctions>
QVideoFrame MyFilterRunnable::run(QVideoFrame *input, const QVideoSurfaceFormat &surfaceFormat, QVideoFilterRunnable::RunFlags flags)
{
QImage img(input->width(), input->height(), QImage::Format_RGBA8888);
bool success = false;
if (input->handleType() == QAbstractVideoBuffer::GLTextureHandle) {
GLuint textureId = input->handle().toUInt();
QOpenGLContext *ctx = QOpenGLContext::currentContext();
QOpenGLFunctions *f = ctx->functions();
GLuint fbo;
f->glGenFramebuffers(1, &fbo);
GLuint prevFbo;
f->glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint *) &prevFbo);
f->glBindFramebuffer(GL_FRAMEBUFFER, fbo);
f->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0);
f->glReadPixels(0, 0, input->width(), input->height(), GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
f->glBindFramebuffer(GL_FRAMEBUFFER, prevFbo);
success = true;
} // else handle other types
if( success ) {
// Process image
return QVideoFrame(img);
} else {
return *input; //Could not apply filter, return unmodified input
}
}
QVideoFilterRunnable *MyFilter::createFilterRunnable()
{
return new MyFilterRunnable;
}
////////////////////////////////////////////////////////////////////
// main.qml
////////////////////////////////////////////////////////////////////
import QtQuick 2.11
import QtQuick.Controls 2.2
import QtMultimedia 5.9
import example.myfilter 1.0
ApplicationWindow {
id: window
visible: true
width: 640
height: 480
Camera {
id: camera
}
MyFilter {
id: filter
}
VideoOutput {
source: camera
autoOrientation: true
filters: [ filter ]
anchors.fill: parent
}
}
(我的 run
实现改编自这里:http://code.qt.io/cgit/qt/qtmultimedia.git/tree/examples/multimedia/video/qmlvideofilter_opencl/rgbframehelper.h)
我正在尝试使用 QCamera 从相机获取图像。
我认为我应该从 QAbstractVideoSurface
派生并实现 present()
,其中 QVideoFrame
表示相机捕获的当前图像作为参数发送。
因为我需要做一些处理,所以我尝试 map()
我的框架,用 bits()
获取数据,做我必须做的任何事情,然后 unmap()
它。但是我在 map()
这是我得到的错误:
W libTest.so: (null):0 ((null)): Unsupported viewfinder pixel format
D SensorManager: registerListener :: 6, LSM6DSL Acceleration Sensor, 200000, 0,
E libEGL : call to OpenGL ES API with no current context (logged once per thread)
E GLConsumer: [SurfaceTexture-0-546-0] attachToContext: invalid current EGLDisplay
F libc : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x4 in tid 663 (qtMainLoopThrea)
我做错了什么?
这是完整的应用程序代码:
///////////////////////////////////////////////
//main.cpp
///////////////////////////////////////////////
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "camera_engine.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
CameraEngine camEngine;
engine.rootContext()->setContextProperty("cameraEngine", &camEngine);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
///////////////////////////////////////////////
//camera_engine.h
///////////////////////////////////////////////
#ifndef __CAMERA_ENGINE_H__
#define __CAMERA_ENGINE_H__
#include <QCamera>
#include "image_reader.h"
class CameraEngine : public QObject {
Q_OBJECT
public:
explicit CameraEngine(QCamera::Position pos = QCamera::BackFace);
~CameraEngine();
public slots:
void start();
private:
QCamera mCamera;
ImageReader mImageReader;
};
#endif // __CAMERA_ENGINE_H__
///////////////////////////////////////////////
//camera_engine.cpp
///////////////////////////////////////////////
#include "camera_engine.h"
CameraEngine::CameraEngine(QCamera::Position pos) : mCamera(pos)
{
mCamera.setViewfinder(&mImageReader);
QCameraViewfinderSettings viewFinderSettings;
viewFinderSettings.setResolution(640, 480);
viewFinderSettings.setMinimumFrameRate(30);
viewFinderSettings.setMaximumFrameRate(30);
viewFinderSettings.setPixelFormat(QVideoFrame::Format_RGB24);
mCamera.setViewfinderSettings(viewFinderSettings);
}
CameraEngine::~CameraEngine()
{
if (mCamera.state() == QCamera::ActiveState) {
mCamera.stop();
}
}
void CameraEngine::start()
{
mCamera.start();
}
///////////////////////////////////////////////
//image_reader.h
///////////////////////////////////////////////
#ifndef CAMERA_IMAGE_READER_H
#define CAMERA_IMAGE_READER_H
#include <QAbstractVideoSurface>
class ImageReader : public QAbstractVideoSurface {
Q_OBJECT
public:
ImageReader() = default;
~ImageReader() = default;
virtual bool present(const QVideoFrame& frame);
virtual QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const;
};
#endif // CAMERA_IMAGE_READER_H
///////////////////////////////////////////////
//image_reader.cpp
///////////////////////////////////////////////
#include "image_reader.h"
#include <QDebug>
bool ImageReader::present(const QVideoFrame &frame)
{
QVideoFrame currentFrame = frame;
currentFrame.map(QAbstractVideoBuffer::ReadOnly); //crashes here
// Do something
currentFrame.unmap();
return true;
}
QList<QVideoFrame::PixelFormat> ImageReader::supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const
{
Q_UNUSED(type)
return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_RGB24;
}
///////////////////////////////////////////////
//main.qml
///////////////////////////////////////////////
import QtQuick 2.11
import QtQuick.Controls 2.2
ApplicationWindow {
id: window
visible: true
width: 640
height: 480
Component.onCompleted: cameraEngine.start()
}
编辑:所以,我认为这可能是因为我的 QVideoFrame 存储为 OpenGL 纹理,而我的 present()
函数可能不在 OpenGL 线程上 运行,因此找不到 OpenGL ES上下文。
有没有办法确保它 运行 在正确的线程上?
Edit2:我发现了这个:http://doc.qt.io/qt-5/qtquick-scenegraph-openglunderqml-example.html
也许我可以修改此代码以获得有效的 OpenGL ES 上下文。不幸的是我现在没有时间去做。那个星期一我会试试,除非有人有更好的建议,然后告诉你结果。
Edit3:所以,显然我的解决方案不是很好,我在 initializeOpenGLFunctions();
我看到了 Antonio Dias 的回答,使用 VideoOutput
和函数 grabToImage
,试了一下,它似乎有效,但是,如果我理解正确的话,grabToImage
"draws" CPU 内存中的 VideoOutput
,我丢失了一些我计划在此过程中使用 QMediaMetaData
获得的元数据。
我也试过直接用NDK,但是相机要求API等级至少24级,而且设置了我找到的所有相关设置后,好像也用不上
Edit4:我实际上不知道我做了什么,但我的程序最终使用了正确的 API 级别,所以我现在将使用 NDK 解决方案。
...unless someone have a better suggestion...
我在 QML 中从 Camera
对象捕获图像的简单方法是调用 grabToImage
并将图像传递给 C++ 进行处理!
此方法不要求您处理活动相机的所有帧,您只有在准备好或需要时才可以抓取!
请记住,尤其是在 android 中,VideoOutput
对象需要可见才能抓取任何东西,例如,您不能在屏幕关闭的情况下抓取图像Android。但是你不需要填满屏幕,你只需要给VideoOutput
屏幕的一些区域并保持它可见并在顶部。
另外,请注意,减小 VideoOutput
的尺寸不会减小您可以捕获的最大尺寸,例如,您可以捕获尺寸较小的 VideoOutput
的 1280x720 图像。
此外,调整大小时必须保留纵横比 VideoOutput
以保持图像的纵横比。
代码:
您可以看到完整的源代码 here!
main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "camerahelper.h"
int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
CameraHelper camera_helper;
camera_helper.start(engine.rootObjects().first());
return app.exec();
}
camerahelper.h:
#ifndef CAMERAHELPER_H
#define CAMERAHELPER_H
#include <QtCore>
#include <QtGui>
#include <QtQuick>
class CameraHelper : public QObject
{
Q_OBJECT
public:
explicit CameraHelper(QObject *parent = nullptr);
signals:
public slots:
void start(QObject *qml_obj);
void grab();
private slots:
void frameReady(const QVariant &frame_variant);
private:
QObject *m_qml_obj;
};
#endif // CAMERAHELPER_H
camerahelper.cpp:
#include "camerahelper.h"
CameraHelper::CameraHelper(QObject *parent) : QObject(parent)
{
}
void CameraHelper::start(QObject *qml_obj)
{
m_qml_obj = qml_obj;
//Connect the QML frameReady SIGNAL to our frameReady SLOT
connect(m_qml_obj, SIGNAL(frameReady(QVariant)), this, SLOT(frameReady(QVariant)));
//Do the first grab
grab();
}
void CameraHelper::grab()
{
//Size of the captured image
QSize size = QSize(320, 240);
//Pass grab size to QML and wait for captured image on the frameReady SIGNAL
QMetaObject::invokeMethod(m_qml_obj, "grab", Q_ARG(QVariant, size.width()), Q_ARG(QVariant, size.height()));
}
void CameraHelper::frameReady(const QVariant &frame_variant)
{
QQuickItemGrabResult *grab_result = qvariant_cast<QQuickItemGrabResult*>(frame_variant); //Cast from QVariant
QImage frame = grab_result->image(); //Get the QImage
grab_result->deleteLater(); //Release QQuickItemGrabResult
//Depending on OS the image can have different formats,
//use convertToFormat to unify all possibles formats to one
frame = frame.convertToFormat(QImage::Format_RGB32);
//Frame is ready to use
grab(); //Do the next frame grab
}
main.qml:
import QtQuick 2.10
import QtQuick.Window 2.10
import QtMultimedia 5.8
Window
{
visible: true
color: "black"
width: 640
height: 480
title: qsTr("Hello World")
signal frameReady(var frame)
Camera
{
id: camera
viewfinder.resolution: "320x240"
}
VideoOutput
{
id: videoOutput
source: camera
autoOrientation: true
visible: true
anchors.fill: parent
}
function grab(grab_width, grab_height)
{
if (!visible)
return
videoOutput.grabToImage(function(frame)
{
frameReady(frame) //Emit frameReady SIGNAL
}, Qt.size(grab_width, grab_height))
}
}
我终于成功了。显然 QCamera
在 Android 上有一些问题,所以我不得不使用 QML Camera
,并用 VideoOutput
显示它,并应用一个过滤器,通过它我可以得到我的图片。
此过滤器由两部分组成:一部分源自 QAbstractVideoFilter
, the other deriving from QVideoFilterRunnable
。
代码如下:
////////////////////////////////////////////////////////////////////
// main.cpp
////////////////////////////////////////////////////////////////////
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "myfilter.hpp"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<MyFilter>("example.myfilter", 1, 0, "MyFilter");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
////////////////////////////////////////////////////////////////////
// myfilter.hpp
////////////////////////////////////////////////////////////////////
#ifndef MYFILTER_H
#define MYFILTER_H
#include <QAbstractVideoFilter>
class MyFilterRunnable : public QVideoFilterRunnable {
public:
QVideoFrame run(QVideoFrame *input, const QVideoSurfaceFormat &surfaceFormat, RunFlags flags);
};
class MyFilter : public QAbstractVideoFilter
{
public:
QVideoFilterRunnable* createFilterRunnable();
};
#endif // MYFILTER_H
////////////////////////////////////////////////////////////////////
// myfilter.cpp
////////////////////////////////////////////////////////////////////
#include "myfilter.hpp"
#include <QOpenGLContext>
#include <QOpenGLFunctions>
QVideoFrame MyFilterRunnable::run(QVideoFrame *input, const QVideoSurfaceFormat &surfaceFormat, QVideoFilterRunnable::RunFlags flags)
{
QImage img(input->width(), input->height(), QImage::Format_RGBA8888);
bool success = false;
if (input->handleType() == QAbstractVideoBuffer::GLTextureHandle) {
GLuint textureId = input->handle().toUInt();
QOpenGLContext *ctx = QOpenGLContext::currentContext();
QOpenGLFunctions *f = ctx->functions();
GLuint fbo;
f->glGenFramebuffers(1, &fbo);
GLuint prevFbo;
f->glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint *) &prevFbo);
f->glBindFramebuffer(GL_FRAMEBUFFER, fbo);
f->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0);
f->glReadPixels(0, 0, input->width(), input->height(), GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
f->glBindFramebuffer(GL_FRAMEBUFFER, prevFbo);
success = true;
} // else handle other types
if( success ) {
// Process image
return QVideoFrame(img);
} else {
return *input; //Could not apply filter, return unmodified input
}
}
QVideoFilterRunnable *MyFilter::createFilterRunnable()
{
return new MyFilterRunnable;
}
////////////////////////////////////////////////////////////////////
// main.qml
////////////////////////////////////////////////////////////////////
import QtQuick 2.11
import QtQuick.Controls 2.2
import QtMultimedia 5.9
import example.myfilter 1.0
ApplicationWindow {
id: window
visible: true
width: 640
height: 480
Camera {
id: camera
}
MyFilter {
id: filter
}
VideoOutput {
source: camera
autoOrientation: true
filters: [ filter ]
anchors.fill: parent
}
}
(我的 run
实现改编自这里:http://code.qt.io/cgit/qt/qtmultimedia.git/tree/examples/multimedia/video/qmlvideofilter_opencl/rgbframehelper.h)