该示例程序说明了触发模式下通过软触发主动取图的方式获取图像。创建句柄,打开相机,设置触发模式,设置触发源(软触发),开始取流,创建取流线程(发送软触发command命令, MV_CC_GetOneFrameTimeout() 取图),停止取流,关闭相机,销毁句柄。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include "MvCameraControl.h"
bool g_bExit = false;
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
unsigned char *
pData = (
unsigned char *)malloc(
sizeof(
unsigned char) *
stParam.nCurValue);
if (NULL == pData)
{
return NULL;
}
unsigned int nDataSize =
stParam.nCurValue;
while(1)
{
if(MV_OK != nRet)
{
printf("failed in TriggerSoftware[%x]\n", nRet);
}
if (nRet == MV_OK)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
}
else
{
printf("Get One Frame failed![%x]\n", nRet);
}
if(g_bExit)
{
break;
}
}
if (pData)
{
free(pData);
}
return 0;
}
int main()
{
void* handle = NULL;
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
if (
stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
if (nPacketSize > 0)
{
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
if (MV_OK != nRet)
{
printf("set AcquisitionFrameRateEnable fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerSource fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
PressEnterToExit();
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}