用的是DALSA Nano的网口相机进行连续采图,根据官方给的GigECameraMultiCast 的Demo进行了一下修改,使其能够按指定的相机配置文件ccf进行指定数量的连续采图,并存储在指定路径的文件夹下。
附上DALSA官网网站Home | Teledyne DALSA
二次开发之前得进行环境的配置。
下面是编写好的连续采图的代码,目前设置是连续采图10张并进行存储。
#ifdef _MSC_VER
#pragma warning(disable: 4786)
#endif
// Disable deprecated function warnings with Visual Studio 2005
#if defined(_MSC_VER) && _MSC_VER >= 1400
#pragma warning(disable: 4995)
#endif
#include "stdio.h"
#include "conio.h"
#include "sapclassbasic.h"
#include "ExampleUtils.h"
#include <iostream>
#include <vector>
#include <string>
#include<opencv2/opencv.hpp>
// Restore deprecated function warnings with Visual Studio 2005
#if defined(_MSC_VER) && _MSC_VER >= 1400
#pragma warning(default: 4995)
#endif
// Static Functions
static void AcqCallback(SapXferCallbackInfo *pInfo);
static BOOL GetOptions(int argc, char *argv[], char *acqServerName, UINT32 *pAcqDeviceIndex);
static BOOL GetOptionsFromCommandLine(int argc, char *argv[], char *acqServerName, UINT32 *pAcqDeviceIndex);
BYTE* pData;
int main(int argc, char* argv[])
{
BOOL master = FALSE;
BOOL selectionOk = FALSE;
UINT32 acqDeviceNumber;
char acqServerName[CORSERVER_MAX_STRLEN];
uchar* imgData = new uchar[4912*3684*10];
char *ccf_path = "C:\\Users\\86152\\Desktop\\001.ccf";
if (!GetOptions(argc, argv, acqServerName, &acqDeviceNumber))
{
printf("\nPress any key to terminate\n");
CorGetch();
return 0;
}
printf("M or m: Start as a master\n");
printf("R or r: Start as a receiver\n");
do
{
char key = (char)_getch();
if (key != 0)
{
if (key == 'q')
return FALSE;
if ((key == 'm') || (key == 'M'))
{
printf("\n---> Starting as a MASTER <---\n\n");
selectionOk = TRUE;
master = TRUE;
}
if ((key == 'r') || (key == 'R'))
{
printf("\n---> Starting as a RECEIVER <---\n\n");
selectionOk = TRUE;
}
}
} while (!selectionOk);
//SapAcqDevice AcqDevice;
SapBuffer Buffers;
SapTransfer Xfer;
SapView View;
//SapLocation loc(acqServerName);
SapLocation loc(acqServerName,0);
SapAcqDevice AcqDevice;
if (SapManager::GetResourceCount(acqServerName, SapManager::ResourceAcqDevice) > 0)
{
if (master)
{
// Create the Acquisition Device in read/write mode.
// You must have set the "Camera Default Write Mode" to "Control"
// with Network Configuration Tool.
AcqDevice = SapAcqDevice(SapLocation(1, 0), ccf_path);
}
else
{
// Create the Acquisition Device in read-only mode.
// Make sure the Camera is properly set by the MASTER before
// the Acquisition Device Creation.
AcqDevice = SapAcqDevice(SapLocation(1,0), ccf_path);
// No change must be done by the MASTER to the Camera configuration after the
// Acquisition Device creation by a RECEIVER... except enabling multicast.
}
Buffers = SapBufferWithTrash(10, &AcqDevice);
View = SapView(&Buffers, SapHwndAutomatic);
//View = SapView(&Buffers, SapHwndDesktop);
Xfer = SapAcqDeviceToBuf(&AcqDevice, &Buffers, AcqCallback, &View);
}
// Create acquisition object
if (!AcqDevice.Create())
goto FreeHandles;
if (master)
{
INT64 multicastAddress = 0xef0b0b0b; //239.11.11.11
// Set the multicast IP address used by the Camera (optional... only if
// you don't want to use the default value of 239.10.10.10.
AcqDevice.SetFeatureValue("multiCastStreamingIPAddress", multicastAddress);
// Enable the multicast.
AcqDevice.SetFeatureValue("multiCastStreamingEnable", TRUE);
}
else
{
unsigned int streamingEnable = 0;
do
{
Sleep(5000);
// Loop until the multicast is enabled by the MASTER.
if (AcqDevice.GetFeatureValue("multiCastStreamingEnable", &streamingEnable) == FALSE)
goto FreeHandles;
} while (!streamingEnable);
Sleep(1000);
}
// Create buffer object
if (!Buffers.Create())
goto FreeHandles;
// Create transfer object
if (!Xfer.Create())
goto FreeHandles;
// Create view object
if (!View.Create())
goto FreeHandles;
int flag = 0;
int PanDuan = 0;
static int framcount = 0;
// Start continous grab
Xfer.Grab();
flag = 1;
while (true)
{
if (flag == 1)
{
std::cout << "Grab" << std::endl;
//Sleep(1000);
std::stringstream ss;
ss << "C:\\Users\\86152\\Desktop\\test1\\" << framcount << ".bmp";
std::string name = ss.str();
const char* savename = name.c_str();
Buffers.Save(savename, "-format bmp");
Buffers.Clear();
//m_Mats.push_back(m_Mat);
framcount++;
if (framcount > int(10))
{
framcount = 0;
std::cout << "Grab Finished" << std::endl;//停止采集
Xfer.Freeze();
break;
}
}
}
// Stop grab
/*
Xfer.Freeze();
if (!Xfer.Wait(5000))
printf("Grab could not stop properly.\n");
*/
FreeHandles:
printf("Press any key to terminate\n");
CorGetch();
// Destroy view object
if (!View.Destroy()) return FALSE;
// Destroy transfer object
if (!Xfer.Destroy()) return FALSE;
// Destroy buffer object
if (!Buffers.Destroy()) return FALSE;
// Destroy acquisition object
if (!AcqDevice.Destroy()) return FALSE;
Sleep(1000);
return 0;
}
static void AcqCallback(SapXferCallbackInfo *pInfo)
{
SapView *pView= (SapView *) pInfo->GetContext();
// Refresh view
pView->Show();
}
static BOOL GetOptions(int argc, char *argv[], char *acqServerName, UINT32 *pAcqDeviceIndex)
{
// Check if arguments were passed
if (argc > 1)
return GetOptionsFromCommandLine(argc, argv, acqServerName, pAcqDeviceIndex);
else
return GetCorAcqDeviceOptionsFromQuestions(acqServerName, pAcqDeviceIndex, TRUE);
}
static BOOL GetOptionsFromCommandLine(int argc, char *argv[], char *acqServerName, UINT32 *pAcqDeviceIndex)
{
// Check the command line for user commands
if ((strcmp(argv[1], "/?") == 0) || (strcmp(argv[1], "-?") == 0))
{
// print help
printf("Usage:\n");
printf("GigECameraMulticast [<acquisition server name> <acquisition device index>]\n");
return FALSE;
}
// Check if enough arguments were passed
if (argc < 3)
{
printf("Invalid command line!\n");
return FALSE;
}
// Validate server name
if (SapManager::GetServerIndex(argv[1]) < 0)
{
printf("Invalid acquisition server name!\n");
return FALSE;
}
// Does the server support acquisition?
int deviceCount = SapManager::GetResourceCount(argv[1], SapManager::ResourceAcqDevice);
if (deviceCount == 0)
{
printf("This server does not support acquisition!\n");
return FALSE;
}
// Validate device index
if (atoi(argv[2]) < 0 || atoi(argv[2]) >= deviceCount)
{
printf("Invalid acquisition device index!\n");
return FALSE;
}
// Fill-in output variables
CorStrncpy(acqServerName, argv[1], CORSERVER_MAX_STRLEN);
*pAcqDeviceIndex = atoi(argv[2]);
return TRUE;
}