hikvision SDK使用学习/实践

news2024/9/25 17:10:34

  函数介绍

//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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1284806.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

简单了解传输层协议之TCP和UDP

目录 一、什么是端口号? 二、TCP协议 2.1 TCP报文格式 2.2 三次握手 2.3 四次挥手 2.4 窗口流量控制 三、UDP协议 3.1 UDP报文格式 3.4 传输过程 一、什么是端口号? 我们自己的一台电脑上有时可能会同时运行多个进程软件来进行上网。那么当网络上的服务器响应我们电…

python中的进制转换和原码,反码,补码

python中的进制转换和原码,反码,补码 计算机文件大小单位 b bit 位(比特) B Byte 字节 1Byte 8 bit #一个字节等于8位 可以简写成 1B 8b 1KB 1024B 1MB 1024KB 1GB 1024MB 1TB 1024GB 1PB 1024TB 1EB 1024PB 进制分类 二进制:由2个数字组成,有0 和 1 pyth…

基于AWS Serverless的Glue服务进行ETL(提取、转换和加载)数据分析(三)——serverless数据分析

3 serverless数据分析 大纲 3 serverless数据分析3.1 创建Lambda3.2 创建API Gateway3.3 结果3.4 总结 3.1 创建Lambda 在Lambda中&#xff0c;我们将使用python3作为代码语言。 步骤图例1、入口2、创建&#xff08;我们选择使用python3.7&#xff09;3、IAM权限&#xff08;…

HarmonyOS引入其他包,以引入请求axios为例

安装文件 安装文件位置: 总目录的oh-package.json5文件 dependencies&#xff1a;生产环境–上线运行时候必须需要的包 devDependencies&#xff1a;开发环境–开发适合为了方便提高效率的包。 包管理工具 OHPM CLI 作为鸿蒙生态三方库的包管理工具&#xff0c;支持OpenHar…

基于Python自动化测试框架之接口测试

前段时间由于公司测试方向的转型&#xff0c;由原来的web页面功能测试转变成接口测试&#xff0c;之前大多都是手工进行&#xff0c;利用postman和jmeter进行的接口测试&#xff0c;后来&#xff0c;组内有人讲原先web自动化的测试框架移驾成接口的自动化框架&#xff0c;使用的…

ESP32 LVGL Gui-Guider的移植

使用参考&#xff1a; ESP32系列之LVGL&#xff08;三&#xff09;&#xff1a;Gui-Guider的使用_esp32 lvgl-CSDN博客 1、拷贝文件&#xff1a; 按照上面的文章&#xff0c;使用Gui-Guider软件生成C代码之后&#xff0c;custom和generated是我们要使用到的文件&#xff0c;…

异常处理 springboot

全局异常处理 RestcontrollerAdvice Exceptonhandler package com.it.Exception;import com.it.pojo.Result; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice;/*全局异常处理器…

鸿蒙4.0开发笔记之ArkTS装饰器语法基础之监听者模式@Watch案例讲解(十四)

1、Watch定义 Watch实际是指状态变量更改通知。如果开发者需要关注某个状态变量的值是否改变&#xff0c;可以使用Watch为状态变量设置回调函数&#xff08;监听函数&#xff09;。 Watch用于监听状态变量的变化&#xff0c;当状态变量变化时&#xff0c;Watch的回调方法将被…

华清远见嵌入式学习——C++——作业5

作业要求&#xff1a; 代码&#xff1a; #include <iostream>using namespace std;//沙发 类 class Sofa { private:string sitting; //是否可坐double *cost; //花费 public://无参构造函数Sofa(){}//有参构造函数Sofa(string s,double c):sitting(s),cost(new double(…

在 Mac 上使用浅色或深色外观

在 Mac 上&#xff0c;选取苹果菜单 >“系统设置”&#xff0c;然后点按边栏中的“外观” 。&#xff08;你可能需要向下滚动。&#xff09;选择右侧的“浅色”、“深色”或“自动”。 “浅色”表示不会发生变化的浅色外观。 “深色”表示不会发生变化的深色外观。“深色模式…

[MySQL--基础]多表查询

前言 ⭐Hello!这里是欧_aita的博客。 ⭐今日语录&#xff1a;生活中最大的挑战就是发现自己是谁。然后&#xff0c;坚定不移地成为那个人。 ⭐个人主页&#xff1a;欧_aita ψ(._. )>⭐个人专栏&#xff1a; 数据结构与算法 MySQL数据库 多表查询 前言多表关系概述&#x1f…

DNS服务器配置与分析

目录 实验目的&#xff1a; 实验原理&#xff1a; 实验步骤&#xff1a; 步骤1&#xff1a;创建拓扑 步骤2&#xff1a;为PC、Client和Server配置IPv4地址、子网掩码和域名服务器 步骤3&#xff1a;启动设备和服务器 步骤4&#xff1a;测试PC-1、Client-1和Server-1之间…

仅 CSS 阅读进度条

为了构建一个阅读进度条&#xff0c;即显示用户向下滚动时阅读文章的进度&#xff0c;很难不考虑 JavaScript。但是&#xff0c;事实证明&#xff0c;您也可以使用纯 CSS 构建阅读进度条。 从本质上讲&#xff0c;一个名为 animation-timeline 的新实验性 CSS 属性可以让你指定…

剑指 Offer(第2版)面试题 16:数值的整数次方

剑指 Offer&#xff08;第2版&#xff09;面试题 16&#xff1a;数值的整数次方 剑指 Offer&#xff08;第2版&#xff09;面试题 16&#xff1a;数值的整数次方解法1&#xff1a;快速幂 - 递归写法解法2&#xff1a;快速幂 - 非递归写法 剑指 Offer&#xff08;第2版&#xff…

强化学习——简单解释

一、说明 最近 OpenAI 上关于 Q-star 的热议激起了我温习强化学习知识的兴趣。这是为强化学习 (RL) 新手提供的复习内容。 二、强化学习的定义 强化学习是人类和其他动物用来学习的学习类型。即&#xff0c;通过阅读房间来学习。&#xff08;从反馈中学习&#xff09;。让我解…

在线直线度测量仪在圆形轧钢中的重要性

在线直线度测量仪在圆形轧钢中的重要性 在现代轧钢生产中&#xff0c;在线直线度测量仪是一种非常重要的工具&#xff0c;它可以帮助工人和产线进行高精度的直线度和直径测量&#xff0c;从而保证产品质量的稳定性和精度。以下是详细介绍直线度测量仪的重要性和应用。 一、测…

高斯平滑处理

本文主要介绍了高斯滤波器的原理及其实现过程 高斯滤波器是一种线性滤波器,能够有效的抑制噪声,平滑图像。其作用原理和均值滤波器类似,都是取滤波器窗口内的像素的均值作为输出。其窗口模板的系数和均值滤波器不同,均值滤波器的模板系数都是相同的为1;而高斯滤波器的模板…

06、pytest将多个测试放在一个类中

官方用例 # content of test_class.py # 实例1 class TestClass:def test_one(self):x "this"assert "h" in xdef test_two(self):x "hello"assert hasattr(x,"check")# content of test_class_demo.py # 每个测试都有唯一的类实例…

逻辑回归 使用Numpy实现逻辑回归

使用Numpy实现逻辑回归 sigmoid 函数 g ( z ) 1 ( 1 e − z ) g(z)\frac{1}{(1e^{−z} )} g(z)(1e−z)1​ # sigmoid 函数 def sigmod(z):return 1/(1np.exp(-z))线性计算与梯度下降 J ( θ ) − 1 m [ ∑ i 1 m y ( i ) l o g ⁡ ( h θ ( x ( i ) ) ) ( 1 − y ( i ) …

算符优先语法分析程序设计与实现

制作一个简单的C语言词法分析程序_用c语言编写词法分析程序-CSDN博客文章浏览阅读378次。C语言的程序中&#xff0c;有很单词多符号和保留字。一些单词符号还有对应的左线性文法。所以我们需要先做出一个单词字符表&#xff0c;给出对应的识别码&#xff0c;然后跟据对应的表格…