ConnectSpecCamera.cpp

通过IP地址连接相机

该示例程序说明如何通过IP地址连接网口相机。通过输入需要连接的相机IP(Camera Ip)和相机对应的网卡IP(Export Ip)来连接相机,输入的格式为xx.xx.xx.xx。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
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);
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
MV_FRAME_OUT stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT));
while(1)
{
if(g_bExit)
{
break;
}
nRet = MV_CC_GetImageBuffer(pUser, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.stFrameInfo.nExtendWidth, stImageInfo.stFrameInfo.nExtendHeight, stImageInfo.stFrameInfo.nFrameNum);
MV_CC_FreeImageBuffer(pUser, &stImageInfo);
}
else
{
printf("No data[0x%x]\n", nRet);
break;
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
MV_CC_DEVICE_INFO stDevInfo = {0};
MV_GIGE_DEVICE_INFO stGigEDev = {0};
// ch:需要连接的相机ip(根据实际填充) | en:The camera IP that needs to be connected (based on actual padding)
printf("Please input Current Camera Ip : ");
char nCurrentIp[128];
scanf("%s", (char*)&nCurrentIp);
// ch:相机对应的网卡ip(根据实际填充) | en:The pc IP that needs to be connected (based on actual padding)
printf("Please input Net Export Ip : ");
char nNetExport[128];
scanf("%s", (char*)&nNetExport);
unsigned int nIp1, nIp2, nIp3, nIp4, nIp;
sscanf(nCurrentIp, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
stGigEDev.nCurrentIp = nIp;
sscanf(nNetExport, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
stGigEDev.nNetExport = nIp;
stDevInfo.nTLayerType = MV_GIGE_DEVICE;// ch:仅支持GigE相机 | en:Only support GigE camera
stDevInfo.SpecialInfo.stGigEInfo = stGigEDev;
do
{
// ch:初始化SDK | en:Initialize SDK
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
// ch:选择设备并创建句柄 | en:Select device and create handle
nRet = MV_CC_CreateHandle(&handle, &stDevInfo);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet[0x%x]\n", nRet);
break;
}
// ch:打开设备 | en:Open device
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValueEx(handle,"GevSCPSPacketSize",nPacketSize);
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);
}
// ch:设置触发模式为off | en:Set trigger mode as off
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
// ch:开始取流 | en:Start grab image
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%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;
}
// ch:停止取流 | en:Stop grab image
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// ch:关闭设备 | en:Close device
if (MV_OK != nRet)
{
printf("Close Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | en:Destroy handle
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
handle = NULL;
} while (0);
if (handle != NULL)
{
handle = NULL;
}
// ch:反初始化SDK | en:Finalize SDK
printf("exit.\n");
return 0;
}