windows 下共享内存使用方法示例

windows下共享内存使用方法较 linux 而言微微复杂

  • 示例实现的功能

    有一个视频文件,一块内存区域 ;

    程序 A,将该视频写入该内存区域 ;

    程序 B,从该内存区域读取该视频 ;

    

  • 代码模块实现

  程序 A:main.h

 #ifndef MAIN_H
#define MAIN_H
#include <tchar.h>
#include <fstream>
#include <math.h>
#include <Windows.h> #include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream> using namespace std;
using namespace cv; #define IMGWIDTH 1920
#define IMGHEIGHT 1080 #define FRAME_SIZE IMGWIDTH*IMGHEIGHT //每帧图像(单通道)的大小
#define BUF_SIZE FRAME_SIZE*5 //设定共享内存总大小(图像头信息 + RGB图像数据信息);
TCHAR shareZoneName[] = TEXT("share_Image"); //共享内存的名字 #define ORIGINAL_IMAGE_HEAD (char*)pBuf+FRAME_SIZE*0 //图像头信息首地址
#define ORIGINAL_IMAGE_DATA (char*)pBuf+FRAME_SIZE*2 //图像数据信息首地址 HANDLE hMapFile; //这块共享内存的句柄
LPCTSTR pBuf; //这块共享内存的首地址 typedef struct
{
int width; //图像宽度
int height; //图像的高度
int frame_no; //该帧图像的帧序号
}imgInfHead; //自定义的一个图像头结构体 #endif

  程序 A: main.cpp

 #include"main.hpp"

 using namespace std;
using namespace cv; // 创建共享内存, 通过句柄 hMapFile,获取共享内存首地址 pBuf
int createFileMapping()
{
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
shareZoneName); // name of mapping object if (hMapFile == NULL)
{
printf("Could not create file mapping object (%d).\n",
GetLastError());
return ;
} pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
,
,
BUF_SIZE); if (pBuf == NULL)
{
printf("Could not map view of file (%d).\n",
GetLastError()); CloseHandle(hMapFile); return ;
}
return ;
} //将 图像头 和 图像数据信息 写入到共享内存中
void copyToMemory(IplImage* IplImageOriginal, int frame_no)
{
if (IplImageOriginal->imageData == NULL)
{
cout << "fail copy to memory!" << endl;
return;
} imgInfHead img_inf_head_original; img_inf_head_original.width = IplImageOriginal->width;
img_inf_head_original.height = IplImageOriginal->height;
img_inf_head_original.frame_no = frame_no; memcpy(ORIGINAL_IMAGE_HEAD, &img_inf_head_original, sizeof(imgInfHead)); //在 共享内存 中保存图像头信息
memcpy(ORIGINAL_IMAGE_DATA, IplImageOriginal->imageData,
          IplImageOriginal->width*IplImageOriginal->height*IplImageOriginal->nChannels); //在 共享内存 中保存图像信息
} int main()
{
VideoCapture capture("VIDEO.mp4"); if (!capture.isOpened())
{
std::cout << "open video failed...!" << endl;
return ;
} createFileMapping(); //创建共享内存 Mat Image;
int frame_no = ;
IplImage* IplImageOrigal = cvCreateImage(Size(IMGWIDTH, IMGHEIGHT), , ); while ( true )
{
capture.read( Image ); if ( Image.empty() )
{
std::cout << "OVER!" << endl;
break;
} IplImageOrigal = &IplImage(Image);
copyToMemory(IplImageOrigal, frame_no); frame_no++;
} CloseHandle(hMapFile);
return ;
}

  程序 B:main.h

 #ifndef MAIN_H
#define MAIN_H #include <tchar.h>
#include <fstream>
#include <math.h>
#include <Windows.h> #include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp> #include <string.h>
#include <iostream> using namespace std;
using namespace cv; //单帧图像长宽
#define IMGWIDTH 1920
#define IMGHEIGHT 1080 #define FRAME_SIZE IMGWIDTH*IMGHEIGHT //单帧大小
#define BUF_SIZE FRAME_SIZE*5 //设定共享内存总大小
#define ORIGINAL_IMAGE_HEAD (char*)pBuf+FRAME_SIZE*0 //图像头首地址
#define ORIGINAL_IMAGE_DATA (char*)pBuf+FRAME_SIZE*2 //图像数据首地址 TCHAR shareZoneName_getSrcImg[] = TEXT("share_Image");
HANDLE hMapFile; //共享内存 句柄
LPCTSTR pBuf; //共享内存 所映射到的 首地址 typedef struct
{
int width;
int height;
int frame_no;
}imgInfHead; //自定义的一个图像头结构体 #endif

  程序 B:main.cpp

 #include "main.h"

 //打开共享内存(通过 共享内存 名称获取对应共享内存句柄,并获取共享内存首地址)
int openFileMapping()
{
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
shareZoneName_getSrcImg); // name of mapping object if (hMapFile == NULL)
{
printf("Could not open file mapping object (%d).\n", GetLastError());
return ;
} pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
,
,
BUF_SIZE); if (pBuf == NULL)
{
printf("Could not map view of file (%d).\n", GetLastError());
CloseHandle(hMapFile);
return ;
}
return ;
} //将共享内存中的图像读取
int copyImageFromMemory(IplImage* &IplImageoOrignal, int* frame_no_adrr)
{
imgInfHead img_inf_head_orginal; memcpy(&img_inf_head_orginal, ORIGINAL_IMAGE_HEAD, sizeof(imgInfHead));
*frame_no_adrr = img_inf_head_orginal.frame_no; //获取该帧图像的 帧序号 IplImageoOrignal = cvCreateImage(cvSize(img_inf_head_orginal.width, img_inf_head_orginal.height), , );
int channels = IplImageoOrignal->nChannels; memcpy(IplImageoOrignal->imageData, ORIGINAL_IMAGE_DATA, img_inf_head_orginal.width*img_inf_head_orginal.height*channels);
return ;
} int main()
{
IplImage* IplImageOriginal;
int frame_no; Mat image_result;
while (true)
{
int flag_accept = openFileMapping();
if (flag_accept)
{
continue;
} int flag_bad = copyImageFromMemory(IplImageOriginal, &frame_no);
if (flag_bad)
{
cout << "复制失败" << endl;
continue;
} Mat(IplImageOriginal).copyTo(image_result); // 转成Mat类型
if (image_result.empty())
{
cvReleaseImage(&IplImageOriginal);
continue;
} //将帧序号显示在图片上
std::stringstream ss;
std::string frame_no_s;
ss << frame_no;
ss >> frame_no_s;
putText(image_result, frame_no_s, cv::Point(, ), cv::FONT_HERSHEY_SIMPLEX, 2.0, cv::Scalar(, , ), , ); imshow("readShareMemImg", image_result);
waitKey();
} CloseHandle(hMapFile);
return ;
}

windows 下共享内存使用方法示例

----------------------------------------------------------------------------------------------------------------------------

上一篇:java实现大数加法、乘法(BigDecimal)


下一篇:Html5、css、JavaScript基础