[Interest] Select front/rear camera on Android

Cornelius Hald hald at icandy.de
Wed Jan 8 12:14:59 CET 2014


Thank you Yoann,

this seems to work pretty well. I'll have to do some more testing on
other devices, but so far it works great on the two devices I've tested
it on.

Cheers,
Conny

On Tue, 2014-01-07 at 13:19 +0000, Lopes Yoann wrote:
> 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
> 
> 





More information about the Interest mailing list