c++ && OpenCV的实时视频传输(UDP on Windows)

udp_video_client.cpp


#include "stdafx.h"
#include "opencv2/opencv.hpp"  
#include "opencv2/imgproc/imgproc.hpp"
#include
#include
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"opencv_world340.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	WSADATA WSAData;
	WORD sockVersion = MAKEWORD(2, 2);
	if (WSAStartup(sockVersion, &WSAData) != 0)
		return 0;

	SOCKET clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (INVALID_SOCKET == clientSocket)
	{
		std::cout << "socket error!";
		return 0;
	}

	/*  socket IP和端口绑定*/
	sockaddr_in dstAddr;
	dstAddr.sin_family = AF_INET;
	dstAddr.sin_port = htons(8888);
	dstAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

	cv::VideoCapture capture(0);//打开摄像头
	cv::Mat image;
	//摄像头读取的图像后续会进行压缩 这里进行压缩相关配置
	std::vector quality;
	quality.push_back(CV_IMWRITE_JPEG_QUALITY);
	quality.push_back(30);//进行50%的压缩
	std::vector data_encode;
	
	while (true) 
	{
		capture >> image;//循环读取摄像头的每一帧
		imencode(".jpg", image, data_encode, quality);//将图像编码
		
		int nSize = data_encode.size();
		unsigned char *encodeImg = new unsigned char[nSize];
		for (int i = 0; i < nSize; i++) { encodeImg[i] = data_encode[i]; }
		//将unsigned char * 指针变量转化为const char * 指针变量 方便进行sendto函数调用
		const char* p = (const char*)(char*)encodeImg;
		sendto(clientSocket, p, nSize, 0, (const sockaddr*)& dstAddr, sizeof(dstAddr));
	}
	closesocket(clientSocket);
	WSACleanup();
	capture.release();
	return 0;
}

udp_video_serv.cpp


#include "stdafx.h"
#include
#include
#include 
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#pragma comment(lib,"opencv_world340.lib")
#pragma comment(lib,"ws2_32.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	//初始化socket资源 
	WSADATA WSAData;
	WORD sockVersion = MAKEWORD(2, 2);
	if (WSAStartup(sockVersion, &WSAData) != 0)
		return 0;

	SOCKET serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);	//创建服务器socket
	if (INVALID_SOCKET == serSocket)
	{
		std::cout << "socket error!";
		return 0;
	}

	//设置传输协议、端口以及目的地址 
	sockaddr_in serAddr;
	serAddr.sin_family = AF_INET;
	serAddr.sin_port = htons(8888);
	serAddr.sin_addr.S_un.S_addr = INADDR_ANY;

	if (bind(serSocket, (sockaddr*)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)	 //将socket绑定地址 
	{
		std::cout << "bind error";
		closesocket(serSocket);
		return 0;
	}

	sockaddr_in clientAddr;
	int iAddrlen = sizeof(clientAddr);
	char buff[14400];	//建立接收缓存字节数组 这里设置大一点 数组长度值是根据经验取的
	std::vector decode;
	cv::namedWindow("image", cv::WINDOW_NORMAL);
	while (true)
	{
		memset(buff, 0, sizeof(buff));	//清空接收缓存数组
		//开始接收数据 
		int len = recvfrom(serSocket, buff, sizeof(buff), 0, (sockaddr*)&clientAddr, &iAddrlen); //len表示当前一次性接收的数据长度
		int pos = 0;
		while (pos < len) {   decode.push_back(buff[pos++]); }//存入vector
		//decode.push_back(0);
		cv::Mat image  = cv::imdecode(decode, CV_LOAD_IMAGE_COLOR);//图像解码
		cv::imshow("image", image);
		cv::waitKey(1);
		decode.clear();
	}
	closesocket(serSocket);		//关闭socket 
	WSACleanup();
	return 0;
}

服务端代码中,图像字节大小如果超过接收缓存字节数组buff大小,会导致udp数据接收失败,这时,可以适当增加数组大小

实际运行时,客户端和服务器端没有先后顺序,这也就是udp通信的一个特点,无需建立连接

代码在windows10  平台下验证通过。

程序运行时,在服务器端可以看到客户端上传的视频图像,无线局域网延迟约为1s。

你可能感兴趣的:(udp,c++,opencv)