📄 realtimedemo.cpp
字号:
}
/// static thread function caller
static DWORD WINAPI ThreadMain(LPVOID lpData)
{
// call the run function in the obcet's context
CGrabThread *This = (CGrabThread*)lpData;
return This->Run();
}
/// thread function
DWORD Run()
{
int LastPixelValue = -1;
long BufferCounter = 0;
int exitcode = 0;
try
{
// set the thread's priority to the desired value
m_pBcam->SetCurrentThreadPriority(GrabThreadPriority);
printf("GrabThreadPriority= %d\n", GrabThreadPriority);
// start the camera
m_pBcam->ContinuousShot = true;
// signal that we are going to grab
SetEvent(m_Running);
m_StopWatch.Start();
while( BufferCounter < NumGrabs )
{
// Wait for something to arrive at the completion port
FunctionCode_t FunctionCode;
unsigned long ErrorCode;
void *pContext;
m_pBcam->WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 5000);
if ( ErrorCode != 0 )
{
printf("Error: %d\n", ErrorCode);
exitcode = 1;
break;
}
if ( FunctionCode == AsyncGrabImage )
{
// Take the time
double ElapsedTime = m_StopWatch.Stop(true);
m_OverallStatistics.AddSample(ElapsedTime);
m_Statistics.AddSample(ElapsedTime);
// Grab Counter
++BufferCounter;
if ( BufferCounter % NumSamples == 0 )
{
// print out statistics for the last NumSamples buffers
printf("%3d-%3d: Min=%6.2f ms (%+3.1f%%) Mean=%6.2f ms (%4.1f Hz) Max=%6.2f ms (%+3.1f%%)\n",
BufferCounter - NumSamples, BufferCounter - 1,
1000.0 * m_Statistics.GetMin(),
100.0 * ((m_Statistics.GetMin()/ m_Statistics.GetMean()) - 1),
1000.0 * m_Statistics.GetMean(),
1.0 / m_Statistics.GetMean(),
1000.0 * m_Statistics.GetMax(),
100.0 * ((m_Statistics.GetMax()/ m_Statistics.GetMean()) - 1)
);
m_Statistics.Reset();
}
// Index of the current buffer
int i = (int)pContext;
// Value of the fist pixel
BYTE CurrentPixelValue = *(m_ppBuffers[i]);
// check for missing images
if(CheckMissingImages && LastPixelValue != -1 )
{
int delta = (256 + CurrentPixelValue - LastPixelValue) % 256;
if ( delta == 0 )
{
if ( BufferCounter < 3 )
printf("==> WARNING : Wrong test image. See definition of constant 'TestImage'!\n");
}
else if ( delta != 1 )
{
printf("==> WARNING : missed %d images\n", delta - 1);
}
}
LastPixelValue = CurrentPixelValue;
// re-queue the buffer
m_pBcam->GrabImageAsync(m_ppBuffers[i], m_ImageBufferSize, (void*)i, false);
}
}
// print the overall statistics
printf("=============================================================================\n");
printf("Overall Statistics:\n");
printf("Min =%6.2f ms (%+3.1f%%) Mean =%6.2f ms (%4.1f Hz) Max =%6.2f ms (%+3.1f%%)\n",
1000.0 * m_OverallStatistics.GetMin(),
100.0 * ((m_OverallStatistics.GetMin()/ m_OverallStatistics.GetMean()) - 1),
1000.0 * m_OverallStatistics.GetMean(),
1.0 / m_OverallStatistics.GetMean(),
1000.0 * m_OverallStatistics.GetMax(),
100.0 * ((m_OverallStatistics.GetMax()/ m_OverallStatistics.GetMean()) - 1)
);
}
catch ( BcamException&e )
{
printf("Bcam Exception caught in grab thread: %s (%d)\n", e.Description(), e.Error() );
exitcode = 1;
}
// Signal that we have finished
SetEvent(m_Finished);
::printf("%s\n", m_OutputBuffer);
return exitcode;
}
/// thread handle
HANDLE m_hThread;
/// thread ID
DWORD m_ThreadId;
/// The BCAM object
CBcam* m_pBcam;
/// The buffers
BYTE **m_ppBuffers;
/// Size of the imagebuffers
unsigned long m_ImageBufferSize;
/// For time measurement
CStopWatch m_StopWatch;
/// For time evaluation
CStatistics m_OverallStatistics;
CStatistics m_Statistics;
/// Buffer to recieve the output of the grab thread
char* m_OutputBuffer;
/// Synchronization objects
HANDLE m_Running, m_Finished;
};
/// Main entry point of the application
void main()
{
try
{
if ( CBcam::DeviceNames().size() == 0 )
{
printf("No camera devices found");
return;
}
CString DeviceName = *(CBcam::DeviceNames().begin());
// Create the driver object and open the driver
CBcam Bcam;
Bcam.Open(DeviceName);
// This yields a max. resolution Mono8/Bayer8 image
Bcam.SetVideoMode(DCS_Format7, DCS_Mode0);
// Make sure the camera will run at full speed
Bcam.FormatSeven[DCS_Mode0].Size = Bcam.FormatSeven[DCS_Mode0].MaxSize();
Bcam.FormatSeven[DCS_Mode0].BytePerPacket = Bcam.FormatSeven[DCS_Mode0].BytePerPacket.Max();
Bcam.Shutter.Raw = Bcam.Shutter.Raw.Min();
// Make the camera show walking stripes
Bcam.TestImage = Testimage;
// Create the image buffers
CSize ImageSize = Bcam.FormatSeven[DCS_Mode0].Size();
unsigned long ImageBufferSize = ImageSize.cx * ImageSize.cy;
PBYTE* ppBuffers = new PBYTE[NumImageBuffers];
for ( int i = 0; i < NumImageBuffers; ++i )
ppBuffers[i] = new BYTE[ImageBufferSize];
printf("NumImageBuffers = %d\n", NumImageBuffers);
printf("ImageSize : W = %d, H = %d\n",ImageSize.cx, ImageSize.cy);
// Allocate Resources (MaxBuffers, MaxBufferSize)
Bcam.AllocateResources(NumImageBuffers, ImageBufferSize);
// Set the priority of the current thread. The grab thread will set its priority himself
int p = GetThreadPriority(GetCurrentThread());
int c = GetPriorityClass(GetCurrentProcess());
SetPriorityClass(ProcessPriorityClass);
SetCurrentThreadPriority(MainThreadPriority);
// Because we are lifting the priority level of the grab thread up to the realtime priority class,
// we must ensure that we don't slow down the driver's queue server thread. Ensure that the queue server
// thread runs on a higher priority level than our thread
unsigned long QueueServerPriority = Bcam.GetQueueServerPriority();
if ( QueueServerPriority <= GrabThreadPriority )
{
QueueServerPriority = GrabThreadPriority + 1;
if ( QueueServerPriority > 31 )
QueueServerPriority = 31;
Bcam.SetQueueServerPriority(QueueServerPriority);
}
printf("QueueServerPriority= %d\n", QueueServerPriority);
printf("Check for missing images %s\n", CheckMissingImages ? "enabled" : "disabled");
printf("\nPlease stand by while grabbing %d images....\n", NumGrabs);
// Enqueue the buffers for grabbing
for(int i=0; i<NumImageBuffers; ++i)
{
Bcam.GrabImageAsync(ppBuffers[i], ImageBufferSize, (void*)i, false);
}
// Create some events for synchronization purposes
CEvent finished; // the thread will signal the event when it finishes the acquisition
finished.Create();
CEvent running; // the thread will signal the event when it enter its run method
running.Create();
// Create and start the thread.
CGrabThread Thread(&Bcam, ppBuffers, ImageBufferSize, running, finished );
Thread.Create();
WaitForSingleObject(running, INFINITE); // give the thread a chance to enter its run method
// ... now the buffers are circulating via the thread function ...
// As long the thread is grabbing, make some CPU stress
while ( WaitForSingleObject(finished, 0) != WAIT_OBJECT_0 )
{
// make Stress
for(long i=0; i<Loops; ++i)
{
long j = i*i;
}
// reduce stress
if ( SleepTime > 0 )
::Sleep(SleepTime);
}
Thread.WaitForDeath(INFINITE);
// clean up
Bcam.ContinuousShot=false;
Bcam.Close();
for ( int i = 0; i < NumImageBuffers; ++i )
delete[] ppBuffers[i];
delete[] ppBuffers;
} catch( BcamException &e )
{
printf( "Exception occurred 0x%X:\n", e.Error() );
printf( "Message: %s\n", (LPCSTR) e.Description() );
printf( "Context: %s\n", (LPCSTR) e.Context() );
}
fprintf(stderr, "Press a key to exit\n");
_getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -