📄 minigrabsample2.c
字号:
//-----------------------------------------------------------------------------
// (c) 2002 by Basler Vision Technologies
// Section: Vision Components
// Project: BCAM
// $Header: minigrabsample2.c, 3, 13.01.2003 13:41:15, Nebelung, H.$
//-----------------------------------------------------------------------------
/**
\file minigrabsample2.c
\brief grab a single image using kernel-mode calls
*/
#include <windows.h>
#include <setupapi.h> /* needed for device enumeration */
#define INITGUID /* if you have not installed the SDK move this line above #include <windows.h>*/
#include <memory.h>
#include <stdio.h>
#include <functions.h> /* BCAM 1394 driver interface */
typedef char *CSTRING;
static CSTRING GetFirstCamera( void );
int main( /*int argc, char* argv[]*/ )
{
DWORD numBytes = 0;
DWORD ret = 0;
OVERLAPPED overlapped;
OVERLAPPED imgOverlapped;
CSTRING DeviceName=NULL;
HANDLE hDevice=INVALID_HANDLE_VALUE;
ArgSetVideoFormatModeFrameRate ArgFM;
ArgAllocateResources argAR;
ResAllocateResources resAR;
ArgCameraControl argCC;
char *pBuffer =NULL;
const int ImageSize = 640 * 480;
overlapped.hEvent = NULL;
overlapped.Internal = 0;
overlapped.InternalHigh = 0;
overlapped.Offset = 0;
overlapped.OffsetHigh = 0;
/*
// Get the devicename of the first camera
*/
DeviceName = GetFirstCamera();
if (NULL == DeviceName)
return -1;
/*
// Create the driver object and open the driver
*/
hDevice = CreateFile(
DeviceName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, /* security */
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, /* non-blocking calls, OVERLAPPED structure is mandatory in each call */
NULL /* template */
);
if (INVALID_HANDLE_VALUE == hDevice)
return -2;
/*
// Setting VideoFormat=0, VideoMode=5 yields 640 x 480, Mono8
*/
ArgFM.FrameRate = 3;
ArgFM.VideoFormat = 0;
ArgFM.VideoMode = 5;
ret = DeviceIoControl( hDevice,
IOCTL_BCAM_SET_VIDEO_FORMAT_MODE_FRAME_RATE,
&ArgFM, sizeof ArgFM,
NULL, 0,
&numBytes,
&overlapped );
if (ret && ret != ERROR_IO_PENDING)
{
fprintf( stderr, "0x%X", GetLastError() );;
return -3;
}
if (! GetOverlappedResult( hDevice, &overlapped, &numBytes, TRUE ))
return -4;
/*
// Create image buffer
*/
pBuffer = (char *) malloc( ImageSize );
if (NULL == pBuffer)
return -5;
/*
// Allocate Resources (MaxBuffers, MaxBufferSize)
*/
argAR.nBytesPerPacket = 160 * 4;
argAR.nMaxBufferSize = ImageSize;
argAR.nNumberOfBuffers = 1;
ret = DeviceIoControl( hDevice,
IOCTL_BCAM_ALLOCATE_RESOURCES,
&argAR, sizeof argAR,
&resAR, sizeof resAR,
&numBytes,
&overlapped );
if (ret && ret != ERROR_IO_PENDING)
{
fprintf( stderr, "0x%X", GetLastError() );
return -6;
}
if (! GetOverlappedResult( hDevice, &overlapped, &numBytes, TRUE ))
{
fprintf( stderr, "0x%X", GetLastError() );
return -7;
}
/*
// Grab the image
*/
/* create a manual-reset event */
imgOverlapped.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
imgOverlapped.Internal = 0;
imgOverlapped.InternalHigh = 0;
imgOverlapped.Offset = 0;
imgOverlapped.OffsetHigh = 0;
ret = DeviceIoControl( hDevice,
IOCTL_BCAM_RECEIVE_DATA,
NULL, 0,
pBuffer, ImageSize,
&numBytes,
&imgOverlapped );
if (ret && ret != ERROR_IO_PENDING)
{
fprintf( stderr, "0x%X", GetLastError() );;
return -8;
}
argCC.Cmd = CamCmd_SingleGrab; /* DCAM: ONE_SHOT */
ret = DeviceIoControl( hDevice,
IOCTL_BCAM_CAMERA_CONTROL,
&argCC, sizeof argCC,
NULL, 0,
&numBytes,
&overlapped );
if (ret && ret != ERROR_IO_PENDING)
{
fprintf( stderr, "0x%X", GetLastError() );;
return -9;
}
/* get result of grab command (blocking) */
if (! GetOverlappedResult( hDevice, &overlapped, &numBytes, TRUE ))
{
fprintf( stderr, "0x%X", GetLastError() );;
return -10;
}
/* wait for the image to be grabbed (timeout = 3 sec) */
ret = WaitForSingleObject( imgOverlapped.hEvent, 3000 );
if (ret == WAIT_FAILED || ret == WAIT_TIMEOUT)
{
if (ret == WAIT_FAILED) fprintf( stderr, "0x%X", GetLastError() );
return -11;
}
/* get resulting image (non-blocking) */
if (! GetOverlappedResult( hDevice, &imgOverlapped, &numBytes, FALSE ))
{
fprintf( stderr, "0x%X", GetLastError() );;
return -12;
}
/*
// image processing...
*/
/*
// clean up
*/
free( pBuffer );
free( DeviceName );
return 0;
}
/* ---------------------------------------------------------------------------
// Function name : GetFirstCamera
// Description : Retrieve the device name of a camera supported by BCAM 1394 driver
// Return type : CSTRING - device name or NULL
// Remarks : The caller has to free memory after usage.
// ---------------------------------------------------------------------------
*/
CSTRING GetFirstCamera( void )
{
char * pdev = NULL;
GUID classguid = GUID_FILE_DEVICE_BCAM_1394;
HDEVINFO hDevInfo = SetupDiGetClassDevs( &classguid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT );
DWORD i = 0;
DWORD res = ERROR_SUCCESS;
SP_DEVICE_INTERFACE_DATA DevInterfaceData;
DevInterfaceData.cbSize = sizeof DevInterfaceData;
if (SetupDiEnumDeviceInterfaces( hDevInfo, NULL, &classguid, i, &DevInterfaceData ))
{
/*
// found a camera - now get the device name
*/
DWORD reqSize;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
/* get the buffer size */
SetupDiGetDeviceInterfaceDetail( hDevInfo, &DevInterfaceData, NULL, 0, &reqSize, NULL );
if (ERROR_INSUFFICIENT_BUFFER != (res = GetLastError()) )
return NULL;
/* allocate temporary buffer */
pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) _alloca( reqSize );
if (NULL == pDetail)
{
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return NULL;
}
/* get the name and copy it */
pDetail->cbSize = sizeof *pDetail;
if (SetupDiGetDeviceInterfaceDetail( hDevInfo, &DevInterfaceData, pDetail, reqSize, &reqSize, NULL ))
{
pdev = (char *) malloc( strlen( pDetail->DevicePath ) + 1 );
if (pdev)
strcpy( pdev, pDetail->DevicePath );
else
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
}
}
return pdev;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -