前言
上次在另外一编文章写了关于利用gRPC c++传输图片的方法。详情请看:
https://blog.youkuaiyun.com/liyangbinbin/article/details/100538412
但是效率不高,因为无论服务器还是客户端都要进行M*N(图像大小是MxN)次的循环,对于小图像来说还好,但是对于大图像来讲,效率不怎么好,后来看到了以复制内存块的方法,就是每次复制一行,这样就能减少循环次数了,BUT。。。靓仔都会懂的,指针这玩意动不动就是非法访问,指向出错,这次行了,下次又不知道什么鬼不行了,就算是行了,再反解回mat图片时图片都变形了,搞得我是十分没有脾气,大喊扑街了,后来看到了capcop博主的一篇文章,详情:
https://blog.youkuaiyun.com/tt_ren/article/details/53227900
原来opencv里面还有个解码编码的函数imdecode/imencode,实在是太好用了,根本不用去这么复杂的操作内存,去管理指针了。直接上代码
客户端代码
#pragma comment(lib,"ws2_32.lib")
#include <iostream>
#include <fstream>
#include <string>
#include <grpc/grpc.h>
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpc++/security/server_credentials.h>
#include "uppic.grpc.pb.h"
#include <grpc++/channel.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include "opencv.hpp"
#include <memory.h>
#include <conio.h>
#include <stdio.h>
using grpc::Status;
using grpc::Channel;
using grpc::ClientContext;
using grpc::ClientWriter;
using namespace namespace_uploadpic;
using grpc::ClientContext;
#define GRPC_DEFAULT_MAX_RECV_MESSAGE_