Android上的Qt QML相机到C++ QImage [英] Qt QML Camera to C++ QImage on Android

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

问题描述

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

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.

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

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.

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

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();
}

此代码可以访问属性.但是我无法通过这种方式访问​​ videoSurface(使用 QCamera 我可以做到).我想知道 QML 相机是如何工作的?是否基于QCamera?我在 QDeclarativeCamera...

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...

所以我有两个问题:

  1. 是否可以将 QML 相机用于 C++ 中的后期处理图像?工作示例将非常有价值.
  2. 您知道在 Qt 中从 Android 相机捕获视频的其他方法吗?

推荐答案

1) 是的,这是可能的.我有两种方法可以做到这一点.

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

将 QAbstractVideoFilter 与 QVideoFilterRunnable 类(仅限 QT 5.5!)一起使用,这非常棒.它们专为这种情况而开发,非常易于使用.

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

这种方法的缺点是,就像在这里所说的那样,是在 Android 上设备 QVideoFrame 指针没有原始像素数据,相反,它有一个需要回读的 OpenGL 纹理(我发布的第二个例子有一个解决这个问题的解决方法),从而使这种方法对于实时目的来说不是很好恕我直言.

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.

我最终用来解决这个问题的是 QVideoProbe 类.

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

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

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

    Camera {
    id: camera

    objectName: "qrCameraQML"
}

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

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

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

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

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"));

然后您所要做的就是将探针连接到实际处理 QVideoFrames 的插槽,然后将探针的源设置为之前投射的 QCamera*:

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_);

在我的示例中,camera_ 和 probe_ 很简单:

On my example camera_ and probe_ are simply:

    QCamera *camera_;

QVideoProbe probe_;

根据我的经验,这种方法比使用 qt 视频过滤器类要快得多(对于 android 平台),但它的缺点是您基本上只能从 qml 读取视频输出,而 AFAIK 您将无法发送后处理视频帧返回到 qml.

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.

如果您确实需要将处理后的图像发送回 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) 不适用于 Qt AFAIK,可能适用于 OpenCv 或其他一些库.

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

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

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