Qt QML Camera到C ++ QImage在Android上 [英] Qt QML Camera to C++ QImage on Android

查看:1566
本文介绍了Qt QML Camera到C ++ QImage在Android上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Qt5.4的程序与一些图像处理。我使用与我的 videoSurface (派生自 QAbstractVideoSurface )获取VideoFrames。它在Windows上运行良好。



但现在我需要我的应用程序的Android版本。我发现 QCamera 在Android上不工作。但我看到QML Camera示例在Android上运行没有问题。



所以我决定在QML中重写我的应用程序。
主要问题:我无法在C ++中访问QML Camera表面。

  void myVideoOutput :: setSource * source)
{
qDebug()<< Q_FUNC_INFO<<资源;

if(source == m_source.data())
return;
m_source = source;
if(m_source){
const QMetaObject * metaObject = m_source.data() - > metaObject();

QStringList属性;
for(int i = metaObject-> propertyOffset(); i properties< QString :: fromLatin1(metaObject-> property(i).name());
qDebug()<<属性;

}
.....
emit sourceChanged();
}

此代码允许访问属性。但我不能访问videoSurface这种方式(使用 QCamera 我可以做到)。我不知道QML相机如何工作?是基于 QCamera 吗?我看到 QCamera * m_camera QDeclarativeCamera ...



因此,我有2个问题:


  1. 是否可以在C ++中使用QML Camera作为后处理图像?



  2. 您知道其他在Qt中从Android相机捕获视频的方法吗? class =h2_lin>解决方案

1)是的,这是可能的。



使用QAbstractVideoFilter以及QVideoFilterRunnable类(QT 5.5 only!),这是非常好的。



网络上有几个很好的例子:



https:// blog .qt.io / blog / 2015/03/20 / introduction-video-filters-in-qt-multimedia /



http://code.qt.io/cgit/qt/qtmultimedia.git/tree/examples/多媒体/视频/ qmlvideofilter_opencl



这种方法的缺点是,像这里,是在Android设备上的QVideoFrame指针doesnt有原始的像素数据,相反,它有一个OpenGL纹理需要被回读(第二个例子我发布了一个解决方法),因此使这种方法对于实时目的IMHO是不实际的。



我最终使用解决这个问题是 QVideoProbe 类别。



首先,您必须命名QML相机的实例:

 相机{
id:camera

objectName:qrCameraQML
}

然后你从C ++端得到这个实例,如下:

  QObject * qmlCamera = engine.rootObjects()。at(0).findChild< QObject *>(qrCameraQML); 

QML相机实例实际上有一个QVariant元素只能通过C ++访问,可以转换为QCamera * :

  camera_ = qvariant_cast< QCamera *>(qmlCamera-> property(mediaObject)) 

然后你需要做的就是将探针连接到一个实际处理QVideoFrames的插槽,然后将探测器的源设置为QCamera * previousfall:

  connect(& probe_,SIGNAL(videoFrameProbed(QVideoFrame) ),this,SLOT(handleFrame(QVideoFrame))); 

probe_.setSource(camera_);

在我的例子中camera_和probe_是简单的:

  QCamera * camera_; 

QVideoProbe probe_;

这种方法对我的经验是比使用qt视频过滤器类,但它的缺点是你基本上只读取qml的视频输出,AFAIK你不能将后处理的视频帧发送回qml。



如果你真的需要发送已处理的图像回qml我建议你尝试第一种方法,看看会发生什么。



2)不与Qt AFAIK,也许与OpenCv,一些其他lib。


I have a Qt5.4-based program with some image processing. I use QCamera with my videoSurface (derived from QAbstractVideoSurface) to get VideoFrames. It works good on Windows.

But now I need Android version of my app. I found out that QCamera do not work on Android. But I see that QML Camera example run on Android with no problems.

So I decided to rewrite my application in QML. The main problem: I can't access QML Camera surface in C++.

void myVideoOutput::setSource(QObject *source)
{
    qDebug() << Q_FUNC_INFO << source;

    if (source == m_source.data())
        return;
    m_source = source;
    if (m_source) {
        const QMetaObject *metaObject = m_source.data()->metaObject();

        QStringList properties;
        for(int i = metaObject->propertyOffset(); i < metaObject >propertyCount(); ++i)
            properties << QString::fromLatin1(metaObject->property(i).name());
        qDebug() << properties;

    }
    .....
    emit sourceChanged();
}

This code give access to properties. But I can't access videoSurface this way (using QCamera I could do it). I wonder how QML Camera works? Is it based on QCamera? I see QCamera *m_camera in QDeclarativeCamera...

So I have 2 questions:

  1. Is it possible to use QML Camera for postprocess images in C++? Working example would be very valuable.
  2. Do you know other ways to capture video from Android camera in Qt?

解决方案

1) Yes, it is possible. I´ve come around two ways to do it.

Using QAbstractVideoFilter alongside with QVideoFilterRunnable classes (QT 5.5 only! ) which are plain great. They were developed specifically for this case scenario, and are pretty easy to use.

There are a few good examples on the web using it:

https://blog.qt.io/blog/2015/03/20/introducing-video-filters-in-qt-multimedia/

http://code.qt.io/cgit/qt/qtmultimedia.git/tree/examples/multimedia/video/qmlvideofilter_opencl

The downside to this approach is, like was said here , is that on Android devices the QVideoFrame pointer doesnt has raw pixel data, instead, it has an OpenGL Texture which need to be read back (the second example I posted has a workaround solving this), thus making this approach not realy good for real time purposes IMHO.

What I ended up using to solve this problem was the QVideoProbe class.

First you have to name the instance of your QML camera:

    Camera {
    id: camera

    objectName: "qrCameraQML"
}

Then you get this instance from C++ side, something like:

QObject *qmlCamera = engine.rootObjects().at(0).findChild<QObject*>("qrCameraQML");

The QML camera instance actually has a QVariant element accessible only via C++ that can be cast into a QCamera* :

camera_ = qvariant_cast<QCamera*>(qmlCamera->property("mediaObject"));

Then all you have to do is to connect the probe to a slot that will actually handle the QVideoFrames and then set the source of the probe as the QCamera* previouslly cast:

    connect(&probe_,SIGNAL(videoFrameProbed(QVideoFrame)),this,SLOT(handleFrame(QVideoFrame)));

probe_.setSource(camera_);

On my example camera_ and probe_ are simply:

    QCamera *camera_;

QVideoProbe probe_;

This approach on my experience was a lot faster(for android platforms) than using the qt video filter classes, but it has the disadvantage that you basically only read the video output from qml, and AFAIK you wont be able to send postprocessed videoframes back to qml.

If you really need to send the processed images back to qml i'd advise you to try the first approach and see what happens.

2)Not with Qt AFAIK, maybe with OpenCv, or some other lib.

这篇关于Qt QML Camera到C ++ QImage在Android上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆