码迷,mamicode.com
首页 > 其他好文 > 详细

分享内存(2) 简单结构体

时间:2018-01-22 19:16:36      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:named   存在   top   mouse   map   ast   sop   text   分享   

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

 

发射端

/*  1包含文件  */
//1.1 系统 必选
#include<iostream>
#include<Windows.h>

//1.2 opencv 可选
#include <opencv2/opencv.hpp>  
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<Mat*>(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;
}

  接收端

 

分享内存(2) 简单结构体

标签:named   存在   top   mouse   map   ast   sop   text   分享   

原文地址:https://www.cnblogs.com/kekeoutlook/p/8330336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!