Set the viewfinder mode for the camera

Updated: April 19, 2023

Before you can start using the camera, you must set the viewfinder mode.

When you set the viewfinder mode, it resets any previous configuration settings on the camera. For more information, see Viewfinder Mode.” It's recommended that you check what viewfinder modes are available on the camera before you set the mode. If the viewfinder mode isn't correctly set, the camera won't function as expected and you may see adverse or undefined results when you start to work with the image buffers from the camera. For information about determining what viewfinder modes are available, see Determine the viewfinder modes available.”

You'll likely know the specific viewfinder mode you want to use because you'll know what camera you're using. For example, if you know your camera supports only the CAMERA_VFMODE_VIDEO mode, there's no reason to determine what video modes are available. In this situation, you should set the viewfinder mode directly to optimize your code. Here's a sample code snippet to show how to do so:
...
...
camera_handle_t cameraHandle;
int err;

// First connect to the camera
err = camera_open(CAMERA_UNIT_1, CAMERA_MODE_RW | CAMERA_MODE_ROLL, &cameraHandle); 
if (err != CAMERA_EOK) {
    printf("Failed to open the camera\n");
    return err;
}

// Set the viewfinder image stream mode
err = camera_set_vf_mode(cameraHandle, CAMERA_VFMODE_VIDEO);
if (err != CAMERA_EOK) {
    printf("Failed to set the viewfinder mode\n");
    camera_close(cameraHandle);
    return err;
}
...
...

Determine the viewfinder modes available

To determine the viewfinder modes that are available, call camera_get_supported_vf_modes() twice. You call the function the first time to determine the number of modes that are available. After you know this number, you must allocate an array large enough to hold this many modes, and then call the function a second time to retrieve the modes. Most cameras support CAMERA_VFMODE_VIDEO, which is the mode necessary to use video.

In the following code snippet, we presume that CAMERA_UNIT_1 is connected to the system:
...
...
uint32_t numSupported;
camera_vfmode_t* modes;
uint32_t x;
int err;
camera_handle_t cameraHandle;

err = camera_open(CAMERA_UNIT_1, CAMERA_MODE_RW | CAMERA_MODE_ROLL, &cameraHandle);
if (err != CAMERA_EOK) {
    printf("Failed to open the camera\n");
    return err;
}

err = camera_get_supported_vf_modes(cameraHandle, 0, &numSupported, NULL);
if (err != EOK) {
    printf("Failed to get number of supported VF modes: err = %d\n", err);
    return err;
}

// Allocate an array big enough to hold all modes
modes = (camera_vfmode_t*) malloc(sizeof(camera_vfmode_t) * numSupported);
if (modes == NULL) {
    printf("Failed to allocated VF modes\n");
    return ENOMEM;
}

// Call the function a second time
if (camera_get_supported_vf_modes(cameraHandle, numSupported,
                                  &numSupported, modes) == EOK) {
    for (x=0; x < numSupported; ++x) {
        printf("\t Enumerator Value: %d\n",  modes[x]);
    }
    // You can now pick a viewfinder mode and set it using camera_set_vf_mode()
    ...
    ...
}
...
...