当前位置:网站首页>Use UDP to send a JPEG image, and UPD will convert it into the mat format of OpenCV after receiving it
Use UDP to send a JPEG image, and UPD will convert it into the mat format of OpenCV after receiving it
2022-07-05 02:43:00 【DSZS123】
Because the project needs to use udp Receive one jpeg The picture file of , And then use opencv Show it .
udp recvfrom after , It's a one-dimensional array .
because jpeg Is a compressed image file , Use it directly opencv Turn this one-dimensional array into Mat The format is incorrect .
resolvent :
The sender needs to use imencode function .
The receiving end needs to use imdecode function .
Now write only the receiver . The code is as follows :
int udp_socket_fd = socket(AF_INET,SOCK_DGRAM,0);
if(udp_socket_fd < 0 )
{
perror("creat socket fail\n");
return -1;
}
struct sockaddr_in local_addr = {0};
local_addr.sin_family = AF_INET; // Use IPv4 agreement
local_addr.sin_port = htons(12349); // Network communication uses big end format
local_addr.sin_addr.s_addr = INADDR_ANY;// Let the system detect the local network card , Auto bind local IP
ret = bind(udp_socket_fd,(struct sockaddr*)&local_addr,sizeof(local_addr));
if(ret < 0)
{
perror("bind fail:");
close(udp_socket_fd);
return -1;
}
else
{
printf("recv ready!!!\n");
}
struct sockaddr_in src_addr = {0}; // For storing each other ( The sender of the message ) Of IP Address information
socklen_t len = sizeof(src_addr); // The size of the address information
char buf[1024*100] = {0};// Message buffer
ret = recvfrom(udp_socket_fd, buf, sizeof(buf), 0, (struct sockaddr *)&src_addr, &len);
if(ret == -1)
{
}
vector<unsigned char> buff;
for(int i=0;i<ret;i++)
{
buff.push_back(buf[i]);
}
cv::Mat show=imdecode(buff,CV_LOAD_IMAGE_COLOR);
This code bit sends the received buf Array (jpep The picture data of ), Turn into Mat show.
边栏推荐
- Hmi-32- [motion mode] add light panel and basic information column
- 问题解决:AttributeError: ‘NoneType‘ object has no attribute ‘append‘
- Six stone programming: advantages of automated testing
- LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
- Application and Optimization Practice of redis in vivo push platform
- [機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
- Elk log analysis system
- Unpool(nn.MaxUnpool2d)
- Elfk deployment
- Acwing game 58 [End]
猜你喜欢
Design of KTV intelligent dimming system based on MCU
Tiny series rendering tutorial
Design of kindergarten real-time monitoring and control system
【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
Scientific research: are women better than men?
A label colorful navigation bar
Openresty ngx Lua Execution stage
Application and Optimization Practice of redis in vivo push platform
Introduce reflow & repaint, and how to optimize it?
2021 Li Hongyi machine learning (1): basic concepts
随机推荐
openresty ngx_lua执行阶段
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
Avoid material "minefields"! Play with super high conversion rate
Leetcode takes out the least number of magic beans
Using druid to connect to MySQL database reports the wrong type
"C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
Design and practice of kubernetes cluster and application monitoring scheme
ELK日志分析系统
Bumblebee: build, deliver, and run ebpf programs smoothly like silk
问题解决:AttributeError: ‘NoneType‘ object has no attribute ‘append‘
Learn game model 3D characters, come out to find a job?
STL container
The database and recharge are gone
2.常见的请求方法
A tab Sina navigation bar
[200 opencv routines] 99 Modified alpha mean filter
[download white paper] does your customer relationship management (CRM) really "manage" customers?
100 basic multiple choice questions of C language (with answers) 04
Variables in postman
[Yu Yue education] National Open University autumn 2018 8109-22t (1) monetary and banking reference questions