[Interest] Select front/rear camera on Android

Lopes Yoann Yoann.Lopes at digia.com
Tue Jan 7 14:19:16 CET 2014


On Jan 7, 2014, at 12:28 PM, Cornelius Hald wrote:

The only workaround is to create a custom QML item in C++...

That's fine with me. However I'm not really sure how to do that as it
looks like QDeclarativeCamera has only private headers. Could you give
me a hint please.

I meant to basically rewrite QDeclarativeCamera adding the missing feature...

But forget about that, there's actually a much simpler workaround using the Camera's mediaObject property.
The property is declared with "SCRIPTABLE false" so it's not accessible from QML, but you can access it from C++.

Create a CameraSelector QML item in C++, something like this:

class CameraSelector : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QObject* cameraObject READ ... WRITE setCameraObject ...)
    Q_PROPERTY(int selectedCameraDevice READ ... WRITE setSelectedCameraDevice ...)

    void setCameraObject(QObject *cam)
    {
        // get the QCamera from the declarative camera's mediaObject property.
        m_camera = qvariant_cast<QCamera*>(cam->property("mediaObject"));

        // get the video device selector control
        QMediaService *service = m_camera->service();
        m_deviceSelector = qobject_cast<QVideoDeviceSelectorControl*>(service->requestControl(QVideoDeviceSelectorControl_iid));
    }

    void setSelectedCameraDevice(int cameraId)
    {
        // A camera might already be started, make sure it's unloaded
        m_camera->unload();

        m_deviceSelector->setSelectedDevice(cameraId);
    }

    QCamera *m_camera;
    QVideoDeviceSelectorControl *m_deviceSelector;
}

Then in QML:

Camera {
    id: camera
}

CameraSelector {
    id: selector
    cameraObject: camera
}

Button {
    id: activateRearCamera
    onClicked: {
        selector. selectedCameraDevice = 0;
        camera.start();
    }
}

Button {
    id: activateFrontCamera
    onClicked: {
        selector. selectedCameraDevice = 1;
        camera.start();
    }
}

I didn't try any of this, but it should work ;)

--
Yoann

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/interest/attachments/20140107/bea7b23d/attachment.html 


More information about the Interest mailing list