【发布时间】:2019-10-11 08:49:39
【问题描述】:
我对 c++ 和一般编程非常陌生。我只是想使用 opencv 读取图片并使用 boost asio 将其显示在 Web 服务器上。这是我对视频中的所有帧执行此操作之前的初始步骤。以下是我的代码 -
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <thread>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
using boost::asio::ip::tcp;
using namespace std;
using namespace cv;
int main(){
try{
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));
for (;;){
tcp::socket socket(io_service);
acceptor.accept(socket);
boost::system::error_code ignored_error;
cv::Mat frame = cv::imread("x.jpg");
vector<uchar> buff;
vector<int> param = vector<int>(2);
param[0]=cv::IMWRITE_JPEG_QUALITY;
param[1]=95;
imencode(".jpg",frame,buff,param);
const char mess[] = "axaxaxaxasaaaaaaaaaaxax";
std::string content(buff.begin(), buff.end());
boost::asio::write(socket, boost::asio::buffer(content), boost::asio::transfer_all(), ignored_error);
// boost::asio::write(socket, boost::asio::buffer(mess), boost::asio::transfer_all(), ignored_error);
}
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
现在发送消息工作正常,但是当我尝试通过内容或 buff 发送图像时,它显示为乱码。我觉得这是因为在发送图片之前我没有发送任何有关图片的信息。但我不知道该怎么做。 或者也许我完全错了。任何帮助/建议将不胜感激。干杯!
【问题讨论】:
标签: c++ networking webserver boost-asio