嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 6 元微信扫码支付:6 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
通过在控制台输入相机序号及相机工作类型,在控制台输出相机所拍摄包裹的体积,长,宽,高信息
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <iostream>
#include <Windows.h>
#include <time.h>
#include <string.h>
using namespace std;
#include "VolumeMeasureC.h"
#pragma comment(lib, "VolMeasure.lib")
void __stdcall ExceptionCallbackFunc(void* pUser, unsigned int nCameraIndex, unsigned int nMsgType)
{
printf("VolumeInfo: Camera off line!, recv (%d) (%d) \r\n.",nCameraIndex, nMsgType);
}
bool IsRightFormat(std::string &str)
{
bool bRightFormat = true;
for (int i = 0; i < str.length(); i )
{
if (str[i] < '0' || str[i] > '9' || (str[0] == '0' && 1 != str.length()))
{
bRightFormat = false;
break;
}
}
return bRightFormat;
}
void main()
{
void* hHandle = NULL;
int nRet = MV_VOLM_OK;
printf("---------------------------------------------------------------\r\n");
printf("Current SDK Version:[%#x]\r\n", VOLM_GetSDKVersion());
//算法库版本信息
VOLM_ALGORITHM_INFO_LIST stAlgorithm = {0};
nRet = VOLM_GetAlgorithmVersion(&stAlgorithm);
if(MV_VOLM_OK == nRet)
{
for (int i = 0; i < stAlgorithm.nAlgorithmNum; i )
{
printf("AlgorithName:%s, VersionInfo:%s.\r\n", stAlgorithm.stAlorithmInfo[i].chAlgorithmName, stAlgorithm.stAlorithmInfo[i].chVersionInfo);
}
}
printf("---------------------------------------------------------------\r\n");
//枚举相机
VOLM_DEVICE_INFO_LIST stCameraInfoList = {0};
nRet = VOLM_EnumStereoCamEx(MV_VOLM_GIGE_DEVICE | MV_VOLM_USB_DEVICE, &stCameraInfoList);
if (MV_VOLM_OK != nRet)
{
printf("VOLM_EnumStereoCamEx failed. nRet[%#x]\r\n ", nRet);
return;
}
else
{
printf("Find Camera Num:[%d], 3D Camera list:\r\n", stCameraInfoList.nDeviceNum);
for (int i = 0; i < stCameraInfoList.nDeviceNum; i )
{
if (VOLM_CAMERA_3D == stCameraInfoList.pDeviceInfo[i]->nReserved[0])
{
if (MV_VOLM_GIGE_DEVICE == stCameraInfoList.pDeviceInfo[i]->nTLayerType)
{
printf("Index:(%d) ModeleNam:%s, SN:%s, UserName:%s\r\n", i, stCameraInfoList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.chModelName,
stCameraInfoList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.chSerialNumber,
stCameraInfoList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (MV_VOLM_USB_DEVICE == stCameraInfoList.pDeviceInfo[i]->nTLayerType)
{
printf("Index:(%d) ModeleNam:%s, SN:%s, UserName:%s\r\n", i, stCameraInfoList.pDeviceInfo[i]->SpecialInfo.stUsb3VInfo.chModelName,
stCameraInfoList.pDeviceInfo[i]->SpecialInfo.stUsb3VInfo.chSerialNumber,
stCameraInfoList.pDeviceInfo[i]->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
//do nothing
}
}
}
}
printf("---------------------------------------------------------------\r\n");
unsigned int nIndex = 0;
while (true)
{
printf("请输入需要连接的相机序号:\n");
std::string str;
getline(std::cin, str);
if(IsRightFormat(str))
{
nIndex = atoi(str.c_str());
}
else
{
printf("输入序号格式不正确,请重新输入:\r\n");
continue;
}
printf("连接的相机:%d \r\n", nIndex);
if(nIndex >= 0 && nIndex < stCameraInfoList.nDeviceNum && VOLM_CAMERA_3D == stCameraInfoList.pDeviceInfo[nIndex]->nReserved[0])
{
break;
}
else
{
printf("输入序号不正确\r\n");
}
}
printf("---------------------------------------------------------------\r\n");
try
{
int nCameType = 0;
printf("主动循环获取体积\n");
printf("请选择体积相机工作模式:\n");
printf("1. 线激光相机本地体积,测量常规体积\n");
printf("5. 线激光相机本地体积,测量积分体积\n");
printf("7. 130W双目相机本地体积(深度图)\n");
printf("8. 160W双目相机本地体积(深度图)\n");
printf("9. 349线激光相机直接出体积\n");
printf("10. 双目相机直接出体积\n");
printf("11. 双目相机本地体积(原始图)\n");
printf("12. 线激光相机本地体积(原始图,积分体积)\n");
printf("13. 双目相机(MV-DS1307-05E)(不支持回调)\n");
printf("请输入对应序号:");
scanf("%d",&nCameType);
printf("工作模式:%d \n", nCameType);
printf("---------------------------------------------------------------\r\n");
//定制双目相机MV-DS1307-05E仅支持序列号方式连接
//其他型号相机支持序列号和Mac地址方式连接
//根据获取序列号创建句柄
if (MV_VOLM_GIGE_DEVICE == stCameraInfoList.pDeviceInfo[nIndex]->nTLayerType)
{
nRet = VOLM_CreateHandleBySerial(&hHandle, (char*)stCameraInfoList.pDeviceInfo[nIndex]->SpecialInfo.stGigEInfo.chSerialNumber);
}
else if (MV_VOLM_USB_DEVICE == stCameraInfoList.pDeviceInfo[nIndex]->nTLayerType)
{
// 单MV-DS1307-05E相机场景
//nRet = VOLM_CreateOBHandle(&hHandle);
//多MV-DS1307-05E相机场景
nRet = VOLM_CreateHandleBySerial(&hHandle, (char*)stCameraInfoList.pDeviceInfo[nIndex]->SpecialInfo.stUsb3VInfo.chSerialNumber);
}
if (MV_VOLM_OK != nRet)
{
printf("VOLM_CreateHandleBySerial failed. nRet[%#x]\r\n ", nRet);
throw -1;
}
else
{
printf("VOLM_CreateHandleBySerial success.\r\n");
}
if (MV_VOLM_GIGE_DEVICE == stCameraInfoList.pDeviceInfo[nIndex]->nTLayerType )
{
//设置心跳时间(可选)
CAMERA_PARAM stCameraParam = {0}; //参数结构体
strcpy(stCameraParam.cParamName, "GevHeartbeatTimeout");
stCameraParam.unParamValue.nParamValue = 3000;
//根据节点名称设置参数
nRet = VOLM_SetParam(hHandle, VOLMEASURE_CFG_CAMERA, &stCameraParam, sizeof(stCameraParam));
if (MV_VOLM_OK != nRet)
{
printf("VOLM_SetParam failed. nRet[%#x]\r\n ", nRet);
throw -1;
}
else
{
printf("VOLM_SetParam success.\r\n");
}
}
//无特殊情况,nCameType的值不能为0,具体定义见CAMERATYPE结构体
nRet = VOLM_SetAlgorithmType(hHandle,nCameType);
if (MV_VOLM_OK != nRet)
{
printf("VOLM_SetAlgorithmType failed. nCameType[%d]. nRet[%#x]\r\n ", nCameType, nRet);
throw -1;
}
else
{
printf("VOLM_SetAlgorithmType success. nCameType[%d]\r\n",nCameType);
}
nRet = VOLM_RegisterExceptionCallBack(hHandle, ExceptionCallbackFunc, NULL);
if (MV_VOLM_OK != nRet)
{
printf("VOLM_RegisterExceptionCallBack failed. nRet[%#x].\r\n",nRet);
}
else
{
printf("VOLM_RegisterExceptionCallBack success.\r\n");
}
//开始工作
nRet = VOLM_Start(hHandle);
if (MV_VOLM_OK != nRet)
{
printf("VOLM_Start failed. nRet[%#x]\r\n ", nRet);
throw -1;
}
else
{
printf("VOLM_Start success\r\n");
}
printf("begin volume info measure!\r\n");
//循环获取体积信息
while(true)
{
VOLM_RESULT_INFO stResultInfo = {0};
//主动获取体积,没有数据时返回非MV_VOLM_OK
nRet = VOLM_GetResult(hHandle,&stResultInfo);
if (MV_VOLM_OK != nRet)
{
//printf("VOLM_GetResult failed. nRet[%#x]\r\n",nRet);
}
else
{
static unsigned int nIndex = 0;
nIndex ;
//根据标记位,判断是否有体积信息
if (stResultInfo.nVolumeFlag)
{
printf("\n count: %d\n", nIndex);
//打印体积信息
printf("VolumeInfo: volume[%.02f] length[%.02f] width[%.02f] height[%.02f]\r\n",
stResultInfo.stVolumeInfo.volume,
stResultInfo.stVolumeInfo.length,
stResultInfo.stVolumeInfo.width,
stResultInfo.stVolumeInfo.height);
//打印坐标点的信息
for(int i=0; i<4; i )
{
printf("coordinate:[%d] [%.02f-%.02f-%.02f] \r\n",
i,
stResultInfo.stVolumeInfo.vertex_pnts[i].x,
stResultInfo.stVolumeInfo.vertex_pnts[i].y,
stResultInfo.stVolumeInfo.vertex_pnts[i].z);
}
}
//根据标记位,判断是否有图像信息
if (stResultInfo.nImgFlag)
{
//所需图片信息
//图片缓存:stResultInfo.stImage.pData, 图片长度:stResultInfo.stImage.nFrameLen
printf("VolumeInfo: recv image.FrameNum[%d] len[%d]\r\n.",stResultInfo.stImage.nFrameNum, stResultInfo.stImage.nFrameLen);
}
}
Sleep(10);
}
//停止工作
nRet = VOLM_Stop(hHandle);
if (MV_VOLM_OK != nRet)
{
printf("VOLM_Stop failed, nRet:(%#x)\r\n", nRet);
}
else
{
printf("VOLM_Stop success.\r\n");
}
if (NULL != hHandle)
{
//销毁句柄
nRet = VOLM_DestroyHandle(hHandle);
if (MV_VOLM_OK != nRet)
{
printf("VOLM_DestroyHandle failed, nRet:(%#x)\r\n", nRet);
}
else
{
printf("VOLM_DestroyHandle success.\r\n");
}
hHandle = NULL;
}
}
catch(...)
{
//处理异常
printf("start volume measure failed. nRet[%#x] \r\n", nRet);
//停止工作
VOLM_Stop(hHandle);
//销毁句柄
if (NULL != hHandle)
{
VOLM_DestroyHandle(hHandle);
hHandle = NULL;
}
}
//让进程暂停,调试使用,实际不使用
system("pause");
return;
}