#include <iostream>
#include <boost/asio.hpp>
#include <vector>
#include <thread>
#include <opencv2/opencv.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
#include <fstream>
#include<sstream>
using namespace std;
using namespace cv;
vector<string> getdata_string;
Mat oldphoto;
//字符串分割函数,
std::vector<int> split(std::string str, std::string pattern) {
std::string::size_type pos;
std::vector<int> result;
str += pattern;//扩展字符串以方便操作
int size = str.size();
//解压的像素的苏亮
int increate_number = 0;
//上次有效的像素值
int last_pixel = -1;
for (int i = 0; i < size; i++) {
pos = str.find(pattern, i);
if (pos < size) {
std::string s = str.substr(i, pos - i);
int number = 0;
number = atoi(s.c_str());
if (number > 1000) {
increate_number = number - 1000;
for (int j = 0; j < increate_number; ++j) {
result.push_back(last_pixel);
}
} else {
increate_number = 0;
result.push_back(number);
last_pixel = number;
}
i = pos + pattern.size() - 1;
}
}
return result;
}
void getData() {
using namespace boost::asio;
boost::mutex mutex;
// 所有asio类都需要io_service对象
io_service iosev;
ip::tcp::acceptor acceptor(iosev, ip::tcp::endpoint(ip::tcp::v4(), 8888));
for (;;) {
mutex.lock();//锁
// socket对象
ip::tcp::socket socket(iosev);
// 等待直到客户端连接进来
acceptor.accept(socket);
// 显示连接进来的客户端
std::cout << socket.remote_endpoint().address() << std::endl;
vector<uint32_t> image;
// 向客户端发送hello world!
boost::system::error_code ec;
socket.write_some(buffer("boost_server3.cpp:hello world!"), ec);
// 如果出错,打印出错信息
if (ec) {
std::cout << boost::system::system_error(ec).what() << std::endl;
break;
}
char get[400000];
ofstream outfile("/home/cai/bag/test1218bag/iamge_data.txt", std::ios::app);
size_t len = socket.read_some(buffer(get), ec);
outfile << get << "\n";
std::cout << get << std::endl;
getdata_string.push_back(get);
mutex.unlock();//解锁
}
}
int main(int argc, char *argv[]) {
int show_time = 500;
//开启一个线程
std::thread t(getData);
int photo_index = 0;
while (1) {
if (getdata_string.size() <= photo_index) {
if (photo_index > 0) {
imshow("dst",oldphoto);
waitKey(show_time);
continue;
} else{
continue;
}
}
string get = getdata_string.at(photo_index);
std::vector<int> image_vector;
//每隔1.5秒更新一次图片
image_vector = split(get, "-");
//在数据传输协议里面已定义第2个为width,第3个为height,第一应该是"outfile_big_graph: "被spilit方法设置为0了
int width = image_vector[2];
int height = image_vector[1];
int name_int = image_vector[0];
string name_string = to_string(name_int);
image_vector.erase(image_vector.begin());
image_vector.erase(image_vector.begin());
image_vector.erase(image_vector.begin());
cv::Mat src = Mat::zeros(height, width, CV_8UC1);
cv::Mat dst;
//上次的灰度值
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int Gray = image_vector[i * width + j];
src.at<uchar>(i, j) = (uchar) Gray;
}
}
int new_width = width * 4;
int new_height = height * 4;
//resize(src, dst, cv::Size(new_width, new_height));
resize(src, dst, cv::Size(800, 800));
// imshow(name_string, dst);
imshow("dst", dst);
oldphoto = dst;
photo_index++;
waitKey(show_time);
}
return 0;
}
网友评论