函数介绍
//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 <cmath>
#include <cstddef>
#include <fcntl.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/video/video.hpp>
#include <opencv4/opencv2/imgproc/types_c.h>
#include <opencv4/opencv2/opencv.hpp>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
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 <<endl;
printf("Now you GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
// 处理图像
// image processing
printf(" 0 to do nothing\n");
printf(" 1 to convert RGB\n");
printf(" 2 to save as BMP\n");
printf("Please Input Index: ");
int nInput = 0;
scanf("%d", &nInput);
switch (nInput)
{
// 不做任何事,继续往下走
// do nothing, and go on next
case 0:
{
break;
}
// 转换图像为RGB格式,用户可根据自身需求转换其他格式
// convert image format to RGB, user can convert to other format by their requirement
case 1:
{
pDataForRGB = (unsigned char*)malloc(stImageInfo.nWidth * stImageInfo.nHeight * 4 + 2048);
if (NULL == pDataForRGB)
{
break;
}
// 像素格式转换
// convert pixel format
MV_CC_PIXEL_CONVERT_PARAM stConvertParam = {0};
// 从上到下依次是:图像宽,图像高,输入数据缓存,输入数据大小,源像素格式,
// 目标像素格式,输出数据缓存,提供的输出缓冲区大小
// Top to bottom are:image width, image height, input data buffer, input data size, source pixel format,
// destination pixel format, output data buffer, provided output buffer size
stConvertParam.nWidth = stImageInfo.nWidth;
stConvertParam.nHeight = stImageInfo.nHeight;
stConvertParam.pSrcData = pData;
stConvertParam.nSrcDataLen = stImageInfo.nFrameLen;
stConvertParam.enSrcPixelType = stImageInfo.enPixelType;
stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed;
stConvertParam.pDstBuffer = pDataForRGB;
stConvertParam.nDstBufferSize = stImageInfo.nWidth * stImageInfo.nHeight * 4 + 2048;
nRet = MV_CC_ConvertPixelType(handle, &stConvertParam);
if (MV_OK != nRet)
{
printf("MV_CC_ConvertPixelType fail! nRet [%x]\n", nRet);
break;
}
FILE* fp = fopen("AfterConvert_RGB.raw", "wb");
if (NULL == fp)
{
printf("fopen failed\n");
break;
}
fwrite(pDataForRGB, 1, stConvertParam.nDstLen, fp);
fclose(fp);
printf("convert succeed\n");
break;
}
case 2:
{
pDataForSaveImage = (unsigned char*)malloc(stImageInfo.nWidth * stImageInfo.nHeight * 4 + 2048);
if (NULL == pDataForSaveImage)
{
break;
}
// 填充存图参数
// fill in the parameters of save image
MV_SAVE_IMAGE_PARAM_EX stSaveParam;
memset(&stSaveParam, 0, sizeof(MV_SAVE_IMAGE_PARAM_EX));
// 从上到下依次是:输出图片格式,输入数据的像素格式,提供的输出缓冲区大小,图像宽,
// 图像高,输入数据缓存,输出图片缓存,JPG编码质量
// Top to bottom are:
stSaveParam.enImageType = MV_Image_Bmp;
stSaveParam.enPixelType = stImageInfo.enPixelType;
stSaveParam.nBufferSize = stImageInfo.nWidth * stImageInfo.nHeight * 4 + 2048;
stSaveParam.nWidth = stImageInfo.nWidth;
stSaveParam.nHeight = stImageInfo.nHeight;
stSaveParam.pData = pData;
stSaveParam.nDataLen = stImageInfo.nFrameLen;
stSaveParam.pImageBuffer = pDataForSaveImage;
stSaveParam.nJpgQuality = 80;
nRet = MV_CC_SaveImageEx2(handle, &stSaveParam);
if(MV_OK != nRet)
{
printf("failed in MV_CC_SaveImage,nRet[%x]\n", nRet);
break;
}
FILE* fp = fopen("ig.bmp", "wb");
if (NULL == fp)
{
printf("fopen failed\n");
break;
}
fwrite(pDataForSaveImage, 1, stSaveParam.nImageLen, fp);
fclose(fp);
printf("save image succeed\n");
break;
}
default:
break;
}
}else
{
printf("No data[%x]\n", nRet);
}
}
void camera::re_iso() {
MV_CC_SetEnumValue(handle, "BalanceWhiteAuto", 2);
MV_CC_SetEnumValue(handle, "ExposureAuto", 1);
}
void PressEnterToExit(void) {
int c;
while ((c = getchar()) != '\n' && c != EOF)
;
fprintf(stderr, "\nPress enter to exit.\n");
while (getchar() != '\n')
;
}
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