Code: Select all
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include "cameralibrary.h"
#include "timebase.h"
#include "Windows.h"
#include "conio.h"
#include "string.h"
#include <math.h>
using namespace CameraLibrary;
int getFR(int low, int high);
void clearKeyboardBuffer();
// Lower and upper bounds for frame rate
const int FRlow = 10;
const int FRhigh = 100;
// Refresh rate for status output (no of frames)
const int dispRate = 10;
int main()
{
printf(" Waiting for cameras to intialize...");
//== Initialize Camera Library ==----
CameraManager::X().WaitForInitialization();
//== Verify all cameras are initialized and then proceed ==----
Camera* tempcamera = CameraManager::X().GetCamera();
//== If no device connected, pop a message box and exit ==--
if (tempcamera == 0)
{
printf("failed\n\n");
printf("\n\n return key to exit...");
getchar();
return 1;
}
printf("complete\n\n");
//== List all connected cameras ==----
CameraList* list = new CameraList;
printf(" Available cameras and/or other hardware:\n");
int cameraCount = list->Count();
for (int i = 0; i < cameraCount; i++)
{
printf(" Camera %d : %s\n", i, (*list)[i].Name());
}
if (cameraCount == 0)
printf(" None\n");
// Find the eSync from a list of devices.
HardwareDeviceList deviceList;
int deviceIndex = -1;
for (int i = 0; i < deviceList.Count(); ++i)
{
std::string devName(deviceList[i].Name());
std::string syncName = "eSync";
size_t index = devName.find(syncName);
if (index != std::string::npos)
{
deviceIndex = i;
break;
}
}
if (deviceIndex == -1)
printf(" eSync not found!\n");
else
{
printf(" Hardware device %d: %s\n", deviceIndex, deviceList[deviceIndex].Name());
}
// sync cameras if > 1 cameras are present along with esync
if (cameraCount > 1 && deviceIndex != -1)
{
printf("\n\n Syncing cameras....");
// create sync module
cModuleSync* sync = cModuleSync::Create();
// get the camera ids
Camera* cameras[kMaxCameras];
for (int i = 0; i < cameraCount; i++)
cameras[i] = CameraManager::X().GetCamera((*list)[i].UID());
// Use the deviceIndex to find the eSync in the deviceList.
Camera* eSync = CameraManager::X().GetDevice(deviceList[deviceIndex].UID());
if (eSync && !eSync->IsSyncAuthority())
{
eSync = nullptr;
}
int flag = 0;
// add cameras to the sync module
for (int i = 0; i < cameraCount; i++)
{
sync->AddCamera(cameras[i]);
flag++;
}
// add eSync to the sync module
if (eSync != nullptr)
{
sync->AddCamera(eSync);
flag++;
}
if (flag == (cameraCount + 1))
printf("\n\n All cameras and eSync device synced. Press ENTER to start cameras, BACKSPACE to abort and exit...");
else
{
printf("\n\n All cameras/eSync not synced! Press any key to exit...");
getchar();
//== Destroy synchronizer
sync->RemoveAllCameras();
cModuleSync::Destroy(sync);
for (int i = 0; i < cameraCount; i++)
{
//Release cameras
for (int i = 0; i < cameraCount; i++)
{
cameras[i]->Release();
}
}
// Disconnect devices and shutdown Camera Library
CameraManager::X().Shutdown();
return 1;
}
char c = _getch();
while (c != '\b' && c != '\r')
c = _getch();
if (c == '\b')
{
//== Destroy synchronizer
sync->RemoveAllCameras();
cModuleSync::Destroy(sync);
for (int i = 0; i < cameraCount; i++)
{
//Release cameras
for (int i = 0; i < cameraCount; i++)
{
cameras[i]->Release();
}
}
// Disconnect devices and shutdown Camera Library
CameraManager::X().Shutdown();
return 0;
}
// Determine camera resolution to size application window
int* cameraWidth = (int*)malloc(cameraCount * sizeof(int));
int* cameraHeight = (int*)malloc(cameraCount * sizeof(int));
// Get desired frame rate from user
int FR = getFR(FRlow, FRhigh);
// set camera parameters and start cameras
for (int i = 0; i < cameraCount; i++)
{
cameras[i]->SetNumeric(1, i+1); // Set camera numbers
std::string s1((*list)[i].Name());
if (s1.find("Color") != std::string::npos) {
cameras[i]->SetVideoType(Core::VideoMode); // Set video type to videomode for color camera
}
else
cameras[i]->SetVideoType(Core::MJPEGMode); // Set video type to MJPEG for IR cameras
cameras[i]->SetAEC(true); // Enable Automatic Exposure Control
cameras[i]->SetAGC(true); // Enable Automatic Gain Control
cameras[i]->SetFrameRate(FR);
cameraWidth[i] = cameras[i]->Width();
cameraHeight[i] = cameras[i]->Height();
cameras[i]->Start();
}
// Start the eSync
eSync->SetFrameRate(FR);
eSync->Start();
// Pool and save frame groups
printf("\n\n Fetching frames...press any key to terminate...\n");
clearKeyboardBuffer();
int framePacketCount = 0;
while (!_kbhit())
{
// Get frame group
FrameGroup* frameGroup = sync->GetFrameGroup();
if (frameGroup)
{
for (int i = 0; i < frameGroup->Count(); i++)
{
Frame* frame = frameGroup->GetFrame(i);
frame->Release();
}
framePacketCount++;
// Print status every 10 frames
if ((framePacketCount % dispRate) == 0)
{
printf("\rReceived frame packet #%d (contains %d frame(s))...", framePacketCount, frameGroup->Count());
if (sync->LastFrameGroupMode() == FrameGroup::Hardware)
{
printf("synchronized...");
}
else
{
printf("unsynchronized...");
}
}
}
}
printf("\n\n Frame collection terminated...press any key to exit...");
clearKeyboardBuffer();
getchar();
//== Destroy synchronizer
sync->RemoveAllCameras();
cModuleSync::Destroy(sync);
for (int i = 0; i < cameraCount; i++)
{
//Release cameras
for (int i = 0; i < cameraCount; i++)
{
cameras[i]->Release();
}
}
}
// Disconnect devices and shutdown Camera Library
CameraManager::X().Shutdown();
}
int getFR(int low, int high)
{
int input;
printf("\n Enter frame rate: [%d to %d]: ", low, high);
scanf("%d", &input);
while (!((input <= high) && (input >= low)))
{
printf("\r [ERROR] Entered value is out of range, must be in [%d, %d]...try again: ",low,high);
fflush(stdout);
scanf("%d", &input);
}
return (input);
}
void clearKeyboardBuffer()
{
while (_kbhit())
{
_getche();
}
}
[img]