ParametrizeCamera_FileAccess.cpp

Import/export files to/from a camera.

The sample code shows how to export the User Set or DPC (Defective Pixel Correction) file of a connected camera to the local PC as a binary file, or import a binary file from the local PC to a connected camera.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include "MvCameraControl.h"
unsigned int g_nMode = 0;
int g_nRet = MV_OK;
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
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);
// 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
{
printf("Not support.\n");
}
return true;
}
static void* ProgressThread(void* pUser)
{
int nRet = MV_OK;
MV_CC_FILE_ACCESS_PROGRESS stFileAccessProgress = {0};
while(1)
{
// Get progress of file access
nRet = MV_CC_GetFileAccessProgress(pUser, &stFileAccessProgress);
if(4 == sizeof(long))
{
printf("State = 0x%x,Completed = %lld,Total = %lld\r\n",
nRet, stFileAccessProgress.nCompleted, stFileAccessProgress.nTotal);
}
else
{
printf("State = 0x%x,Completed = %ld,Total = %ld\r\n",
nRet, stFileAccessProgress.nCompleted, stFileAccessProgress.nTotal);
}
if (nRet != MV_OK || (stFileAccessProgress.nCompleted != 0 && stFileAccessProgress.nCompleted == stFileAccessProgress.nTotal))
{
break;
}
usleep(50000);
}
return 0;
}
static void* FileAccessThread(void* pUser)
{
MV_CC_FILE_ACCESS stFileAccess = {0};
stFileAccess.pUserFileName = "UserSet1.bin";
stFileAccess.pDevFileName = "UserSet1";
if (1 == g_nMode)
{
// Reading mode
g_nRet = MV_CC_FileAccessRead(pUser, &stFileAccess);
if (MV_OK != g_nRet)
{
printf("File Access Read fail! nRet [0x%x]\n", g_nRet);
}
}
else if (2 == g_nMode)
{
// Writinge mode
g_nRet = MV_CC_FileAccessWrite(pUser, &stFileAccess);
if (MV_OK != g_nRet)
{
printf("File Access Write fail! nRet [0x%x]\n", g_nRet);
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
// Initialize SDK.
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
// Enumerate devices
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (unsigned 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 Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
// Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// Reading mode
g_nMode = 1;
printf("Read to file.\n");
pthread_t nReadHandle;
nRet = pthread_create(&nReadHandle, NULL ,FileAccessThread , handle);
if (nRet != 0)
{
printf("File access thread create failed.ret = %d\n",nRet);
break;
}
usleep(5000);
pthread_t nReadProcessHandle;
nRet = pthread_create(&nReadProcessHandle, NULL ,ProgressThread , handle);
if (nRet != 0)
{
printf("Progress thread create failed.ret = %d\n",nRet);
break;
}
void *statusRead;
void *statusReadProcess;
pthread_join(nReadHandle, &statusRead);
pthread_join(nReadProcessHandle, &statusReadProcess);
if (MV_OK == g_nRet)
{
printf("File Access Read Success!\n");
}
printf("\n");
// Writing mode
g_nMode = 2;
printf("Write from file.\n");
pthread_t nWriteHandle;
nRet = pthread_create(&nWriteHandle, NULL ,FileAccessThread , handle);
if (nRet != 0)
{
printf("File access thread create failed.ret = %d\n",nRet);
break;
}
usleep(5000);
pthread_t nWriteProgressHandle;
nRet = pthread_create(&nWriteProgressHandle, NULL ,ProgressThread , handle);
if (nRet != 0)
{
printf("Progress thread create failed.ret = %d\n",nRet);
break;
}
void *statusWrite;
void *statusWriteProcess;
pthread_join(nWriteHandle, &statusWrite);
pthread_join(nWriteProgressHandle, &statusWriteProcess);
if (MV_OK == g_nRet)
{
printf("File Access Write Success!\n");
}
// Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
// Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
handle = NULL;
} while (0);
if (handle != NULL)
{
handle = NULL;
}
// Finalize SDK
printf("exit.\n");
return 0;
}