Android QVideoFrame::map()崩溃
我正在尝试使用QCamera从相机获取图像。 我认为我应该从Android QVideoFrame::map()崩溃,android,c++,qt,opengl-es,android-ndk,Android,C++,Qt,Opengl Es,Android Ndk,我正在尝试使用QCamera从相机获取图像。 我认为我应该从QAbstractVideoSurface派生并实现present(),其中表示相机捕获的当前图像的QVideoFrame作为参数发送 由于我需要做一些处理,我尝试map()我的帧,用位()获取数据,做我必须做的任何事情,然后unmap()它。但是,我在map()上发生了崩溃 以下是我得到的错误: W libTest.so: (null):0 ((null)): Unsupported viewfinder pixel format D
QAbstractVideoSurface
派生并实现present()
,其中表示相机捕获的当前图像的QVideoFrame
作为参数发送
由于我需要做一些处理,我尝试map()
我的帧,用位()获取数据,做我必须做的任何事情,然后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()
}
///////////////////////////////////////////////
//main.cpp
///////////////////////////////////////////////
#包括
#包括
#包括
#包括“camera_engine.h”
int main(int argc,char*argv[])
{
QCoreApplication::setAttribute(Qt::AA_enableHighdDiscaling);
QGUI应用程序应用程序(argc、argv);
qqmlaplicationengine;
照相机引擎;
engine.rootContext()->setContextProperty(“cameraEngine”和&camEngine);
engine.load(QUrl(QStringLiteral(“qrc:/main.qml”));
if(engine.rootObjects().isEmpty())
返回-1;
返回app.exec();
}
///////////////////////////////////////////////
//照相机引擎
///////////////////////////////////////////////
#ifndef uU摄像机U引擎U H__
#定义摄像头引擎__
#包括
#包括“image_reader.h”
类CameraEngine:公共QObject{
Q_对象
公众:
显式摄影机引擎(QCamera::Position pos=QCamera::BackFace);
~CameraEngine();
公众时段:
void start();
私人:
QCamera-mCamera;
图像阅读器;
};
#endif/\摄像机\发动机\ H__
///////////////////////////////////////////////
//照相机引擎.cpp
///////////////////////////////////////////////
#包括“camera_engine.h”
CameraEngine::CameraEngine(QCamera::Position pos):mCamera(pos)
{
mCamera.setViewfinder(&mImageReader);
QCameraViewfinderSettings视图查找设置;
viewFinderSettings.setResolution(640480);
viewFinderSettings.setMinimumFrameRate(30);
viewFinderSettings.setMaximumFrameRate(30);
setPixelFormat(QVideoFrame::Format_RGB24);
setViewfinderSettings(viewFinderSettings);
}
CameraEngine::~CameraEngine()
{
if(mCamera.state()==QCamera::ActiveState){
mCamera.stop();
}
}
void CameraEngine::start()
{
mCamera.start();
}
///////////////////////////////////////////////
//image_reader.h
///////////////////////////////////////////////
#ifndef摄像机、图像、读卡器
#定义摄像头\图像\读卡器\u H
#包括
类ImageReader:公共QAbstractVideoSurface{
Q_对象
公众:
ImageReader()=默认值;
~ImageReader()=默认值;
虚拟现实(const-QVideoFrame和frame);
虚拟QList支持的像素格式(QAbstractVideoBuffer::HandleType类型)常量;
};
#endif//CAMERA\u IMAGE\u READER\u H
///////////////////////////////////////////////
//image_reader.cpp
///////////////////////////////////////////////
#包括“image_reader.h”
#包括
布尔图像阅读器::当前(常量QVideoFrame和frame)
{
QVideoFrame currentFrame=帧;
currentFrame.map(QAbstractVideoBuffer::ReadOnly);//此处崩溃
//做点什么
currentFrame.unmap();
返回true;
}
QList ImageReader::supportedPixelFormats(QAbstractVideoBuffer::HandleType类型)常量
{
Q_未使用(类型)
返回QList()
…除非有人有更好的建议…
<>我在QML中从代码>相机> /COD>对象中获取图像的简单方法是调用<代码> GruttoIsIs/COD>并将图像传递给C++来处理它!
此方法不强制您处理活动摄影机的所有帧,您只能在准备好或需要时进行抓取
请记住,尤其是在安卓系统中,视频输出
对象需要可见才能抓取任何东西,例如,在安卓系统上,您不能在屏幕关闭的情况下抓取图像。但是,您不需要填充屏幕,您只需为视频输出
提供一些屏幕区域,并使其可见并位于顶部即可。
另外,请注意,减小VideoOutput
的大小不会减小您可以捕获的最大大小,例如,您可以使用较小的VideoOutput
捕获1280x720图像
此外,在调整视频输出的大小时,必须保留纵横比,以保持图像的纵横比
代码:
你可以看到完整的源代码
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();
}
我终于成功了。显然,QCamera
在Android上有一些问题,所以我不得不使用一个QML摄像头,用一个视频输出来显示它,并应用一个过滤器,通过它我可以得到我的图像
此过滤器由两部分组成:一部分来自,另一部分来自
代码如下:
////////////////////////////////////////////////////////////////////
// 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
}
}
////////////////////////////////////////////////////////////////////
//main.cpp
////////////////////////////////////////////////////////////////////
#包括
#包括
#包括“myfilter.hpp”
int main(int argc,char*argv[])
{
QCoreApplication::setAttribute(Qt::AA_enableHighdDiscaling);
QGUI应用程序应用程序(argc、argv);
qqmlaplicationengine;
qmlRegisterType(“example.myfilter”,1,0,“myfilter”);
engine.load(QUrl(QStringLiteral(“qrc:/main.qml”));
if(engine.rootObjects().isEmpty())
返回-1;
返回app.exec();
}
////////////////////////////////////////////////////////////////////
//myfilter.hpp
////////////////////////////////////////////////////////////////////
#ifndef MYFILTER\u H
#定义MYFILTER_H
#包括
类MyFilterRunnable:公共QVideoFilterRunnable{
公众:
QVideoFrame运行(QVideoFrame*输入,常量QVideoSurfaceFormat和surfaceFormat,运行标志);
};
类MyFilter:公共QAbstractVideoFilter
{
公众:
QVideoFilterRunnable*createFilterRunnable();
};
#endif//MYFILTER\u H
//////////////////////
#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
}
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))
}
}
////////////////////////////////////////////////////////////////////
// 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
}
}