It relies on information provided in the Enumeration, Acquisition, and NodeMapInfo examples.
#include <iostream>
#include <sstream>
using namespace std;
{
if (result)
{
cout << "\n*** OPERATION COMPLETE ***\n";
}
else
{
cout << "\n*** OPERATION FAILED ***\n";
}
}
{
int result = 0;
cout << endl << "*** DEVICE INFORMATION ***" << endl << endl;
try
{
FeatureList_t features;
CCategoryPtr category = nodeMap.GetNode(
"DeviceInformation");
if (IsReadable(category))
{
category->GetFeatures(features);
FeatureList_t::const_iterator it;
for (it = features.begin(); it != features.end(); ++it)
{
cout << pfeatureNode->GetName() << " : ";
cout << (IsReadable(pValue) ? pValue->ToString() : "Node not readable");
cout << endl;
}
}
else
{
cout << "Device control information not available." << endl;
}
}
{
cout <<
"Error: " << e.
what() << endl;
result = -1;
}
return result;
}
{
system = System::GetInstance();
camList = system->GetCameras();
const unsigned int numCameras = camList.
GetSize();
cout << "Number of cameras detected: " << numCameras << endl << endl;
if (numCameras == 0)
{
system->ReleaseInstance();
cout << "Not enough cameras!" << endl;
cout << "Done! Press Enter to exit..." << endl;
getchar();
return false;
}
pCam = nullptr;
return true;
}
{
#if DEBUG
#endif
{
cout << msg << endl;
}
}
{
cout << endl << endl << "*** IMAGE ACQUISITION ***" << endl << endl;
bool result = true;
try
{
{
cout << "Unable to set acquisition mode to continuous (enum retrieval). Aborting..." << endl << endl;
return false;
}
CEnumEntryPtr ptrAcquisitionModeContinuous = ptrAcquisitionMode->GetEntryByName(
"Continuous");
{
cout << "Unable to set acquisition mode to continuous (entry retrieval). Aborting..." << endl << endl;
return false;
}
const int64_t acquisitionModeContinuous = ptrAcquisitionModeContinuous->GetValue();
ptrAcquisitionMode->SetIntValue(acquisitionModeContinuous);
CEnumEntryPtr ptrPixelFormat = pCam->PixelFormat.GetEntry(PixelFormat_Mono8);
{
pCam->PixelFormat.SetValue(PixelFormat_Mono8);
}
else
{
pCam->PixelFormat.SetValue(PixelFormat_BayerRG8);
}
pCam->BeginAcquisition();
cout << "Acquiring images..." << endl;
string deviceSerialNumber("");
CStringPtr ptrStringSerial = nodeMapTLDevice.GetNode(
"DeviceSerialNumber");
{
deviceSerialNumber = ptrStringSerial->GetValue();
PrintDebugMessage(
"Device serial number retrieved as " + deviceSerialNumber +
"...");
}
cout << endl;
for (
unsigned int imageCnt = 0; imageCnt <
k_numImages; imageCnt++)
{
try
{
ImagePtr pResultImage = pCam->GetNextImage(1000);
if (pResultImage->IsIncomplete())
{
cout << "Image incomplete with image status " << pResultImage->GetImageStatus() << "..." << endl
<< endl;
}
else
{
"Grabbed image " + to_string(static_cast<long long>(imageCnt)) +
", width = " + to_string(static_cast<long long>(pResultImage->GetWidth())) +
", height = " + to_string(static_cast<long long>(pResultImage->GetHeight())));
pReferenceImage->DeepCopy(pResultImage);
}
pResultImage->Release();
}
{
cout <<
"Error: " << e.
what() << endl;
result = false;
}
}
pCam->EndAcquisition();
}
{
cout <<
"Error: " << e.
what() << endl;
result = false;
}
return result;
}
{
bool result = true;
try
{
pCam->FileOperationSelector.SetValue(FileOperationSelector_Delete);
pCam->FileOperationExecute.Execute();
if (pCam->FileOperationStatus.GetValue() != FileOperationStatus_Success)
{
cout << "Failed to delete file!" << endl;
return false;
}
}
{
cout <<
"Unexpected exception : " << e.
what() << endl;
result = false;
}
return result;
}
{
bool result = true;
try
{
pCam->FileOperationSelector.SetValue(FileOperationSelector_Open);
pCam->FileOpenMode.SetValue(FileOpenMode_Write);
pCam->FileOperationExecute.Execute();
if (pCam->FileOperationStatus.GetValue() != FileOperationStatus_Success)
{
cout << "Failed to open file for writing!" << endl;
return false;
}
}
{
cout <<
"Unexpected exception : " << e.
what();
result = false;
}
return result;
}
{
bool result = true;
try
{
pCam->FileOperationSelector.SetValue(FileOperationSelector_Write);
pCam->FileOperationExecute.Execute();
if (pCam->FileOperationStatus.GetValue() != FileOperationStatus_Success)
{
cout << "Failed to write to file!" << endl;
return false;
}
}
{
cout <<
"Unexpected exception : " << e.
what();
result = false;
}
return result;
}
{
bool result = true;
try
{
pCam->FileOperationSelector.SetValue(FileOperationSelector_Close);
pCam->FileOperationExecute.Execute();
if (pCam->FileOperationStatus.GetValue() != FileOperationStatus_Success)
{
cout << "Failed to close file!" << endl;
return false;
}
}
{
cout <<
"Unexpected exception : " << e.
what();
result = false;
}
return result;
}
{
bool result = true;
{
return false;
}
try
{
INodeMap& nodeMapTLDevice = pCam->GetTLDeviceNodeMap();
pCam->Init();
ImagePtr pReferenceImage = Image::Create();
if (!
AcquireImages(pCam, nodeMap, nodeMapTLDevice, pReferenceImage))
{
return false;
}
{
try
{
cout << "\nSaving raw image to disk for debugging purpose..." << endl;
ostringstream filename;
filename << "rawImage.png";
pReferenceImage->Save(filename.str().c_str());
cout << "Image saved at " << filename.str() << endl;
}
{
cout <<
"Unable to save an image file : " << e.
what();
}
}
cout << endl << "*** UPLOADING IMAGE ***" << endl;
if (pCam->FileSelector == NULL)
{
cout << "File selector not supported on device!";
return false;
}
pCam->FileSelector.GetEntries(selectorList);
for (unsigned int i = 0; i < selectorList.size(); i++)
{
{
cout << node->GetSymbolic() << " not supported!" << endl;
continue;
}
{
pCam->FileSelector.SetIntValue((int64_t)node->GetNumericValue());
if (pCam->FileSize.GetValue() > 0)
{
{
cout << "Failed to delete file!" << endl;
continue;
}
}
{
cout << "Failed to open file!" << endl;
{
cout << "Problem opening file node." << endl;
return false;
}
{
cout << "Problem opening file node." << endl;
return false;
}
}
if (pCam->FileAccessLength.GetValue() < pCam->FileAccessBuffer.GetLength())
{
try
{
pCam->FileAccessLength.SetValue(pCam->FileAccessBuffer.GetLength());
}
{
cout <<
"Unable to set FileAccessLength to FileAccessBuffer length : " << e.
what();
}
}
pCam->FileAccessOffset.SetValue(0);
int64_t totalBytesToWrite = pReferenceImage->GetBufferSize();
int64_t intermediateBufferSize = pCam->FileAccessLength.GetValue();
int64_t writeIterations = (totalBytesToWrite / intermediateBufferSize) +
(totalBytesToWrite % intermediateBufferSize == 0 ? 0 : 1);
if (totalBytesToWrite == 0)
{
cout << "Empty Image. No data will be written to camera." << endl;
return false;
}
PrintDebugMessage(
"Total Bytes to write : " + to_string(
static_cast<long long>(totalBytesToWrite)));
PrintDebugMessage(
"FileAccessLength : " + to_string(
static_cast<long long>(intermediateBufferSize)));
PrintDebugMessage(
"Write Iterations : " + to_string(
static_cast<long long>(writeIterations)));
int64_t index = 0;
int64_t bytesLeftToWrite = totalBytesToWrite;
int64_t totalBytesWritten = 0;
bool paddingRequired = false;
int numPaddings = 0;
cout << "Writing data to device" << endl;
unsigned char* pImageData = static_cast<unsigned char*>(pReferenceImage->GetData());
for (unsigned int i = 0; i < writeIterations; i++)
{
if (intermediateBufferSize > bytesLeftToWrite)
{
unsigned int remainder = bytesLeftToWrite % 4;
if (remainder != 0)
{
paddingRequired = true;
numPaddings = 4 - remainder;
}
}
int64_t tmpBufferSize = intermediateBufferSize <= bytesLeftToWrite
? intermediateBufferSize
: (bytesLeftToWrite + numPaddings);
std::unique_ptr<unsigned char> tmpBuffer(
new unsigned char[static_cast<unsigned int>(tmpBufferSize)]);
memcpy(
tmpBuffer.get(),
&pImageData[index],
static_cast<unsigned int>(
(intermediateBufferSize <= bytesLeftToWrite) ? intermediateBufferSize : bytesLeftToWrite));
if (paddingRequired)
{
for (int j = 0; j < numPaddings; j++)
{
unsigned char* pTmpBuffer = tmpBuffer.get();
pTmpBuffer[bytesLeftToWrite + j] = 255;
}
}
index = index +
(intermediateBufferSize <= bytesLeftToWrite ? intermediateBufferSize : bytesLeftToWrite);
pCam->FileAccessBuffer.Set(tmpBuffer.get(), tmpBufferSize);
if (intermediateBufferSize > bytesLeftToWrite)
{
pCam->FileAccessLength.SetValue(bytesLeftToWrite);
}
{
cout << "Writing to stream failed!" << endl;
break;
}
int64_t sizeWritten = pCam->FileOperationResult.GetValue();
PrintDebugMessage(
"File Access Offset: " + to_string(pCam->FileAccessOffset.GetValue()));
totalBytesWritten += sizeWritten;
"Bytes written: " + to_string(static_cast<long long>(totalBytesWritten)) + " of " +
to_string(static_cast<long long>(totalBytesToWrite)));
bytesLeftToWrite = totalBytesToWrite - totalBytesWritten;
PrintDebugMessage(
"Bytes left: " + to_string(
static_cast<long long>(bytesLeftToWrite)));
cout << "Progress : " << (i * 100 / writeIterations) << " %" << endl;
}
cout << "Writing complete" << endl;
{
cout << "Failed to close file!" << endl;
}
}
}
pCam->DeInit();
pCam = nullptr;
system->ReleaseInstance();
cout << endl << "Done! Press Enter to exit..." << endl;
getchar();
}
{
cout <<
"Unexpected exception : " << e.
what();
result = false;
}
return result;
}
{
bool result = true;
cout << "Opening file for reading..." << endl;
try
{
pCamera->FileOperationSelector.SetValue(FileOperationSelector_Open);
pCamera->FileOpenMode.SetValue(FileOpenMode_Read);
pCamera->FileOperationExecute.Execute();
if (pCamera->FileOperationStatus.GetValue() != FileOperationStatus_Success)
{
cout << "Failed to open file for reading!" << endl;
return false;
}
}
{
cout <<
"Unexpected exception : " << e.
what() << endl;
result = false;
}
return result;
}
{
bool result = true;
try
{
pCamera->FileOperationSelector.SetValue(FileOperationSelector_Read);
pCamera->FileOperationExecute.Execute();
if (pCamera->FileOperationStatus.GetValue() != FileOperationStatus_Success)
{
cout << "Failed to read file!" << endl;
return false;
}
}
{
cout <<
"Unexpected exception : " << e.
what() << endl;
result = false;
}
return result;
}
{
bool result = true;
{
return false;
}
const LibraryVersion spinnakerLibraryVersion = system->GetLibraryVersion();
cout <<
"Spinnaker library version: " << spinnakerLibraryVersion.
major <<
"." << spinnakerLibraryVersion.
minor
<<
"." << spinnakerLibraryVersion.
type <<
"." << spinnakerLibraryVersion.
build << endl
<< endl;
try
{
INodeMap& nodeMapTLDevice = pCam->GetTLDeviceNodeMap();
pCam->Init();
cout << endl << "*** DOWNLOADING IMAGE ***" << endl;
if (pCam->FileSelector == NULL)
{
cout << "File selector not supported on device!";
return false;
}
pCam->FileSelector.GetEntries(selectorList);
for (unsigned int i = 0; i < selectorList.size(); i++)
{
{
cout << node->GetSymbolic() << " not supported!" << endl;
continue;
}
{
pCam->FileSelector.SetIntValue((int64_t)node->GetNumericValue());
int64_t bytesToRead = pCam->FileSize.GetValue();
if (bytesToRead == 0)
{
cout << "No data available to read!" << endl;
continue;
}
PrintDebugMessage(
"Total data to download : " + to_string(
static_cast<long long>(bytesToRead)));
{
cout << "Failed to open file!" << endl;
continue;
}
if (pCam->FileAccessLength.GetValue() < pCam->FileAccessBuffer.GetLength())
{
try
{
pCam->FileAccessLength.SetValue(pCam->FileAccessBuffer.GetLength());
}
{
cout <<
"Unable to set FileAccessLength to FileAccessBuffer length : " << e.
what() << endl;
}
}
pCam->FileAccessOffset.SetValue(0);
int64_t intermediateBufferSize = pCam->FileAccessLength.GetValue();
int64_t iterations =
(bytesToRead / intermediateBufferSize) + (bytesToRead % intermediateBufferSize == 0 ? 0 : 1);
int64_t index = 0;
int64_t totalSizeRead = 0;
std::unique_ptr<unsigned char> dataBuffer(new unsigned char[static_cast<unsigned int>(bytesToRead)]);
uint8_t* pData = dataBuffer.get();
for (unsigned int i = 0; i < iterations; i++)
{
{
cout << "Reading stream failed!" << endl;
break;
}
int64_t sizeRead = pCam->FileOperationResult.GetValue();
pCam->FileAccessBuffer.Get(&pData[index], sizeRead);
index = index + sizeRead;
totalSizeRead += sizeRead;
"Bytes read: " + to_string(static_cast<long long>(totalSizeRead)) + " of " +
to_string(static_cast<long long>(bytesToRead)));
cout << "Progress : " << (i * 100 / iterations) << " %" << endl;
}
{
cout << "Failed to close file!" << endl;
}
cout << endl;
ostringstream filename;
filename << "DeviceStreamRead-";
if (pCam->DeviceSerialNumber != NULL)
{
filename << pCam->DeviceSerialNumber.GetValue().c_str() << "-";
}
filename << ".bmp";
CEnumEntryPtr ptrPixelFormat = pCam->PixelFormat.GetEntry(PixelFormat_Mono8);
{
pCam->PixelFormat.SetValue(PixelFormat_Mono8);
}
else
{
pCam->PixelFormat.SetValue(PixelFormat_BayerGB8);
}
const int64_t width = pCam->Width.GetValue();
const int64_t height = pCam->Height.GetValue();
const int64_t offSetX = pCam->OffsetX.GetValue();
const int64_t offSetY = pCam->OffsetX.GetValue();
cout << "Width : " << width << endl;
cout << "Height : " << height << endl;
cout << "OffSetX : " << offSetX << endl;
cout << "OffSetY : " << offSetY << endl;
static_cast<size_t>(width),
static_cast<size_t>(height),
static_cast<size_t>(offSetX),
static_cast<size_t>(offSetY),
pixelFormat,
pData);
{
try
{
cout << "\nSaving raw image to disk for debugging purpose..." << endl;
ostringstream filename;
filename << "downloaded_output.bmp";
pImage->Save(filename.str().c_str());
cout << "Image saved at " << filename.str() << endl;
}
{
cout <<
"Unable to write image data to the PC : " << e.
what() << endl;
}
}
pImage->Save(filename.str().c_str());
cout << "Image saved at " << filename.str() << endl;
cout << "\n*** SAVING IMAGE ***\n";
}
}
pCam->DeInit();
pCam = nullptr;
system->ReleaseInstance();
cout << endl << "Done! Press Enter to exit..." << endl;
getchar();
}
{
cout <<
"Unexpected exception : " << e.
what() << endl;
result = false;
}
return result;
}
{
cout << "Usage: FileAcess </u | /d>" << endl;
cout << "Options:" << endl;
cout << "/u : Grab an image and store it on camera." << endl;
cout << "/d : Download saved image from camera and store it on desktop." << endl;
cout << "/v : Enable verbose output." << endl;
cout << "/? : Print usage informaion." << endl;
cout << endl << endl;
}
int main(
int argc,
char* argv[])
{
FILE* tempFile = fopen("test.txt", "w+");
if (tempFile == nullptr)
{
cout << "Failed to create file in current folder. Please check "
"permissions."
<< endl;
cout << "Press Enter to exit..." << endl;
getchar();
return -1;
}
fclose(tempFile);
remove("test.txt");
int result = 0;
cout << "Application build date: " << __DATE__ << " " << __TIME__ << endl << endl;
std::vector<std::string> args(argv, argv + argc);
if (args.size() == 1)
{
return result;
}
bool uploadImage = false;
bool downloadImage = false;
for (size_t i = 1; i < args.size(); ++i)
{
if (args[i] == "/v" || args[i] == "/V")
{
}
if (args[i] == "?")
{
return result;
}
if (args[i] == "/u" || args[i] == "/U")
{
uploadImage = true;
}
if (args[i] == "/d" || args[i] == "/D")
{
downloadImage = true;
}
}
if (uploadImage)
{
{
result = -1;
}
return result;
}
if (downloadImage)
{
{
result = -1;
}
return result;
}
return result;
}
int AcquireImages(CameraPtr pCam, INodeMap &nodeMap, INodeMap &nodeMapTLDevice)
Definition Acquisition.cpp:199
int main(int, char **)
Definition Acquisition.cpp:527
int PrintDeviceInfo(INodeMap &nodeMap)
Definition Acquisition.cpp:441
const unsigned int k_numImages
Definition AcquisitionMultipleCamerasWriteToFile.cpp:55
bool InitializeSystem(SystemPtr &system, CameraList &camList, CameraPtr &pCam)
Definition FileAccess_QuickSpin.cpp:104
bool ExecuteWriteCommand(CameraPtr pCam)
Definition FileAccess_QuickSpin.cpp:407
bool ExecuteDeleteCommand(CameraPtr pCam)
Definition FileAccess_QuickSpin.cpp:356
bool CloseFile(CameraPtr pCam)
Definition FileAccess_QuickSpin.cpp:431
void PrintUsage()
Definition FileAccess_QuickSpin.cpp:1019
static void PrintDebugMessage(string msg)
Definition FileAccess_QuickSpin.cpp:154
bool OpenFileToRead(CameraPtr pCamera)
Definition FileAccess_QuickSpin.cpp:734
bool UploadImage()
Definition FileAccess_QuickSpin.cpp:456
static bool _enableDebug
Definition FileAccess_QuickSpin.cpp:47
static gcstring _fileSelector
Definition FileAccess_QuickSpin.cpp:48
static void PrintResultMessage(bool result)
Definition FileAccess_QuickSpin.cpp:51
bool OpenFileToWrite(CameraPtr pCam)
Definition FileAccess_QuickSpin.cpp:381
bool ExecuteReadCommand(CameraPtr pCamera)
Definition FileAccess_QuickSpin.cpp:759
bool DownloadImage()
Definition FileAccess_QuickSpin.cpp:783
Used to hold a list of camera objects.
Definition CameraList.h:42
void Clear()
Clears the list of cameras and destroys their corresponding reference counted objects.
CameraPtr GetByIndex(unsigned int index) const
Returns a pointer to a camera object at the "index".
unsigned int GetSize() const
Returns the size of the camera list.
A reference tracked pointer to a camera object.
Definition CameraPtr.h:44
The Exception object represents an error that is returned from the library.
Definition Exception.h:51
virtual const char * what() const
virtual override for what().
Encapsulates a GenApi pointer dealing with the dynamic_cast automatically.
Definition Pointer.h:75
A reference tracked pointer to an image object.
Definition ImagePtr.h:46
A reference tracked pointer to a system object.
Definition SystemPtr.h:44
PixelFormatEnums
Definition CameraDefs.h:946
bool IsWritable(EAccessMode AccessMode)
Tests if writable.
Definition INode.h:277
bool IsReadable(EAccessMode AccessMode)
Tests if readable.
Definition INode.h:253
interface SPINNAKER_API_ABSTRACT INodeMap
Interface to access the node map.
Definition INodeMap.h:54
node_vector NodeList_t
a list of node references
Definition INode.h:54
Definition Autovector.h:36
Provides easier access to the current version of Spinnaker.
Definition SpinnakerDefs.h:658
unsigned int minor
Minor version of the library.
Definition SpinnakerDefs.h:663
unsigned int major
Major version of the library.
Definition SpinnakerDefs.h:660
unsigned int type
Version type of the library.
Definition SpinnakerDefs.h:666
unsigned int build
Build number of the library.
Definition SpinnakerDefs.h:669