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>
#define FLI_TEST_MAX_SUPPORTED_CAMERAS (4)
#define MSEC_TO_NSEC(__msec) ((uint64_t)__msec * 1000000)
#define NSEC_TO_MSEC(__nsec) (__nsec / 1000000)
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 int32_t s_iDeviceHandle;
uint32_t uiNumDetectedDevices;
static FPRODEVICEINFO s_camDeviceInfo[FLI_TEST_MAX_SUPPORTED_CAMERAS];
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);
uiNumDetectedDevices = FLI_TEST_MAX_SUPPORTED_CAMERAS;
if ((iResult >= 0) && (uiNumDetectedDevices > 0))
{
s_iDeviceHandle = -1;
iResult =
FPROCam_Open(&s_camDeviceInfo[0], &s_iDeviceHandle);
if ((iResult >= 0) && (s_iDeviceHandle >= 0))
{
if (iResult >= 0)
iResult = SetFrameInfo(s_iDeviceHandle,s_uiCaps);
if (iResult >= 0)
{
pFrame = (uint8_t *)malloc(uiFramSizeInBytes);
if (pFrame)
{
if (iResult >= 0)
{
iGrabResult = 0;
for (i = 0; (i < 10) && (iResult >= 0) && (iGrabResult >= 0); ++i)
{
if (iResult >= 0)
{
uiSizeGrabbed = uiFramSizeInBytes;
iGrabResult =
FPROFrame_GetVideoFrame(s_iDeviceHandle, pFrame, &uiSizeGrabbed, (uint32_t)(NSEC_TO_MSEC(s_uiExposureTimeNSec)));
if (iGrabResult >= 0)
{
printf("Got frame %d.\n",i);
strFileName = "SimpleImageLoop.rcd";
SaveProcessedFile(MakeProcessedFileName(strFileName, i, "", ".rcd"), NULL, 0, (uint8_t*)pFrame, uiFramSizeInBytes);
if (uiSizeGrabbed == uiFramSizeInBytes)
{
}
else
{
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");
}
}
else
{
printf("Failed to open camera device!\n");
}
}
else
{
printf("No Cameras found!\n");
}
if (pFrame)
free(pFrame);
return 0;
}
int32_t
SetFrameInfo(int32_t iDeviceHandle, uint32_t *pCaps)
{
int32_t iResult;
if (NULL == pCaps)
return(-1);
iResult = 0;
if (iResult >= 0)
if (iResult >= 0)
if (iResult >= 0)
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;
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
{
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