根据示例,跑通埃科光电线阵相机的采图流程。
/*
This example gives the user an insight on how to access features from a camera.
The program will retrieve a list of features and display their access mode, data type and value.
There is also an example of how to change the value of a feature.
*/
#include <stdlib.h>
#include <malloc.h>
#include <Windows.h>
#include <stdio.h>
#include <vector>
#include "IKapC.h"
#include <QDebug>
/*
* Global parameters
*/
ITKDEVICE g_hCamera = NULL;
ITKSTREAM g_hStream = NULL;
ITKBUFFER g_hBuffer;
char* g_saveFileName = "E:\\CodeCImage.bmp";
char* g_bufferData = NULL;
#define CHECK( errc ) if ( ITKSTATUS_OK != errc ) printErrorAndExit( errc )
/* This function can be used to configure camera, form more examples please see IKapC usage. */
void ConfigureCamera();
/* This function can be used to create stream and add buffer. */
void CreateStreamAndBuffer();
/* This function can be used to configure stream, form more examples please see IKapC usage. */
void ConfigureStream();
/* This function will be unregister callback. */
void UnRegisterCallback();
/* This function can be used to register the stream callback function. */
void IKAPC_CC cbStartOfStream(uint32_t eventType, void* pContext);
void IKAPC_CC cbEndOfStream(uint32_t eventType, void* pContext);
void IKAPC_CC cbOnEndOfFrame(uint32_t eventType, void* pContext);
void IKAPC_CC cbOnTimeOut(uint32_t eventType, void* pContext);
void IKAPC_CC cbOnFrameLost(uint32_t eventType, void* pContext);
/* This method demonstrates how to retrieve the IKapC error message
for the last failed function call. */
void printErrorAndExit(ITKSTATUS errc);
/* This function can be used to wait for user input at the end of the sample program. */
void pressEnterToExit();
int main(void)
{
ITKSTATUS res; /* Return value of IKapC methods. */
uint32_t numDevices = 0; /* Number of available devices. */
uint32_t devIndex = 0; /* Index of device to be opened. */
qDebug() << "Itek Console GigeVision Camera Features Example(C version)";
/* Before using any IKapC methods, the IKapC runtime must be initialized. */
res = ItkManInitialize();
CHECK(res);
qDebug() << "1";
/* List and configure camera. */
ConfigureCamera();
qDebug() << "2";
/* Create stream and add buffer .*/
CreateStreamAndBuffer();
qDebug() << "3";
/* Configure stream. */
ConfigureStream();
qDebug() << "4";
/* Grab images in continuous mode. */
res = ItkStreamStart(g_hStream, 1);
CHECK(res);
qDebug() << "5";
// 等20秒,让相机采取一帧图像
Sleep(20000);
//ItkBufferSave(g_hBuffer, g_saveFileName, ITKBUFFER_VAL_BMP);
/* Stop grab image. */
ItkStreamStop(g_hStream);
qDebug() << "6";
/* UnRegister callback. */
UnRegisterCallback();
qDebug() << "7";
/* Free stream and buffer. */
ItkStreamRemoveBuffer(g_hStream, g_hBuffer);
ItkBufferFree(g_hBuffer);
ItkDevFreeStream(g_hStream);
res = ItkDevClose(g_hCamera);
CHECK(res);
if (g_bufferData != NULL)
free(g_bufferData);
/* Shut down the pylon runtime system. Don't call any IKapC method after
calling ItkManTerminate(). */
ItkManTerminate();
pressEnterToExit();
qDebug() << "8";
return EXIT_SUCCESS;
}
/* This function can be used to configure camera, form more examples please see IKapC usage. */
void ConfigureCamera()
{
ITKSTATUS res = ITKSTATUS_OK;
uint32_t numCameras = 0;
/* Enumerate all camera devices. You must call
ItkManGetDeviceCount() before creating a device. */
res = ItkManGetDeviceCount(&numCameras);
qDebug() << "numCameras:" << numCameras;
CHECK(res);
if (numCameras == 0)
{
qDebug() << "No cameras found.";
ItkManTerminate();
pressEnterToExit();
exit(EXIT_FAILURE);
}
/* Open first CameraLink Camera. */
for (uint32_t i = 0; i < numCameras; i++)
{
ITKDEV_INFO di;
/* Get device info. */
res = ItkManGetDeviceInfo(i, &di);
//CHECK(res);
qDebug() << "Using camera: serial: "<< di.SerialNumber;
qDebug() << "name: " << di.FullName;
qDebug() << "interface: " << di.DeviceClass;
// Only use CameraLink camera with proper serial number
if (strcmp(di.DeviceClass, "GigEVision") == 0 && strcmp(di.SerialNumber, "") != 0)
{
ITKGIGEDEV_INFO gvInfo;
/* Open camera. */
res = ItkDevOpen(i, ITKDEV_VAL_ACCESS_MODE_EXCLUSIVE, &g_hCamera);
CHECK(res);
res = ItkManGetGigEDeviceInfo(i, &gvInfo);
CHECK(res);
break;
}
}
}
/* This function can be used to create stream and add buffer. */
void CreateStreamAndBuffer()
{
uint32_t streamCount = 0; /*Stream count. */
int64_t nWidth = 0; /* Image width. */
int64_t nHeight = 0; /* Image height. */
uint32_t nFormat = ITKBUFFER_VAL_FORMAT_MONO8; /* Image format */
int64_t nImageSize = 0; /* Image size */
char pixelFormat[16]; /* Image pixel format. */
uint32_t pixelFormatLen = 16; /* Image pixel format length. */
// Get stream count
ITKSTATUS res = ITKSTATUS_OK;
res = ItkDevGetStreamCount(g_hCamera, &streamCount);
qDebug() << "streamCount: " <<streamCount;
CHECK(res);
if (streamCount == 0)
{
qDebug() << "Camera does not have image stream channel.";
/* Before exiting a program, ItkManTerminate() should be called to release
all pylon related resources. */
ItkManTerminate();
pressEnterToExit();
exit(EXIT_FAILURE);
}
res = ItkDevGetInt64(g_hCamera, "Width", &nWidth);
CHECK(res);
res = ItkDevGetInt64(g_hCamera, "Height", &nHeight);
CHECK(res);
res = ItkDevToString(g_hCamera, "PixelFormat", pixelFormat, &pixelFormatLen);
CHECK(res);
if (strcmp(pixelFormat, "Mono8") == 0)
nFormat = ITKBUFFER_VAL_FORMAT_MONO8;
else if (strcmp(pixelFormat, "Mono10") == 0)
nFormat = ITKBUFFER_VAL_FORMAT_MONO10;
else if (strcmp(pixelFormat, "Mono10Packed") == 0)
nFormat = ITKBUFFER_VAL_FORMAT_MONO10PACKED;
else if (strcmp(pixelFormat, "BayerGR8") == 0)
nFormat = ITKBUFFER_VAL_FORMAT_BAYER_GR8;
else if (strcmp(pixelFormat, "BayerRG8") == 0)
nFormat = ITKBUFFER_VAL_FORMAT_BAYER_RG8;
else if (strcmp(pixelFormat, "BayerGB8") == 0)
nFormat = ITKBUFFER_VAL_FORMAT_BAYER_GB8;
else if (strcmp(pixelFormat, "BayerBG8") == 0)
nFormat = ITKBUFFER_VAL_FORMAT_BAYER_BG8;
else
{
qDebug() << "Camera does not support pixel format :" << pixelFormat;
/* Before exiting a program, ItkManTerminate() should be called to release
all pylon related resources. */
ItkManTerminate();
pressEnterToExit();
exit(EXIT_FAILURE);
}
res = ItkBufferNew(nWidth, nHeight, nFormat, &g_hBuffer);
CHECK(res);
// Get buffer size
res = ItkBufferGetPrm(g_hBuffer, ITKBUFFER_PRM_SIZE, &nImageSize);
CHECK(res);
// apply buffer data
g_bufferData = (char*)malloc(nImageSize);
if (g_bufferData == NULL)
{
pressEnterToExit();
exit(EXIT_FAILURE);
}
/* Allocate stream handle for image grab. */
res = ItkDevAllocStream(g_hCamera, 0, g_hBuffer, &g_hStream);
CHECK(res);
}
/* This function can be used to configure stream, form more examples please see IKapC usage. */
void ConfigureStream()
{
ITKSTATUS res = ITKSTATUS_OK;
uint32_t xferMode = ITKSTREAM_VAL_TRANSFER_MODE_SYNCHRONOUS_WITH_PROTECT; /* Transfer image in asynchronous mode. */
uint32_t startMode = ITKSTREAM_VAL_START_MODE_NON_BLOCK;
uint32_t timeOut = INFINITE; /* Image transfer timeout. */
/* Set block mode which means the grab will not be stopped before an entire image
come into buffer. */
res = ItkStreamSetPrm(g_hStream, ITKSTREAM_PRM_START_MODE, &startMode);
CHECK(res);
res = ItkStreamSetPrm(g_hStream, ITKSTREAM_PRM_TRANSFER_MODE, &xferMode);
CHECK(res);
res = ItkStreamSetPrm(g_hStream, ITKSTREAM_PRM_TIME_OUT, &timeOut);
CHECK(res);
/* Register callback which will be called at the begin of stream. */
res = ItkStreamRegisterCallback(g_hStream, ITKSTREAM_VAL_EVENT_TYPE_START_OF_STREAM, cbStartOfStream, g_hStream);
CHECK(res);
/* Register callback which will be called at the end of stream. */
res = ItkStreamRegisterCallback(g_hStream, ITKSTREAM_VAL_EVENT_TYPE_END_OF_STREAM, cbEndOfStream, g_hStream);
CHECK(res);
/* Register callback which will be called at the end of one image completely. */
res = ItkStreamRegisterCallback(g_hStream, ITKSTREAM_VAL_EVENT_TYPE_END_OF_FRAME, cbOnEndOfFrame, g_hStream);
CHECK(res);
res = ItkStreamRegisterCallback(g_hStream, ITKSTREAM_VAL_EVENT_TYPE_TIME_OUT, cbOnTimeOut, g_hStream);
CHECK(res);
res = ItkStreamRegisterCallback(g_hStream, ITKSTREAM_VAL_EVENT_TYPE_FRAME_LOST, cbOnFrameLost, g_hStream);
CHECK(res);
}
/* This function will be unregister callback. */
void UnRegisterCallback()
{
ITKSTATUS res = ITKSTATUS_OK;
res = ItkStreamUnregisterCallback(g_hStream, ITKSTREAM_VAL_EVENT_TYPE_START_OF_STREAM);
res = ItkStreamUnregisterCallback(g_hStream, ITKSTREAM_VAL_EVENT_TYPE_END_OF_STREAM);
res = ItkStreamUnregisterCallback(g_hStream, ITKSTREAM_VAL_EVENT_TYPE_END_OF_FRAME);
res = ItkStreamUnregisterCallback(g_hStream, ITKSTREAM_VAL_EVENT_TYPE_TIME_OUT);
res = ItkStreamUnregisterCallback(g_hStream, ITKSTREAM_VAL_EVENT_TYPE_FRAME_LOST);
}
/* This function can be used to register the stream callback function. */
void IKAPC_CC cbStartOfStream(uint32_t eventType, void* pContext)
{
qDebug() << "On start of stream.";
}
void IKAPC_CC cbEndOfStream(uint32_t eventType, void* pContext)
{
qDebug() << "On end of stream." ;
}
void IKAPC_CC cbOnEndOfFrame(uint32_t eventType, void* pContext)
{
qDebug() << "On end of frame." ;
ITKSTATUS res = ITKSTATUS_OK;
unsigned bufferStatus = 0;
int64_t nImageSize = 0;
ITKBUFFER hBuffer = g_hBuffer;
res = ItkBufferGetPrm(hBuffer, ITKBUFFER_PRM_STATE, &bufferStatus);
CHECK(res);
if (bufferStatus == ITKBUFFER_VAL_STATE_FULL || bufferStatus == ITKBUFFER_VAL_STATE_UNCOMPLETED)
{
// Save buffer
//res = ItkBufferSave(hBuffer,g_saveFileName,ITKBUFFER_VAL_TIFF);
res = ItkBufferSave(hBuffer, g_saveFileName, ITKBUFFER_VAL_BMP);
CHECK(res);
// Read buffer
res = ItkBufferGetPrm(hBuffer, ITKBUFFER_PRM_SIZE, &nImageSize);
CHECK(res);
res = ItkBufferRead(hBuffer, 0, g_bufferData, (uint32_t)nImageSize);
CHECK(res);
}
}
void IKAPC_CC cbOnTimeOut(uint32_t eventType, void* pContext)
{
qDebug() << "on time out";
}
void IKAPC_CC cbOnFrameLost(uint32_t eventType, void* pContext)
{
qDebug() << "on frame lost";
}
/* This function demonstrates how to retrieve the error message for the last failed
function call. */
void printErrorAndExit(ITKSTATUS errc)
{
qDebug() << "Error Code:" << errc;
ItkManTerminate(); /* Releases all resources. */
pressEnterToExit();
exit(EXIT_FAILURE);
}
/* This function can be used to wait for user input at the end of the sample program. */
void pressEnterToExit(void)
{
qDebug() << "Exit";
//while (getchar() != '\n');
}