Open the camera

The first thing you must do to use the Camera library is to connect to the camera.

To connect to the camera, you use camera_open(), which returns a reference to a camera_handle_t handle. When your application successfully connects and if you receive a valid handle from the Camera API, it confirms that your application can access the specified camera with the requested access mode. Each application can have multiple camera handles open to multiple cameras — up to 16 camera handles open per application. The cameras that are available to the Sensor service are configured in the sensor configuration file. For more information about configuring the sensor configuration file and the Sensor service, see the Sensor chapter in the System Services guide.

When you call camera_open(), you must specify the following arguments:
the camera unit
To connect to the camera, you need to know its identifier, which is specified as an enumerator of camera_unit_t. This identifier specifies the camera to access. For the Camera library, you can use identifiers such as CAMERA_UNIT_1 or CAMERA_UNIT_2, which map to the cameras defined in the sensor configuration file as SENSOR_UNIT_1 and SENSOR_UNIT_2, respectively. For example, if you specified CAMERA_UNIT_1, it maps to SENSOR_UNIT_1. If you don't know what cameras are connected to your system, you can use Camera API calls to query what's connected to your system. For more information, see Determine the cameras connected to your system
the mode of access
This mode specifies what access permissions you require on the camera. Typically, your camera application requires read/write access to a camera. Only one application can have write access to a camera at any given time. Multiple applications with read-only access can query information about the camera, but won't be able to modify its configuration, that is, write to the camera roll (e.g., can't record or encode on the camera), or start/stop the viewfinder. For this reason, ensure that you call camera_close(), after your application is done with its write access to the camera, otherwise other applications won't have write access to that camera.

You can specify the access using one or more Camera access mode flags flags, which are OR'ed together.

In general, the flags control permissions to the camera and the image buffers, however, CAMERA_MODE_ROLL gives you access to the camera roll. The camera roll is the default directory where the Sensor service puts video files, which is required when you record video. This directory to use as the camera roll is specified using the -r option when you start the Sensor service. For more information about the camera roll, see Understanding the camera roll.”

When camera_open() returns successfully (CAMERA_EOK), a valid handle is returned. You must use this handle to make other Camera API calls to the specified camera. Since you have the camera open (connected to it), you must call camera_close() whenever an error occurs when you make Camera API calls. For more information about closing the camera, see Stop viewfinder and close camera.”

The following code snippet shows you how to determine the cameras connected to your system and how to connect to the first camera returned to you:
...
...
camera_unit_t unit;
camera_unit_t *supportedCameras;
uint32_t numSupported;
camera_handle_t cameraHandle;

// Get how many cameras are supported, which is returned in the reference
// to numSupported.
if (camera_get_supported_cameras(0, &numSupported, NULL) != CAMERA_EOK){
    printf("Failed to get the number of supported cameras\n");
}

// Allocate an array big enough to hold unit of all cameras
   supportedCameras = (camera_unit_t*) malloc(sizeof(camera_unit_t) * numSupported);

// Get the list of supported cameras by calling the 
// camera_get_supported_cameras() again.
if (camera_get_supported_cameras(numSupported, &numSupported, supportedCameras) != CAMERA_EOK ){
    printf("Failed to get a list of supported cameras\n");
    free(supportedCameras);
}

// We'll just use the first camera that we get. If there are more, we'll ignore it for now
printf("assigning to unit\n");
unit = supportedCameras[0];
free(supportedCameras);
printf("The number of cameras connected to the system: %d \n. Assigning to camera unit #%d\n", numSupported ,unit);

if (camera_open(unit, CAMERA_MODE_RW | CAMERA_MODE_ROLL, &cameraHandle) != CAMERA_EOK) {
    printf("Failed to connect to the camera\n");
}
else
{
    printf("We got a valid camera handle\n");
}
...
...

Determine the cameras connected to your system

If you don't know what cameras are connected to your system, use camera_get_supported_cameras() to determine the cameras that are connected to the system. You may need to call camera_get_supported_cameras() twice. The first time you call the function is to determine the proper-sized array that's required to store the number of cameras connected to your system, and then you call the function a second time with the allocated array to retrieve the units. If you know the array size ahead of time, it isn't necessary to call the function twice. Here's a code snippet:
// Get how many cameras are supported
    err = camera_get_supported_cameras(0, &numSupported, NULL);
    if (err != CAMERA_EOK) {
        printf("Failed to get number of supported cameras: err = %d\n", err);
        return err;
    }
    // Allocate an array big enough to hold unit of all cameras
    supportedCameras = (camera_unit_t*) malloc(sizeof(camera_unit_t) * numSupported);
    if (supportedCameras == NULL) {
        printf("Failed to allocate memory for supported cameras\n");
        return ENOMEM;
    }
    // Get the list of supported cameras
    err = camera_get_supported_cameras(numSupported, &numSupported, supportedCameras);
    if (err != CAMERA_EOK) {
        printf("Failed to get list of supported cameras: err = %d\n", err);
        free(supportedCameras);
        return err;
    }