简体   繁体   English

启用/禁用摄像头(QML 摄像头)

[英]Enable / disable camera (QML Camera)

I have qml camera widget in camera.qml.我在camera.qml 中有qml 相机小部件。 the qml component is loaded from a Qt widget "WidgetCamera" that is placed on a stack widget in the background. qml 组件是从放置在后台堆栈小部件上的 Qt 小部件“WidgetCamera”加载的。 The widget starts the camera device already at creation.小部件在创建时就已启动相机设备。

How to make the camera start only when the widget is shown in the foreground.如何使相机仅在小部件显示在前景中时启动。 And vice versa, how release the camera when the widget goes to the background?反之亦然,当小部件进入后台时如何释放相机?

camera.qml相机.qml

Item {
    width: 640
    height: 360

    Camera {
        id: camera
    }

    VideoOutput {
        source: camera
        anchors.fill: parent
    }
}

widgetCamera.h小工具相机.h

class WidgetCamera : public QWidget
{
    Q_OBJECT
    public:
    WidgetCamera() {
       QQuickWidget *qw= new QQuickWidget;
       qw->setSource(QUrl("qrc:///camera.qml"));
       // ...
    }
}

mainwindow.h主窗口.h

class MainWindow : QMainWindow
{
    Q_OBJECT
    public:
    MainWindow() {
    QStackedWidget *sw = new QStackedWidget;
    sw->addWidget(new QWidget());
    sw->addWidget(new WidgetCamera());

    // ...

    }
}

QML camera type has start() , stop() methods accessible directly inside QML. QML 相机类型具有可直接在 QML 内部访问的start()stop()方法。 But to be able to turn the camera on/off at will from c++ side, you should first introduce it as a member in your MainWindow class, eg like this:但是为了能够从 C++ 端随意打开/关闭相机,您应该首先将其作为 MainWindow 类中的成员引入,例如像这样:

#include "widgetCamera.h"

class MainWindow : QMainWindow
{
    Q_OBJECT
    private:
        WidgetCamera* _cameraWidget;

public:
    MainWindow() {
        QStackedWidget *sw = new QStackedWidget;
        sw->addWidget(new QWidget());
        _cameraWidget = new WidgetCamera();
        sw->addWidget(_cameraWidget);
        // PS: Make sure you free your instances correctly, too

        // ...

    }

}

Now, in your WidgetCamera class, you should also introduce a member variable to access the actual QML widget faster, similar to the above.现在,在您的 WidgetCamera 类中,您还应该引入一个成员变量来更快地访问实际的 QML 小部件,类似于上述。 Let's stick with the "qw" you already gave it.让我们坚持你已经给它的“qw”。

Then, make sure you give objectNames to all QML children that you want to access (in this case, we want the camera), like this:然后,确保将 objectNames 提供给要访问的所有 QML 子级(在本例中,我们需要相机),如下所示:

Item {
    width: 640
    height: 360

    Camera {
        id: camera
        objectName: "theCamera"
    }

    VideoOutput {
        source: camera
        anchors.fill: parent
    }
}

Once you have that, you need a function to enable/disable the capturing of the camera, which could be done like this:一旦你有了它,你需要一个功能来启用/禁用相机的捕获,这可以像这样完成:

void WidgetCamera::disableCapture() {
    QObject* qmlCamera = qw->findChild<QObject*>("theCamera");
    QCamera* camera = qvariant_cast<QCamera*>(qmlCamera->property("mediaObject"));
    camera->stop();
}

Now, obviously, a lot of this could be improved and optimized (like making the actual c++ QCamera a member of WidgetCamera, etc.), but this should get you started.现在,很明显,其中很多都可以改进和优化(例如使实际的 c++ QCamera 成为 WidgetCamera 的成员等),但这应该可以帮助您入门。

As to when you want to call that function to enable/disable the camera, that is completely up to you.至于何时要调用该函数来启用/禁用相机,这完全取决于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM