Viewfinder Mode

The viewfinder mode is used to specify the main operating mode of the camera.

The viewfinder, in the context of the Camera library is the raw data from the imaging datapath. This viewfinder shouldn't be confused with the viewfinder that you find in a camera. The viewfinder is basically a stream of image buffers (video) that you can either show on a display, process in a stream, or both.

One thing that's always required is that you must always set the viewfinder mode before you use any other calls from the Camera library. You must also set the viewfinder mode before you set any configuration settings on the camera. The viewfinder mode that you select impacts the features that are available on the camera, as well as the default settings for the imaging datapath. If you don't, any configuration settings that you make are lost because setting the viewfinder mode resets the camera configuration to its default values for the viewfinder. The reason that the reset occurs is to ensure that the camera is in a valid state for use.

In addition, different cameras may support different viewfinder modes, therefore, it's a good idea to query the modes that are supported by the camera before you set the viewfinder mode. Setting the viewfinder mode allows for the discovery of camera capabilities and the available configuration modes and settings. For more information about the defined viewfinder modes, see camera_vfmode_t.

For example, when you set the viewfinder mode to CAMERA_VFMODE_VIDEO, you are indicating that your application needs to get a stream of frames from a camera that you can choose to later encode. After you have set the viewfinder mode, all of the query functions (camera_get_support_*()) available with this API (e.g., camera_get_supported_vf_resolutions(), camera_has_feature(), etc.) should properly return lists that should work with your intended use case.

For example:
Note: In the following code snippet, we are using one camera configured that's assigned as CAMERA_UNIT_1.
camera_handle_t cameraHandle;              

// Open the camera assigned "CAMERA_UNIT_1"
err = camera_open(CAMERA_UNIT_1, CAMERA_MODE_RW | CAMERA_MODE_ROLL, &cameraHandle);
if (err != EOK) {
    printf("Failed to open the camera: err = %d\n", err);
    return err;
}

// Set video viewfinder mode for video
err = camera_set_vf_mode(cameraHandle, CAMERA_VFMODE_VIDEO);
if (err != EOK) {
     printf("Failed to set VF mode: err = %d\n", err);
     camera_close(cameraHandle);
     break;
}
// Configure the camera after. First determine if the camera has video stabilization
// by checking for the CAMERA_FEATURE_VIDEOSTABILIZATION feature
bool hasViewfinderWindow = camera_has_feature(cameraHandle, CAMERA_FEATURE_VIDEOSTABILIZATION);
if (hasViewfinderWindow) {
    printf("We have camera stabilization");
}
            

Viewfinder window

By default, the Camera library creates a viewfinder window. The viewfinder window is a Screen, child window that's managed by the Sensor service. This conveniently allows you to create a parent Screen window in your application and then join the viewfinder window to your application. This helps to simplify the code you need to write in your application to handle the image buffers from the Sensor service and refresh the video feed. The application logic for that is handled by the Sensor service.

Depending on the requirements of your application, there are situations where you won't require this viewfinder window to be created. For example, you might want to:
  • encoding the image buffers and writing them to file
  • performing algorithms on the image buffers
  • processing the image buffers and manipulating the image buffers
  • handle posting the image buffers in your application yourself
In these situations, when you don't require the viewfinder window, you can disable the automatic creation of the viewfinder service. To tell the Sensor service to not create a viewfinder window, call camera_set_vf_property() with the CAMERA_IMGPROP_CREATEWINDOW property set to zero. For example:
camera_set_vf_property(cameraHandle, CAMERA_IMGPROP_CREATEWINDOW, 0);
                 

After you set the mode, you can modify the viewfinder settings using camera_set_vf_property(), configure the camera, and then start the viewfinder using camera_start_viewfinder(). When you are finished using the camera, you can stop the viewfinder using the camera_stop_viewfinder() function.

If you want to change the video mode, you must first call camera_stop_viewfinder() to stop the viewfinder, and then call call camera_set_vf_mode() to change the viewfinder mode. To start getting image buffers again, you can call camera_start_viewfinder(). Keep in mind that any settings you have previously set to the camera are lost when you call camera_set_vf_mode(). For that reason, you may want to re-apply any sensor configuration settings.