函数介绍
//1. 枚举设备
int MV_CC_EnumDevices(unsigned int nTLayerType,
MV_CC_DEVICE_INFO_LIST *pstDevList);
//2. 创建设备句柄
int MV_CC_CreateHandle(void **handle, const MV_CC_DEVIEC_INFO *pstDevInfo);
//参数:
handle [out] //设备句柄,输出参数;
pstDevInfo [in] //设备信息版本、MAC地址、传输层类型以及其它设备信息;
//返回值:
//成功,返回MV_OK (0);失败,返回错误码。
//3. 关闭设备
int MV_CC_CloseDevice(void *handle);
//参数:
handle [in] //设备句柄,MV_CC_CreateHandle或MV_CC_CreateHandleWithoutLog的[out]参数。
//4. 释放句柄
int MV_CC_DestroyHandle(void *handle);
// 5. 注册图像数据回调函数,支持获取chunk信息
int MV_CC_RegisterImageCallBackEx(void *handle, const char *pEventName, \
cbEvent cbEvent, void *pUser);
//参数:
//pEventName [in] 事件名;
//fEventCallBack [in] 接收Event事件的回调函数
//pUser [in] 用户自定义变量
//6. 开始采集图像
int MV_CC_StartGrabbing(void *handle);
//7. 获取一帧图像数据
int MV_CC_GetOneFrame(void *handle, unsigned char *pData, \
unsigned int nDataSize, \
MV_FRAME_OUT_INFO *pFrameInfo
);
//参数:
pData [in] // 用于保存图像数据的缓存地址;
nDataSize [in] // 缓存区大小;
pFrameInfo [out] // 获取到的帧信息;
//8. 获取相机节点值
int MV_CC_GetIntValue(void *handle, const char *strKey, MVCC_INTVALUE *pIntValue);
//参数:
strKey [in] // 节点名称;
pIntValue [out] // 获取到的节点值;
//可以用来获取需要的节点值。
自己实践代码:
camera.hpp
#ifndef CAMERA_CLASS_H_INCLUDED
#define CAMERA_CLASS_H_INCLUDED
#include "/opt/MVS/include/MvCameraControl.h"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
class camera;
#ifdef __cplusplus
extern "C"{
class camera {
private:
void *handle;
bool g_bExit;
int nRet;
unsigned int g_nPayloadSize;
unsigned char *pDataForRGB;
unsigned char *pData;
unsigned char *pDataForSaveImage;
MV_CC_DEVICE_INFO *pDeviceInfo;
MV_CC_DEVICE_INFO_LIST stDeviceList;
MVCC_INTVALUE stParam;
MV_FRAME_OUT stOutFrame;
MV_CC_PIXEL_CONVERT_PARAM CvtParam;
MV_SAVE_IMAGE_PARAM_EX stSaveParam;
MV_FRAME_OUT_INFO_EX stImageInfo;
public:
camera();
bool PrintDeviceInfo(MV_CC_DEVICE_INFO *);
void close_cam();
void start_cam();
void get_pic();
void re_iso();
};
}
#endif
#endif // CAMERA_CLASS_H_INCLUDED
camera.cpp
#include "camera.hpp"
camera::camera() {
nRet = MV_OK;
handle = NULL;
g_bExit = false;
g_nPayloadSize = 0;
pData = NULL;
pDataForSaveImage = NULL;
pDataForRGB = (unsigned char *)malloc(1280 * 680 * 4 + 2048);
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
CvtParam = {0};
stOutFrame = {0};
memset(&stOutFrame, 0, sizeof(MV_FRAME_OUT));
}
void camera::start_cam() {
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
if (MV_OK != nRet) {
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
return;
}
unsigned int nIndex = 0;
if (stDeviceList.nDeviceNum > 0) {
for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++) {
pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo) {
break;
} else if (strcmp(
(char *)pDeviceInfo->SpecialInfo.stGigEInfo.chSerialNumber,
"DA1645182") == 0) {
nIndex = i;
PrintDeviceInfo(pDeviceInfo);
break;
}
}
} else {
cout << "Find no Device" << endl;
}
// 选择设备并创建句柄
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet) {
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
return;
}
// 打开设备
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet) {
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
return;
}
// // 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_SetIntValue(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);
}
}
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet) {
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
return;
}
// ch:获取数据包大小 | en:Get payload size
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
if (MV_OK != nRet) {
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return;
}
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet) {
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
return;
}
}
bool camera::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);
// 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 {
printf("Not support.\n");
}
return true;
}
void camera::close_cam() {
int nRet = MV_CC_StopGrabbing(handle);
if (MV_OK == nRet)
cout << "Stopped Grabbing !" << endl;
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet) {
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
return;
}
}
void camera::get_pic() {
MV_FRAME_OUT_INFO_EX stImageInfo = {0};
memset(&stImageInfo, 0, (sizeof(MV_FRAME_OUT_INFO_EX)) );
pData = (unsigned char *)malloc((sizeof(unsigned char) * stParam.nCurValue ) );
if (NULL == pData) {
return;
}
unsigned int nDataSize = stParam.nCurValue;
nRet = MV_CC_GetOneFrameTimeout(handle, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
cout << pData <
main.cpp
#include "camera.hpp"
int main() {
int key;
camera cam;
cam.start_cam();
int c;
while (1) {
cam.get_pic();
if ((c = getchar()) == '\n') {
cam.close_cam();
break;
}
}
return 0;
}
参考:https://www.cnblogs.com/xiawuhao2013/p/9295781.html