C++的心酸历程

编程入门 行业动态 更新时间:2024-10-26 14:32:21

C++的<a href=https://www.elefans.com/category/jswz/34/1758067.html style=心酸历程"/>

C++的心酸历程

好久没有写博客了,因为我掉进了C的坑。从python转C,是一条漫漫长路。主要的障碍是编程习惯,核心问题却是自我设限。起初总感觉C很难,很抗拒,越是抗拒越是难。但是工作原因不得已而为之,坚持下来了。终于告别了对C的恐慌,故此值得记录一下。此文只是简单记录一下,基础的类型转换、mkfifo的简单使用以及cmake的使用,以便以后开发。
大佬说,流只是一种概念!大佬说的话要记下来!我利用mkfifo,其实就是一种管道,把文件看成一个队列,一端只写,一端只读,就会形成流。缺点却是读的速度太慢,会出现丢帧的现象。
新建一个streamer_client.cpp:

//
// Created by neuron-drop on 2020/4/1.
//
extern "C" {
#include<fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <zconf.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
}
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
#define FIFO_CLIENT "./fifo"
int main()
{int fd;int nwrite;if ((mkfifo(FIFO_CLIENT, O_CREAT|O_EXCL|O_RDWR) < 0) && (errno != EEXIST)){perror("can not creat fifoserver");exit(1);}//以只写方式打开管道文件fd=open(FIFO_CLIENT,O_WRONLY,0);if(fd==-1){perror("open\n");exit(1);}// 使用opencv打开摄像头VideoCapture cap("rtsp://192.168.124.108/live0");Mat frame;if (cap.isOpened() == false)cout << "can't not capture an usb camera from your computer!!!" << endl;while (1){cap >> frame;if (frame.empty())continue;// 获取图片像素点数int size = frame.total() * frame.elemSize();//为管道写入数据nwrite=write(fd,frame.data, size);if(nwrite<0){perror("write\n");}}close(fd); //关闭管道return 0;
}

构建服务端,接收客户端的数据。新建一个streamer_server.cpp:

//
// Created by neuron-drop on 2020/4/1.
//
extern "C" {
#include<fcntl.h>
//#include<memory.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <zconf.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
}
#include "opencv2/imgcodecs/legacy/constants_c.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
#define FIFO "fifo"Mat bytesToMat(Byte * bytes)
{Mat image = Mat(3000,4000,CV_8UC3,bytes).clone(); // make a copyreturn image;
}
int main() {int fd;int nread;printf("Preparing for reading bytes...\n");//以只读方式打开管道文件if ((mkfifo(FIFO, O_CREAT | O_EXCL | O_RDONLY) < 0) && (errno != EEXIST)) {perror("can not creat fifoserver");exit(1);}// 给文件以读写权限,否则无法写入char cmd[100];sprintf(cmd, "chmod 704 %s", FIFO);system(cmd);fd = open(FIFO, O_RDONLY | O_NONBLOCK, 0);if (fd == -1) {perror("open");exit(1);}// 根据相机的图片大小设置Byte * bytes = new Byte[3000*4000*3];while (1) {nread = read(fd, bytes, 3000*4000*3);if (nread == -1) {perror("read\n");} else {try{if(strlen((char*)bytes)){// 将bytes转MatMat frame =  bytesToMat(bytes);imshow("frame", frame);waitKey(1);}}catch(const char* msg) {​cout << msg << endl;continue;}}}}//    close(fd); //关闭管道
//    pause(); /*暂停,等待信号*/
//    unlink(FIFO); //删除文件
}

数据类型说明,对于初级c手,最痛苦的莫过于数据类型之间的转换了。上面我是将Byte直接写入了文件,对于c来说除了基本的数据类型以外,其他的数据类型可以理解成是一种基本类型的限制或者扩展,比如有int ,就会有short int或者 unsigned int等等。而Byte,python开发会本能地反应是二进制,其实它只是 typedef unsigned char Byte;所以数据类型为uchar* data的frame.data写入文件后,却可以使用Byte去读取它的原因。至于bytes转Mat,这里就不赘述了,原因也很简单,只不过是给Mat结构体进行初始化罢了。
才恍然发现,原来C的数据类型如此简单!只是我们开始学习的时候听到别人说很难,所以进行的心理设限。

cmake是一个强大的工具,能有效的节省了编译的时间,但是对于新手来说一开始就使用它,的确困难重重,但是公司的大佬选择使用了cmake,所以本人也不得已而为之。这里推荐使用clion作为开发工具。

cmake_minimum_required(VERSION 3.15)
project(createStreamer)set(CMAKE_CXX_STANDARD 14)# find required opencv
find_package(OpenCV REQUIRED)
# directory of opencv headers
include_directories(${OpenCV_INCLUDE_DIRS})
# name of executable file and path of source file
add_executable(server src/streamer_server.cpp)
add_executable(client src/streamer_client.cpp)
target_link_libraries(client ${OpenCV_LIBS})
target_link_libraries(server ${OpenCV_LIBS})#查找当前目录下的所有源文件,并将名称保存到 SRC 变量
#aux_source_directory(./src SRC)
#指定生成目标,第一个参数为目标名,之后参数为源文件列表
#add_executable(main src/main.cpp)
#添加第三方链接库
#target_link_libraries(python_dvp dvp)
#生成链接库文件,三个参数分别是链接库名、链接库类型、源码文件
#add_library(python_dvp SHARED src/python_ext.cpp)
#设置生成目录
#SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR})
# add_library(createStreamer SHARED src/python-api.c)
# set_target_properties(createStreamer PROPERTIES PREFIX "")
# set_target_properties(createStreamer PROPERTIES OUTPUT_NAME python-api )

注释里面已经有了基本的使用说明了,这些对新手来说差不多就够了。C++语言是比较优美的了,开发速度其实也不慢,至于说pyhton开发快,那是别人的api太多了,所以很多python开发只知道如何实现(流程),却无法自己实现。所以python与C++,配合使用,也是一种不错的选择!

此次最大的感悟就是,不要自我设限!

更多推荐

C++的心酸历程

本文发布于:2024-02-25 20:44:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1700245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:心酸   历程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!