Not yet rated
Tags:



Problem

Many phones and tablets have two cameras. How can I select the front-facing camera on a device?

Solution

You can check the .position property of a Camera and then check to see if it matches the FRONT constant defined in CameraPosition.FRONT

Detailed explanation

/**
 * Gets the front facing camera of a device
 *
 * @return The front facing camera of the device or null if no such camera is avialable
 */
public function getFrontCamera():Camera
{
    //First check if the device supports a camera
    if (Camera.isSupported == false)
    {
        return null;
    }
    var numCameras:int = Camera.names.length;
    var frontCam:Camera;
    
    //Loop through all the available cameras on the device
    for (var i:int = 0; i < numCameras; i++)
    {
        frontCam = Camera.getCamera(Camera.names[i]);
       
        //If the camera.position property matches the constant CameraPosition.FRONT we have found
        //the front camera
        if (frontCam.position == CameraPosition.FRONT)
        {
            break;
        }
       
        //Make sure the camera object is set to null
        frontCam = null;
    }
    //Return the found camera
    return frontCam;
}

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes