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

Stop camera from auto starting



  • Hello,
    In my project, I offer a view from one of the webcam.
    However, I do not want the camera to start as soon as my app starts. I want the user to be able to select the camera from a list if he wants to (which is purely optional in the project).
    However, regardless of what I have tried, the Camera seems to always "open" the capture device and "start" streaming immediately. This is not what I want...
    What am I doing wrong?
    Here is my code:

           ComboBox {  // for selecting one of many cameras
                      model: QtMultimedia.availableCameras
                      textRole: "displayName";
                      currentIndex: -1
                      onCurrentIndexChanged: if (currentIndex!=-1) camera.deviceId= QtMultimedia.availableCameras[currentIndex].deviceId
            }
            Camera { id: camera; deviceId: ""; } // tried also: cameraState: UnloadedState
            VideoOutput {
                width: window.width; height: parent.height-y;
                source: camera
            }
    

    Thanks


  • Moderators

    @Cyrille-de-Brebisson
    you could try
    Camera has a start() and stop() function. So you could try

    Camera { 
        id: camera; deviceId: ""; 
        Component.onCompleted: camera.stop()
    }
    

    should work, but it's untested.


  • Moderators

    @Cyrille-de-Brebisson
    docs for Camera's deviceId property say

    If no value is provided or if set to an empty string, the system's default camera will be used.

    Try something like the following (untested though):

    Camera {
         id: camera
        deviceId: ""
        Component.onCompleted: camera.stop()
    } 
    

    or also:

    VideoOutput {
         width: window.width; height: parent.height-y;
         source: camera.deviceId.length > 0 ? camera : undefined
    }
    


  • Hello,

    I have tried:

    Camera {
         id: camera
        deviceId: "" // also tried deviceId: "nocamera"
        Component.onCompleted: camera.stop()
    }
    

    The result is that the camera DOES start when the app starts, and then imediately stops.
    I can see the "on light" on my camera turning on then off, the view widget does keep the last frame on, but more problematic for me, the OS does warn the user that the app wants access to the camera, which is why I would like to avoid.
    Maybe the solution is to dynamically create the camera at runtime and assign it to the VideoOutput? But I have no clue how to do that.

    Cyrille


  • Moderators

    @Cyrille-de-Brebisson
    The warning that the App is using the camera should come wether or not you actually start the camera. At least that is the way on mobile

    However you can lock into the QML component Loader, to dynamically load QML files on demand.


Log in to reply