代码拉取完成,页面将自动刷新
//
// Created by Trisoil on 2022/11/28.
//
#include "HIKCam.hpp"
#include "MvCameraControl.h"
#include <cstdio>
#include "opencv2/opencv.hpp"
bool HIKCam::printDeviceInfo(MV_CC_DEVICE_INFO* hk_device) {
if (NULL == hk_device)
{
printf("The Pointer of hk_device is NULL!\n");
return false;
}
if (hk_device->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((hk_device->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((hk_device->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((hk_device->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (hk_device->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// print current ip and user defined name
printf("CurrentIp: %d.%d.%d.%d\n", nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n", hk_device->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (hk_device->nTLayerType == MV_USB_DEVICE)
{
printf("UserDefinedName: %s\n", hk_device->SpecialInfo.stUsb3VInfo.chUserDefinedName);
printf("Serial Number: %s\n", hk_device->SpecialInfo.stUsb3VInfo.chSerialNumber);
printf("Device Number: %d\n\n", hk_device->SpecialInfo.stUsb3VInfo.nDeviceNumber);
}
else
{
printf("Not support.\n");
}
return true;
}
bool HIKCam::HIKCam_Init()
{
unsigned int sdk_V = MV_CC_GetSDKVersion();
printf("SDK version is [0x%x]\n", sdk_V);
//2.查找目标设备
HIKCam::Status = MV_OK;
HIKCam::handle = NULL;
MV_CC_DEVICE_INFO_LIST hk_devices;
memset(&hk_devices, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
HIKCam::Status = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &hk_devices);
if (HIKCam::Status != MV_OK) {
std::cout << "enum devices faild!" << std::endl;
return 0;
}
std::cout << "共检测到相机的个数为:" << hk_devices.nDeviceNum << std::endl;
//3.判断设备是否可访问
if (hk_devices.nDeviceNum > 0)
{
for (unsigned int i = 0; i < hk_devices.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = hk_devices.pDeviceInfo[i];
bool access = MV_CC_IsDeviceAccessible(pDeviceInfo, 1);
if (!access){
printf("[device %d]:could not access... nRet:[0x%x]\n", i, HIKCam::Status);
}
else {
printf("[device %d]:is OK... nRet:[0x%x]\n", i, HIKCam::Status);
}
}
}
else
{
printf("Find No Devices!\n");
return 0;
}
if (hk_devices.nDeviceNum > 0) {
MV_CC_DEVICE_INFO* hk_camera = hk_devices.pDeviceInfo[0];
if (printDeviceInfo(hk_camera) == false) {
return 0;
}
}
else {
std::cout << "no device found" << std::endl;
return 0;
}
//4.创建相机句柄
HIKCam::Status = MV_CC_CreateHandle(&handle, hk_devices.pDeviceInfo[0]);
if (HIKCam::Status != MV_OK) {
return 0;
}
//5.打开设备
HIKCam::Status = MV_CC_OpenDevice(HIKCam::handle);
if (HIKCam::Status != MV_OK) {
std::cout << "设备未打开" << std::endl;
return 0;
}
else {
std::cout << "设备已经打开" << std::endl;
}
//6.设置触发模式为off
HIKCam::Status = MV_CC_SetEnumValue(HIKCam::handle, "TriggerMode", 0);
if (HIKCam::Status != MV_OK) {
return 0;
}
//7.获取数据包的大小
MVCC_INTVALUE hk_param;
memset(&hk_param, 0, sizeof(MVCC_INTVALUE));
HIKCam::Status = MV_CC_GetIntValue(HIKCam::handle, "PayloadSize", &hk_param);
if (HIKCam::Status != MV_OK) {
return 0;
}
HIKCam::payload_size = hk_param.nCurValue;
//8.开始截流
HIKCam::Status = MV_CC_StartGrabbing(HIKCam::handle);
if (HIKCam::Status != MV_OK) {
std::cout << "grab image failed!" << std::endl;
return 0;
}
memset(&hk_imginfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
HIKCam::img_data = (unsigned char*)malloc(sizeof(unsigned char) * (HIKCam::payload_size));
if (HIKCam::img_data == NULL) {
return 0;
}
}
bool HIKCam::Destroy_Cam() {
//9.停止截流
HIKCam::Status = MV_CC_StopGrabbing(HIKCam::handle);
if (HIKCam::Status != MV_OK) {
return 0;
}
//10.关闭设备
HIKCam::Status = MV_CC_CloseDevice(HIKCam::handle);
if (HIKCam::Status != MV_OK) {
return 0;
}
//11.销毁相机句柄
HIKCam::Status = MV_CC_DestroyHandle(HIKCam::handle);
if (HIKCam::Status != MV_OK) {
return 0;
}
}
bool HIK_To_OpenCV(MV_FRAME_OUT_INFO_EX *hk_imginfo, unsigned char* data, cv::Mat& src_img, void *handle) {
cv::Mat cv_img;
if (hk_imginfo->enPixelType == PixelType_Gvsp_Mono8) {
cv_img = cv::Mat(hk_imginfo->nHeight, hk_imginfo->nWidth, CV_8UC1, data);
}
else if (hk_imginfo->enPixelType == PixelType_Gvsp_RGB8_Packed) {
for (unsigned int j = 0; j < hk_imginfo->nHeight; j++) {
for (unsigned int i = 0; i < hk_imginfo->nWidth; i++) {
unsigned char red = data[j * (hk_imginfo->nWidth * 3) + i * 3];
data[j * (hk_imginfo->nWidth * 3) + i * 3] = data[j * (hk_imginfo->nWidth * 3) + i * 3 + 2];
data[j * (hk_imginfo->nWidth * 3) + i * 3 + 2] = red;
}
}
cv_img = cv::Mat(hk_imginfo->nHeight, hk_imginfo->nWidth, CV_8UC3, data);
}
else if (hk_imginfo->enPixelType == PixelType_Gvsp_BayerRG8){
auto *pDataForRGB = (unsigned char*) malloc(hk_imginfo->nWidth * hk_imginfo->nHeight * 4 +2048);
if (pDataForRGB == NULL){
printf("pDataRGB is NULL!\n");
return false;
}
MV_CC_PIXEL_CONVERT_PARAM stConvertParam = {0};
stConvertParam.nWidth = hk_imginfo->nWidth;
stConvertParam.nHeight = hk_imginfo->nHeight;
stConvertParam.pSrcData = data;
stConvertParam.nSrcDataLen = hk_imginfo->nFrameLen;
stConvertParam.enSrcPixelType = hk_imginfo->enPixelType;
stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed;
stConvertParam.pDstBuffer = pDataForRGB;
stConvertParam.nDstBufferSize = hk_imginfo->nWidth * hk_imginfo->nHeight*4 + 2048;
int nRet = MV_CC_ConvertPixelType(handle, &stConvertParam);
if (nRet != MV_OK)
{
printf("Failed! [%x]", nRet);
return false;
}
//printf("Convert pixeltype succeed\n");
for (unsigned int j = 0; j < hk_imginfo->nHeight; j++) {
for (unsigned int i = 0; i < hk_imginfo->nWidth; i++) {
unsigned char red = pDataForRGB[j * (hk_imginfo->nWidth * 3) + i * 3];
pDataForRGB[j * (hk_imginfo->nWidth * 3) + i * 3] = pDataForRGB[j * (hk_imginfo->nWidth * 3) + i * 3 + 2];
pDataForRGB[j * (hk_imginfo->nWidth * 3) + i * 3 + 2] = red;
}
}
cv_img = cv::Mat(hk_imginfo->nHeight, hk_imginfo->nWidth, CV_8UC3, pDataForRGB);
free(pDataForRGB);
pDataForRGB = NULL;
}
else {
// MV_CC_ConvertPixelType()
printf("unsupported pixel format\n");
return false;
}
if (cv_img.data == NULL) {
return false;
}
cv_img.copyTo(src_img);
return true;
}
bool HIKCam::Get_One_Frame(cv::Mat& frame) {
Status = MV_CC_GetOneFrameTimeout(handle, img_data, payload_size, &hk_imginfo, 1000);
if (Status != MV_OK) {
free(img_data);
img_data = NULL;
return 0;
}
if (!HIK_To_OpenCV(&hk_imginfo, img_data, frame, handle)) {
return 0;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。