HighBandwidthDecode.cpp

高清无损压缩

该示例程序演示通过高清无损压缩算法将从相机取到的无损压缩码流解码成裸数据。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define IMAGE_NAME_LEN 256
{
switch(enType)
{
return true;
default:
return false;
}
}
// ch:等待用户输入enter键来结束取流或结束程序
//en: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');
}
{
if(NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_GIGE_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_CAMERALINK_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_CXP_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_XOF_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chModelName);
}
else
{
printf("Not Sopport.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
unsigned char* pDstBuf = NULL;
bool isDevOpen = false;
do
{
// ch:初始化SDK | en:Initialize SDK
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
//ch:枚举设备 | en:enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if(NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Input camera index(0-%d):", stDeviceList.nDeviceNum-1);
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if(nIndex >= stDeviceList.nDeviceNum)
{
printf("InPut Error!\n");
break;
}
//ch:选择设备并创建句柄 | en:select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
//ch:打开设备 | en:open device
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
isDevOpen = true;
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
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
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]!\n", nRet);
break;
}
// ch:获取数据包大小 | en:Get payload size
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
//使用camera新的接口
nRet = MV_CC_GetIntValueEx(handle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
unsigned int nPayloadSize = stParam.nCurValue;
// ch:开始取流 | en:Start grab image
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
MV_FRAME_OUT stImageInfo = {0};
unsigned int nImageNum = 10;
char chImageName[IMAGE_NAME_LEN] = {0};
MV_CC_HB_DECODE_PARAM stDecodeParam = {0};
for (unsigned int i = 0; i < nImageNum; i++)
{
nRet = MV_CC_GetImageBuffer(handle, &stImageInfo, 1000);
if(MV_OK == nRet)
{
printf("Get One Frame: Width[%d], Height[%d], FrameNum[%d], PixelFormat[0x%x]\n",
stImageInfo.stFrameInfo.nExtendWidth, stImageInfo.stFrameInfo.nExtendHeight, stImageInfo.stFrameInfo.nFrameNum, stImageInfo.stFrameInfo.enPixelType);
if (false == IsHBPixelFormat(stImageInfo.stFrameInfo.enPixelType))
{
printf("Not HB Picture!\n");
MV_CC_FreeImageBuffer(handle, &stImageInfo);
break;
}
//ch:无损压缩解码 | en:Lossless compression decoding
stDecodeParam.pSrcBuf = stImageInfo.pBufAddr;
stDecodeParam.nSrcLen = stImageInfo.stFrameInfo.nFrameLenEx;
if (NULL == pDstBuf)
{
pDstBuf = (unsigned char *)malloc(sizeof(unsigned char) * (nPayloadSize));
if (NULL == pDstBuf)
{
printf("malloc pDsrData fail!\n");
MV_CC_FreeImageBuffer(handle, &stImageInfo);
break;
}
}
stDecodeParam.pDstBuf = pDstBuf;
stDecodeParam.nDstBufSize = nPayloadSize;
if (NULL== stDecodeParam.pSrcBuf)
{
printf("stDecodeParam.pSrcBuf is null !\n");
MV_CC_FreeImageBuffer(handle, &stImageInfo);
break;
}
else
{
nRet = MV_CC_HB_Decode(handle, &stDecodeParam);
if (nRet != MV_OK)
{
printf("Decode fail![0x%x]\n", nRet);
MV_CC_FreeImageBuffer(handle, &stImageInfo);
break;
}
}
FILE* fp = NULL;
sprintf(chImageName, "Image_w%d_h%d_fn%03d.raw", stDecodeParam.nWidth, stDecodeParam.nHeight, stImageInfo.stFrameInfo.nFrameNum);
fp = fopen(chImageName, "wb");
if (NULL == fp)
{
printf("Open file failed\n");
MV_CC_FreeImageBuffer(handle, &stImageInfo);
break;
}
fwrite(stDecodeParam.pDstBuf, 1, stDecodeParam.nDstBufLen, fp);
fclose(fp);
nRet = MV_CC_FreeImageBuffer(handle, &stImageInfo);
if (MV_OK != nRet)
{
printf("Free ImageBuffer fail! nRet [0x%x]\n", nRet);
break;
}
printf("Decode succeed\n");
}
else
{
printf("Get Image fail! nRet[0x%x]\n", nRet);
}
}
// 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;
}
isDevOpen = false;
// ch:销毁句柄 | en:Destroy handle
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
handle = NULL;
} while (0);
//释放内存
if(pDstBuf)
{
free(pDstBuf);
pDstBuf = NULL;
}
if (isDevOpen == true)
{
}
if (handle != NULL)
{
handle = NULL;
}
// ch:反初始化SDK | en:Finalize SDK
printf("exit.\n");
return 0;
}