FLI libflipro API
SimpleImageLoop.cpp

Simple Image Loop Example Code.
This example shows you how to create a simple image loop to grab and process image frames one at a time.

#if defined(_WIN32) || defined(_WINDOWS)
#include "stdafx.h"
#else
#include <stdio.h>
#endif
#include "stdint.h"
#include "stdlib.h"
#include "libflipro.h"
#define FLI_TEST_MAX_SUPPORTED_CAMERAS (4)
#define MSEC_TO_NSEC(__msec) ((uint64_t)__msec * 1000000)
#define NSEC_TO_MSEC(__nsec) (__nsec / 1000000)
// Static Function declarations
static int32_t SetFrameInfo(int32_t iDeviceHandle, FPROCAP *pCaps);
// Static Data declarations
static int32_t s_iDeviceHandle;
uint32_t uiNumDetectedDevices;
static FPRODEVICEINFO s_camDeviceInfo[FLI_TEST_MAX_SUPPORTED_CAMERAS];
static FPROCAP s_camCapabilities;
static uint64_t s_uiExposureTimeNSec;
int main()
{
int32_t iResult;
int32_t iGrabResult;
uint32_t i;
uint8_t *pFrame;
uint32_t uiFramSizeInBytes;
uint32_t uiSizeGrabbed;
uint32_t uiCamCapSize;
pFrame = NULL;
s_uiExposureTimeNSec= MSEC_TO_NSEC(50);
// first get the list of available devices
uiNumDetectedDevices = FLI_TEST_MAX_SUPPORTED_CAMERAS;
iResult = FPROCam_GetCameraList(s_camDeviceInfo, &uiNumDetectedDevices);
if ((iResult >= 0) && (uiNumDetectedDevices > 0))
{
// Open the first device in the list
s_iDeviceHandle = -1;
iResult = FPROCam_Open(&s_camDeviceInfo[0], &s_iDeviceHandle);
if ((iResult >= 0) && (s_iDeviceHandle >= 0))
{
// Different camera models support a different set of capabilities.
// The API allows you to retrieve the capabilities so that you can obtain
// images properly and configure your applications accordingly. In all cases,
// you need to know the size of the Meta Data supplied by the camera that is
// prepended to every image. This size is contained in the capabilities structure.
uiCamCapSize = sizeof(FPROCAP);
iResult = FPROSensor_GetCapabilities(s_iDeviceHandle, &s_camCapabilities, &uiCamCapSize);
// Set up your exposure and frame parameters
if (iResult >= 0)
iResult = SetFrameInfo(s_iDeviceHandle,&s_camCapabilities);
if (iResult >= 0)
{
// Make sure we have space for the image frame.
// Calculating the byte size of the image that will be generated by the camera can become
// quite involved. The size depends not only on the image dimensions and pixel size, but
// also on the size of the meta data returned for each frame, the number of pre and post row
// reference pixels, the number of pre and post frame reference rows, as well as the
// horizontal and vertical binning factors. In the most simple case, with 12 bit pixels,
// you can use the FPRO_IMAGE_DIMENSIONS_TO_FRAMEBYTES() provided macro to calculate the
// byte size of the image data and then just add the meta data size. Note that this assumes no
// reference pixels and 1:1 binning:
//
// uiFramSizeInBytes = FPRO_IMAGE_DIMENSIONS_TO_FRAMEBYTES(2048, 2048) + s_camCapabilities.uiMetaDataSize;
//
// However, things get complicated quickly when applying the various settings described above.
// To facilitate calculating the actual byte size of the data you will receive for an image, the
// API provides the FPROFrame_ComputeFrameSize() function. Note that before you use this function,
// all imaging parameters must be set on the camera. The reason is because this call uses the actual
// camera settings to determine the size of the resulting image data.
uiFramSizeInBytes = FPROFrame_ComputeFrameSize(s_iDeviceHandle);
pFrame = (uint8_t *)malloc(uiFramSizeInBytes);
if (pFrame)
{
if (iResult >= 0)
{
// all is well - now you can start grabbing image frames
// This loop will grab 10 frames.
iGrabResult = 0;
for (i = 0; (i < 10) && (iResult >= 0) && (iGrabResult >= 0); ++i)
{
// Start the capture and tell it to get 1 frame
iResult = FPROFrame_CaptureStart(s_iDeviceHandle, 1);
if (iResult >= 0)
{
// Grab the frame- Here you can save the requested image size if you like as
// the FPROFrame_GetVideoFrame() will return the actual number of bytes received.
// Whatever you decide, make sure you pass the correct size into this API in order
// to get the correct number of bytes for your frame.
uiSizeGrabbed = uiFramSizeInBytes;
iGrabResult = FPROFrame_GetVideoFrame(s_iDeviceHandle, pFrame, &uiSizeGrabbed, NSEC_TO_MSEC(s_uiExposureTimeNSec));
// Regardless of how the capture turned out, stop the capture
iResult = FPROFrame_CaptureStop(s_iDeviceHandle);
// If the FPROFrame_GetVideoFrame() succeeded- then process it
if (iGrabResult >= 0)
{
printf("Got frame %d.\n",i);
// here you can do whatever you want with the frame data.
if (uiSizeGrabbed == uiFramSizeInBytes)
{
// got the correct number of bytes
}
else
{
// something bad happened with the image transfer
printf("Got incorrect size for frame %d: got 0x%x, wanted 0x%x\n",i, uiSizeGrabbed, uiFramSizeInBytes);
}
}
else
{
printf("ERROR returned from FPROFrame_GetVideoFrame\n");
}
}
}
}
}
else
{
printf("ERROR getting frame memory.... leaving\n");
}
}
// Close up shop
iResult = FPROCam_Close(s_iDeviceHandle);
}
}
// Give our memory back
if (pFrame)
free(pFrame);
return 0;
}
int32_t
SetFrameInfo(int32_t iDeviceHandle, FPROCAP *pCaps)
{
int32_t iResult;
// assume success
iResult = 0;
// Enable/disable image data
// The power on default of the camera is to have image data enabled.
// This is just shown here in case you are working with test frames and
// need to set the camera back up to get you real image data.
iResult = FPROFrame_SetImageDataEnable(iDeviceHandle, true);
// Set the exposure time
// The default camera exposure time is 50msecs (for the GSENSE 400)
// The FPROCtrl_SetExposureTime() API expects the exposure time in
// nano seconds. The frameDelay parameter is also in nanoseconds
if (iResult >= 0)
iResult = FPROCtrl_SetExposure(iDeviceHandle, s_uiExposureTimeNSec ,MSEC_TO_NSEC(5),false);
// Set the Image area
// You should set the camera image dimensions to their maximum values.
// Not all cameras support sub-frame capture.
// But if you were to change the values this is how you would do it.
if (iResult >= 0)
iResult = FPROFrame_SetImageArea(iDeviceHandle, 0, 0, pCaps->uiMaxPixelImageWidth, pCaps->uiMaxPixelImageHeight);
// return our result
return(iResult);
}