📄 samplecode.cpp
字号:
//-----------------------------------------------------------------------------
// (c) 2002 by Basler Vision Technologies
// Section: Vision Components
// Project: BCAM
// $Header: SampleCode.cpp, 6, 24.06.2004 13:28:07, Moebius, V.$
//-----------------------------------------------------------------------------
/**
\file SampleCode.cpp
\brief Action Command handlers
*/
#include "stdafx.h"
#include "resource.h"
#include "MainFrame.h"
#include <bcam.h>
using namespace Bcam;
LRESULT CMainFrame::OnCreateBitmap(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// 640(W) x 480(H), 8 bpp, monochrome
CDibPtr ptrDib;
ptrDib.Create( CSize( 640, 480 ), 8, CDib::TopDown, CDib::Monochrome ),
m_view.SetBitmap( ptrDib );
return 0;
}
LRESULT CMainFrame::OnAccessPixels(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
if (m_view.m_ptrBitmap.IsNull())
return 0;
// The DibSection containes everything there is to know about a Windows bitmap
CDibPtr ptrDib = m_view.m_ptrBitmap;
// Write a fancy pattern to the image :-)
if (ptrDib->GetBitsPerPixel() == 8)
{
char *PtrLine = (char*) ptrDib->GetPixels();
for(long y=0; y<ptrDib->GetSize().cy; ++y)
{
char *Ptr = PtrLine;
for(long x=0; x<ptrDib->GetSize().cx; ++x)
*(Ptr++) = (char)((x + y)%256);
PtrLine += ptrDib->GetWidthBytes();
}
}
else if (ptrDib->GetBitsPerPixel() == 24)
{
RGBTRIPLE *PtrLine = (RGBTRIPLE*) ptrDib->GetPixels();
RGBTRIPLE *Ptr;
CSize sz = ptrDib->GetSize();
for(long y=0; y<sz.cy; ++y)
{
Ptr = PtrLine;
for(long x=0; x<sz.cx; ++x)
{
Ptr->rgbtRed = (char)((x + y)%256);
Ptr->rgbtGreen = (char)((256 * x + y)%256);
Ptr->rgbtBlue = (char)((x + 256 * y)%256);
++Ptr;
}
PtrLine = (RGBTRIPLE*)((char*)PtrLine + ptrDib->GetWidthBytes());
}
}
else
{
::Beep(5000, 500);
}
m_view.Invalidate();
return 0;
}
LRESULT CMainFrame::OnLoadFromFile(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CFileDialog dlg(TRUE, _T("bmp"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Bitmap Files (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0"), m_hWnd);
if(dlg.DoModal() == IDOK)
{
CDibPtr ptrDib;
ptrDib.LoadBMP( dlg.m_szFileName );
m_view.SetBitmap( ptrDib );
}
return 0;
}
LRESULT CMainFrame::OnSaveToFile(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
if( m_view.m_ptrBitmap.IsNull())
return 0;
CFileDialog dlg(FALSE, _T("bmp"), _T("Temp.bmp"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Bitmap Files (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0"), m_hWnd);
if(dlg.DoModal() == IDOK)
{
if (m_view.m_ptrBitmap->GetOrientation() == CDib::TopDown)
{
m_view.m_ptrBitmap->Topple();
}
m_view.m_ptrBitmap->StoreBMP( dlg.m_szFileName );
}
return 0;
}
LRESULT CMainFrame::OnEnumerateAllCameras(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// Link with Setupapi.lib and bcamapi.lib
CString Buffer(_T("Devices found :\n"));
std::list<CString> DeviceNames = CBcam::DeviceNames();
std::list<CString>::iterator pName;
for(pName = DeviceNames.begin(); pName != DeviceNames.end(); pName++)
{
Buffer += *pName + "\n";
}
MessageBox(Buffer, _T("EnumerateAllCameras"), MB_OK | MB_ICONINFORMATION);
ATLTRACE(Buffer);
return 0;
}
LRESULT CMainFrame::OnGrabSingleImage(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)
Bcam.AllocateResources(1, 640*480 );
// Grab the image with 3 sec timeout
char pBuffer[640*480];
Bcam.GrabImage(pBuffer, 640*480, 3000);
}
/**** Elaborate ***/
// Use exception handling
try
{
// Get the devicename of the first camera
// Make sure there is at least one 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 );
// This is the parameter set for : 640 x 480, Mono8, 7.5 fps
const DCSVideoFormat VideoFormat = DCS_Format0;
const DCSVideoMode VideoMode = DCS_Mode5;
const DCSVideoFrameRate VideoFrameRate = DCS_7_5fps;
// Get information about this parameter set through helper class BcamUtiltiy
CString VideoResolutionName = BcamUtility::VideoResolutionName(VideoFormat, VideoMode);
CString VideoFrameRateName = BcamUtility::VideoFrameRateName(VideoFrameRate);
DCSColorCode ColorCode = BcamUtility::ColorCode(VideoFormat, VideoMode);
CString ColorCodeName = BcamUtility::ColorCodeName(ColorCode);
unsigned short BitsPerPixel = BcamUtility::BitsPerPixel(ColorCode);
CSize ImageSize = BcamUtility::ImageSize(VideoFormat, VideoMode);
unsigned long BytePerPacket = BcamUtility::BytePerPacket(VideoFormat, VideoMode, VideoFrameRate);
// Make sure this parameters set ist supported
ATLASSERT(Bcam.IsVideoModeSupported(VideoFormat));
ATLASSERT(Bcam.IsVideoModeSupported(VideoFormat, VideoMode));
ATLASSERT(Bcam.IsVideoModeSupported(VideoFormat, VideoMode, VideoFrameRate));
// Set the parameters
Bcam.SetVideoMode(VideoFormat, VideoMode, VideoFrameRate);
// Tell the user
CString Buffer, B;
Buffer += (B.Format("VideoResolution \t= %s\n", VideoResolutionName ), B);
Buffer += (B.Format("FrameRate \t= %s\n", VideoFrameRateName ), 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("BytePerPacket \t= %lu\n", BytePerPacket ), B);
MessageBox(Buffer, _T("OnGrabSingleImage"), MB_OK | MB_ICONINFORMATION);
// Create suitable a bitmap
// Beware : The camera sends the bitmap top-down (= top line first)
// so the image's hight must be negative
CDibPtr ptrDib;
ptrDib.Create( ImageSize,8, CDib::TopDown, CDib::Monochrome );
char *pBuffer = (char*) ptrDib->GetPixels();
// Allocate Resources (max number of buffers, max buffer size)
Bcam.AllocateResources(1, ImageSize.cx * ImageSize.cy );
// Grab the image with 3 sec. timeout
Bcam.GrabImage(pBuffer, ImageSize.cx * ImageSize.cy, 3000);
// Free Resources (optional)
Bcam.FreeResources();
// Close the driver (optional)
Bcam.Close();
// Show the image
m_view.SetBitmap( ptrDib );
} catch( BcamException &e )
{
// Show Errormessage
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::OnSetGain(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 );
// Get and Set the gain via the raw gain register
unsigned long Gain = Bcam.Gain.Raw();
Bcam.Gain.Raw = Gain;
}
/**** 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 );
// Check, if the feature is supported
ATLASSERT( Bcam.Gain.IsSupported() );
// Get the feature's range, read it and set it
unsigned long Min = Bcam.Gain.Raw.Min();
unsigned long Max = Bcam.Gain.Raw.Max();
unsigned long New = (Min + Max) >> 1;
unsigned long Old = Bcam.Gain.Raw();
Bcam.Gain.Raw = New;
// Tell the user
CString FeatureName = BcamUtility::FeatureName(FeatureID_Gain);
CString Buffer, B;
Buffer += (B.Format("Feature \t= %s\n", FeatureName ), B);
Buffer += (B.Format("Min \t= %lu\n", Min ), B);
Buffer += (B.Format("Max \t= %lu\n", Max ), B);
Buffer += (B.Format("Old \t= %lu\n", Old ), B);
Buffer += (B.Format("New \t= %lu\n", New ), B);
MessageBox(Buffer, _T("OnSetGain : Raw Values"), MB_OK | MB_ICONINFORMATION);
if( Bcam.Gain.IsSupported(inqAbsControl) )
{
// Switch to abs values
Bcam.Gain.AbsControl = true;
// Get the feature's range, read it and set it
double Min = Bcam.Gain.Abs.Min();
double Max = Bcam.Gain.Abs.Max();
double New = (Min + Max) / 2.0;
double Old = Bcam.Gain.Abs();
Bcam.Gain.Abs = New;
// Tell the user
CString FeatureName = BcamUtility::FeatureName(FeatureID_Gain);
CString Buffer, B;
Buffer += (B.Format("Feature \t= %s\n", FeatureName ), B);
Buffer += (B.Format("Min \t= %f\n", Min ), B);
Buffer += (B.Format("Max \t= %f\n", Max ), B);
Buffer += (B.Format("Old \t= %f\n", Old ), B);
Buffer += (B.Format("New \t= %f\n", New ), B);
MessageBox(Buffer, _T("OnSetGain : Abs Values"), MB_OK | MB_ICONINFORMATION);
// Switch abs values off since the camera will remember
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -