python共享内存通信mapofview_例程使用(1-2)共享内存 传递简单结构体

功能:读取USB视频,鼠标点击点(x,y)保存在共享内存

发射端

/* 1包含文件 */

//1.1 系统 必选

#include

#include

//1.2 opencv 可选

#include

using namespace cv;

using namespace std;

/* 2自定数据区 */

//2.1 要存储的数据

typedef struct

{

int x;

int y;

/*int zx;

int zy;*/

int width;

int height;

int flag;

}TrackBox; //目标检测的上下顶点;

TrackBox BOX;

/*3 共享内存*/

//3.1内存变量

HANDLE hMapFile;

LPCTSTR pBuf;

TCHAR szName[] = TEXT("Local\\FHY_SYSTEM_0"); //指向同一块共享内存的名字

#define FRAME_SIZE 1920*1080// 要存的数据大小(图像)

#define BUF_SIZE FRAME_SIZE*10 //分配10倍大

#define BOX_DATA (char*)pBuf+FRAME_SIZE*0 //数据存储的起始位置

//3.2 初始化

int intShareroom() {

hMapFile = CreateFileMapping(

INVALID_HANDLE_VALUE, // use paging file

NULL, // default security

PAGE_READWRITE, // read/write access

0, // maximum object size (high-order DWORD)

BUF_SIZE, // maximum object size (low-order DWORD)

szName); // name of mapping object

if (hMapFile == NULL)

{

printf(TEXT("Could not create file mapping object (%d).\n"),

GetLastError());

return 1;

}

pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object

FILE_MAP_ALL_ACCESS, // read/write permission

0,

0,

BUF_SIZE);

if (pBuf == NULL)

{

printf(TEXT("Could not map view of file (%d).\n"),

GetLastError());

CloseHandle(hMapFile);

return 1;

}

}

/* 4 测试 鼠标点击输出 x y 存入共享内存 */

//4.1 鼠标事件

void onMouse(int event, int x, int y, int flags, void* param)

{

//cout << "flag =" << flag << endl;

Mat *im = reinterpret_cast(param);

switch (event)

{

case CV_EVENT_LBUTTONDOWN: //鼠标左键按下响应:返回坐标和灰度

BOX.x = x;

BOX.y = y;

BOX.flag = flags;

cout << "at(" << BOX.x << "," << BOX.y << ")" << "flag =" << BOX.flag << endl;

//cout << "flag =" << BOX.flag << endl;

//memcpy(&BOX, BOX_DATA, sizeof(TrackBox));

memcpy(BOX_DATA, &BOX, sizeof(TrackBox));

//memcpy(&flag, TRANSFER_FLAG, sizeof(int));

//memcpy(&point, DECTION_BOX_DATA, sizeof(CvPoint));

break;

}

}

// 4.2 读取视频

int imge_test() {

// 构造一个VideoWriter

//VideoWriter video("test.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(1920, 1080));

VideoCapture capture(0);

if (!capture.isOpened())

{

return -1;

}

Mat frame;

capture.set(CV_CAP_PROP_FRAME_WIDTH, 1920);

capture.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);

bool stop = false;

while (1)

{

//flag = 0;

capture >> frame;

cvNamedWindow("当前视频", 0);

resize(frame, frame, Size(1920, 1080));

//Sleep(10);

cvSetMouseCallback("当前视频", onMouse, &frame);

imshow("当前视频", frame);

waitKey(1);

}

}

int main()

{ //3 共享内存初始化

intShareroom();

//4 图像鼠标点击测试

imge_test();

//3 共享内存释放

UnmapViewOfFile(pBuf); //释放;

CloseHandle(hMapFile);

return 0;

}

接收端

#include

#include

using namespace std;

// 1.1 定义共享内存

#define FRAME_SIZE 1920*1080 // 1单个大小

#define BUF_SIZE FRAME_SIZE*10 // 2设置单个成倍数

#define recBOX_DATA (char*)pBuffer+FRAME_SIZE*0 // 3在共享内存中的存储的开始地址

// 1.2

TCHAR szName[] = TEXT("Local\\FHY_SYSTEM_0"); //4指向同一块共享内存的名字

HANDLE hMapFile; //创建句柄

LPCTSTR pBuffer;

// 2 要发送的数据格式

typedef struct

{

int x;

int y;

int width;

int height;

int flag;

}TrackBox; //目标检测的上下顶点;

TrackBox recBOX;

// 3初始化

void initShareMemory()

{

hMapFile = CreateFileMapping(

INVALID_HANDLE_VALUE, // use paging file

NULL, // default security

PAGE_READWRITE, // read/write access

0, // maximum object size (high-order DWORD)

BUF_SIZE, // maximum object size (low-order DWORD)

szName); // name of mapping object

if (hMapFile == NULL)

{

cout << "Could not create file mapping object" << GetLastError() << endl;

}

pBuffer = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object

FILE_MAP_ALL_ACCESS, // read/write permission

0,

0,

BUF_SIZE);

if (pBuffer == NULL)

{

printf("Could not map view of file (%d).\n",

GetLastError());

}

}

void main() {

initShareMemory();

while (true)

{

// 4 主函数中 使用

memcpy(&recBOX, recBOX_DATA, sizeof(TrackBox));// 取共享内存值

int itarget_x, itarget_y, itarget_h, itarget_w;

if (recBOX.flag != 0) // 标志位判断数据是否有效

{

itarget_x = recBOX.x;// 中心

itarget_y = recBOX.y;

itarget_h = recBOX.height;

itarget_w = recBOX.width;

//qDebug("target_x:%d target_y:%d", recBOX.x, recBOX.y);

}

else {

itarget_x = 1920 / 2;// 中心

itarget_y = 1080 / 2;

//qDebug("target_x:%d target_y:%d", itarget_x, itarget_y);

}

cout << "x" << itarget_x << "y" << itarget_y << "h" << itarget_h << "w" << itarget_w << endl;

}

}

封装成类

类定义

ShareMemray.h

#pragma once

#ifndef ShareMemray

#define ShareMemray

#include

#include

#include

using namespace cv;

using namespace std;

#define FRAME_SIZE 1920*1080// 要存的数据大小(图像)

#define BUF_SIZE FRAME_SIZE*10//分配10倍大

#define BOX_DATA (char*)pBuf+FRAME_SIZE*0//数据存储的起始位置 存BOX_DATA pBuf+FRAME_SIZE*0 - pBuf+FRAME_SIZE*0+sizeof(TrackBox)

#define MAT_DATA (char*)pBuf+FRAME_SIZE*1 // 存MAT_DATA pBuf+FRAME_SIZE*1 - pBuf+FRAME_SIZE*1+sizeof(Mat)

typedef struct

{

int x;

int y;

int width;

int height;

int flag;

}TrackBox; //目标检测的上下顶点;

class SHAREDMEMORY {

private:

TrackBox BOX;

HANDLE hMapFile;

LPCTSTR pBuf;

TCHAR szName[30] = TEXT("Local\\FHY_SYSTEM_0"); //指向同一块共享内存的名字

public:

//1 初始化

int intShareroom();

void SendBox(TrackBox BOX);

void RecBox(TrackBox BOX);

void stop();

};

#endif //SHAREDMEMORY_HPP

文件定义

ShareMemray.cpp

#include "ShareMemray.h"

//3.2 初始化

int SHAREDMEMORY::intShareroom() {

hMapFile = CreateFileMapping(

INVALID_HANDLE_VALUE, // use paging file

NULL, // default security

PAGE_READWRITE, // read/write access

0, // maximum object size (high-order DWORD)

BUF_SIZE, // maximum object size (low-order DWORD)

szName); // name of mapping object

if (hMapFile == NULL)

{

printf(TEXT("Could not create file mapping object (%d).\n"),

GetLastError());

return 1;

}

pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object

FILE_MAP_ALL_ACCESS, // read/write permission

0,

0,

BUF_SIZE);

if (pBuf == NULL)

{

printf(TEXT("Could not map view of file (%d).\n"),

GetLastError());

CloseHandle(hMapFile);

return 1;

}

}

void SHAREDMEMORY::SendBox(TrackBox BOX) {

memcpy(BOX_DATA, &BOX, sizeof(TrackBox));

}

void SHAREDMEMORY::RecBox(TrackBox BOX) {

memcpy(&BOX, BOX_DATA, sizeof(TrackBox));

}

void SHAREDMEMORY::stop() {

UnmapViewOfFile(pBuf); //释放;

CloseHandle(hMapFile);

}

发送端

#pragma once

#ifndef MAIN

#define MAIN

/* 1包含文件 */

//1.1 系统 必选

#include

#include

#include "../Include/ShareMemray.h"

//1.2 opencv 可选

#include

using namespace cv;

using namespace std;

TrackBox BOX1;

/* 4 测试 鼠标点击输出 x y 存入共享内存 */

//4.1 鼠标事件

void onMouse(int event, int x, int y, int flags, void* param)

{

//cout << "flag =" << flag << endl;

Mat *im = reinterpret_cast(param);

switch (event)

{

case CV_EVENT_LBUTTONDOWN: //鼠标左键按下响应:返回坐标和灰度

BOX1.x = x;

BOX1.y = y;

BOX1.flag = flags;

cout << "at(" << BOX1.x << "," << BOX1.y << ")" << "flag =" << BOX1.flag << endl;

break;

}

}

// 4.2 读取视频

int imge_test(SHAREDMEMORY sharesend) {

// 构造一个VideoWriter

//VideoWriter video("test.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(1920, 1080));

VideoCapture capture(0);

if (!capture.isOpened())

{

return -1;

}

Mat frame;

capture.set(CV_CAP_PROP_FRAME_WIDTH, 1920);

capture.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);

bool stop = false;

while (1)

{

//flag = 0;

capture >> frame;

cvNamedWindow("当前视频", 0);

resize(frame, frame, Size(1920, 1080));

//Sleep(10);

cvSetMouseCallback("当前视频", onMouse, &frame);

imshow("当前视频", frame);

waitKey(1);

if (BOX1.flag == 1) {

sharesend.SendBox(BOX1);

BOX1.flag = 0;

}

}

}

int main()

{

//共享内存初始化

SHAREDMEMORY sharesend;

sharesend.intShareroom();

//共享内存发送信息

imge_test(sharesend);

//共享内存发送消息

sharesend.stop();

return 0;

}

#endif

接收端

#include

#include

#include "../Include/ShareMemray.h"

using namespace std;

TrackBox recBOX;

void main() {

SHAREDMEMORY sharerec;

sharerec.intShareroom();

while (true)

{

sharerec.RecBox(recBOX);

int itarget_x, itarget_y, itarget_h, itarget_w;

if (recBOX.flag != 0) // 标志位判断数据是否有效

{

itarget_x = recBOX.x;// 中心

itarget_y = recBOX.y;

itarget_h = recBOX.height;

itarget_w = recBOX.width;

//qDebug("target_x:%d target_y:%d", recBOX.x, recBOX.y);

}

else {

itarget_x = 1920 / 2;// 中心

itarget_y = 1080 / 2;

//qDebug("target_x:%d target_y:%d", itarget_x, itarget_y);

}

cout << "x" << itarget_x << "y" << itarget_y << "h" << itarget_h << "w" << itarget_w << endl;

}

sharerec.stop();

}

如何发送一个新的自定义结构体

1在共享内存头文件构造这个结构体

typedef struct

{

int x;

int y;

int width;

int height;

int flag;

}TrackBox; //目标检测的上下顶点;

2 在发送类里面加入这个结构体声明

3 确定在开辟的贡献内存里,分哪一块用来存

3-1 确认共享内存总的区间,足以存下

总空间名字

总空间大小

3-2 在我我们开辟的共享内存里,分那一块用来存

4 实际存入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用QT共享内存方式进行进程间通信例程: 首先,在发送进程中,我们需要创建一个共享内存对象,并将数据写入共享内存中: ```cpp #include <QSharedMemory> #include <QBuffer> // 创建共享内存对象 QSharedMemory sharedMemory("my_shared_memory"); // 打开共享内存 if (!sharedMemory.create(sizeof(int))) { qDebug() << "Cannot create shared memory segment."; return; } // 将数据写入共享内存中 QBuffer buffer; QDataStream out(&buffer); out << static_cast<int>(42); const QByteArray data = buffer.data(); memcpy(sharedMemory.data(), data.constData(), qMin(sharedMemory.size(), data.size())); ``` 然后,在接收进程中,我们需要打开共享内存,并从共享内存中读取数据: ```cpp #include <QSharedMemory> #include <QBuffer> // 创建共享内存对象 QSharedMemory sharedMemory("my_shared_memory"); // 打开共享内存 if (!sharedMemory.attach()) { qDebug() << "Cannot attach shared memory segment."; return; } // 从共享内存中读取数据 QBuffer buffer; const char *data = static_cast<const char*>(sharedMemory.constData()); buffer.setData(data, sharedMemory.size()); int value; QDataStream in(&buffer); in >> value; qDebug() << "Received value:" << value; ``` 以上例程中,我们创建了一个名为“my_shared_memory”的共享内存对象,然后在发送进程中将一个整数值写入共享内存中,接着在接收进程中打开共享内存,并从中读取整数值并输出。 需要注意的是,共享内存方式的进程间通信需要保证多个进程都能够访问同一个共享内存对象,因此需要使用相同的共享内存名称。此外,需要在访问共享内存时进行同步控制,以避免数据竞争问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值