使用SOCKET搭建linux和window实现实时摄像头传输(linux传输win端使用C++mfc显示)--linux端开发

配置:
Ubuntu16.04
opencv-3.4.12
Ubuntu安装opencv-亲测

cpp代码

#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/videoio.hpp>
//----------------socket网络编程所需头文件-------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>//用于储存编码后的视屏数据
#include <sys/ioctl.h>

using namespace std;
using namespace cv;
 

static int src_sockfd;
string IP_ADDRES="192.168.1.9";
int PORT=8898;




int main( int argc, char** argv )
{
	// 打开摄像头
	// VideoCapture capture(0); 

	// 打开文件
	VideoCapture capture;
	capture.open("/mnt/hgfs/VWFile/1.mp4");
	if (!capture.isOpened()) {
		printf("could not read this video file...\n");
		return -1;
    }
    //socket
	struct sockaddr_in src_addr;
	//创建socket
	if ((src_sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
	{
		perror("Socket failed!\n");
		exit(1);
	}
	printf("img Socket id = %d\n",src_sockfd);
	//设置sockaddr_in 结构体中相关参数
	memset(&src_addr,0, sizeof(src_addr));
	src_addr.sin_family = AF_INET;
	src_addr.sin_port = htons(PORT);
	src_addr.sin_addr.s_addr = inet_addr(IP_ADDRES.c_str());
	//inet_pton(AF_INET, servInetAddr, &serv_addr.sin_addr);
	bzero(&(src_addr.sin_zero), 8);
    
    //创建显示窗口
    namedWindow("CSI Camera", WINDOW_AUTOSIZE);
    Mat src_img;
    //逐帧显示
	while (capture.read(src_img))
    {
 
        resize(src_img, src_img, Size(640, 480));
 
        imshow("CSI Camera",src_img);
        vector<uchar> data_encode;
        vector<int>quality =vector<int>(2);
        quality[0]=IMWRITE_JPEG_QUALITY;//编码格式
        quality[1]=50;//图片压缩比例:50为压缩为50%
        imencode(".jpg", src_img, data_encode,quality);//将图像编码
        int nSize=data_encode.size();
        //cout<<"src图像的数据大小: "<<nSize<<endl;
       // write(sockfd, reinterpret_cast<uchar*>(&nSize), sizeof(int));
       sendto(src_sockfd,&data_encode[0],nSize,0,(struct sockaddr*)&src_addr, sizeof(src_addr));
        int keycode = cv::waitKey(30) & 0xff ; //ESC键退出
            if (keycode == 27) break ;
    }
    close(src_sockfd);//结束之后关闭这个网络套接字
    destroyWindow("CSI Camera");
}

我使用Cmake进行编译

MakeList.txt文件

# cmake needs this line
cmake_minimum_required(VERSION 2.8)

# Define project name
project(opencv_socket _project)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

if(CMAKE_VERSION VERSION_LESS "2.8.11")
  # Add OpenCV headers location to your include paths
  include_directories(${OpenCV_INCLUDE_DIRS})
endif()

# Declare the executable target built from your sources
add_executable(opencv_socket example.cpp)

# Link your application with OpenCV libraries
target_link_libraries(opencv_socket PRIVATE ${OpenCV_LIBS})

编译/运行

cmake .
make
./opencv_socket

最终效果演示。
在这里插入图片描述
此项目Linux端+Window

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mxmevol

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值