简体   繁体   中英

Use poco library to display usb camera image

I am using POCO library and ROS on a linux board. The project was built with POCO library rather than async_web_server_cpp, so I need to write code to achieve usb camera image dispaly over http. I tried save the image on disk, then display it successfully. But when I try to transmit the realtime image, I get stucked for serval days. The problem is how to use stream. My code is not optimized.

int data_size;
uint8_t img_array[990000];

void Image_Recv_Callback(const sensor_msgs::Image::ConstPtr& msg)
{
  data_size = msg->height*msg->step;
  
  for(int i=0;i<data_size;i++)
     img_array[i] = msg->data[i];

  return;
}

class MyRequestHandler : public HTTPRequestHandler
{
public:
  virtual void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp)
  {
    resp.setStatus(HTTPResponse::HTTP_OK);
    resp.setContentType("text/html");
   
    std::ostream& out = resp.send();
    Poco::Base64Encoder b64out(out);
    
    out << "<html>";
    out << "<h1>Hello world!</h1>";

    std::iostream img_stm(NULL);

    for(int i=0;i<data_size;i++)
      img_stm << img_array[i];
    
    
    out << "<body>"
        << "<img src=";
    out << "\"data:image/png;";//base64,
    StreamCopier::copyStream(img_stm, b64out);
    out << "\">";
    out << "</img>";
    out << "</body>";
    
    out << "</html>";
  
  
  }
private:
  static int count;
};

Very grateful if you can help me figure it out!

  1. If you send image after imencode the below code help you and if you're using base64 encode in server side you gonna decode in client side

2)I am sending sending data after imencode i used below snippent for get back the image.

#include<vector>
#include<iostream>
#include "Poco/StreamCopier.h"
Poco::StreamCopier::copyToString(req.stream(), recv_string);

// ##########Receiving image data   #######
std::vector<unsigned char> data(recv_string.begin(), recv_string.end());
cv::Mat image = cv::imdecode(data, 1);
cv::imshow("image at server side", image);
cv::waitKey(1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM