Qt的QML相机与C ++的QImage在Android [英] Qt QML Camera to C++ QImage on Android

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

问题描述

我有一个Qt5.4为基础的方案具有一定的图像处理。我用式Qcamera 我的 videoSurface (来源于 QAbstractVideoSurface )得到VideoFrames。它的工作原理好于Windows。

但现在我需要我的应用程序的Andr​​oid版本。我发现式Qcamera 不要在Android上运行。但是,我看到QML相机比如运行在Android上没有任何问题。

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

 无效myVideoOutput ::的SetSource(QObject的*源)
{
    qDebug()&其中;&其中; Q_FUNC_INFO<<资源;

    如果(来源== m_source.data())
        返回;
    m_source =来源;
    如果(m_source){
        常量QMetaObject *元对象= m_source.data() - >元对象();

        QStringList中的属性;
        对于(INT I = metaObject-> propertyOffset(); I<元对象> propertyCount(); ++ I)
            物业LT;< :: QString的fromLatin1(metaObject->财产(一)。名称());
        qDebug()&其中;&其中;性能;

    }
    .....
    发射sourceChanged();
}
 

这code授予访问权限属性。但我不能访问videoSurface这种方式(使用式Qcamera 我能做到这一点)。我不知道如何QML相机的作品?它是基于式Qcamera ?我看式Qcamera *在 QDeclarativeCamera ...

m_camera

所以,我有2个问题:

  1. 是否有可能使用QML相机的图像后处理在C ++中?工作的例子是非常有价值的。
  2. 你知道其他的方式来捕获的Qt从Android摄像头的视频?
解决方案

1)是的,这是可能的。从来就过来两种方法可以做到这一点。

使用QAbstractVideoFilter旁边有QVideoFilterRunnable类(QT 5.5只!),这是普通的大。它们进行专门为此情况开发的,是pretty的易于使用。

有使用它在网络上的几个很好的例子:

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

<一个href="http://$c$c.qt.io/cgit/qt/qtmultimedia.git/tree/examples/multimedia/video/qmlvideofilter_opencl" rel="nofollow">http://$c$c.qt.io/cgit/qt/qtmultimedia.git/tree/examples/multimedia/video/qmlvideofilter_opencl

这样做的缺点方法,就像有人说的这里,是在Android设备QVideoFrame指针犯规具有原始像素数据,相反,它有一个OpenGL纹理这就需要回读(第二个例子我张贴有一种变通方法解决此),从而使这种方法用于实时目的,恕我直言,不是真的好。

我结束了使用来解决这个问题是 QVideoProbe 类。

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

 照相机{
    ID:摄像头

    对象名:qrCameraQML
}
 

然后你从C ++侧获得这种情况下,是这样的:

 的QObject * qmlCamera = engine.rootObjects()在(0).findChild&LT; QObject的*&GT;(qrCameraQML);
 

在QML相机实例实际上只有通过C ++,可被转换成式Qcamera *访问的的QVariant元素:

  camera_ = qvariant_cast&LT;式Qcamera *&GT;(qmlCamera-&GT;财产(的mediaObject));
 

然后,所有你需要做的是将探头连接到一个插槽,实际上将处理QVideoFrames,然后设置探头为式Qcamera * previouslly投的来源:

 连接(安培; probe_,SIGNAL(videoFrameProbed(QVideoFrame)),这一点,SLOT(handleFrame(QVideoFrame)));

probe_.setSource(camera_);
 

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

 式Qcamera * camera_;

QVideoProbe probe_;
 

这对我的经验方法比使用Qt视频过滤器类快了很多(针对Android平台),但它有,你基本上只能读取QML视频输出的缺点,并AFAIK,你将无法发送后处理videoframes回QML。

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

2)不使用Qt AFAIK,可能与OpenCV的,或其他一些库。

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相机与C ++的QImage在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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