在 Qt Quick 的 Linux 桌面上使用相机

Using Camera on Linux Desktop in Qt Quick

我在 Qt Quick 中有一个简单的项目,我需要在其中处理来自相机的输出。该项目应在 Android、Windows 和 Linux 上 运行。到目前为止,我在 Android 上成功连接到相机,但在 Linux.

上连接失败

我的设置如下:

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

main.qml

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtMultimedia 5.9

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("EyeGaze")

    SwipeView {
        id: swipeView
        anchors.fill: parent
        currentIndex: tabBar.currentIndex

        CameraViewForm {}

        AboutForm {}
    }

    footer: TabBar {
        id: tabBar
        currentIndex: swipeView.currentIndex

        TabButton {
            text: qsTr("Main")
        }
        TabButton {
            text: qsTr("About")
        }
    }
}

CameraViewForm.qml

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtMultimedia 5.9

Page {
    width: 600
    height: 400

    header: Label {
        text: qsTr("Camera View")
        horizontalAlignment: Text.AlignHCenter
        font.pixelSize: Qt.application.font.pixelSize * 2
        padding: 10
    }

    Camera {
           id: camera
           position: Camera.FrontFace
       }

       VideoOutput {
           source: camera
           anchors.fill: parent
           focus: visible // to receive focus and capture key events when visible
       }
}

我得到 CameraBin error: "Could not read from resource." 和相机视图中的空白屏幕。

我尝试通过 C++ 代码检查摄像头可用性(使用 QCameraInfo::availableCameras()),我发现我的笔记本电脑确实在 /dev/video0 处有一个网络摄像头,该程序似乎可以访问该摄像头。

我是不是访问了摄像头?我应该从 C++ 代码而不是 QML 来做吗?

实际上,您的代码应该有效(至少在我这边有效)。这里有一些提示。

首先,检查是否有任何东西已经在使用您的网络摄像头:

lsof /dev/video0

fuser /dev/video0

如果没有输出——很好,继续。否则,请检查您的网络摄像头发生了什么以及实际使用它的人。


检查网络摄像头的权限是什么:

ls -la /dev/video0

可能是这样的:

crw-rw----+ 1 root video 81, 0 тра 10 13:38 /dev/video0

检查你的用户是否在video组,否则按

添加
adduser YOUR_USER video

希望对您有所帮助!