Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Multiple cameras QML



  • Hello everyone,

    I have a problem with setting up 2 camera windows in QML.

    The error I get is: [0x800705aa] Failed to start (Insufficient system resources exist to complete the requested service.)
    When I start the program only the last camera window is showing the camera capture. If I start 2 instances of the application then each of the application shows one camera but the image on one is frozen and on the other one it is working OK.
    alt text

    If I start the application once with only camera 1 and second time only with camera 2 then both cameras are working simultaneously:
    alt text

    The code for camera:

    Item{
            id:cameraWindow1
            anchors.left: parent.left
            anchors.right: swipeView.left
            height: parent.height
            signal closed
    
            Rectangle{
                anchors.fill: parent
                color: "red"
            }
    
            Camera{
                id:camera1
            }
        }
    
        VideoOutput{
            id:vo1
            z:99
            source: camera1
            anchors.fill: cameraWindow1
            //focus: visible
            //orientation: 270
        }
    
    Item{
            id:cameraWindow2
            anchors.left: swipeView.right
            anchors.right: parent.right
            height: parent.height
            signal closed
    
            Rectangle{
                anchors.fill: parent
                color: "blue"
            }
    
            Camera{
                id:camera2
            }
        }
    
        VideoOutput{
            id:vo2
            z:99
            source: camera2
            anchors.fill: cameraWindow2
            //focus: visible
            //orientation: 270
        }
    

    Code to specify the camera source:

    Component.onCompleted: {
            var myL = QtMultimedia.availableCameras;
            console.log("Loaded", myL.length);
            camera1.deviceId = myL[0].deviceId;
            camera2.deviceId = myL[1].deviceId;
        }
    

    The console returns "qml: Loaded 2" for myL.length

    Does anyone have more experience with creating new instances of the same object in QML? Especially cameras.



  • Hello community,

    After a few days, I think I solved the problem with the cameras.
    I made myself a QML file named "CamCam.qml" with the following code:

    import QtQuick 2.14
    import QtMultimedia 5.12
    
    Item{
        id:cameraWindow
        anchors.fill: parent
        signal closed
        property alias theID: camera.deviceId
        property alias backgroundColor: background.color
        property alias orientation: vo.orientation
    
        Rectangle{
            id: background
            anchors.fill: parent
            color: "blue"
        }
    
        Camera{
            id:camera
        }
    
        VideoOutput{
            id: vo
            z:99
            source: camera
            anchors.fill: parent
        }
    
    }
    

    And then I use it in the "main.qml":

    Item{
            id: cameraLeft
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.right: swipeView.left
            anchors.bottom: parent.bottom
            
            CamCam{
                theID: QtMultimedia.availableCameras[0].deviceId
                backgroundColor: "red"
            }
        }
        
    Item{
            id: cameraRight
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.left: swipeView.right
            anchors.bottom: parent.bottom
            
            CamCam{
                theID: QtMultimedia.availableCameras[1].deviceId
                backgroundColor: "blue"
            }
        }
    

    It is important to set the deviceId as soon as possible. As I did here "theID: QtMultimedia.availableCameras[x].deviceId"

    Now it works perfectly:
    alt text


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since you have a resource error, please give more details about the machine/device you are using for development.

    You should as well add the Qt version you are using as well as platform your are working and targeting.



  • Thank you for the warm welcome,

    Oh yes, sorry about that.
    The computer is this:
    alt text

    Qt:
    Qt Creator 4.11.1
    Based on Qt 5.14.1 (MSVC 2017, 32 bit)
    Built on Feb 5 2020 10:36:21
    From revision b2ddeacfb5

    My target is windows application:
    alt text

    Thank you!


  • Lifetime Qt Champion

    What are these two cameras ?



  • They are USB cameras, not web cameras.
    5a9e5980-6569-41b6-ba47-bfa6d6871839-image.png

    https://i.imgur.com/x71om5B.jpg



  • Hello community,

    After a few days, I think I solved the problem with the cameras.
    I made myself a QML file named "CamCam.qml" with the following code:

    import QtQuick 2.14
    import QtMultimedia 5.12
    
    Item{
        id:cameraWindow
        anchors.fill: parent
        signal closed
        property alias theID: camera.deviceId
        property alias backgroundColor: background.color
        property alias orientation: vo.orientation
    
        Rectangle{
            id: background
            anchors.fill: parent
            color: "blue"
        }
    
        Camera{
            id:camera
        }
    
        VideoOutput{
            id: vo
            z:99
            source: camera
            anchors.fill: parent
        }
    
    }
    

    And then I use it in the "main.qml":

    Item{
            id: cameraLeft
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.right: swipeView.left
            anchors.bottom: parent.bottom
            
            CamCam{
                theID: QtMultimedia.availableCameras[0].deviceId
                backgroundColor: "red"
            }
        }
        
    Item{
            id: cameraRight
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.left: swipeView.right
            anchors.bottom: parent.bottom
            
            CamCam{
                theID: QtMultimedia.availableCameras[1].deviceId
                backgroundColor: "blue"
            }
        }
    

    It is important to set the deviceId as soon as possible. As I did here "theID: QtMultimedia.availableCameras[x].deviceId"

    Now it works perfectly:
    alt text



  • Hi NiLe, I am very new to qml ,could you please post your code?


  • Qt Champions 2019

    @lxy12345 Isn't the code just above in the last post from @NiLe ?



  • @jsulm When I tried the code, the error occured.
    Here is my code:
    main.qml
    import QtQuick 2.12
    import QtMultimedia 5.12
    import QtQuick.Window 2.12

    Window {
    visible: true
    width: 1200
    height: 500
    title: qsTr("Hello World")
    Item{
    id: cameraLeft
    anchors.left: parent.left
    anchors.top: parent.top
    anchors.right: swipeView.left
    anchors.bottom: parent.bottom

            CamCam{
                theID: QtMultimedia.availableCameras[0].deviceId
                backgroundColor: "red"
            }
        }
    
    Item{
            id: cameraRight
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.left: swipeView.right
            anchors.bottom: parent.bottom
    
            CamCam{
                theID: QtMultimedia.availableCameras[1].deviceId
                backgroundColor: "blue"
            }
        }
    

    }

    CamCam.qml:
    import QtQuick 2.12
    import QtMultimedia 5.12

    Item{
    id:cameraWindow
    anchors.fill: parent
    signal closed
    property alias theID: camera.deviceId
    property alias backgroundColor: background.color
    property alias orientation: vo.orientation

    Rectangle{
        id: background
        anchors.fill: parent
        color: "blue"
    }
    
    Camera{
        id:camera
    }
    
    VideoOutput{
        id: vo
        z:99
        source: camera
        anchors.fill: parent
    }
    

    }

    The errors:
    qrc:/main.qml:15: ReferenceError: swipeView is not defined
    qrc:/main.qml:32: TypeError: Cannot read property 'deviceId' of undefined
    qrc:/main.qml:28: ReferenceError: swipeView is not defined
    libv4l2: error setting pixformat: Device or resource busy
    CameraBin error: "Device '/dev/video0' is busy"

    The environment is ubuntu16.04(virtual machine), qt5.12.9. The camera is USB camera


  • Qt Champions 2019

    @lxy12345 said in Multiple cameras QML:

    anchors.right: swipeView.left

    Since you don't have swipeView replace it with parent.


Log in to reply