📄 samplecode.cpp
字号:
unsigned long BytePerFrame = Bcam.FormatSeven[VideoMode].BytePerFrame();
unsigned long PixelPerFrame = Bcam.FormatSeven[VideoMode].PixelPerFrame();
unsigned long PacketsPerFrame = Bcam.FormatSeven[VideoMode].PacketsPerFrame();
unsigned long PaddingBytes = (BytePerPacket*PacketsPerFrame) - (BitsPerPixel*PixelPerFrame/8);
// Tell the user
CString Buffer, B;
Buffer += (B.Format("VideoFormat \t= %lu\n", VideoFormat), B);
Buffer += (B.Format("VideoMode \t= %lu\n", VideoMode), B);
Buffer += (B.Format("ColorCode \t= %s\n", ColorCodeName ), B);
Buffer += (B.Format("BitsPerPixel \t= %u\n", BitsPerPixel ), B);
Buffer += (B.Format("ImageSize \t= %lu x %lu\n", ImageSize.cx, ImageSize.cy ), B);
Buffer += (B.Format("BytePerFrame \t= %lu\n", BytePerFrame ), B);
Buffer += (B.Format("PixelPerFrame \t= %lu\n", PixelPerFrame ), B);
Buffer += (B.Format("BytePerPacket \t= %lu\n", BytePerPacket ), B);
Buffer += (B.Format("PacketsPerFrame \t= %lu\n", PacketsPerFrame ), B);
Buffer += (B.Format("PaddingBytes \t= %lu\n", PaddingBytes ), B);
MessageBox(Buffer, _T("OnGrabFormat7"), MB_OK | MB_ICONINFORMATION);
// Create a suitable bitmap
CDibPtr ptrDib;
ptrDib.Create( AoiSize, 8, CDib::TopDown, CDib::Monochrome );
// Get a pointer to the image buffer
char *pBuffer = (char*) ptrDib->GetPixels();
// Allocate Resources (max num buffers, MaxImageSize
Bcam.AllocateResources(1, AoiSize.cx * AoiSize.cy );
// Grab the image
Bcam.GrabImage(pBuffer, AoiSize.cx * AoiSize.cy, 3000); // timeout 3 sec.
// Show the image
m_view.SetBitmap( ptrDib );
} catch( BcamException &e )
{
CString Buffer, B;
Buffer += (B.Format("Exception 0x%X occurred\n", e.Error() ), B);
Buffer += (B.Format("Message = %s\n", e.Description() ), B);
Buffer += (B.Format("Context = %s\n", e.Context()), B);
MessageBox(Buffer, _T("OnGrabSingleImage"), MB_OK | MB_ICONEXCLAMATION);
}
return 0;
}
LRESULT CMainFrame::OnGrabNonBlocking(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
/**** Simple ***/
{
// Get the devicename of the first camera
std::list<CString> DeviceNames = CBcam::DeviceNames();
if (DeviceNames.size() == 0) return 0;
CString DeviceName = *(DeviceNames.begin());
// Create the driver object and open the driver
CBcam Bcam;
Bcam.Open( DeviceName );
// Parametrize camera for 640 x 480, Mono8, 7.5 fps
Bcam.SetVideoMode(DCS_Format0, DCS_Mode5, DCS_7_5fps);
// Allocate Resources (max number of buffers, max buffer size)
// Note that max num buffers = 2 since we're going to enqueue two grab commands
Bcam.AllocateResources(2, 640*480 );
// Enqueue two grab commands
char pBuffer1[640*480];
char pBuffer2[640*480];
Bcam.GrabImageAsync(pBuffer1, 640*480);
Bcam.GrabImageAsync(pBuffer2, 640*480);
// Do something else...
// Wait for the commands to finish
FunctionCode_t FunctionCode;
unsigned long ErrorCode;
void *pContext;
Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 3000); // timeout 3 sec.
ATLTRACE("Buffer1 : Errorcode = 0x%x\n", ErrorCode);
Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 3000); // timeout 3 sec.
ATLTRACE("Buffer2 : Errorcode = 0x%x\n", ErrorCode);
}
/**** Elaborate ***/
// Use exception handling
try
{
// Get the devicename of the first camera
ATLASSERT(CBcam::DeviceNames().size() > 0);
CString DeviceName = *(CBcam::DeviceNames().begin());
ATLTRACE(_T("Devicename = %s\n"), DeviceName);
// Create the driver object and open the driver
CBcam Bcam;
Bcam.Open( DeviceName );
// Parameter set for : 640 x 480, Mono8, 7.5 fps
const DCSVideoFormat VideoFormat = DCS_Format0;
const DCSVideoMode VideoMode = DCS_Mode5;
const DCSVideoFrameRate VideoFrameRate = DCS_15fps;
Bcam.SetVideoMode(VideoFormat, VideoMode, VideoFrameRate);
// Create suitable a bitmap
CSize ImageSize = BcamUtility::ImageSize(VideoFormat, VideoMode);
CDibPtr ptrDib;
ptrDib.Create( ImageSize, 8, CDib::TopDown, CDib::Monochrome );
// Get a pointer to the image buffer
char *pBuffer = (char*) ptrDib->GetPixels();
// Allocate Resources (max num buffers, MaxImageSize
Bcam.AllocateResources(1, ImageSize.cx * ImageSize.cy );
// Enqueue a SetGain command (non-blocking)
unsigned long Gain = Bcam.Gain.Raw();
Bcam.Gain.Raw.SetAsync(Gain);
// Enqueue a Grab command (non-blocking) AND a OneShot command
void *pContext = pBuffer;
Bcam.GrabImageAsync(pBuffer, ImageSize.cx * ImageSize.cy, pContext);
// Wait for the three enqueued commands to succeed
for(int i=0;i<3;++i)
{
FunctionCode_t FunctionCode;
unsigned long ErrorCode;
void *pContext;
Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 3000); // timeout 3 sec.
ATLASSERT(!ErrorCode);
switch(FunctionCode)
{
case AsyncGainRawSet:
case AsyncOneShot:
break;
case AsyncGrabImage:
ATLASSERT(pContext == pBuffer);
break;
default:
ATLASSERT(false);
}
}
// Show the image
m_view.SetBitmap( ptrDib );
} catch( BcamException &e )
{
CString Buffer, B;
Buffer += (B.Format("Exception 0x%X occurred\n", e.Error() ), B);
Buffer += (B.Format("Message = %s\n", e.Description() ), B);
Buffer += (B.Format("Context = %s\n", e.Context()), B);
MessageBox(Buffer, _T("OnGrabSingleImage"), MB_OK | MB_ICONEXCLAMATION);
}
return 0;
}
LRESULT CMainFrame::OnGrabWithHardwareTrigger(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// Use exception handling
try
{
// Get the devicename of the first camera
std::list<CString> DeviceNames = CBcam::DeviceNames();
if (DeviceNames.size() == 0) return 0;
CString DeviceName = *(DeviceNames.begin());
// Create the driver object and open the driver
CBcam Bcam;
Bcam.Open( DeviceName );
// Parametrize camera for 640 x 480, Mono8, 7.5 fps
Bcam.SetVideoMode(DCS_Format0, DCS_Mode5, DCS_7_5fps);
// Enable hardware trigger
Bcam.Trigger.OnOff = true;
if(Bcam.Trigger.IsModeSupported(DCS_TriggerMode0))
Bcam.Trigger.Mode = DCS_TriggerMode0;
if(Bcam.Trigger.IsSupported(inqPolarity))
Bcam.Trigger.Polarity = HighActive;
// Allocate Resources (max number of buffers, max buffer size)
Bcam.AllocateResources(1, 640*480 );
// Enqueue grab commands
CDibPtr ptrDib;
ptrDib.Create( CSize( 640, 480 ), 8, CDib::TopDown, CDib::Monochrome );
// Get a pointer to the image buffer
char *pBuffer = (char*) ptrDib->GetPixels();
Bcam.GrabImageAsync(pBuffer, 640*480);
// Wait 5 sec. for the hardware trigger to arrive
Sleep(5000);
// Check if the trigger has arrived
FunctionCode_t FunctionCode;
unsigned long ErrorCode;
void *pContext;
Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 0); // timeout 0
ATLASSERT(ErrorCode == 0 && FunctionCode == AsyncOneShot);
Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 0); // timeout 0
if(!ErrorCode)
{
ATLASSERT(FunctionCode == AsyncGrabImage);
char *pBuffer = (char*)pContext;
// Process the pBuffer...
m_view.SetBitmap( ptrDib );
MessageBox("Trigger arrived", _T("OnGrabWithHardwareTrigger"), MB_OK | MB_ICONEXCLAMATION);
}
else if ( ErrorCode == WAIT_TIMEOUT )
{
// We don't believe in the HW trigger any more
// so we cancel the grab command
Bcam.Cancel();
// Wait for the cancelled command to arrive at the completion port
Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 10000); // timeout 10s
#pragma message("TODO : Change code for final version")
//ATLASSERT(ErrorCode == ERROR_OPERATION_ABORTED);
MessageBox("Trigger did not arrive", _T("OnGrabWithHardwareTrigger"), MB_OK | MB_ICONEXCLAMATION);
}
else
{
ATLASSERT(false);
}
Bcam.Trigger.OnOff = false;
} catch( BcamException &e )
{
CString Buffer, B;
Buffer += (B.Format("Exception 0x%X occurred\n", e.Error() ), B);
Buffer += (B.Format("Message = %s\n", e.Description() ), B);
Buffer += (B.Format("Context = %s\n", e.Context()), B);
MessageBox(Buffer, _T("OnGrabWithHardwareTrigger"), MB_OK | MB_ICONEXCLAMATION);
}
return 0;
}
LRESULT CMainFrame::OnGetInfo(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// Use exception handling
try
{
// Get the devicename of the first camera
std::list<CString> DeviceNames = CBcam::DeviceNames();
if (DeviceNames.size() == 0) return 0;
CString TheDeviceName = *(DeviceNames.begin());
// Create the driver object and open the driver
CBcam Bcam;
Bcam.Open( TheDeviceName );
// Parametrize camera for 640 x 480, Mono8, 7.5 fps
Bcam.SetVideoMode(DCS_Format0, DCS_Mode5, DCS_7_5fps);
// Allocate Resources (max number of buffers, max buffer size)
Bcam.AllocateResources(1, 640*480 );
// Get Information about the camera
CString DeviceName = Bcam.Info.DeviceName();
CString ModelName = Bcam.Info.ModelName();
CString VendorName = Bcam.Info.VendorName();
CString NodeId = Bcam.Info.NodeId();
// Get version information about the camera and the driver
CString DriverSoftwareVersion = Bcam.Info.DriverSoftwareVersion();
CString CameraDcamVersion;
switch (Bcam.Info.CameraDcamVersion())
{
case 0x100:
CameraDcamVersion = "1.04";
break;
case 0x101:
CameraDcamVersion = "1.20";
break;
case 0x102:
CameraDcamVersion = "1.30";
break;
default:
CameraDcamVersion = "Unknown";
}
CString CameraFirmwareVersion = Bcam.Info.CameraFirmwareVersion();
// Get information about the current connection to the camera
// This information changes with AllocateResources!
long IsoChannel = Bcam.Info.IsoChannel();
BcamIsoSpeed IsoSpeedEnum = Bcam.Info.IsoSpeed();
// Tell the user
CString IsoSpeed;
switch(IsoSpeedEnum)
{
case IsoSpeed_100:
IsoSpeed = "S100";
break;
case IsoSpeed_200:
IsoSpeed = "S200";
break;
case IsoSpeed_400:
IsoSpeed = "S400";
break;
case IsoSpeed_800:
IsoSpeed = "S800";
break;
case IsoSpeed_1600:
IsoSpeed = "S1600";
break;
case IsoSpeed_3200:
IsoSpeed = "S3200";
break;
case IsoSpeed_Unknown:
IsoSpeed = "Unknown";
break;
default:
ATLASSERT(false);
}
CString Buffer, B;
Buffer += (B.Format("DeviceName \t= %s\n", DeviceName), B);
Buffer += (B.Format("ModelName \t\t= %s\n", ModelName), B);
Buffer += (B.Format("VendorName \t= %s\n", VendorName ), B);
Buffer += (B.Format("NodeId \t\t= %s\n", NodeId ), B);
Buffer += (B.Format("\n"), B);
Buffer += (B.Format("DriverSoftwareVersion \t= %s\n", DriverSoftwareVersion ), B);
Buffer += (B.Format("CameraDcamVersion \t= %s\n", CameraDcamVersion ), B);
Buffer += (B.Format("CameraFirmwareVersion \t= %s\n", CameraFirmwareVersion ), B);
Buffer += (B.Format("\n"), B);
Buffer += (B.Format("IsoChannel \t\t= %ld\n", IsoChannel ), B);
Buffer += (B.Format("IsoSpeed \t\t= %s\n", IsoSpeed ), B);
MessageBox(Buffer, _T("OnGetInfo"), MB_OK | MB_ICONINFORMATION);
} catch( BcamException &e )
{
CString Buffer, B;
Buffer += (B.Format("Exception 0x%X occurred\n", e.Error() ), B);
Buffer += (B.Format("Message = %s\n", e.Description() ), B);
Buffer += (B.Format("Context = %s\n", e.Context()), B);
MessageBox(Buffer, _T("OnGrabSingleImage"), MB_OK | MB_ICONEXCLAMATION);
}
return 0;
}
LRESULT CMainFrame::OnGrabContinuously(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// Use exception handling
try
{
// Get the devicename of the first camera
std::list<CString> DeviceNames = CBcam::DeviceNames();
if (DeviceNames.size() == 0) return 0;
CString DeviceName = *(DeviceNames.begin());
ATLTRACE(_T("Devicename = %s\n"), DeviceName);
// Create the driver object and open the driver
CBcam Bcam;
Bcam.Open( DeviceName );
// Parameter set for : 640 x 480, Mono8, 7.5 fps
const DCSVideoFormat VideoFormat = DCS_Format0;
const DCSVideoMode VideoMode = DCS_Mode5;
const DCSVideoFrameRate VideoFrameRate = DCS_15fps;
Bcam.SetVideoMode(VideoFormat, VideoMode, VideoFrameRate);
// Create suitable a bitmap
CSize ImageSize = BcamUtility::ImageSize(VideoFormat, VideoMode);
CDibPtr ptrDib;
ptrDib.Create( ImageSize, 8, CDib::TopDown, CDib::Monochrome );
// Get a pointer to the image buffer
char *pBuffer = (char*)ptrDib->GetPixels();
// Allocate Resources (max num buffers, MaxImageSize
Bcam.AllocateResources(1, ImageSize.cx * ImageSize.cy);
Bcam.ContinuousShot = true;
Bcam.GrabImageAsync(pBuffer, ImageSize.cx * ImageSize.cy, (void*)pBuffer, false);
for( int i=0; i<10; i++)
{
unsigned long ErrorCode;
FunctionCode_t FunctionCode;
void *pContext;
Bcam.WaitForCompletion(&FunctionCode, &ErrorCode, &pContext, 3000);
if (ErrorCode)
throw BcamException( ErrorCode );
if (FunctionCode == AsyncGrabImage)
Bcam.GrabImageAsync(pContext, ImageSize.cx * ImageSize.cy, pContext, false);
}
// Turn continuous shot off
Bcam.ContinuousShot = false;
Bcam.Cancel();
// Show the image
m_view.SetBitmap( ptrDib );
} catch( BcamException &e )
{
CString Buffer, B;
Buffer += (B.Format("Exception 0x%X occurred\n", e.Error() ), B);
Buffer += (B.Format("Message = %s\n", e.Description() ), B);
Buffer += (B.Format("Context = %s\n", e.Context()), B);
MessageBox(Buffer, _T("OnGrabContinously"), MB_OK | MB_ICONEXCLAMATION);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -