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 <string>
#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, uint32_t *pCaps);
static std::string MakeProcessedFileName(std::string& rstrFileName, uint32_t uiFrameNumber, const char* pSuffix, const char* pExt);
static void SaveProcessedFile(std::string strFileName, uint8_t* pMetaData, uint32_t uiMetaSize, uint8_t* pImageData, uint64_t uiImageByteSize);
// Static Data declarations
static int32_t s_iDeviceHandle;
uint32_t uiNumDetectedDevices;
static FPRODEVICEINFO s_camDeviceInfo[FLI_TEST_MAX_SUPPORTED_CAMERAS];
static uint32_t s_uiCaps[(uint32_t)FPROCAPS::FPROCAP_NUM];
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 uiNumCaps;
std::string strFileName;
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.
uiNumCaps = (uint32_t)FPROCAPS::FPROCAP_NUM;
iResult = FPROSensor_GetCapabilityList(s_iDeviceHandle, s_uiCaps, &uiNumCaps);
// Set up your exposure and frame parameters
if (iResult >= 0)
iResult = SetFrameInfo(s_iDeviceHandle,s_uiCaps);
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, (uint32_t)(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);
strFileName = "SimpleImageLoop.rcd";
SaveProcessedFile(MakeProcessedFileName(strFileName, i, "", ".rcd"), NULL, 0, (uint8_t*)pFrame, uiFramSizeInBytes);
// 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");
}
}
else
{
printf("An error occured setting up the camera\n");
}
// Close up shop
iResult = FPROCam_Close(s_iDeviceHandle);
}
else
{
printf("Failed to open camera device!\n");
}
}
else
{
printf("No Cameras found!\n");
}
// Give our memory back
if (pFrame)
free(pFrame);
return 0;
}
int32_t
SetFrameInfo(int32_t iDeviceHandle, uint32_t *pCaps)
{
int32_t iResult;
if (NULL == pCaps)
return(-1);
// 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[(uint32_t)FPROCAPS::FPROCAP_MAX_PIXEL_WIDTH], pCaps[(uint32_t)FPROCAPS::FPROCAP_MAX_PIXEL_HEIGHT]);
// Set up for the camera to control the shutter (if you have one)
// The default is for user control of the shutter through the
// FPROCtrl_SetShutterOpen() API. Disabling the override as below
// allows the camera to control the shutter for exposures.
if (iResult >= 0)
iResult = FPROCtrl_SetShutterOverride(iDeviceHandle, false);
// return our result
return(iResult);
}
std::string MakeProcessedFileName(std::string& rstrFileName, uint32_t uiFrameNumber, const char* pSuffix, const char* pExt)
{
std::string strNew;
std::string strExt;
char cNumBuff[32];
size_t iIndex;
// find the last '.' in the file name-
iIndex = rstrFileName.find_last_of('.', rstrFileName.length());
if (iIndex != std::string::npos)
{
strNew = rstrFileName.substr(0, iIndex);
snprintf(cNumBuff, sizeof(cNumBuff), "_%d_", uiFrameNumber);
strNew += cNumBuff;
strNew += pSuffix;
}
else
{
// No '.', just add the suffix to the end
strNew = rstrFileName + pSuffix;
}
strNew += pExt;
return(strNew);
}
void SaveProcessedFile(std::string rstrFileName, uint8_t* pMetaData, uint32_t uiMetaSize, uint8_t* pImageData, uint64_t uiImageByteSize)
{
FILE* pFile;
int32_t iResult;
#if defined(_WIN32) || defined(_WINDOWS)
fopen_s(&pFile, rstrFileName.c_str(), "w+b");
#else
pFile = fopen(rstrFileName.c_str(), "w+b");
#endif
if (pFile)
{
iResult = 0;
if (pMetaData)
{
if (fwrite(pMetaData, 1, uiMetaSize, pFile) != uiMetaSize)
{
printf("Error writing Meta Data to file %s\n", rstrFileName.c_str());
iResult = -1;
}
}
if (iResult >= 0)
{
if (fwrite(pImageData, 1, (size_t)uiImageByteSize, pFile) != uiImageByteSize)
{
printf("Error writing Image Data to file %s\n", rstrFileName.c_str());
}
}
fclose(pFile);
}
else
{
printf("Error opening output file %s\n", rstrFileName.c_str());
}
}
Finger Lakes Instrumentation Camera API.
LIBFLIPRO_API FPRODebug_EnableLevel(bool bEnable, FPRODBGLEVEL eLevel)
LIBFLIPRO_API FPROSensor_GetCapabilityList(int32_t iHandle, uint32_t *pCapList, uint32_t *pNumCaps)
Retrieves the capabilities list for the connected camera.
LIBFLIPRO_API FPROCam_Open(FPRODEVICEINFO *pDevInfo, int32_t *pHandle)
Connects to the camera specified by the pDevInfo parameter.
LIBFLIPRO_API FPROCam_Close(int32_t iHandle)
Disconnects from the camera an releases the handle.
#define FPRO_DEBUG_TRACE
TRACE, DEBUG, REGRW, INFO, WARNING, and ERROR debug is output.
Definition: libflipro.h:562
@ FPROCAP_NUM
Number of supported capabilities.
Definition: libflipro.h:877
@ FPROCAP_MAX_PIXEL_WIDTH
Max allowed image width in pixels.
Definition: libflipro.h:860
@ FPROCAP_MAX_PIXEL_HEIGHT
Max allowed image height in pixels.
Definition: libflipro.h:861
LIBFLIPRO_API FPROFrame_CaptureStop(int32_t iHandle)
Stops the active image capture.
LIBFLIPRO_API FPROFrame_GetVideoFrame(int32_t iHandle, uint8_t *pFrameData, uint32_t *pSize, uint32_t uiTimeoutMS)
Retrieves an image frame from the camera.
LIBFLIPRO_API FPROFrame_SetImageDataEnable(int32_t iHandle, bool bEnable)
Enables image data imaging.
LIBFLIPRO_API FPROCtrl_SetShutterOverride(int32_t iHandle, bool bOverride)
Sets the shutter control override.
LIBFLIPRO_API FPROFrame_ComputeFrameSize(int32_t iHandle)
Computes the size in bytes of the image frame.
LIBFLIPRO_API FPROCtrl_SetExposure(int32_t iHandle, uint64_t uiExposureTime, uint64_t uiFrameDelay, bool bImmediate)
Sets the exposure time of the image sensor.
LIBFLIPRO_API FPROFrame_CaptureStart(int32_t iHandle, uint32_t uiFrameCount)
Initiates the capture of the configured image.
LIBFLIPRO_API FPROFrame_SetImageArea(int32_t iHandle, uint32_t uiColOffset, uint32_t uiRowOffset, uint32_t uiWidth, uint32_t uiHeight)
Sets the area of the image sensor to be used to produce image frame data.
LIBFLIPRO_API FPROCam_GetCameraList(FPRODEVICEINFO *pDeviceInfo, uint32_t *pNumDevices)
FPROCam_GetCameraList.
Definition: libflipro.h:365