Opening the camera

You can use the open(CameraUnit::Type unit) function to open a camera. The CameraUnit::Type unit parameter is the target camera that you want to open. If you leave this parameter blank, the open() function opens the rear camera unit by default.

The Camera::open() function emits the cameraOpened() signal when the camera is successfully opened, or emits the cameraOpenFailed() signal when the camera is not opened successfully.

Opening a camera unit

Most devices have a rear camera, however, some devices also have a front camera. These cameras are identified with the CameraUnit::Rear and CameraUnit::Front enumerations. It's always a good idea to determine which camera units are supported by the device before attempting to open one.

For more information about the enumerations that represent the camera unit types, see CameraUnit.

Finding supported cameras in QML

You can use the supportedCameras property in your QML code to determine which cameras are supported on the device. When the creationCompleted() signal is emitted, use the supportedCameras property to get a QVariantList of supported cameras.

Here's a QML code sample that shows you how a custom function, called getCameraUnit() which takes a QVariantList list of camera units as its only parameter, can be used along with the supportedCameras property to find an accessible CameraUnit on the device.

// This function returns an accessible camera unit. It starts
// by first checking to see if the Rear camera unit is 
// available and if it's accessible, it's returned. If not,
// then it checks to see if the front camera unit is available,
// and again, if it's accessible it's returned. If there are no
// accessible camera units, this function returns null and shows
// a toast to the user.
function getCameraUnit(camUnitList) {
    // Find a supported camera unit.
    if (camUnitList.length == 0 || camUnitList[0] == CameraUnit.None) {
        // Show a toast.
        qmlToast.body = "No camera units are accessible";
        qmlToast.show();

        return null;
    }
    
    // We prefer to have the Rear camera unit open first,
    // so we test for that one first, and if we find it, we
    // return it. Otherwise, we'll look for the Front camera 
    // unit, and if we find it, we'll return it.
    for (var i = 0; i < camUnitList.length; ++ i) {
        if (camUnitList[i] == CameraUnit.Rear)
            return camUnitList[i];
    }
    
    for (var j = 0; j < camUnitList.length; ++ j) {
        if (camUnitList[i] == CameraUnit.Front)
            return camUnitList[i];
    }    
}

When an accessible camera unit is found using the getCameraUnit() function, you can open it in your QML code as shown below:

Container {
    // ...

    onCreationCompleted: {
    var cameraUnit = getCameraUnit(qmlCameraObj.supportedCameras);
    
    if (cameraUnit != null) {
        qmlCameraObj.open(cameraUnit);

        // ...
    }
    
    // ...
}

// ...

Finding supported cameras in C++

You can use the supportedCameras() function to determine which cameras are supported. This function returns a QList containing all of the supported cameras on the device. You can use the following C++ code in your app's constructor to open a camera unit.

 
// App's constructor.
ApplicationUI::ApplicationUI(Application *app) : QObject(app)
{
// ...

// Retrieve a list of accessible camera units.
QList<CameraUnit::Type> list = m_pCamera->supportedCameras(); 

if(list.isEmpty() || list.contains(CameraUnit::None))
{
    // Use our custom showToast() function to display
    // a message to the user.
    showToast("No cameras are accessible");
}
else if(list.contains(CameraUnit::Rear))
{ 
    // The default camera unit is rear 
    // so no parameter is necessary. 
    m_pCamera->open();
} 
else if (list.contains(CameraUnit::Front))
{ 
    // Open the front camera unit.
    m_pCamera->open(CameraUnit::Front);
}
else
{
    // Report unknown error.
    showToast("An unknown error has occurred");
}

// ...

Finding an accessible camera unit

If a camera is not supported, then it's clear that the camera is also not accessible to your app. When a camera is supported, this doesn't necessarily guarantee that it's also readily available to your app at that point in time. Why? Well it may not be available if it's already been acquired, or if it's being used by another process. However, when a camera is supported and available, then it's also accessible to your app.

You can use the allCamerasAccessible() function (in C++), or the allCamerasAccessible property (in QML) to find a camera that is both supported and available to your app.

The allCamerasAccessible() function or property can be used to check whether a camera is supported and available. When your app confirms that it can access the camera, it can open the camera.

Here's a QML sample that shows you how to use the allCamerasAccessible property in your QML code. This code sample assumes that you have a Camera object defined in your QML code with an id of qmlCameraObj. The code sample uses the allCamerasAccessible property to determine if the Camera is accessible and opens the rear camera of the device.

Container {
    // The QML Camera object.
    Camera {
        id: qmlCameraObj
        
        // ...
    }

    // ...
    
    onCreationCompleted: {
        // Check to see if your app has access to the camera.
        if (qmlCameraObj.allCamerasAccessible) {
            // Open the rear camera.
            qmlCameraObj.open(CameraUnit.Rear);
        }
    }    
}

Here's a C++ sample showing you how to use the allCamerasAccessible() function along with the Camera::open() function to open the front camera of the device.

 
if(m_pCameraObj->allCamerasAccessible()) 
{
    // Open the front camera unit. 
    m_pCameraObj->open(CameraUnit::Front);
}

Last modified: 2013-12-20

comments powered by Disqus