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

Multiple cameras [solved]



  • I am new at the QtMultimedia 5.x QML interface, but I tried this super simple QCamera demo below and it worked perfectly. It sees the camera and displays the viewfinder for me. This is the basis for an app I am developing.

    However my system has two cameras. I am wondering if there is an API for selecting which physical camera to use. I see the availableDevices() method on the C++ object but I have to admin I am not connecting this to any selection mechanism.

    I don't need a chooser, I can hard code an index into a list of available cameras for a demo....

    Thanks in advance!!
    @
    import QtQuick 2.0
    import QtMultimedia 5.0

    Item {
    width: 640
    height: 360

    Camera {
        id: camera
    
        imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash
    
        exposure {
            exposureCompensation: -1.0
            exposureMode: Camera.ExposurePortrait
        }
    
        flash.mode: Camera.FlashRedEyeReduction
    
        imageCapture {
            onImageCaptured: {
                photoPreview.source = preview  // Show the preview in an Image
            }
        }
    }
    
    VideoOutput {
        source: camera
        anchors.fill: parent
        focus : visible // to receive focus and capture key events when visible
    }
    
    Image {
        id: photoPreview
    }
    

    }
    @


  • Lifetime Qt Champion

    Hi,

    availableDevices returns all the camera it can find so you can make a simple loop to create a camera + view finder for each entries returned.

    Something like this:
    @foreach (const QByteArray &device, QCamera::availableDevices()) {
    QCamera *camera = new QCamera(device, this);
    QCameraViewfinder *viewFinder = new QCameraViewfinder;
    camera->setViewfinder(viewFinder);
    // rest of logic to setup widgets or anything else
    }
    @

    You can retrieve the camera name with QCamera::deviceDescription with these two function you can build your selection mechanism.

    Hope it helps

    PS: please enclose your code with coding tags to make it readable



  • Thanks, I understood how to do it on the C++ side.

    So when I find the camera I am interested in, all I do is export it:

    @QCamera *selectedCamera = new QCamera(selectedDevice, this);
    QQuickView view;
    view.rootContext()->setContextProperty("selectedCamera", selectedCamera);@

    and set the QML Camera's mediaObject property to that exported object?

    @Camera {
    id: myCamera
    mediaObject: selectedCamera
    }@

    Or do I share the &QByteArray returned from availableDevices() as a contextProperty? Either way, if it's that easy I'll be very happy.


  • Lifetime Qt Champion

    Sorry, QML is currently outside my competence... You'll have to test or maybe ask on the mailing list (there you'll find Qt developers/maintainers this forum is more user oriented)



  • Yes this works exactly as expected. put the C++ QCamera object into the root context by name, then you can use that name as the value in the mediaobject: field for the QML Camera and the correct camera will be used. If you do not do this and you have multiple cameras, then the camera at array index 0 of the availableDevices() returned list will be used by default.


  • Lifetime Qt Champion

    Great !

    I also saw something about it in the "Qt Contributor Summit page":http://qt-project.org/wiki/Qt_Multimedia about QtMultimedia.

    You might be interested



  • I'm trying to set the "mediaObject" property of my camera object in qml i am running Windows 7 64bit Qt 5.2 and wondering if this is a Windows problem? Here's the error i get : Cannot assign to non-existent property "mediaObject"



  • Hi,

    It sounds like maybe something changed in 5.2 which I have not used yet. The code as posted worked in 5.1.1

    David



  • The relevant bug seems to be this: https://bugreports.qt-project.org/browse/QTBUG-23770




Log in to reply